command_request.py 4.9 KB

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