Prompt the user to select a logfile with YcmToggleLogs

When no argument is given to the YcmToggleLogs command, instead of displaying
the list of available logfiles, prompt the user to open (or close if already
open) one of them.
This commit is contained in:
micbou 2017-12-22 15:00:27 +01:00
parent 290dd94721
commit 497a803188
No known key found for this signature in database
GPG Key ID: C7E8FD1F3BDA1E05
5 changed files with 78 additions and 23 deletions

View File

@ -1283,9 +1283,15 @@ completion engine.
### The `:YcmToggleLogs` command
This command opens in separate windows the logfiles given as arguments or closes
them if they are already open in the editor. When no argument is given, list the
available logfiles. Only for debugging purpose.
This command presents the list of logfiles created by YCM, the [ycmd
server][ycmd], and the semantic engine server for the current filetype, if any.
One of these logfiles can be opened in the editor (or closed if already open) by
entering the corresponding number or by clicking on it with the mouse.
Additionally, this command can take the logfile names as arguments. Use the
`<TAB>` key (or any other key defined by the `wildchar` option) to complete the
arguments or to cycle through them (depending on the value of the `wildmode`
option). Each logfile given as an argument is directly opened (or closed if
already open) in the editor. Only for debugging purposes.
### The `:YcmCompleter` command

View File

@ -1553,9 +1553,15 @@ semantic completion engine.
-------------------------------------------------------------------------------
The *:YcmToggleLogs* command
This command opens in separate windows the logfiles given as arguments or
closes them if they are already open in the editor. When no argument is given,
list the available logfiles. Only for debugging purpose.
This command presents the list of logfiles created by YCM, the ycmd server
[43], and the semantic engine server for the current filetype, if any. One of
these logfiles can be opened in the editor (or closed if already open) by
entering the corresponding number or by clicking on it with the mouse.
Additionally, this command can take the logfile names as arguments. Use the
'<TAB>' key (or any other key defined by the 'wildchar' option) to complete the
arguments or to cycle through them (depending on the value of the 'wildmode'
option). Each logfile given as an argument is directly opened (or closed if
already open) in the editor. Only for debugging purposes.
-------------------------------------------------------------------------------
The *:YcmCompleter* command

View File

@ -273,24 +273,56 @@ def YouCompleteMe_ToggleLogs_WithParameters_test( ycm,
@YouCompleteMeInstance()
# Select the second item of the list which is the ycmd stderr logfile.
@patch( 'ycm.vimsupport.SelectFromList', return_value = 1 )
@patch( 'ycm.vimsupport.OpenFilename', new_callable = ExtendedMock )
def YouCompleteMe_ToggleLogs_WithoutParameters_SelectLogfileNotAlreadyOpen_test(
ycm, open_filename, *args ):
current_buffer = VimBuffer( 'current_buffer' )
with MockVimBuffers( [ current_buffer ], current_buffer ):
ycm.ToggleLogs()
open_filename.assert_has_exact_calls( [
call( ycm._server_stderr, { 'size': 12,
'watch': True,
'fix': True,
'focus': False,
'position': 'end' } )
] )
@YouCompleteMeInstance()
# Select the third item of the list which is the ycmd stdout logfile.
@patch( 'ycm.vimsupport.SelectFromList', return_value = 2 )
@patch( 'ycm.vimsupport.CloseBuffersForFilename', new_callable = ExtendedMock )
def YouCompleteMe_ToggleLogs_WithoutParameters_SelectLogfileAlreadyOpen_test(
ycm, close_buffers_for_filename, *args ):
logfile_buffer = VimBuffer( ycm._server_stdout, window = 1 )
with MockVimBuffers( [ logfile_buffer ], logfile_buffer ):
ycm.ToggleLogs()
close_buffers_for_filename.assert_has_exact_calls( [
call( ycm._server_stdout )
] )
@YouCompleteMeInstance()
@patch( 'ycm.vimsupport.SelectFromList',
side_effect = RuntimeError( 'Error message' ) )
@patch( 'ycm.vimsupport.PostVimMessage' )
def YouCompleteMe_ToggleLogs_WithoutParameters_test( ycm, post_vim_message ):
# We test on a Python buffer because the Python completer has subserver
# logfiles.
python_buffer = VimBuffer( 'buffer.py', filetype = 'python' )
with MockVimBuffers( [ python_buffer ], python_buffer ):
def YouCompleteMe_ToggleLogs_WithoutParameters_NoSelection_test(
ycm, post_vim_message, *args ):
current_buffer = VimBuffer( 'current_buffer' )
with MockVimBuffers( [ current_buffer ], current_buffer ):
ycm.ToggleLogs()
assert_that(
# Argument passed to PostVimMessage.
post_vim_message.call_args[ 0 ][ 0 ],
matches_regexp(
'Available logfiles are:\n'
'jedihttp_\d+_stderr_.+.log\n'
'jedihttp_\d+_stdout_.+.log\n'
'ycm_.+.log\n'
'ycmd_\d+_stderr_.+.log\n'
'ycmd_\d+_stdout_.+.log' )
equal_to( 'Error message' )
)

View File

@ -542,8 +542,8 @@ def SelectFromList( prompt, items ):
|items| should not contain leading ordinals: they are added automatically.
Returns the 0-based index in the list |items| that the user selected, or a
negative number if no valid item was selected.
Returns the 0-based index in the list |items| that the user selected, or an
exception if no valid item was selected.
See also :help inputlist()."""

View File

@ -652,9 +652,20 @@ class YouCompleteMe( object ):
def ToggleLogs( self, *filenames ):
logfiles = self.GetLogfiles()
if not filenames:
vimsupport.PostVimMessage(
'Available logfiles are:\n'
'{0}'.format( '\n'.join( sorted( list( logfiles ) ) ) ) )
sorted_logfiles = sorted( list( logfiles ) )
try:
logfile_index = vimsupport.SelectFromList(
'Which logfile do you wish to open (or close if already open)?',
sorted_logfiles )
except RuntimeError as e:
vimsupport.PostVimMessage( str( e ) )
return
logfile = logfiles[ sorted_logfiles[ logfile_index ] ]
if not vimsupport.BufferIsVisibleForFilename( logfile ):
self._OpenLogfile( logfile )
else:
self._CloseLogfile( logfile )
return
for filename in set( filenames ):