123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332 |
- from collections import defaultdict
- from ycm import vimsupport
- from ycm.diagnostic_filter import DiagnosticFilter, CompileLevel
- from ycm import text_properties as tp
- import vim
- YCM_VIM_PROPERTY_ID = 0
- class DiagnosticInterface:
- def __init__( self, bufnr, user_options ):
- self._bufnr = bufnr
- self._user_options = user_options
- self._diagnostics = []
- self._diag_filter = DiagnosticFilter.CreateFromOptions( user_options )
-
- self._line_to_diags = defaultdict( list )
- self._previous_diag_line_number = -1
- self._diag_message_needs_clearing = False
- def OnCursorMoved( self ):
- if self._user_options[ 'echo_current_diagnostic' ]:
- line, _ = vimsupport.CurrentLineAndColumn()
- line += 1
- if line != self._previous_diag_line_number:
- self._EchoDiagnosticForLine( line )
- def GetErrorCount( self ):
- return self._DiagnosticsCount( _DiagnosticIsError )
- def GetWarningCount( self ):
- return self._DiagnosticsCount( _DiagnosticIsWarning )
- def PopulateLocationList( self, open_on_edit = False ):
-
- if not self._user_options[ 'always_populate_location_list' ]:
- self._UpdateLocationLists( open_on_edit )
- return bool( self._diagnostics )
- def UpdateWithNewDiagnostics( self, diags, open_on_edit = False ):
- self._diagnostics = [ _NormalizeDiagnostic( x ) for x in
- self._ApplyDiagnosticFilter( diags ) ]
- self._ConvertDiagListToDict()
- if ( self._user_options[ 'update_diagnostics_in_insert_mode' ] or
- 'i' not in vim.eval( 'mode()' ) ):
- self.RefreshDiagnosticsUI( open_on_edit )
- def RefreshDiagnosticsUI( self, open_on_edit = False ):
- if self._user_options[ 'echo_current_diagnostic' ]:
- self._EchoDiagnostic()
- if self._user_options[ 'enable_diagnostic_signs' ]:
- self._UpdateSigns()
- self.UpdateMatches()
- if self._user_options[ 'always_populate_location_list' ]:
- self._UpdateLocationLists( open_on_edit )
- def DiagnosticsForLine( self, line_number ):
- return self._line_to_diags[ line_number ]
- def _ApplyDiagnosticFilter( self, diags ):
- filetypes = vimsupport.GetBufferFiletypes( self._bufnr )
- diag_filter = self._diag_filter.SubsetForTypes( filetypes )
- return filter( diag_filter.IsAllowed, diags )
- def _EchoDiagnostic( self ):
- line, _ = vimsupport.CurrentLineAndColumn()
- line += 1
- self._EchoDiagnosticForLine( line )
- def _EchoDiagnosticForLine( self, line_num ):
- self._previous_diag_line_number = line_num
- diags = self._line_to_diags[ line_num ]
- text = None
- if diags:
- first_diag = diags[ 0 ]
- text = first_diag[ 'text' ]
- if first_diag.get( 'fixit_available', False ):
- text += ' (FixIt)'
- if self._user_options[ 'echo_current_diagnostic' ] == 'virtual-text':
- if self._diag_message_needs_clearing:
-
- tp.ClearTextProperties( self._bufnr, type = 'YcmVirtError' )
- tp.ClearTextProperties( self._bufnr, type = 'YcmVirtWarning' )
- self._diag_message_needs_clearing = False
- if not text:
- return
- def MakeVritualTextProperty( prop_type, text, position='after' ):
- vimsupport.AddTextProperty( self._bufnr,
- line_num,
- 0,
- prop_type,
- {
- 'text': text,
- 'text_align': position,
- 'text_wrap': 'wrap'
- } )
- MakeVritualTextProperty(
- 'YcmPadding',
- ' ' * vim.buffers[ self._bufnr ].options[ 'shiftwidth' ] ),
- MakeVritualTextProperty(
- 'YcmVirtError' if _DiagnosticIsError( first_diag )
- else 'YcmVirtWarning',
- [ line for line in text.splitlines() if line ][ 0 ] )
- else:
- if not text:
- if self._diag_message_needs_clearing:
-
- vimsupport.PostVimMessage( '', warning = False )
- self._diag_message_needs_clearing = False
- return
- vimsupport.PostVimMessage( text, warning = False, truncate = True )
- self._diag_message_needs_clearing = True
- def _DiagnosticsCount( self, predicate ):
- count = 0
- for diags in self._line_to_diags.values():
- count += sum( 1 for d in diags if predicate( d ) )
- return count
- def _UpdateLocationLists( self, open_on_edit = False ):
- vimsupport.SetLocationListsForBuffer(
- self._bufnr,
- vimsupport.ConvertDiagnosticsToQfList( self._diagnostics ),
- open_on_edit )
- def UpdateMatches( self ):
- if not self._user_options[ 'enable_diagnostic_highlighting' ]:
- return
- props_to_remove = vimsupport.GetTextProperties( self._bufnr )
- for diags in self._line_to_diags.values():
-
- for diag in reversed( diags ):
- for line, column, name, extras in _ConvertDiagnosticToTextProperties(
- self._bufnr,
- diag ):
- global YCM_VIM_PROPERTY_ID
-
-
- diag_prop = vimsupport.DiagnosticProperty(
- YCM_VIM_PROPERTY_ID,
- name,
- line,
- column,
- extras[ 'end_col' ] - column if 'end_col' in extras else column )
- try:
- props_to_remove.remove( diag_prop )
- except ValueError:
- extras.update( {
- 'id': YCM_VIM_PROPERTY_ID
- } )
- vimsupport.AddTextProperty( self._bufnr,
- line,
- column,
- name,
- extras )
- YCM_VIM_PROPERTY_ID += 1
- for prop in props_to_remove:
- vimsupport.RemoveDiagnosticProperty( self._bufnr, prop )
- def _UpdateSigns( self ):
- signs_to_unplace = vimsupport.GetSignsInBuffer( self._bufnr )
- signs_to_place = []
- for line, diags in self._line_to_diags.items():
- if not diags:
- continue
-
-
-
- name = 'YcmError' if _DiagnosticIsError( diags[ 0 ] ) else 'YcmWarning'
- sign = {
- 'lnum': line,
- 'name': name,
- 'buffer': self._bufnr,
- 'group': 'ycm_signs'
- }
- try:
- signs_to_unplace.remove( sign )
- except ValueError:
- signs_to_place.append( sign )
- vim.eval( f'sign_placelist( { signs_to_place } )' )
- vim.eval( f'sign_unplacelist( { signs_to_unplace } )' )
- def _ConvertDiagListToDict( self ):
- self._line_to_diags = defaultdict( list )
- for diag in self._diagnostics:
- location = diag[ 'location' ]
- bufnr = vimsupport.GetBufferNumberForFilename( location[ 'filepath' ] )
- if bufnr == self._bufnr:
- line_number = location[ 'line_num' ]
- self._line_to_diags[ line_number ].append( diag )
- for diags in self._line_to_diags.values():
-
-
- diags.sort( key = lambda diag: ( diag[ 'kind' ],
- diag[ 'location' ][ 'column_num' ] ) )
- _DiagnosticIsError = CompileLevel( 'error' )
- _DiagnosticIsWarning = CompileLevel( 'warning' )
- def _NormalizeDiagnostic( diag ):
- def ClampToOne( value ):
- return value if value > 0 else 1
- location = diag[ 'location' ]
- location[ 'column_num' ] = ClampToOne( location[ 'column_num' ] )
- location[ 'line_num' ] = ClampToOne( location[ 'line_num' ] )
- return diag
- def _ConvertDiagnosticToTextProperties( bufnr, diagnostic ):
- properties = []
- name = ( 'YcmErrorProperty' if _DiagnosticIsError( diagnostic ) else
- 'YcmWarningProperty' )
- if vimsupport.VimIsNeovim():
- name = name.replace( 'Property', 'Section' )
- location_extent = diagnostic[ 'location_extent' ]
- if location_extent[ 'start' ][ 'line_num' ] <= 0:
- location = diagnostic[ 'location' ]
- line, column = vimsupport.LineAndColumnNumbersClamped(
- bufnr,
- location[ 'line_num' ],
- location[ 'column_num' ]
- )
- properties.append( ( line, column, name, {} ) )
- else:
- start_line, start_column = vimsupport.LineAndColumnNumbersClamped(
- bufnr,
- location_extent[ 'start' ][ 'line_num' ],
- location_extent[ 'start' ][ 'column_num' ]
- )
- end_line, end_column = vimsupport.LineAndColumnNumbersClamped(
- bufnr,
- location_extent[ 'end' ][ 'line_num' ],
- location_extent[ 'end' ][ 'column_num' ]
- )
- properties.append( (
- start_line,
- start_column,
- name,
- { 'end_lnum': end_line,
- 'end_col': end_column } ) )
- for diagnostic_range in diagnostic[ 'ranges' ]:
- start_line, start_column = vimsupport.LineAndColumnNumbersClamped(
- bufnr,
- diagnostic_range[ 'start' ][ 'line_num' ],
- diagnostic_range[ 'start' ][ 'column_num' ]
- )
- end_line, end_column = vimsupport.LineAndColumnNumbersClamped(
- bufnr,
- diagnostic_range[ 'end' ][ 'line_num' ],
- diagnostic_range[ 'end' ][ 'column_num' ]
- )
- if not _IsValidRange( start_line, start_column, end_line, end_column ):
- continue
- properties.append( (
- start_line,
- start_column,
- name,
- { 'end_lnum': end_line,
- 'end_col': end_column } ) )
- return properties
- def _IsValidRange( start_line, start_column, end_line, end_column ):
-
- if start_line > end_line:
- return False
-
- if start_line < end_line:
- return True
-
- if start_column > end_column:
- return False
-
- return True
|