@@ -15,36 +15,6 @@ import LanguageServerProtocol
15
15
import LSPLogging
16
16
import SKSupport
17
17
18
- public struct DocumentTokens {
19
- /// Lexical tokens, e.g. keywords, raw identifiers, ...
20
- public var lexical : [ SyntaxHighlightingToken ] = [ ]
21
- /// Syntactic tokens, e.g. declarations, etc.
22
- public var syntactic : [ SyntaxHighlightingToken ] = [ ]
23
- /// Semantic tokens, e.g. variable references, type references, ...
24
- public var semantic : [ SyntaxHighlightingToken ] = [ ]
25
-
26
- private var merged : [ SyntaxHighlightingToken ] {
27
- [ lexical, syntactic, semantic] . reduce ( [ ] ) { $0. mergingTokens ( with: $1) }
28
- }
29
- public var mergedAndSorted : [ SyntaxHighlightingToken ] {
30
- merged. sorted { $0. start < $1. start }
31
- }
32
-
33
- /// Modifies the syntax highlighting tokens of each kind
34
- /// (lexical, syntactic, semantic) according to `action`.
35
- public mutating func withMutableTokensOfEachKind( _ action: ( inout [ SyntaxHighlightingToken ] ) -> Void ) {
36
- action ( & lexical)
37
- action ( & syntactic)
38
- action ( & semantic)
39
- }
40
-
41
- // Replace all lexical tokens in `range`.
42
- public mutating func replaceLexical( in range: Range < Position > , with newTokens: [ SyntaxHighlightingToken ] ) {
43
- lexical. removeAll { $0. range. overlaps ( range) }
44
- lexical += newTokens
45
- }
46
- }
47
-
48
18
public struct DocumentSnapshot {
49
19
public var document : Document
50
20
public var version : Int
@@ -146,8 +116,8 @@ public final class DocumentManager {
146
116
147
117
/// Applies the given edits to the document.
148
118
///
149
- /// - parameter beforeCallback : Optional closure to call before each edit.
150
- /// - parameter afterCallback : Optional closure to call after each edit.
119
+ /// - parameter willEditDocument : Optional closure to call before each edit.
120
+ /// - parameter updateDocumentTokens : Optional closure to call after each edit.
151
121
/// - parameter before: The document contents *before* the edit is applied.
152
122
/// - parameter after: The document contents *after* the edit is applied.
153
123
/// - returns: The contents of the file after all the edits are applied.
@@ -157,16 +127,16 @@ public final class DocumentManager {
157
127
_ uri: DocumentURI ,
158
128
newVersion: Int ,
159
129
edits: [ TextDocumentContentChangeEvent ] ,
160
- beforeCallback : ( ( _ before: DocumentSnapshot , TextDocumentContentChangeEvent ) -> Void ) ? = nil ,
161
- afterCallback : ( ( _ after: DocumentSnapshot ) -> DocumentTokens ? ) ? = nil
130
+ willEditDocument : ( ( _ before: DocumentSnapshot , TextDocumentContentChangeEvent ) -> Void ) ? = nil ,
131
+ updateDocumentTokens : ( ( _ after: DocumentSnapshot ) -> DocumentTokens ? ) ? = nil
162
132
) throws -> DocumentSnapshot {
163
133
return try queue. sync {
164
134
guard let document = documents [ uri] else {
165
135
throw Error . missingDocument ( uri)
166
136
}
167
137
168
138
for edit in edits {
169
- if let f = beforeCallback {
139
+ if let f = willEditDocument {
170
140
f ( document. latestSnapshot, edit)
171
141
}
172
142
@@ -199,10 +169,9 @@ public final class DocumentManager {
199
169
var token = $0
200
170
if token. start. line == range. upperBound. line
201
171
&& token. start. utf16index >= range. upperBound. utf16index {
202
- token. start. utf16index += lastLineLengthDelta
203
- token. start. line += lineDelta
172
+ token. move ( lineDelta: lineDelta, utf16indexDelta: lastLineLengthDelta)
204
173
} else if token. start. line > range. upperBound. line {
205
- token. start . line += lineDelta
174
+ token. move ( lineDelta : lineDelta)
206
175
}
207
176
return token
208
177
} )
@@ -213,7 +182,7 @@ public final class DocumentManager {
213
182
document. latestTokens = DocumentTokens ( )
214
183
}
215
184
216
- if let f = afterCallback , let tokens = f ( document. latestSnapshot) {
185
+ if let f = updateDocumentTokens , let tokens = f ( document. latestSnapshot) {
217
186
document. latestTokens = tokens
218
187
}
219
188
}
@@ -270,21 +239,21 @@ extension DocumentManager {
270
239
}
271
240
}
272
241
273
- /// Convenience wrapper for `edit(_:newVersion:edits:beforeCallback:afterCallback :)`
242
+ /// Convenience wrapper for `edit(_:newVersion:edits:willEditDocument:updateDocumentTokens :)`
274
243
/// that logs on failure.
275
244
@discardableResult
276
245
func edit(
277
246
_ note: DidChangeTextDocumentNotification ,
278
- beforeCallback : ( ( _ before: DocumentSnapshot , TextDocumentContentChangeEvent ) -> Void ) ? = nil ,
279
- afterCallback : ( ( _ after: DocumentSnapshot ) -> DocumentTokens ? ) ? = nil
247
+ willEditDocument : ( ( _ before: DocumentSnapshot , TextDocumentContentChangeEvent ) -> Void ) ? = nil ,
248
+ updateDocumentTokens : ( ( _ after: DocumentSnapshot ) -> DocumentTokens ? ) ? = nil
280
249
) -> DocumentSnapshot ? {
281
250
return orLog ( " failed to edit document " , level: . error) {
282
251
try edit (
283
252
note. textDocument. uri,
284
253
newVersion: note. textDocument. version ?? - 1 ,
285
254
edits: note. contentChanges,
286
- beforeCallback : beforeCallback ,
287
- afterCallback : afterCallback
255
+ willEditDocument : willEditDocument ,
256
+ updateDocumentTokens : updateDocumentTokens
288
257
)
289
258
}
290
259
}
0 commit comments