buffer.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. # Emulates Vim buffer
  22. # Used to store buffer related information like diagnostics, latest parse
  23. # request. Stores buffer change tick at the parse request moment, allowing
  24. # to effectively determine whether reparse is needed for the buffer.
  25. class Buffer:
  26. def __init__( self, bufnr, user_options, filetypes ):
  27. self._number = bufnr
  28. self._parse_tick = 0
  29. self._handled_tick = 0
  30. self._parse_request = None
  31. self._should_resend = False
  32. self._diag_interface = DiagnosticInterface( bufnr, user_options )
  33. self._open_loclist_on_ycm_diags = user_options[
  34. 'open_loclist_on_ycm_diags' ]
  35. self._semantic_highlighting = SemanticHighlighting( bufnr,
  36. user_options )
  37. self.UpdateFromFileTypes( filetypes )
  38. def FileParseRequestReady( self, block = False ):
  39. return bool( self._parse_request and
  40. ( block or self._parse_request.Done() ) )
  41. def SendParseRequest( self, extra_data ):
  42. # Don't send a parse request if one is in progress
  43. if self._parse_request is not None and not self._parse_request.Done():
  44. self._should_resend = True
  45. return
  46. self._should_resend = False
  47. self._parse_request = EventNotification( 'FileReadyToParse',
  48. extra_data = extra_data )
  49. self._parse_request.Start()
  50. # Decrement handled tick to ensure correct handling when we are forcing
  51. # reparse on buffer visit and changed tick remains the same.
  52. self._handled_tick -= 1
  53. self._parse_tick = self._ChangedTick()
  54. def NeedsReparse( self ):
  55. return self._parse_tick != self._ChangedTick()
  56. def ShouldResendParseRequest( self ):
  57. return ( self._should_resend
  58. or ( bool( self._parse_request )
  59. and self._parse_request.ShouldResend() ) )
  60. def UpdateDiagnostics( self, force = False ):
  61. if force or not self._async_diags:
  62. self.UpdateWithNewDiagnostics( self._parse_request.Response(), False )
  63. else:
  64. # We need to call the response method, because it might throw an exception
  65. # or require extra config confirmation, even if we don't actually use the
  66. # diagnostics.
  67. self._parse_request.Response()
  68. def UpdateWithNewDiagnostics( self, diagnostics, async_message ):
  69. self._async_diags = async_message
  70. self._diag_interface.UpdateWithNewDiagnostics(
  71. diagnostics,
  72. not self._async_diags and self._open_loclist_on_ycm_diags )
  73. def UpdateMatches( self ):
  74. self._diag_interface.UpdateMatches()
  75. def PopulateLocationList( self, open_on_edit = False ):
  76. return self._diag_interface.PopulateLocationList( open_on_edit )
  77. def GetResponse( self ):
  78. return self._parse_request.Response()
  79. def IsResponseHandled( self ):
  80. return self._handled_tick == self._parse_tick
  81. def MarkResponseHandled( self ):
  82. self._handled_tick = self._parse_tick
  83. def OnCursorMoved( self ):
  84. self._diag_interface.OnCursorMoved()
  85. def GetErrorCount( self ):
  86. return self._diag_interface.GetErrorCount()
  87. def GetWarningCount( self ):
  88. return self._diag_interface.GetWarningCount()
  89. def RefreshDiagnosticsUI( self ):
  90. return self._diag_interface.RefreshDiagnosticsUI()
  91. def DiagnosticsForLine( self, line_number ):
  92. return self._diag_interface.DiagnosticsForLine( line_number )
  93. def UpdateFromFileTypes( self, filetypes ):
  94. self._filetypes = filetypes
  95. # We will set this to true if we ever receive any diagnostics asyncronously.
  96. self._async_diags = False
  97. def SendSemanticTokensRequest( self ):
  98. self._semantic_highlighting.SendRequest()
  99. def SemanticTokensRequestReady( self ):
  100. return self._semantic_highlighting.IsResponseReady()
  101. def UpdateSemanticTokens( self ):
  102. return self._semantic_highlighting.Update()
  103. def _ChangedTick( self ):
  104. return vimsupport.GetBufferChangedTick( self._number )
  105. class BufferDict( dict ):
  106. def __init__( self, user_options ):
  107. self._user_options = user_options
  108. def __missing__( self, key ):
  109. # Python does not allow to return assignment operation result directly
  110. new_value = self[ key ] = Buffer(
  111. key,
  112. self._user_options,
  113. vimsupport.GetBufferFiletypes( key ) )
  114. return new_value