Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RFC] Make all subcommands async #4176

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
45 changes: 35 additions & 10 deletions autoload/youcompleteme.vim
Expand Up @@ -1452,6 +1452,7 @@ function! youcompleteme#GetCommandResponseAsync( callback, ... ) abort

let s:pollers.command.requests[ request_id ] = {
\ 'response_func': 'StringResponse',
\ 'response_func_args': '',
\ 'callback': a:callback
\ }
if s:pollers.command.id == -1
Expand All @@ -1477,6 +1478,7 @@ function! youcompleteme#GetRawCommandResponseAsync( callback, ... ) abort

let s:pollers.command.requests[ request_id ] = {
\ 'response_func': 'Response',
\ 'response_func_args': '',
\ 'callback': a:callback
\ }
if s:pollers.command.id == -1
Expand Down Expand Up @@ -1507,10 +1509,18 @@ function! s:PollCommands( timer_id ) abort
let poll_again = 1
continue
else
let result = py3eval( 'ycm_state.GetCommandRequest( '
\ . 'int( vim.eval( "request_id" ) ) ).'
\ . request.response_func
\ . '()' )
if len( request.response_func_args ) == 0
let result = py3eval( 'ycm_state.GetCommandRequest( '
\ . 'int( vim.eval( "request_id" ) ) ).'
\ . request.response_func
\ . '()' )
else
let result = py3eval( 'ycm_state.GetCommandRequest( '
\ . 'int( vim.eval( "request_id" ) ) ).'
\ . request.response_func
\ . '(' . request.response_func_args . ')' )
let request.response_func_args = ''
endif
endif

" This request is done
Expand All @@ -1526,13 +1536,28 @@ function! s:PollCommands( timer_id ) abort
endfunction


function! s:EmptyCallback( result ) abort
endfunction


function! s:CompleterCommand( mods, count, line1, line2, ... )
py3 ycm_state.SendCommandRequest(
\ vim.eval( 'a:000' ),
\ vim.eval( 'a:mods' ),
\ vimsupport.GetBoolValue( 'a:count != -1' ),
\ vimsupport.GetIntValue( 'a:line1' ),
\ vimsupport.GetIntValue( 'a:line2' ) )
let request_id = py3eval( 'ycm_state.SendCommandRequestAsync(' .
\ ' vim.eval( "a:000" ),' .
\ ' vimsupport.GetBoolValue( "a:count != -1" ),' .
\ ' vimsupport.GetIntValue( "a:line1" ),' .
\ ' vimsupport.GetIntValue( "a:line2" ),' .
\ ' False )' )
let s:pollers.command.requests[ request_id ] = {
\ 'response_func': 'RunPostCommandActionsIfNeeded',
\ 'response_func_args':
\ '"' . a:mods . '","' . g:ycm_goto_buffer_command . '"',
\ 'callback': function( 's:EmptyCallback' )
\ }
if s:pollers.command.id == -1
let s:pollers.command.id =
\ timer_start( s:pollers.command.wait_milliseconds,
\ function( 's:PollCommands' ) )
endif
endfunction


Expand Down
12 changes: 0 additions & 12 deletions python/ycm/client/command_request.py
Expand Up @@ -215,18 +215,6 @@ def SendCommandRequestAsync( arguments, extra_data = None, silent = True ):
return request


def SendCommandRequest( arguments,
modifiers,
buffer_command = DEFAULT_BUFFER_COMMAND,
extra_data = None ):
request = SendCommandRequestAsync( arguments,
extra_data = extra_data,
silent = False )
# Block here to get the response
request.RunPostCommandActionsIfNeeded( modifiers, buffer_command )
return request.Response()


def GetCommandResponse( arguments, extra_data = None ):
request = SendCommandRequestAsync( arguments,
extra_data = extra_data,
Expand Down
63 changes: 29 additions & 34 deletions python/ycm/tests/command_test.py
Expand Up @@ -27,18 +27,16 @@

class CommandTest( TestCase ):
@YouCompleteMeInstance( { 'g:ycm_extra_conf_vim_data': [ 'tempname()' ] } )
def test_SendCommandRequest_ExtraConfVimData_Works( self, ycm ):
def test_SendCommandRequestAsync_ExtraConfVimData_Works( self, ycm ):
current_buffer = VimBuffer( 'buffer' )
with MockVimBuffers( [ current_buffer ], [ current_buffer ] ):
with patch( 'ycm.youcompleteme.SendCommandRequest' ) as send_request:
ycm.SendCommandRequest( [ 'GoTo' ], 'aboveleft', False, 1, 1 )
with patch( 'ycm.youcompleteme.SendCommandRequestAsync' ) as send_request:
ycm.SendCommandRequestAsync( [ 'GoTo' ], False, 1, 1 )
assert_that(
# Positional arguments passed to SendCommandRequest.
# Positional arguments passed to SendCommandRequestAsync.
send_request.call_args[ 0 ],
contains_exactly(
contains_exactly( 'GoTo' ),
'aboveleft',
'same-buffer',
has_entries( {
'options': has_entries( {
'tab_size': 2,
Expand All @@ -47,46 +45,44 @@ def test_SendCommandRequest_ExtraConfVimData_Works( self, ycm ):
'extra_conf_data': has_entries( {
'tempname()': '_TEMP_FILE_'
} ),
} )
} ),
True
)
)


@YouCompleteMeInstance( {
'g:ycm_extra_conf_vim_data': [ 'undefined_value' ] } )
def test_SendCommandRequest_ExtraConfData_UndefinedValue( self, ycm ):
def test_SendCommandRequestAsync_ExtraConfData_UndefinedValue( self, ycm ):
current_buffer = VimBuffer( 'buffer' )
with MockVimBuffers( [ current_buffer ], [ current_buffer ] ):
with patch( 'ycm.youcompleteme.SendCommandRequest' ) as send_request:
ycm.SendCommandRequest( [ 'GoTo' ], 'belowright', False, 1, 1 )
with patch( 'ycm.youcompleteme.SendCommandRequestAsync' ) as send_request:
ycm.SendCommandRequestAsync( [ 'GoTo' ], False, 1, 1 )
assert_that(
# Positional arguments passed to SendCommandRequest.
# Positional arguments passed to SendCommandRequestAsync.
send_request.call_args[ 0 ],
contains_exactly(
contains_exactly( 'GoTo' ),
'belowright',
'same-buffer',
has_entries( {
'options': has_entries( {
'tab_size': 2,
'insert_spaces': True,
} )
} )
} ),
True
)
)


@YouCompleteMeInstance()
def test_SendCommandRequest_BuildRange_NoVisualMarks( self, ycm, *args ):
def test_SendCommandRequestAsync_BuildRange_NoVisualMarks( self, ycm, *args ):
current_buffer = VimBuffer( 'buffer', contents = [ 'first line',
'second line' ] )
with MockVimBuffers( [ current_buffer ], [ current_buffer ] ):
with patch( 'ycm.youcompleteme.SendCommandRequest' ) as send_request:
ycm.SendCommandRequest( [ 'GoTo' ], '', True, 1, 2 )
with patch( 'ycm.youcompleteme.SendCommandRequestAsync' ) as send_request:
ycm.SendCommandRequestAsync( [ 'GoTo' ], True, 1, 2 )
send_request.assert_called_once_with(
[ 'GoTo' ],
'',
'same-buffer',
{
'options': {
'tab_size': 2,
Expand All @@ -102,24 +98,23 @@ def test_SendCommandRequest_BuildRange_NoVisualMarks( self, ycm, *args ):
'column_num': 12
}
}
}
},
True
)


@YouCompleteMeInstance()
def test_SendCommandRequest_BuildRange_VisualMarks( self, ycm, *args ):
def test_SendCommandRequestAsync_BuildRange_VisualMarks( self, ycm, *args ):
current_buffer = VimBuffer( 'buffer',
contents = [ 'first line',
'second line' ],
visual_start = [ 1, 4 ],
visual_end = [ 2, 8 ] )
with MockVimBuffers( [ current_buffer ], [ current_buffer ] ):
with patch( 'ycm.youcompleteme.SendCommandRequest' ) as send_request:
ycm.SendCommandRequest( [ 'GoTo' ], 'tab', True, 1, 2 )
with patch( 'ycm.youcompleteme.SendCommandRequestAsync' ) as send_request:
ycm.SendCommandRequestAsync( [ 'GoTo' ], True, 1, 2, False )
send_request.assert_called_once_with(
[ 'GoTo' ],
'tab',
'same-buffer',
{
'options': {
'tab_size': 2,
Expand All @@ -135,31 +130,31 @@ def test_SendCommandRequest_BuildRange_VisualMarks( self, ycm, *args ):
'column_num': 9
}
}
}
},
False
)


@YouCompleteMeInstance()
def test_SendCommandRequest_IgnoreFileTypeOption( self, ycm, *args ):
def test_SendCommandRequestAsync_IgnoreFileTypeOption( self, ycm, *args ):
current_buffer = VimBuffer( 'buffer' )
with MockVimBuffers( [ current_buffer ], [ current_buffer ] ):
expected_args = (
[ 'GoTo' ],
'',
'same-buffer',
{
'completer_target': 'python',
'options': {
'tab_size': 2,
'insert_spaces': True
},
}
},
True
)

with patch( 'ycm.youcompleteme.SendCommandRequest' ) as send_request:
ycm.SendCommandRequest( [ 'ft=python', 'GoTo' ], '', False, 1, 1 )
with patch( 'ycm.youcompleteme.SendCommandRequestAsync' ) as send_request:
ycm.SendCommandRequestAsync( [ 'ft=python', 'GoTo' ], False, 1, 1 )
send_request.assert_called_once_with( *expected_args )

with patch( 'ycm.youcompleteme.SendCommandRequest' ) as send_request:
ycm.SendCommandRequest( [ 'GoTo', 'ft=python' ], '', False, 1, 1 )
with patch( 'ycm.youcompleteme.SendCommandRequestAsync' ) as send_request:
ycm.SendCommandRequestAsync( [ 'GoTo', 'ft=python' ], False, 1, 1 )
send_request.assert_called_once_with( *expected_args )
38 changes: 12 additions & 26 deletions python/ycm/youcompleteme.py
Expand Up @@ -32,8 +32,7 @@
from ycm.client.ycmd_keepalive import YcmdKeepalive
from ycm.client.base_request import BaseRequest, BuildRequestData
from ycm.client.completer_available_request import SendCompleterAvailableRequest
from ycm.client.command_request import ( SendCommandRequest,
SendCommandRequestAsync,
from ycm.client.command_request import ( SendCommandRequestAsync,
GetCommandResponse )
from ycm.client.completion_request import CompletionRequest
from ycm.client.resolve_completion_request import ResolveCompletionItem
Expand Down Expand Up @@ -403,25 +402,6 @@ def _GetCommandRequestArguments( self,
return final_arguments, extra_data



def SendCommandRequest( self,
arguments,
modifiers,
has_range,
start_line,
end_line ):
final_arguments, extra_data = self._GetCommandRequestArguments(
arguments,
has_range,
start_line,
end_line )
return SendCommandRequest(
final_arguments,
modifiers,
self._user_options[ 'goto_buffer_command' ],
extra_data )


def GetCommandResponse( self, arguments ):
final_arguments, extra_data = self._GetCommandRequestArguments(
arguments,
Expand All @@ -431,18 +411,24 @@ def GetCommandResponse( self, arguments ):
return GetCommandResponse( final_arguments, extra_data )


def SendCommandRequestAsync( self, arguments ):
def SendCommandRequestAsync( self,
arguments,
has_range = False,
start_line = 0,
end_line = 0,
silent = True ):
final_arguments, extra_data = self._GetCommandRequestArguments(
arguments,
False,
0,
0 )
has_range,
start_line,
end_line )

request_id = self._next_command_request_id
self._next_command_request_id += 1
self._command_requests[ request_id ] = SendCommandRequestAsync(
final_arguments,
extra_data )
extra_data,
silent )
return request_id


Expand Down