Skip to content

Commit

Permalink
Make all subcommands async
Browse files Browse the repository at this point in the history
  • Loading branch information
bstaletic committed Aug 26, 2023
1 parent 4f1dcf4 commit 6b8c800
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 82 deletions.
43 changes: 33 additions & 10 deletions autoload/youcompleteme.vim
Original file line number Diff line number Diff line change
Expand Up @@ -1507,10 +1507,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 +1534,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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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

0 comments on commit 6b8c800

Please sign in to comment.