buffer.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. DIAGNOSTIC_UI_FILETYPES = { 'cpp', 'cs', 'c', 'objc', 'objcpp', 'cuda',
  21. 'javascript', 'typescript', 'typescriptreact' }
  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.UpdateFromFileTypes( filetypes )
  35. def FileParseRequestReady( self, block = False ):
  36. return bool( self._parse_request and
  37. ( block or self._parse_request.Done() ) )
  38. def SendParseRequest( self, extra_data ):
  39. # Don't send a parse request if one is in progress
  40. if self._parse_request is not None and not self._parse_request.Done():
  41. self._should_resend = True
  42. return
  43. self._should_resend = False
  44. self._parse_request = EventNotification( 'FileReadyToParse',
  45. extra_data = extra_data )
  46. self._parse_request.Start()
  47. # Decrement handled tick to ensure correct handling when we are forcing
  48. # reparse on buffer visit and changed tick remains the same.
  49. self._handled_tick -= 1
  50. self._parse_tick = self._ChangedTick()
  51. def NeedsReparse( self ):
  52. return self._parse_tick != self._ChangedTick()
  53. def ShouldResendParseRequest( self ):
  54. return ( self._should_resend
  55. or ( bool( self._parse_request )
  56. and self._parse_request.ShouldResend() ) )
  57. def UpdateDiagnostics( self, force = False ):
  58. if force or not self._async_diags:
  59. self.UpdateWithNewDiagnostics( self._parse_request.Response() )
  60. else:
  61. # We need to call the response method, because it might throw an exception
  62. # or require extra config confirmation, even if we don't actually use the
  63. # diagnostics.
  64. self._parse_request.Response()
  65. def UpdateWithNewDiagnostics( self, diagnostics ):
  66. self._diag_interface.UpdateWithNewDiagnostics( diagnostics )
  67. def UpdateMatches( self ):
  68. self._diag_interface.UpdateMatches()
  69. def PopulateLocationList( self ):
  70. return self._diag_interface.PopulateLocationList()
  71. def GetResponse( self ):
  72. return self._parse_request.Response()
  73. def IsResponseHandled( self ):
  74. return self._handled_tick == self._parse_tick
  75. def MarkResponseHandled( self ):
  76. self._handled_tick = self._parse_tick
  77. def OnCursorMoved( self ):
  78. self._diag_interface.OnCursorMoved()
  79. def GetErrorCount( self ):
  80. return self._diag_interface.GetErrorCount()
  81. def GetWarningCount( self ):
  82. return self._diag_interface.GetWarningCount()
  83. def UpdateFromFileTypes( self, filetypes ):
  84. self._filetypes = filetypes
  85. self._async_diags = not any( x in DIAGNOSTIC_UI_FILETYPES
  86. for x in filetypes )
  87. def _ChangedTick( self ):
  88. return vimsupport.GetBufferChangedTick( self._number )
  89. class BufferDict( dict ):
  90. def __init__( self, user_options ):
  91. self._user_options = user_options
  92. def __missing__( self, key ):
  93. # Python does not allow to return assignment operation result directly
  94. new_value = self[ key ] = Buffer(
  95. key,
  96. self._user_options,
  97. vimsupport.GetBufferFiletypes( key ) )
  98. return new_value