buffer.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. # Copyright (C) 2016, Davit Samvelyan
  2. #
  3. # This file is part of YouCompleteMe.
  4. #
  5. # YouCompleteMe is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # YouCompleteMe is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with YouCompleteMe. If not, see <http://www.gnu.org/licenses/>.
  17. from ycm import vimsupport
  18. from ycm.client.event_notification import EventNotification
  19. from ycm.diagnostic_interface import DiagnosticInterface
  20. from ycm.semantic_highlighting import SemanticHighlighting
  21. from ycm.inlay_hints import InlayHints
  22. # Emulates Vim buffer
  23. # Used to store buffer related information like diagnostics, latest parse
  24. # request. Stores buffer change tick at the parse request moment, allowing
  25. # to effectively determine whether reparse is needed for the buffer.
  26. class Buffer:
  27. def __init__( self, bufnr, user_options, filetypes ):
  28. self._number = bufnr
  29. self._parse_tick = 0
  30. self._handled_tick = 0
  31. self._parse_request = None
  32. self._should_resend = False
  33. self._diag_interface = DiagnosticInterface( bufnr, user_options )
  34. self._open_loclist_on_ycm_diags = user_options[
  35. 'open_loclist_on_ycm_diags' ]
  36. self.semantic_highlighting = SemanticHighlighting( bufnr )
  37. self.inlay_hints = InlayHints( bufnr )
  38. self.UpdateFromFileTypes( filetypes )
  39. def FileParseRequestReady( self, block = False ):
  40. return ( bool( self._parse_request ) and
  41. ( block or self._parse_request.Done() ) )
  42. def SendParseRequest( self, extra_data ):
  43. # Don't send a parse request if one is in progress
  44. if self._parse_request is not None and not self._parse_request.Done():
  45. self._should_resend = True
  46. return
  47. self._should_resend = False
  48. self._parse_request = EventNotification( 'FileReadyToParse',
  49. extra_data = extra_data )
  50. self._parse_request.Start()
  51. # Decrement handled tick to ensure correct handling when we are forcing
  52. # reparse on buffer visit and changed tick remains the same.
  53. self._handled_tick -= 1
  54. self._parse_tick = self._ChangedTick()
  55. def ParseRequestPending( self ):
  56. return bool( self._parse_request ) and not self._parse_request.Done()
  57. def NeedsReparse( self ):
  58. return self._parse_tick != self._ChangedTick()
  59. def ShouldResendParseRequest( self ):
  60. return ( self._should_resend
  61. or ( bool( self._parse_request )
  62. and self._parse_request.ShouldResend() ) )
  63. def UpdateDiagnostics( self, force = False ):
  64. if force or not self._async_diags:
  65. self.UpdateWithNewDiagnostics( self._parse_request.Response(), False )
  66. else:
  67. # We need to call the response method, because it might throw an exception
  68. # or require extra config confirmation, even if we don't actually use the
  69. # diagnostics.
  70. self._parse_request.Response()
  71. def UpdateWithNewDiagnostics( self, diagnostics, async_message ):
  72. self._async_diags = async_message
  73. self._diag_interface.UpdateWithNewDiagnostics(
  74. diagnostics,
  75. not self._async_diags and self._open_loclist_on_ycm_diags )
  76. def UpdateMatches( self ):
  77. self._diag_interface.UpdateMatches()
  78. def PopulateLocationList( self, open_on_edit = False ):
  79. return self._diag_interface.PopulateLocationList( open_on_edit )
  80. def GetResponse( self ):
  81. return self._parse_request.Response()
  82. def IsResponseHandled( self ):
  83. return self._handled_tick == self._parse_tick
  84. def MarkResponseHandled( self ):
  85. self._handled_tick = self._parse_tick
  86. def OnCursorMoved( self ):
  87. self._diag_interface.OnCursorMoved()
  88. def GetErrorCount( self ):
  89. return self._diag_interface.GetErrorCount()
  90. def GetWarningCount( self ):
  91. return self._diag_interface.GetWarningCount()
  92. def RefreshDiagnosticsUI( self ):
  93. return self._diag_interface.RefreshDiagnosticsUI()
  94. def ClearDiagnosticsUI( self ):
  95. return self._diag_interface.ClearDiagnosticsUI()
  96. def DiagnosticsForLine( self, line_number ):
  97. return self._diag_interface.DiagnosticsForLine( line_number )
  98. def UpdateFromFileTypes( self, filetypes ):
  99. self._filetypes = filetypes
  100. # We will set this to true if we ever receive any diagnostics asyncronously.
  101. self._async_diags = False
  102. def _ChangedTick( self ):
  103. return vimsupport.GetBufferChangedTick( self._number )
  104. class BufferDict( dict ):
  105. def __init__( self, user_options ):
  106. self._user_options = user_options
  107. def __missing__( self, key ):
  108. # Python does not allow to return assignment operation result directly
  109. new_value = self[ key ] = Buffer(
  110. key,
  111. self._user_options,
  112. vimsupport.GetBufferFiletypes( key ) )
  113. return new_value