command_request.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. # Copyright (C) 2013 Google Inc.
  2. #
  3. # This file is part of YouCompleteMe.
  4. #
  5. # YouCompleteMe is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # YouCompleteMe is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with YouCompleteMe. If not, see <http://www.gnu.org/licenses/>.
  17. from __future__ import unicode_literals
  18. from __future__ import print_function
  19. from __future__ import division
  20. from __future__ import absolute_import
  21. from future import standard_library
  22. standard_library.install_aliases()
  23. from builtins import * # noqa
  24. import vim
  25. from ycm.client.base_request import BaseRequest, BuildRequestData, ServerError
  26. from ycm import vimsupport
  27. from ycmd.utils import ToUnicode
  28. def _EnsureBackwardsCompatibility( arguments ):
  29. if arguments and arguments[ 0 ] == 'GoToDefinitionElseDeclaration':
  30. arguments[ 0 ] = 'GoTo'
  31. return arguments
  32. class CommandRequest( BaseRequest ):
  33. def __init__( self, arguments, completer_target = None ):
  34. super( CommandRequest, self ).__init__()
  35. self._arguments = _EnsureBackwardsCompatibility( arguments )
  36. self._completer_target = ( completer_target if completer_target
  37. else 'filetype_default' )
  38. self._response = None
  39. def Start( self ):
  40. request_data = BuildRequestData()
  41. request_data.update( {
  42. 'completer_target': self._completer_target,
  43. 'command_arguments': self._arguments
  44. } )
  45. try:
  46. self._response = self.PostDataToHandler( request_data,
  47. 'run_completer_command' )
  48. except ServerError as e:
  49. vimsupport.PostMultiLineNotice( e )
  50. def Response( self ):
  51. return self._response
  52. def RunPostCommandActionsIfNeeded( self ):
  53. if not self.Done() or self._response is None:
  54. return
  55. # If not a dictionary or a list, the response is necessarily a
  56. # scalar: boolean, number, string, etc. In this case, we print
  57. # it to the user.
  58. if not isinstance( self._response, ( dict, list ) ):
  59. return self._HandleBasicResponse()
  60. if 'fixits' in self._response:
  61. return self._HandleFixitResponse()
  62. if 'message' in self._response:
  63. return self._HandleMessageResponse()
  64. if 'detailed_info' in self._response:
  65. return self._HandleDetailedInfoResponse()
  66. # The only other type of response we understand is GoTo, and that is the
  67. # only one that we can't detect just by inspecting the response (it should
  68. # either be a single location or a list)
  69. return self._HandleGotoResponse()
  70. def _HandleGotoResponse( self ):
  71. if isinstance( self._response, list ):
  72. vimsupport.SetQuickFixList(
  73. [ _BuildQfListItem( x ) for x in self._response ] )
  74. vim.eval( 'youcompleteme#OpenGoToList()' )
  75. else:
  76. vimsupport.JumpToLocation( self._response[ 'filepath' ],
  77. self._response[ 'line_num' ],
  78. self._response[ 'column_num' ] )
  79. def _HandleFixitResponse( self ):
  80. if not len( self._response[ 'fixits' ] ):
  81. vimsupport.EchoText( "No fixits found for current line" )
  82. else:
  83. chunks = self._response[ 'fixits' ][ 0 ][ 'chunks' ]
  84. try:
  85. vimsupport.ReplaceChunks( chunks )
  86. except RuntimeError as e:
  87. vimsupport.PostMultiLineNotice( e.message )
  88. def _HandleBasicResponse( self ):
  89. vimsupport.EchoText( self._response )
  90. def _HandleMessageResponse( self ):
  91. vimsupport.EchoText( self._response[ 'message' ] )
  92. def _HandleDetailedInfoResponse( self ):
  93. vimsupport.WriteToPreviewWindow( self._response[ 'detailed_info' ] )
  94. def SendCommandRequest( arguments, completer ):
  95. request = CommandRequest( arguments, completer )
  96. # This is a blocking call.
  97. request.Start()
  98. request.RunPostCommandActionsIfNeeded()
  99. return request.Response()
  100. def _BuildQfListItem( goto_data_item ):
  101. qf_item = {}
  102. if 'filepath' in goto_data_item:
  103. qf_item[ 'filename' ] = ToUnicode( goto_data_item[ 'filepath' ] )
  104. if 'description' in goto_data_item:
  105. qf_item[ 'text' ] = ToUnicode( goto_data_item[ 'description' ] )
  106. if 'line_num' in goto_data_item:
  107. qf_item[ 'lnum' ] = goto_data_item[ 'line_num' ]
  108. if 'column_num' in goto_data_item:
  109. # ycmd returns columns 1-based, and QuickFix lists require "byte offsets".
  110. # See :help getqflist and equivalent comment in
  111. # vimsupport.ConvertDiagnosticsToQfList.
  112. #
  113. # When the Vim help says "byte index", it really means "1-based column
  114. # number" (which is somewhat confusing). :help getqflist states "first
  115. # column is 1".
  116. qf_item[ 'col' ] = goto_data_item[ 'column_num' ]
  117. return qf_item