buffer.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 __future__ import unicode_literals
  18. from __future__ import print_function
  19. from __future__ import division
  20. from __future__ import absolute_import
  21. # Not installing aliases from python-future; it's unreliable and slow.
  22. from builtins import * # noqa
  23. from ycm import vimsupport
  24. from ycm.client.event_notification import EventNotification
  25. from ycm.diagnostic_interface import DiagnosticInterface
  26. DIAGNOSTIC_UI_FILETYPES = { 'cpp', 'cs', 'c', 'objc', 'objcpp', 'cuda',
  27. 'javascript', 'typescript' }
  28. DIAGNOSTIC_UI_ASYNC_FILETYPES = { 'java' }
  29. # Emulates Vim buffer
  30. # Used to store buffer related information like diagnostics, latest parse
  31. # request. Stores buffer change tick at the parse request moment, allowing
  32. # to effectively determine whether reparse is needed for the buffer.
  33. class Buffer( object ):
  34. def __init__( self, bufnr, user_options, async_diags ):
  35. self.number = bufnr
  36. self._parse_tick = 0
  37. self._handled_tick = 0
  38. self._parse_request = None
  39. self._async_diags = async_diags
  40. self._diag_interface = DiagnosticInterface( bufnr, user_options )
  41. def FileParseRequestReady( self, block = False ):
  42. return bool( self._parse_request and
  43. ( block or self._parse_request.Done() ) )
  44. def SendParseRequest( self, extra_data ):
  45. self._parse_request = EventNotification( 'FileReadyToParse',
  46. extra_data = extra_data )
  47. self._parse_request.Start()
  48. # Decrement handled tick to ensure correct handling when we are forcing
  49. # reparse on buffer visit and changed tick remains the same.
  50. self._handled_tick -= 1
  51. self._parse_tick = self._ChangedTick()
  52. def NeedsReparse( self ):
  53. return self._parse_tick != self._ChangedTick()
  54. def ShouldResendParseRequest( self ):
  55. return bool( self._parse_request and self._parse_request.ShouldResend() )
  56. def UpdateDiagnostics( self, force=False ):
  57. if force or not self._async_diags:
  58. self.UpdateWithNewDiagnostics( self._parse_request.Response() )
  59. else:
  60. # We need to call the response method, because it might throw an exception
  61. # or require extra config confirmation, even if we don't actually use the
  62. # diagnostics.
  63. self._parse_request.Response()
  64. def UpdateWithNewDiagnostics( self, diagnostics ):
  65. self._diag_interface.UpdateWithNewDiagnostics( diagnostics )
  66. def UpdateMatches( self ):
  67. self._diag_interface.UpdateMatches()
  68. def PopulateLocationList( self ):
  69. return self._diag_interface.PopulateLocationList()
  70. def GetResponse( self ):
  71. return self._parse_request.Response()
  72. def IsResponseHandled( self ):
  73. return self._handled_tick == self._parse_tick
  74. def MarkResponseHandled( self ):
  75. self._handled_tick = self._parse_tick
  76. def OnCursorMoved( self ):
  77. self._diag_interface.OnCursorMoved()
  78. def GetErrorCount( self ):
  79. return self._diag_interface.GetErrorCount()
  80. def GetWarningCount( self ):
  81. return self._diag_interface.GetWarningCount()
  82. def _ChangedTick( self ):
  83. return vimsupport.GetBufferChangedTick( self.number )
  84. class BufferDict( dict ):
  85. def __init__( self, user_options ):
  86. self._user_options = user_options
  87. def __missing__( self, key ):
  88. # Python does not allow to return assignment operation result directly
  89. new_value = self[ key ] = Buffer(
  90. key,
  91. self._user_options,
  92. any( x in DIAGNOSTIC_UI_ASYNC_FILETYPES
  93. for x in vimsupport.GetBufferFiletypes( key ) ) )
  94. return new_value