command_request.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #!/usr/bin/env python
  2. #
  3. # Copyright (C) 2013 Strahinja Val Markovic <val@markovic.io>
  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. import time
  21. from ycm.client.base_request import BaseRequest, BuildRequestData, ServerError
  22. from ycm import vimsupport
  23. from ycm.utils import ToUtf8IfNeeded
  24. class CommandRequest( BaseRequest ):
  25. def __init__( self, arguments, completer_target = None ):
  26. super( CommandRequest, self ).__init__()
  27. self._arguments = arguments
  28. self._completer_target = ( completer_target if completer_target
  29. else 'filetype_default' )
  30. self._is_goto_command = (
  31. True if arguments and arguments[ 0 ].startswith( 'GoTo' ) else False )
  32. self._response = None
  33. def Start( self ):
  34. request_data = BuildRequestData()
  35. request_data.update( {
  36. 'completer_target': self._completer_target,
  37. 'command_arguments': self._arguments
  38. } )
  39. try:
  40. self._response = self.PostDataToHandler( request_data,
  41. 'run_completer_command' )
  42. except ServerError as e:
  43. vimsupport.PostVimMessage( e )
  44. def Response( self ):
  45. return self._response
  46. def RunPostCommandActionsIfNeeded( self ):
  47. if not self._is_goto_command or not self.Done() or not self._response:
  48. return
  49. if isinstance( self._response, list ):
  50. defs = [ _BuildQfListItem( x ) for x in self._response ]
  51. vim.eval( 'setqflist( %s )' % repr( defs ) )
  52. vim.eval( 'youcompleteme#OpenGoToList()' )
  53. else:
  54. vimsupport.JumpToLocation( self._response[ 'filepath' ],
  55. self._response[ 'line_num' ] + 1,
  56. self._response[ 'column_num' ] )
  57. def SendCommandRequest( arguments, completer ):
  58. request = CommandRequest( arguments, completer )
  59. request.Start()
  60. while not request.Done():
  61. time.sleep( 0.1 )
  62. request.RunPostCommandActionsIfNeeded()
  63. return request.Response()
  64. def _BuildQfListItem( goto_data_item ):
  65. qf_item = {}
  66. if 'filepath' in goto_data_item:
  67. qf_item[ 'filename' ] = ToUtf8IfNeeded( goto_data_item[ 'filepath' ] )
  68. if 'description' in goto_data_item:
  69. qf_item[ 'text' ] = ToUtf8IfNeeded( goto_data_item[ 'description' ] )
  70. if 'line_num' in goto_data_item:
  71. qf_item[ 'lnum' ] = goto_data_item[ 'line_num' ] + 1
  72. if 'column_num' in goto_data_item:
  73. qf_item[ 'col' ] = goto_data_item[ 'column_num' ]
  74. return qf_item