buffer.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. # Emulates Vim buffer
  27. # Used to store buffer related information like diagnostics, latest parse
  28. # request. Stores buffer change tick at the parse request moment, allowing
  29. # to effectively determine whether reparse is needed for the buffer.
  30. class Buffer( object ):
  31. def __init__( self, bufnr, user_options ):
  32. self.number = bufnr
  33. self._parse_tick = 0
  34. self._handled_tick = 0
  35. self._parse_request = None
  36. self._diag_interface = DiagnosticInterface( bufnr, user_options )
  37. def FileParseRequestReady( self, block = False ):
  38. return bool( self._parse_request and
  39. ( block or self._parse_request.Done() ) )
  40. def SendParseRequest( self, extra_data ):
  41. self._parse_request = EventNotification( 'FileReadyToParse',
  42. extra_data = extra_data )
  43. self._parse_request.Start()
  44. # Decrement handled tick to ensure correct handling when we are forcing
  45. # reparse on buffer visit and changed tick remains the same.
  46. self._handled_tick -= 1
  47. self._parse_tick = self._ChangedTick()
  48. def NeedsReparse( self ):
  49. return self._parse_tick != self._ChangedTick()
  50. def UpdateDiagnostics( self ):
  51. self._diag_interface.UpdateWithNewDiagnostics(
  52. self._parse_request.Response() )
  53. def PopulateLocationList( self ):
  54. return self._diag_interface.PopulateLocationList()
  55. def GetResponse( self ):
  56. return self._parse_request.Response()
  57. def IsResponseHandled( self ):
  58. return self._handled_tick == self._parse_tick
  59. def MarkResponseHandled( self ):
  60. self._handled_tick = self._parse_tick
  61. def OnCursorMoved( self ):
  62. self._diag_interface.OnCursorMoved()
  63. def GetErrorCount( self ):
  64. return self._diag_interface.GetErrorCount()
  65. def GetWarningCount( self ):
  66. return self._diag_interface.GetWarningCount()
  67. def _ChangedTick( self ):
  68. return vimsupport.GetBufferChangedTick( self.number )
  69. class BufferDict( dict ):
  70. def __init__( self, user_options ):
  71. self._user_options = user_options
  72. def __missing__( self, key ):
  73. # Python does not allow to return assignment operation result directly
  74. new_value = self[ key ] = Buffer( key, self._user_options )
  75. return new_value