From 67d2a0fbf8976e0fe2f17585742af4a7ff707ef7 Mon Sep 17 00:00:00 2001 From: micbou Date: Sun, 26 Nov 2017 16:30:59 +0100 Subject: [PATCH] Fix error when filetype is a number The VimExpressionToPythonType function automatically convert a number represented as a string to an integer. This causes an error when used to evaluate a filetype set to a number as the result is split on the dot character to get a list of filetypes and an integer cannot be split. Use vim.eval and ToUnicode instead. --- python/ycm/tests/vimsupport_test.py | 11 ++++++++++- python/ycm/vimsupport.py | 6 +++--- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/python/ycm/tests/vimsupport_test.py b/python/ycm/tests/vimsupport_test.py index d6975de5..f3dea356 100644 --- a/python/ycm/tests/vimsupport_test.py +++ b/python/ycm/tests/vimsupport_test.py @@ -32,7 +32,7 @@ MockVimModule() from ycm import vimsupport from nose.tools import eq_ -from hamcrest import assert_that, calling, equal_to, has_entry, raises +from hamcrest import assert_that, calling, contains, equal_to, has_entry, raises from mock import MagicMock, call, patch from ycmd.utils import ToBytes import os @@ -1553,6 +1553,15 @@ def SelectFromList_Negative_test( vim_eval ): raises( RuntimeError, vimsupport.NO_SELECTION_MADE_MSG ) ) +def Filetypes_IntegerFiletype_test(): + current_buffer = VimBuffer( 'buffer', number = 1, filetype = '42' ) + with MockVimBuffers( [ current_buffer ], current_buffer ): + assert_that( vimsupport.CurrentFiletypes(), contains( '42' ) ) + assert_that( vimsupport.GetBufferFiletypes( 1 ), contains( '42' ) ) + assert_that( vimsupport.FiletypesForBuffer( current_buffer ), + contains( '42' ) ) + + @patch( 'ycm.vimsupport.VariableExists', return_value = False ) @patch( 'ycm.vimsupport.SearchInCurrentBuffer', return_value = 0 ) @patch( 'vim.current' ) diff --git a/python/ycm/vimsupport.py b/python/ycm/vimsupport.py index bc5dbbd7..1e84f16a 100644 --- a/python/ycm/vimsupport.py +++ b/python/ycm/vimsupport.py @@ -589,18 +589,18 @@ def EscapeForVim( text ): def CurrentFiletypes(): - return VimExpressionToPythonType( "&filetype" ).split( '.' ) + return ToUnicode( vim.eval( "&filetype" ) ).split( '.' ) def GetBufferFiletypes( bufnr ): command = 'getbufvar({0}, "&ft")'.format( bufnr ) - return VimExpressionToPythonType( command ).split( '.' ) + return ToUnicode( vim.eval( command ) ).split( '.' ) def FiletypesForBuffer( buffer_object ): # NOTE: Getting &ft for other buffers only works when the buffer has been # visited by the user at least once, which is true for modified buffers - return GetBufferOption( buffer_object, 'ft' ).split( '.' ) + return ToUnicode( GetBufferOption( buffer_object, 'ft' ) ).split( '.' ) def VariableExists( variable ):