@@ -149,8 +149,7 @@ public final class DocumentManager {
149
149
public func edit(
150
150
_ uri: DocumentURI ,
151
151
newVersion: Int ,
152
- edits: [ TextDocumentContentChangeEvent ] ,
153
- requiresUpcomingEdits: Bool = false
152
+ edits: [ TextDocumentContentChangeEvent ]
154
153
) throws -> ( preEditSnapshot: DocumentSnapshot , postEditSnapshot: DocumentSnapshot , edits: [ SourceEdit ] ) {
155
154
return try queue. sync {
156
155
guard let document = documents [ uri] else {
@@ -160,9 +159,7 @@ public final class DocumentManager {
160
159
161
160
var upcomingEdits : [ SourceEdit ] = [ ]
162
161
for edit in edits {
163
- if requiresUpcomingEdits {
164
- upcomingEdits. append ( willEditDocument ( document. latestLineTable, edit: edit) )
165
- }
162
+ upcomingEdits. append ( SourceEdit ( edit: edit, lineTableBeforeEdit: document. latestLineTable) )
166
163
167
164
if let range = edit. range {
168
165
document. latestLineTable. replace (
@@ -195,34 +192,6 @@ public final class DocumentManager {
195
192
}
196
193
}
197
194
198
- private func willEditDocument( _ before: LineTable , edit: TextDocumentContentChangeEvent ) -> SourceEdit {
199
- guard let range = edit. range else {
200
- return SourceEdit (
201
- range: Range (
202
- uncheckedBounds: (
203
- AbsolutePosition ( utf8Offset: 0 ) ,
204
- upper: AbsolutePosition ( utf8Offset: before. content. utf8. count)
205
- )
206
- ) ,
207
- replacement: edit. text
208
- )
209
- }
210
- guard let offset = before. utf8OffsetOf ( line: range. lowerBound. line, utf16Column: range. lowerBound. utf16index) ,
211
- let end = before. utf8OffsetOf ( line: range. upperBound. line, utf16Column: range. upperBound. utf16index)
212
- else {
213
- fatalError ( " invalid edit \( range) " )
214
- }
215
- return SourceEdit (
216
- range: Range (
217
- uncheckedBounds: (
218
- AbsolutePosition ( utf8Offset: offset) ,
219
- upper: AbsolutePosition ( utf8Offset: end)
220
- )
221
- ) ,
222
- replacement: edit. text
223
- )
224
- }
225
-
226
195
}
227
196
228
197
extension DocumentManager {
@@ -249,16 +218,36 @@ extension DocumentManager {
249
218
/// that logs on failure.
250
219
@discardableResult
251
220
func edit(
252
- _ note: DidChangeTextDocumentNotification ,
253
- requiresUpcomingEdits: Bool = false
221
+ _ note: DidChangeTextDocumentNotification
254
222
) -> ( preEditSnapshot: DocumentSnapshot , postEditSnapshot: DocumentSnapshot , edits: [ SourceEdit ] ) ? {
255
223
return orLog ( " failed to edit document " , level: . error) {
256
224
return try edit (
257
225
note. textDocument. uri,
258
226
newVersion: note. textDocument. version,
259
- edits: note. contentChanges,
260
- requiresUpcomingEdits: requiresUpcomingEdits
227
+ edits: note. contentChanges
228
+ )
229
+ }
230
+ }
231
+ }
232
+
233
+ fileprivate extension SourceEdit {
234
+
235
+ init ( edit: TextDocumentContentChangeEvent , lineTableBeforeEdit: LineTable ) {
236
+ if let range = edit. range {
237
+ guard let offset = lineTableBeforeEdit. utf8OffsetOf ( line: range. lowerBound. line, utf16Column: range. lowerBound. utf16index) ,
238
+ let end = lineTableBeforeEdit. utf8OffsetOf ( line: range. upperBound. line, utf16Column: range. upperBound. utf16index) else {
239
+ fatalError ( " invalid edit \( range) " )
240
+ }
241
+ self . init (
242
+ range: AbsolutePosition ( utf8Offset: offset) ..< AbsolutePosition ( utf8Offset: end) ,
243
+ replacement: edit. text
244
+ )
245
+ } else {
246
+ self . init (
247
+ range: AbsolutePosition ( utf8Offset: 0 ) ..< AbsolutePosition ( utf8Offset: lineTableBeforeEdit. content. utf8. count) ,
248
+ replacement: edit. text
261
249
)
262
250
}
263
251
}
252
+
264
253
}
0 commit comments