command_request.py 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. elif self._response.get( 'file_only' ):
  109. vimsupport.JumpToLocation( self._response[ 'filepath' ],
  110. None,
  111. None,
  112. modifiers,
  113. buffer_command )
  114. else:
  115. vimsupport.JumpToLocation( self._response[ 'filepath' ],
  116. self._response[ 'line_num' ],
  117. self._response[ 'column_num' ],
  118. modifiers,
  119. buffer_command )
  120. def _HandleFixitResponse( self ):
  121. if not len( self._response[ 'fixits' ] ):
  122. vimsupport.PostVimMessage( 'No fixits found for current line',
  123. warning = False )
  124. else:
  125. try:
  126. fixit_index = 0
  127. # If there is more than one fixit, we need to ask the user which one
  128. # should be applied.
  129. #
  130. # If there's only one, triggered by the FixIt subcommand (as opposed to
  131. # `RefactorRename`, for example) and whose `kind` is not `quicfix`, we
  132. # still need to as the user for confirmation.
  133. fixits = self._response[ 'fixits' ]
  134. if ( len( fixits ) > 1 or
  135. ( len( fixits ) == 1 and
  136. self._command == 'FixIt' and
  137. fixits[ 0 ].get( 'kind' ) != 'quickfix' ) ):
  138. fixit_index = vimsupport.SelectFromList(
  139. "FixIt suggestion(s) available at this location. "
  140. "Which one would you like to apply?",
  141. [ fixit[ 'text' ] for fixit in fixits ] )
  142. chosen_fixit = fixits[ fixit_index ]
  143. if chosen_fixit[ 'resolve' ]:
  144. self._request_data.update( { 'fixit': chosen_fixit } )
  145. response = self.PostDataToHandler( self._request_data,
  146. 'resolve_fixit' )
  147. if response is None:
  148. return
  149. fixits = response[ 'fixits' ]
  150. assert len( fixits ) == 1
  151. chosen_fixit = fixits[ 0 ]
  152. vimsupport.ReplaceChunks(
  153. chosen_fixit[ 'chunks' ],
  154. silent = self._command == 'Format' )
  155. except RuntimeError as e:
  156. vimsupport.PostVimMessage( str( e ) )
  157. def _HandleBasicResponse( self ):
  158. vimsupport.PostVimMessage( self._response, warning = False )
  159. def _HandleMessageResponse( self ):
  160. vimsupport.PostVimMessage( self._response[ 'message' ], warning = False )
  161. def _HandleDetailedInfoResponse( self ):
  162. vimsupport.WriteToPreviewWindow( self._response[ 'detailed_info' ] )
  163. def SendCommandRequestAsync( arguments, extra_data = None, silent = True ):
  164. request = CommandRequest( arguments,
  165. extra_data = extra_data,
  166. silent = silent )
  167. request.Start()
  168. # Don't block
  169. return request
  170. def SendCommandRequest( arguments,
  171. modifiers,
  172. buffer_command = DEFAULT_BUFFER_COMMAND,
  173. extra_data = None ):
  174. request = SendCommandRequestAsync( arguments,
  175. extra_data = extra_data,
  176. silent = False )
  177. # Block here to get the response
  178. request.RunPostCommandActionsIfNeeded( modifiers, buffer_command )
  179. return request.Response()
  180. def GetCommandResponse( arguments, extra_data = None ):
  181. request = SendCommandRequestAsync( arguments,
  182. extra_data = extra_data,
  183. silent = True )
  184. # Block here to get the response
  185. return request.StringResponse()