From 497a8031887e04b25ccf54b4f1664ec8a8877d55 Mon Sep 17 00:00:00 2001 From: micbou Date: Fri, 22 Dec 2017 15:00:27 +0100 Subject: [PATCH] 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. --- README.md | 12 ++++-- doc/youcompleteme.txt | 12 ++++-- python/ycm/tests/youcompleteme_test.py | 56 ++++++++++++++++++++------ python/ycm/vimsupport.py | 4 +- python/ycm/youcompleteme.py | 17 ++++++-- 5 files changed, 78 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index f895c298..46278c4f 100644 --- a/README.md +++ b/README.md @@ -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 +`` 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 diff --git a/doc/youcompleteme.txt b/doc/youcompleteme.txt index 81716ae1..55786f93 100644 --- a/doc/youcompleteme.txt +++ b/doc/youcompleteme.txt @@ -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 +'' 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 diff --git a/python/ycm/tests/youcompleteme_test.py b/python/ycm/tests/youcompleteme_test.py index 75e90582..a908870b 100644 --- a/python/ycm/tests/youcompleteme_test.py +++ b/python/ycm/tests/youcompleteme_test.py @@ -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' ) ) diff --git a/python/ycm/vimsupport.py b/python/ycm/vimsupport.py index 7c529169..fe3832e7 100644 --- a/python/ycm/vimsupport.py +++ b/python/ycm/vimsupport.py @@ -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().""" diff --git a/python/ycm/youcompleteme.py b/python/ycm/youcompleteme.py index c9ef35ed..c53bef81 100644 --- a/python/ycm/youcompleteme.py +++ b/python/ycm/youcompleteme.py @@ -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 ):