command_request.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. # Not installing aliases from python-future; it's unreliable and slow.
  22. from builtins import * # noqa
  23. from ycm.client.base_request import BaseRequest, BuildRequestData
  24. from ycm import vimsupport
  25. from ycmd.utils import ToUnicode
  26. def _EnsureBackwardsCompatibility( arguments ):
  27. if arguments and arguments[ 0 ] == 'GoToDefinitionElseDeclaration':
  28. arguments[ 0 ] = 'GoTo'
  29. return arguments
  30. class CommandRequest( BaseRequest ):
  31. def __init__( self, arguments, completer_target = None, extra_data = None ):
  32. super( CommandRequest, self ).__init__()
  33. self._arguments = _EnsureBackwardsCompatibility( arguments )
  34. self._command = arguments and arguments[ 0 ]
  35. self._completer_target = ( completer_target if completer_target
  36. else 'filetype_default' )
  37. self._extra_data = extra_data
  38. self._response = None
  39. def Start( self ):
  40. request_data = BuildRequestData()
  41. if self._extra_data:
  42. request_data.update( self._extra_data )
  43. request_data.update( {
  44. 'completer_target': self._completer_target,
  45. 'command_arguments': self._arguments
  46. } )
  47. self._response = self.PostDataToHandler( request_data,
  48. 'run_completer_command' )
  49. def Response( self ):
  50. return self._response
  51. def RunPostCommandActionsIfNeeded( self, modifiers ):
  52. if not self.Done() or self._response is None:
  53. return
  54. # If not a dictionary or a list, the response is necessarily a
  55. # scalar: boolean, number, string, etc. In this case, we print
  56. # it to the user.
  57. if not isinstance( self._response, ( dict, list ) ):
  58. return self._HandleBasicResponse()
  59. if 'fixits' in self._response:
  60. return self._HandleFixitResponse()
  61. if 'message' in self._response:
  62. return self._HandleMessageResponse()
  63. if 'detailed_info' in self._response:
  64. return self._HandleDetailedInfoResponse()
  65. # The only other type of response we understand is GoTo, and that is the
  66. # only one that we can't detect just by inspecting the response (it should
  67. # either be a single location or a list)
  68. return self._HandleGotoResponse( modifiers )
  69. def _HandleGotoResponse( self, modifiers ):
  70. if isinstance( self._response, list ):
  71. vimsupport.SetQuickFixList(
  72. [ _BuildQfListItem( x ) for x in self._response ] )
  73. vimsupport.OpenQuickFixList( focus = True, autoclose = True )
  74. else:
  75. vimsupport.JumpToLocation( self._response[ 'filepath' ],
  76. self._response[ 'line_num' ],
  77. self._response[ 'column_num' ],
  78. modifiers )
  79. def _HandleFixitResponse( self ):
  80. if not len( self._response[ 'fixits' ] ):
  81. vimsupport.PostVimMessage( 'No fixits found for current line',
  82. warning = False )
  83. else:
  84. try:
  85. fixit_index = 0
  86. # When there are multiple fixit suggestions, present them as a list to
  87. # the user hand have her choose which one to apply.
  88. if len( self._response[ 'fixits' ] ) > 1:
  89. fixit_index = vimsupport.SelectFromList(
  90. "Multiple FixIt suggestions are available at this location. "
  91. "Which one would you like to apply?",
  92. [ fixit[ 'text' ] for fixit in self._response[ 'fixits' ] ] )
  93. vimsupport.ReplaceChunks(
  94. self._response[ 'fixits' ][ fixit_index ][ 'chunks' ],
  95. silent = self._command == 'Format' )
  96. except RuntimeError as e:
  97. vimsupport.PostVimMessage( str( e ) )
  98. def _HandleBasicResponse( self ):
  99. vimsupport.PostVimMessage( self._response, warning = False )
  100. def _HandleMessageResponse( self ):
  101. vimsupport.PostVimMessage( self._response[ 'message' ], warning = False )
  102. def _HandleDetailedInfoResponse( self ):
  103. vimsupport.WriteToPreviewWindow( self._response[ 'detailed_info' ] )
  104. def SendCommandRequest( arguments, completer, modifiers, extra_data = None ):
  105. request = CommandRequest( arguments, completer, extra_data )
  106. # This is a blocking call.
  107. request.Start()
  108. request.RunPostCommandActionsIfNeeded( modifiers )
  109. return request.Response()
  110. def _BuildQfListItem( goto_data_item ):
  111. qf_item = {}
  112. if 'filepath' in goto_data_item:
  113. qf_item[ 'filename' ] = ToUnicode( goto_data_item[ 'filepath' ] )
  114. if 'description' in goto_data_item:
  115. qf_item[ 'text' ] = ToUnicode( goto_data_item[ 'description' ] )
  116. if 'line_num' in goto_data_item:
  117. qf_item[ 'lnum' ] = goto_data_item[ 'line_num' ]
  118. if 'column_num' in goto_data_item:
  119. # ycmd returns columns 1-based, and QuickFix lists require "byte offsets".
  120. # See :help getqflist and equivalent comment in
  121. # vimsupport.ConvertDiagnosticsToQfList.
  122. #
  123. # When the Vim help says "byte index", it really means "1-based column
  124. # number" (which is somewhat confusing). :help getqflist states "first
  125. # column is 1".
  126. qf_item[ 'col' ] = goto_data_item[ 'column_num' ]
  127. return qf_item