command_request.py 9.1 KB

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