command_request.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. import vim
  18. from ycm.client.base_request import BaseRequest, BuildRequestData, ServerError
  19. from ycm import vimsupport
  20. from ycmd.utils import ToUtf8IfNeeded
  21. def _EnsureBackwardsCompatibility( arguments ):
  22. if arguments and arguments[ 0 ] == 'GoToDefinitionElseDeclaration':
  23. arguments[ 0 ] = 'GoTo'
  24. return arguments
  25. class CommandRequest( BaseRequest ):
  26. def __init__( self, arguments, completer_target = None ):
  27. super( CommandRequest, self ).__init__()
  28. self._arguments = _EnsureBackwardsCompatibility( arguments )
  29. self._completer_target = ( completer_target if completer_target
  30. else 'filetype_default' )
  31. self._response = None
  32. def Start( self ):
  33. request_data = BuildRequestData()
  34. request_data.update( {
  35. 'completer_target': self._completer_target,
  36. 'command_arguments': self._arguments
  37. } )
  38. try:
  39. self._response = self.PostDataToHandler( request_data,
  40. 'run_completer_command' )
  41. except ServerError as e:
  42. vimsupport.PostMultiLineNotice( e )
  43. def Response( self ):
  44. return self._response
  45. def RunPostCommandActionsIfNeeded( self ):
  46. if not self.Done() or self._response is None:
  47. return
  48. # If not a dictionary or a list, the response is necessarily a
  49. # scalar: boolean, number, string, etc. In this case, we print
  50. # it to the user.
  51. if not isinstance( self._response, ( dict, list ) ):
  52. return self._HandleBasicResponse()
  53. if 'fixits' in self._response:
  54. return self._HandleFixitResponse()
  55. if 'message' in self._response:
  56. return self._HandleMessageResponse()
  57. if 'detailed_info' in self._response:
  58. return self._HandleDetailedInfoResponse()
  59. # The only other type of response we understand is GoTo, and that is the
  60. # only one that we can't detect just by inspecting the response (it should
  61. # either be a single location or a list)
  62. return self._HandleGotoResponse()
  63. def _HandleGotoResponse( self ):
  64. if isinstance( self._response, list ):
  65. vimsupport.SetQuickFixList(
  66. [ _BuildQfListItem( x ) for x in self._response ] )
  67. vim.eval( 'youcompleteme#OpenGoToList()' )
  68. else:
  69. vimsupport.JumpToLocation( self._response[ 'filepath' ],
  70. self._response[ 'line_num' ],
  71. self._response[ 'column_num' ] )
  72. def _HandleFixitResponse( self ):
  73. if not len( self._response[ 'fixits' ] ):
  74. vimsupport.EchoText( "No fixits found for current line" )
  75. else:
  76. chunks = self._response[ 'fixits' ][ 0 ][ 'chunks' ]
  77. try:
  78. vimsupport.ReplaceChunks( chunks )
  79. except RuntimeError as e:
  80. vimsupport.PostMultiLineNotice( e.message )
  81. def _HandleBasicResponse( self ):
  82. vimsupport.EchoText( self._response )
  83. def _HandleMessageResponse( self ):
  84. vimsupport.EchoText( self._response[ 'message' ] )
  85. def _HandleDetailedInfoResponse( self ):
  86. vimsupport.WriteToPreviewWindow( self._response[ 'detailed_info' ] )
  87. def SendCommandRequest( arguments, completer ):
  88. request = CommandRequest( arguments, completer )
  89. # This is a blocking call.
  90. request.Start()
  91. request.RunPostCommandActionsIfNeeded()
  92. return request.Response()
  93. def _BuildQfListItem( goto_data_item ):
  94. qf_item = {}
  95. if 'filepath' in goto_data_item:
  96. qf_item[ 'filename' ] = ToUtf8IfNeeded( goto_data_item[ 'filepath' ] )
  97. if 'description' in goto_data_item:
  98. qf_item[ 'text' ] = ToUtf8IfNeeded( goto_data_item[ 'description' ] )
  99. if 'line_num' in goto_data_item:
  100. qf_item[ 'lnum' ] = goto_data_item[ 'line_num' ]
  101. if 'column_num' in goto_data_item:
  102. # ycmd returns columns 1-based, and QuickFix lists require "byte offsets".
  103. # See :help getqflist and equivalent comment in
  104. # vimsupport.ConvertDiagnosticsToQfList.
  105. #
  106. # When the Vim help says "byte index", it really means "1-based column
  107. # number" (which is somewhat confusing). :help getqflist states "first
  108. # column is 1".
  109. qf_item[ 'col' ] = goto_data_item[ 'column_num' ]
  110. return qf_item