Use fnameescape to escape filepath

This commit is contained in:
micbou 2017-11-26 18:13:33 +01:00
parent d99eb5d5cb
commit c3859791ea
No known key found for this signature in database
GPG Key ID: C7E8FD1F3BDA1E05
3 changed files with 10 additions and 11 deletions

View File

@ -47,6 +47,7 @@ MATCHADD_REGEX = re.compile(
MATCHDELETE_REGEX = re.compile( '^matchdelete\((?P<id>\d+)\)$' ) MATCHDELETE_REGEX = re.compile( '^matchdelete\((?P<id>\d+)\)$' )
OMNIFUNC_REGEX_FORMAT = ( OMNIFUNC_REGEX_FORMAT = (
'^{omnifunc_name}\((?P<findstart>[01]),[\'"](?P<base>.*)[\'"]\)$' ) '^{omnifunc_name}\((?P<findstart>[01]),[\'"](?P<base>.*)[\'"]\)$' )
FNAMEESCAPE_REGEX = re.compile( '^fnameescape\(\'(?P<filepath>.+)\'\)$' )
# One-and only instance of mocked Vim object. The first 'import vim' that is # 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, # 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: if result is not None:
return result return result
match = FNAMEESCAPE_REGEX.search( value )
if match:
return match.group( 'filepath' )
raise ValueError( 'Unexpected evaluation: {0}'.format( value ) ) raise ValueError( 'Unexpected evaluation: {0}'.format( value ) )

View File

@ -1606,13 +1606,6 @@ def InsertNamespace_append_test( vim_current, *args ):
AssertBuffersAreEqualAsBytes( expected_buffer, vim_current.buffer ) 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', @patch( 'ycmd.user_options_store._USER_OPTIONS',
{ 'goto_buffer_command': 'same-buffer' } ) { 'goto_buffer_command': 'same-buffer' } )
@patch( 'vim.command', new_callable = ExtendedMock ) @patch( 'vim.command', new_callable = ExtendedMock )

View File

@ -371,8 +371,9 @@ def BufferIsUsable( buffer_object ):
return not BufferModified( buffer_object ) or HiddenEnabled( buffer_object ) return not BufferModified( buffer_object ) or HiddenEnabled( buffer_object )
def EscapedFilepath( filepath ): def EscapeFilepathForVimCommand( filepath ):
return filepath.replace( ' ' , r'\ ' ) to_eval = "fnameescape('{0}')".format( EscapeForVim( filepath ) )
return GetVariableValue( to_eval )
# Both |line| and |column| need to be 1-based # Both |line| and |column| need to be 1-based
@ -422,8 +423,8 @@ def JumpToLocation( filename, line, column ):
vim_command = GetVimCommand( user_command ) vim_command = GetVimCommand( user_command )
try: try:
vim.command( 'keepjumps {0} {1}'.format( vim_command, escaped_filename = EscapeFilepathForVimCommand( filename )
EscapedFilepath( filename ) ) ) vim.command( 'keepjumps {0} {1}'.format( vim_command, escaped_filename ) )
# When the file we are trying to jump to has a swap file # 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, # Vim opens swap-exists-choices dialog and throws vim.error with E325 error,
# or KeyboardInterrupt after user selects one of the options. # or KeyboardInterrupt after user selects one of the options.