| 1208 | } |
| 1209 | |
| 1210 | function applyTextEdits( |
| 1211 | plugin: LSPPlugin, |
| 1212 | view: EditorView, |
| 1213 | edits: TextEdit[], |
| 1214 | ): boolean { |
| 1215 | const changes: Change[] = []; |
| 1216 | for (const edit of edits) { |
| 1217 | if (!edit?.range) continue; |
| 1218 | let fromBase: number; |
| 1219 | let toBase: number; |
| 1220 | try { |
| 1221 | fromBase = plugin.fromPosition(edit.range.start, plugin.syncedDoc); |
| 1222 | toBase = plugin.fromPosition(edit.range.end, plugin.syncedDoc); |
| 1223 | } catch (_) { |
| 1224 | continue; |
| 1225 | } |
| 1226 | const fromResult = plugin.unsyncedChanges.mapPos( |
| 1227 | fromBase, |
| 1228 | 1, |
| 1229 | MapMode.TrackDel, |
| 1230 | ); |
| 1231 | const toResult = plugin.unsyncedChanges.mapPos( |
| 1232 | toBase, |
| 1233 | -1, |
| 1234 | MapMode.TrackDel, |
| 1235 | ); |
| 1236 | if (fromResult == null || toResult == null) continue; |
| 1237 | const insert = |
| 1238 | typeof edit.newText === "string" |
| 1239 | ? edit.newText.replace(/\r\n/g, "\n") |
| 1240 | : ""; |
| 1241 | changes.push({ from: fromResult, to: toResult, insert }); |
| 1242 | } |
| 1243 | if (!changes.length) return false; |
| 1244 | changes.sort((a, b) => a.from - b.from || a.to - b.to); |
| 1245 | view.dispatch({ changes }); |
| 1246 | return true; |
| 1247 | } |
| 1248 | |
| 1249 | function buildFormattingOptions( |
| 1250 | view: EditorView, |