From c3859791eac3fefd779e86d015cbf80fd538c181 Mon Sep 17 00:00:00 2001 From: micbou Date: Sun, 26 Nov 2017 18:13:33 +0100 Subject: [PATCH] Use fnameescape to escape filepath --- python/ycm/tests/test_utils.py | 5 +++++ python/ycm/tests/vimsupport_test.py | 7 ------- python/ycm/vimsupport.py | 9 +++++---- 3 files changed, 10 insertions(+), 11 deletions(-) diff --git a/python/ycm/tests/test_utils.py b/python/ycm/tests/test_utils.py index 8aa65ba5..b1593281 100644 --- a/python/ycm/tests/test_utils.py +++ b/python/ycm/tests/test_utils.py @@ -47,6 +47,7 @@ MATCHADD_REGEX = re.compile( MATCHDELETE_REGEX = re.compile( '^matchdelete\((?P\d+)\)$' ) OMNIFUNC_REGEX_FORMAT = ( '^{omnifunc_name}\((?P[01]),[\'"](?P.*)[\'"]\)$' ) +FNAMEESCAPE_REGEX = re.compile( '^fnameescape\(\'(?P.+)\'\)$' ) # One-and only instance of mocked Vim object. The first 'import vim' that is # executed binds the vim module to the instance of MagicMock that is created, @@ -211,6 +212,10 @@ def _MockVimEval( value ): if result is not None: return result + match = FNAMEESCAPE_REGEX.search( value ) + if match: + return match.group( 'filepath' ) + raise ValueError( 'Unexpected evaluation: {0}'.format( value ) ) diff --git a/python/ycm/tests/vimsupport_test.py b/python/ycm/tests/vimsupport_test.py index f3dea356..f8ebcda3 100644 --- a/python/ycm/tests/vimsupport_test.py +++ b/python/ycm/tests/vimsupport_test.py @@ -1606,13 +1606,6 @@ def InsertNamespace_append_test( vim_current, *args ): AssertBuffersAreEqualAsBytes( expected_buffer, vim_current.buffer ) -def EscapedFilepath_test(): - eq_( vimsupport.EscapedFilepath( '/path/ with /sp ac es' ), - '/path/\ with\ /sp\ ac\ es' ) - eq_( vimsupport.EscapedFilepath( ' relative path/ with / spaces ' ), - '\ relative\ path/\ with\ /\ spaces\ ' ) - - @patch( 'ycmd.user_options_store._USER_OPTIONS', { 'goto_buffer_command': 'same-buffer' } ) @patch( 'vim.command', new_callable = ExtendedMock ) diff --git a/python/ycm/vimsupport.py b/python/ycm/vimsupport.py index 1e84f16a..92d759b9 100644 --- a/python/ycm/vimsupport.py +++ b/python/ycm/vimsupport.py @@ -371,8 +371,9 @@ def BufferIsUsable( buffer_object ): return not BufferModified( buffer_object ) or HiddenEnabled( buffer_object ) -def EscapedFilepath( filepath ): - return filepath.replace( ' ' , r'\ ' ) +def EscapeFilepathForVimCommand( filepath ): + to_eval = "fnameescape('{0}')".format( EscapeForVim( filepath ) ) + return GetVariableValue( to_eval ) # Both |line| and |column| need to be 1-based @@ -422,8 +423,8 @@ def JumpToLocation( filename, line, column ): vim_command = GetVimCommand( user_command ) try: - vim.command( 'keepjumps {0} {1}'.format( vim_command, - EscapedFilepath( filename ) ) ) + escaped_filename = EscapeFilepathForVimCommand( filename ) + vim.command( 'keepjumps {0} {1}'.format( vim_command, escaped_filename ) ) # When the file we are trying to jump to has a swap file # Vim opens swap-exists-choices dialog and throws vim.error with E325 error, # or KeyboardInterrupt after user selects one of the options.