Browse Source

Add function to capture Vim command output

micbou 7 years ago
parent
commit
b182556804

+ 1 - 5
python/ycm/syntax_parse.py

@@ -24,7 +24,6 @@ from builtins import *  # noqa
 
 from future.utils import itervalues
 import re
-import vim
 from ycm import vimsupport
 
 SYNTAX_GROUP_REGEX = re.compile(
@@ -70,10 +69,7 @@ class SyntaxGroup( object ):
 
 
 def SyntaxKeywordsForCurrentBuffer():
-  vim.command( 'redir => b:ycm_syntax' )
-  vim.command( 'silent! syntax list' )
-  vim.command( 'redir END' )
-  syntax_output = vimsupport.VimExpressionToPythonType( 'b:ycm_syntax' )
+  syntax_output = vimsupport.CaptureVimCommand( 'syntax list' )
   return _KeywordsFromSyntaxListOutput( syntax_output )
 
 

+ 6 - 4
python/ycm/tests/event_notification_test.py

@@ -517,8 +517,9 @@ def EventNotification_BufferUnload_BuildRequestForDeletedAndUnsavedBuffers_test(
   )
 
 
-@patch( 'ycm.syntax_parse.SyntaxKeywordsForCurrentBuffer',
-        return_value = [ 'foo', 'bar' ] )
+@patch( 'ycm.vimsupport.CaptureVimCommand', return_value = """
+fooGroup xxx foo bar
+             links to Statement""" )
 @YouCompleteMeInstance( { 'seed_identifiers_with_syntax': 1 } )
 def EventNotification_FileReadyToParse_SyntaxKeywords_SeedWithCache_test(
     ycm, *args ):
@@ -551,8 +552,9 @@ def EventNotification_FileReadyToParse_SyntaxKeywords_SeedWithCache_test(
       )
 
 
-@patch( 'ycm.syntax_parse.SyntaxKeywordsForCurrentBuffer',
-        return_value = [ 'foo', 'bar' ] )
+@patch( 'ycm.vimsupport.CaptureVimCommand', return_value = """
+fooGroup xxx foo bar
+             links to Statement""" )
 @YouCompleteMeInstance( { 'seed_identifiers_with_syntax': 1 } )
 def EventNotification_FileReadyToParse_SyntaxKeywords_ClearCacheIfRestart_test(
     ycm, *args ):

+ 1 - 1
python/ycm/tests/test_utils.py

@@ -48,7 +48,7 @@ OMNIFUNC_REGEX_FORMAT = (
   '^{omnifunc_name}\((?P<findstart>[01]),[\'"](?P<base>.*)[\'"]\)$' )
 FNAMEESCAPE_REGEX = re.compile( '^fnameescape\(\'(?P<filepath>.+)\'\)$' )
 SIGN_LIST_REGEX = re.compile(
-  "^silent sign place buffer=(?P<bufnr>\d+)$" )
+  "^silent! sign place buffer=(?P<bufnr>\d+)$" )
 SIGN_PLACE_REGEX = re.compile(
   '^sign place (?P<id>\d+) name=(?P<name>\w+) line=(?P<line>\d+) '
   'buffer=(?P<bufnr>\d+)$' )

+ 11 - 5
python/ycm/vimsupport.py

@@ -171,6 +171,15 @@ def GetBufferChangedTick( bufnr ):
   return GetIntValue( 'getbufvar({0}, "changedtick")'.format( bufnr ) )
 
 
+def CaptureVimCommand( command ):
+  vim.command( 'redir => b:ycm_command' )
+  vim.command( 'silent! {}'.format( command ) )
+  vim.command( 'redir END' )
+  output = vim.eval( 'b:ycm_command' )
+  vim.command( 'unlet b:ycm_command' )
+  return output
+
+
 class DiagnosticSign( namedtuple( 'DiagnosticSign',
                                   [ 'id', 'line', 'name', 'buffer_number' ] ) ):
   # We want two signs that have different ids but the same location to compare
@@ -182,11 +191,8 @@ class DiagnosticSign( namedtuple( 'DiagnosticSign',
 
 
 def GetSignsInBuffer( buffer_number ):
-  vim.command( 'redir => b:ycm_sign' )
-  vim.command( 'silent sign place buffer={}'.format( buffer_number ) )
-  vim.command( 'redir END' )
-  sign_output = vim.eval( 'b:ycm_sign' )
-  vim.command( 'unlet b:ycm_sign' )
+  sign_output = CaptureVimCommand(
+    'sign place buffer={}'.format( buffer_number ) )
   signs = []
   for line in sign_output.split( '\n' ):
     match = SIGN_PLACE_REGEX.search( line )