buffer.py 4.0 KB

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