command_request.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 not self._response:
  53. return
  54. if self._is_goto_command:
  55. self._HandleGotoResponse()
  56. elif self._is_fixit_command:
  57. self._HandleFixitResponse()
  58. elif 'message' in self._response:
  59. self._HandleMessageResponse()
  60. def _HandleGotoResponse( self ):
  61. if isinstance( self._response, list ):
  62. defs = [ _BuildQfListItem( x ) for x in self._response ]
  63. vim.eval( 'setqflist( %s )' % repr( defs ) )
  64. vim.eval( 'youcompleteme#OpenGoToList()' )
  65. else:
  66. vimsupport.JumpToLocation( self._response[ 'filepath' ],
  67. self._response[ 'line_num' ],
  68. self._response[ 'column_num' ] )
  69. def _HandleFixitResponse( self ):
  70. if not len( self._response[ 'fixits' ] ):
  71. vimsupport.EchoText( "No fixits found for current line" )
  72. else:
  73. chunks = self._response[ 'fixits' ][ 0 ][ 'chunks' ]
  74. vimsupport.ReplaceChunksList( chunks )
  75. vimsupport.EchoTextVimWidth( "FixIt applied "
  76. + str( len( chunks ) )
  77. + " changes" )
  78. def _HandleMessageResponse( self ):
  79. vimsupport.EchoText( self._response[ 'message' ] )
  80. def SendCommandRequest( arguments, completer ):
  81. request = CommandRequest( arguments, completer )
  82. # This is a blocking call.
  83. request.Start()
  84. request.RunPostCommandActionsIfNeeded()
  85. return request.Response()
  86. def _BuildQfListItem( goto_data_item ):
  87. qf_item = {}
  88. if 'filepath' in goto_data_item:
  89. qf_item[ 'filename' ] = ToUtf8IfNeeded( goto_data_item[ 'filepath' ] )
  90. if 'description' in goto_data_item:
  91. qf_item[ 'text' ] = ToUtf8IfNeeded( goto_data_item[ 'description' ] )
  92. if 'line_num' in goto_data_item:
  93. qf_item[ 'lnum' ] = goto_data_item[ 'line_num' ]
  94. if 'column_num' in goto_data_item:
  95. qf_item[ 'col' ] = goto_data_item[ 'column_num' ] - 1
  96. return qf_item