command_request.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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._is_goto_command = (
  32. self._arguments and self._arguments[ 0 ].startswith( 'GoTo' ) )
  33. self._is_fixit_command = (
  34. self._arguments and self._arguments[ 0 ].startswith( 'FixIt' ) )
  35. self._response = None
  36. def Start( self ):
  37. request_data = BuildRequestData()
  38. request_data.update( {
  39. 'completer_target': self._completer_target,
  40. 'command_arguments': self._arguments
  41. } )
  42. try:
  43. self._response = self.PostDataToHandler( request_data,
  44. 'run_completer_command' )
  45. except ServerError as e:
  46. vimsupport.PostMultiLineNotice( e )
  47. def Response( self ):
  48. return self._response
  49. def RunPostCommandActionsIfNeeded( self ):
  50. if not self.Done() or self._response is None:
  51. return
  52. if self._is_goto_command:
  53. return self._HandleGotoResponse()
  54. if self._is_fixit_command:
  55. return self._HandleFixitResponse()
  56. # If not a dictionary or a list, the response is necessarily a
  57. # scalar: boolean, number, string, etc. In this case, we print
  58. # it to the user.
  59. if not isinstance( self._response, ( dict, list ) ):
  60. return self._HandleBasicResponse()
  61. if 'message' in self._response:
  62. return self._HandleMessageResponse()
  63. if 'detailed_info' in self._response:
  64. return self._HandleDetailedInfoResponse()
  65. def _HandleGotoResponse( self ):
  66. if isinstance( self._response, list ):
  67. defs = [ _BuildQfListItem( x ) for x in self._response ]
  68. vim.eval( 'setqflist( %s )' % repr( defs ) )
  69. vim.eval( 'youcompleteme#OpenGoToList()' )
  70. else:
  71. vimsupport.JumpToLocation( self._response[ 'filepath' ],
  72. self._response[ 'line_num' ],
  73. self._response[ 'column_num' ] )
  74. def _HandleFixitResponse( self ):
  75. if not len( self._response[ 'fixits' ] ):
  76. vimsupport.EchoText( "No fixits found for current line" )
  77. else:
  78. chunks = self._response[ 'fixits' ][ 0 ][ 'chunks' ]
  79. vimsupport.ReplaceChunksList( chunks )
  80. vimsupport.EchoTextVimWidth( "FixIt applied "
  81. + str( len( chunks ) )
  82. + " changes" )
  83. def _HandleBasicResponse( self ):
  84. vimsupport.EchoText( self._response )
  85. def _HandleMessageResponse( self ):
  86. vimsupport.EchoText( self._response[ 'message' ] )
  87. def _HandleDetailedInfoResponse( self ):
  88. vimsupport.WriteToPreviewWindow( self._response[ 'detailed_info' ] )
  89. def SendCommandRequest( arguments, completer ):
  90. request = CommandRequest( arguments, completer )
  91. # This is a blocking call.
  92. request.Start()
  93. request.RunPostCommandActionsIfNeeded()
  94. return request.Response()
  95. def _BuildQfListItem( goto_data_item ):
  96. qf_item = {}
  97. if 'filepath' in goto_data_item:
  98. qf_item[ 'filename' ] = ToUtf8IfNeeded( goto_data_item[ 'filepath' ] )
  99. if 'description' in goto_data_item:
  100. qf_item[ 'text' ] = ToUtf8IfNeeded( goto_data_item[ 'description' ] )
  101. if 'line_num' in goto_data_item:
  102. qf_item[ 'lnum' ] = goto_data_item[ 'line_num' ]
  103. if 'column_num' in goto_data_item:
  104. # ycmd returns columns 1-based, and QuickFix lists require "byte offsets".
  105. # See :help getqflist and equivalent comment in
  106. # vimsupport.ConvertDiagnosticsToQfList.
  107. #
  108. # When the Vim help says "byte index", it really means "1-based column
  109. # number" (which is somewhat confusing). :help getqflist states "first
  110. # column is 1".
  111. qf_item[ 'col' ] = goto_data_item[ 'column_num' ]
  112. return qf_item