command_request.py 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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 ycm.client.base_request import BaseRequest, BuildRequestData
  18. from ycm import vimsupport
  19. DEFAULT_BUFFER_COMMAND = 'same-buffer'
  20. def _EnsureBackwardsCompatibility( arguments ):
  21. if arguments and arguments[ 0 ] == 'GoToDefinitionElseDeclaration':
  22. arguments[ 0 ] = 'GoTo'
  23. return arguments
  24. class CommandRequest( BaseRequest ):
  25. def __init__( self, arguments, extra_data = None, silent = False ):
  26. super( CommandRequest, self ).__init__()
  27. self._arguments = _EnsureBackwardsCompatibility( arguments )
  28. self._command = arguments and arguments[ 0 ]
  29. self._extra_data = extra_data
  30. self._response = None
  31. self._request_data = None
  32. self._response_future = None
  33. self._silent = silent
  34. self._bufnr = extra_data.pop( 'bufnr', None ) if extra_data else None
  35. def Start( self ):
  36. if self._bufnr is not None:
  37. self._request_data = BuildRequestData( self._bufnr )
  38. else:
  39. self._request_data = BuildRequestData()
  40. if self._extra_data:
  41. self._request_data.update( self._extra_data )
  42. self._request_data.update( {
  43. 'command_arguments': self._arguments
  44. } )
  45. self._response_future = self.PostDataToHandlerAsync(
  46. self._request_data,
  47. 'run_completer_command' )
  48. def Done( self ):
  49. return bool( self._response_future ) and self._response_future.done()
  50. def Response( self ):
  51. if self._response is None and self._response_future is not None:
  52. # Block
  53. self._response = self.HandleFuture( self._response_future,
  54. display_message = not self._silent )
  55. return self._response
  56. def RunPostCommandActionsIfNeeded( self,
  57. modifiers,
  58. buffer_command = DEFAULT_BUFFER_COMMAND ):
  59. # This is a blocking call if not Done()
  60. self.Response()
  61. if self._response is None:
  62. # An exception was raised and handled.
  63. return
  64. # If not a dictionary or a list, the response is necessarily a
  65. # scalar: boolean, number, string, etc. In this case, we print
  66. # it to the user.
  67. if not isinstance( self._response, ( dict, list ) ):
  68. return self._HandleBasicResponse()
  69. if 'fixits' in self._response:
  70. return self._HandleFixitResponse()
  71. if 'message' in self._response:
  72. return self._HandleMessageResponse()
  73. if 'detailed_info' in self._response:
  74. return self._HandleDetailedInfoResponse()
  75. # The only other type of response we understand is GoTo, and that is the
  76. # only one that we can't detect just by inspecting the response (it should
  77. # either be a single location or a list)
  78. return self._HandleGotoResponse( buffer_command, modifiers )
  79. def StringResponse( self ):
  80. # Retuns a supporable public API version of the response. The reason this
  81. # exists is that the ycmd API here is wonky as it originally only supported
  82. # text-responses and now has things like fixits and such.
  83. #
  84. # The supportable public API is basically any text-only response. All other
  85. # response types are returned as empty strings
  86. # This is a blocking call if not Done()
  87. self.Response()
  88. # Completer threw an error ?
  89. if self._response is None:
  90. return ""
  91. # If not a dictionary or a list, the response is necessarily a
  92. # scalar: boolean, number, string, etc. In this case, we print
  93. # it to the user.
  94. if not isinstance( self._response, ( dict, list ) ):
  95. return str( self._response )
  96. if 'message' in self._response:
  97. return self._response[ 'message' ]
  98. if 'detailed_info' in self._response:
  99. return self._response[ 'detailed_info' ]
  100. # The only other type of response we understand is 'fixits' and GoTo. We
  101. # don't provide string versions of them.
  102. return ""
  103. def _HandleGotoResponse( self, buffer_command, modifiers ):
  104. if isinstance( self._response, list ):
  105. vimsupport.SetQuickFixList(
  106. [ vimsupport.BuildQfListItem( x ) for x in self._response ] )
  107. vimsupport.OpenQuickFixList( focus = True, autoclose = True )
  108. else:
  109. vimsupport.JumpToLocation( self._response[ 'filepath' ],
  110. self._response[ 'line_num' ],
  111. self._response[ 'column_num' ],
  112. modifiers,
  113. buffer_command )
  114. def _HandleFixitResponse( self ):
  115. if not len( self._response[ 'fixits' ] ):
  116. vimsupport.PostVimMessage( 'No fixits found for current line',
  117. warning = False )
  118. else:
  119. try:
  120. fixit_index = 0
  121. # If there is more than one fixit, we need to ask the user which one
  122. # should be applied.
  123. #
  124. # If there's only one, triggered by the FixIt subcommand (as opposed to
  125. # `RefactorRename`, for example) and whose `kind` is not `quicfix`, we
  126. # still need to as the user for confirmation.
  127. fixits = self._response[ 'fixits' ]
  128. if ( len( fixits ) > 1 or
  129. ( len( fixits ) == 1 and
  130. self._command == 'FixIt' and
  131. fixits[ 0 ].get( 'kind' ) != 'quickfix' ) ):
  132. fixit_index = vimsupport.SelectFromList(
  133. "FixIt suggestion(s) available at this location. "
  134. "Which one would you like to apply?",
  135. [ fixit[ 'text' ] for fixit in fixits ] )
  136. chosen_fixit = fixits[ fixit_index ]
  137. if chosen_fixit[ 'resolve' ]:
  138. self._request_data.update( { 'fixit': chosen_fixit } )
  139. response = self.PostDataToHandler( self._request_data,
  140. 'resolve_fixit' )
  141. if response is None:
  142. return
  143. fixits = response[ 'fixits' ]
  144. assert len( fixits ) == 1
  145. chosen_fixit = fixits[ 0 ]
  146. vimsupport.ReplaceChunks(
  147. chosen_fixit[ 'chunks' ],
  148. silent = self._command == 'Format' )
  149. except RuntimeError as e:
  150. vimsupport.PostVimMessage( str( e ) )
  151. def _HandleBasicResponse( self ):
  152. vimsupport.PostVimMessage( self._response, warning = False )
  153. def _HandleMessageResponse( self ):
  154. vimsupport.PostVimMessage( self._response[ 'message' ], warning = False )
  155. def _HandleDetailedInfoResponse( self ):
  156. vimsupport.WriteToPreviewWindow( self._response[ 'detailed_info' ] )
  157. def SendCommandRequestAsync( arguments, extra_data = None, silent = True ):
  158. request = CommandRequest( arguments,
  159. extra_data = extra_data,
  160. silent = silent )
  161. request.Start()
  162. # Don't block
  163. return request
  164. def SendCommandRequest( arguments,
  165. modifiers,
  166. buffer_command = DEFAULT_BUFFER_COMMAND,
  167. extra_data = None ):
  168. request = SendCommandRequestAsync( arguments,
  169. extra_data = extra_data,
  170. silent = False )
  171. # Block here to get the response
  172. request.RunPostCommandActionsIfNeeded( modifiers, buffer_command )
  173. return request.Response()
  174. def GetCommandResponse( arguments, extra_data = None ):
  175. request = SendCommandRequestAsync( arguments,
  176. extra_data = extra_data,
  177. silent = True )
  178. # Block here to get the response
  179. return request.StringResponse()