From 65e6b55268ef2329a36c9ff8787227a146f6b989 Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Sat, 4 Jan 2014 18:32:42 -0800 Subject: [PATCH] Now showing red squiggle under diagnostic location --- autoload/youcompleteme.vim | 23 +++++++++++++++++++++++ python/ycm/diagnostic_interface.py | 18 +++++++++++++++++- python/ycm/vimsupport.py | 15 +++++++++++++++ 3 files changed, 55 insertions(+), 1 deletion(-) diff --git a/autoload/youcompleteme.vim b/autoload/youcompleteme.vim index 508dcddd..dc4d5460 100644 --- a/autoload/youcompleteme.vim +++ b/autoload/youcompleteme.vim @@ -75,6 +75,7 @@ function! youcompleteme#Enable() endif call s:SetUpSigns() + call s:SetUpSyntaxHighlighting() if g:ycm_allow_changing_updatetime set ut=2000 @@ -194,6 +195,28 @@ function! s:SetUpSigns() endfunction +function! s:SetUpSyntaxHighlighting() + " We try to ensure backwards compatibility with Syntastic if the user has + " already defined styling for Syntastic highlight groups. + + if !hlexists( 'YcmErrorSection' ) + if hlexists( 'SyntasticError' ) + highlight link YcmErrorSection SyntasticError + else + highlight link YcmErrorSection SpellBad + endif + endif + + if !hlexists( 'YcmWarningSection' ) + if hlexists( 'SyntasticWarning' ) + highlight link YcmWarningSection SyntasticWarning + else + highlight link YcmWarningSection SpellCap + endif + endif +endfunction + + function! s:SetUpBackwardsCompatibility() let complete_in_comments_and_strings = \ get( g:, 'ycm_complete_in_comments_and_strings', 0 ) diff --git a/python/ycm/diagnostic_interface.py b/python/ycm/diagnostic_interface.py index 654dd36a..dce604ed 100644 --- a/python/ycm/diagnostic_interface.py +++ b/python/ycm/diagnostic_interface.py @@ -44,6 +44,7 @@ class DiagnosticInterface( object ): self._buffer_number_to_line_to_diags = _ConvertDiagListToDict( diags ) self._next_sign_id = _UpdateSigns( self._buffer_number_to_line_to_diags, self._next_sign_id ) + _UpdateSquiggles( self._buffer_number_to_line_to_diags ) def _EchoDiagnosticForLine( self, line_num ): @@ -56,6 +57,17 @@ class DiagnosticInterface( object ): vimsupport.EchoTextVimWidth( diags[ 0 ][ 'text' ] ) +def _UpdateSquiggles( buffer_number_to_line_to_diags ): + vimsupport.ClearYcmSyntaxMatches() + line_to_diags = buffer_number_to_line_to_diags[ vim.current.buffer.number ] + + for diags in line_to_diags.itervalues(): + for diag in diags: + vimsupport.AddDiagnosticSyntaxMatch( diag[ 'lnum' ], + diag[ 'col' ], + _DiagnosticIsError( diag ) ) + + def _UpdateSigns( buffer_number_to_line_to_diags, next_sign_id ): for buffer_number, line_to_diags in buffer_number_to_line_to_diags.iteritems(): if not vimsupport.BufferIsVisible( buffer_number ): @@ -67,7 +79,7 @@ def _UpdateSigns( buffer_number_to_line_to_diags, next_sign_id ): vimsupport.PlaceSign( next_sign_id, line, buffer_number, - diag[ 'type' ] == 'E' ) + _DiagnosticIsError( diag ) ) next_sign_id += 1 return next_sign_id @@ -83,3 +95,7 @@ def _ConvertDiagListToDict( diag_list ): diags.sort( key = lambda diag: itemgetter( 'col', 'type' ) ) return buffer_to_line_to_diags + +def _DiagnosticIsError( diag ): + return diag[ 'type' ] == 'E' + diff --git a/python/ycm/vimsupport.py b/python/ycm/vimsupport.py index 9ac07d8a..4067010b 100644 --- a/python/ycm/vimsupport.py +++ b/python/ycm/vimsupport.py @@ -107,6 +107,7 @@ def GetBufferFilepath( buffer_object ): return os.path.join( os.getcwd(), str( buffer_object.number ) ) +# TODO: only unplace our signs, not all signs def UnplaceAllSignsInBuffer( buffer_number ): if buffer_number < 0: return @@ -119,6 +120,20 @@ def PlaceSign( sign_id, line_num, buffer_num, is_error = True ): sign_id, line_num, sign_name, buffer_num ) ) +def ClearYcmSyntaxMatches(): + matches = VimExpressionToPythonType( 'getmatches()' ) + for match in matches: + if match[ 'group' ].startswith( 'Ycm' ): + vim.eval( 'matchdelete({0})'.format( match[ 'id' ] ) ) + + +# Returns the ID of the newly added match +def AddDiagnosticSyntaxMatch( line_num, column_num, is_error ): + group = 'YcmErrorSection' if is_error else 'YcmWarningSection' + return GetIntValue( + "matchadd('{0}', '\%{1}l\%{2}c')".format( group, line_num, column_num ) ) + + # Given a dict like {'a': 1}, loads it into Vim as if you ran 'let g:a = 1' # When |overwrite| is True, overwrites the existing value in Vim. def LoadDictIntoVimGlobals( new_globals, overwrite = True ):