-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
Copy pathbuffer.py
165 lines (113 loc) · 5.04 KB
/
buffer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# Copyright (C) 2016, Davit Samvelyan
#
# This file is part of YouCompleteMe.
#
# YouCompleteMe is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# YouCompleteMe is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with YouCompleteMe. If not, see <http://www.gnu.org/licenses/>.
from ycm import vimsupport
from ycm.client.event_notification import EventNotification
from ycm.diagnostic_interface import DiagnosticInterface
from ycm.semantic_highlighting import SemanticHighlighting
from ycm.inlay_hints import InlayHints
# Emulates Vim buffer
# Used to store buffer related information like diagnostics, latest parse
# request. Stores buffer change tick at the parse request moment, allowing
# to effectively determine whether reparse is needed for the buffer.
class Buffer:
def __init__( self, bufnr, user_options, filetypes ):
self._number = bufnr
self._parse_tick = 0
self._handled_tick = 0
self._parse_request = None
self._should_resend = False
self._diag_interface = DiagnosticInterface( bufnr, user_options )
self._open_loclist_on_ycm_diags = user_options[
'open_loclist_on_ycm_diags' ]
self.semantic_highlighting = SemanticHighlighting( bufnr )
self.inlay_hints = InlayHints( bufnr )
self.UpdateFromFileTypes( filetypes )
def FileParseRequestReady( self, block = False ):
return ( bool( self._parse_request ) and
( block or self._parse_request.Done() ) )
def SendParseRequest( self, extra_data ):
# Don't send a parse request if one is in progress
if self._parse_request is not None and not self._parse_request.Done():
self._should_resend = True
return
self._should_resend = False
self._parse_request = EventNotification( 'FileReadyToParse',
extra_data = extra_data )
self._parse_request.Start()
# Decrement handled tick to ensure correct handling when we are forcing
# reparse on buffer visit and changed tick remains the same.
self._handled_tick -= 1
self._parse_tick = self._ChangedTick()
def ParseRequestPending( self ):
return bool( self._parse_request ) and not self._parse_request.Done()
def NeedsReparse( self ):
return self._parse_tick != self._ChangedTick()
def ShouldResendParseRequest( self ):
return ( self._should_resend
or ( bool( self._parse_request )
and self._parse_request.ShouldResend() ) )
def UpdateDiagnostics( self, force = False ):
if force or not self._async_diags:
self.UpdateWithNewDiagnostics( self._parse_request.Response(), False )
else:
# We need to call the response method, because it might throw an exception
# or require extra config confirmation, even if we don't actually use the
# diagnostics.
self._parse_request.Response()
def UpdateWithNewDiagnostics( self, diagnostics, async_message ):
self._async_diags = async_message
self._diag_interface.UpdateWithNewDiagnostics(
diagnostics,
not self._async_diags and self._open_loclist_on_ycm_diags )
def UpdateMatches( self ):
self._diag_interface.UpdateMatches()
def PopulateLocationList( self, open_on_edit = False ):
return self._diag_interface.PopulateLocationList( open_on_edit )
def GetResponse( self ):
return self._parse_request.Response()
def IsResponseHandled( self ):
return self._handled_tick == self._parse_tick
def MarkResponseHandled( self ):
self._handled_tick = self._parse_tick
def OnCursorMoved( self ):
self._diag_interface.OnCursorMoved()
def GetErrorCount( self ):
return self._diag_interface.GetErrorCount()
def GetWarningCount( self ):
return self._diag_interface.GetWarningCount()
def RefreshDiagnosticsUI( self ):
return self._diag_interface.RefreshDiagnosticsUI()
def ClearDiagnosticsUI( self ):
return self._diag_interface.ClearDiagnosticsUI()
def DiagnosticsForLine( self, line_number ):
return self._diag_interface.DiagnosticsForLine( line_number )
def UpdateFromFileTypes( self, filetypes ):
self._filetypes = filetypes
# We will set this to true if we ever receive any diagnostics asyncronously.
self._async_diags = False
def _ChangedTick( self ):
return vimsupport.GetBufferChangedTick( self._number )
class BufferDict( dict ):
def __init__( self, user_options ):
self._user_options = user_options
def __missing__( self, key ):
# Python does not allow to return assignment operation result directly
new_value = self[ key ] = Buffer(
key,
self._user_options,
vimsupport.GetBufferFiletypes( key ) )
return new_value