Extremely basic documentChanges handling
This commit is contained in:
parent
a884736063
commit
4352954991
|
|
@ -1,12 +1,12 @@
|
||||||
{
|
{
|
||||||
"name": "miller",
|
"name": "miller",
|
||||||
"version": "0.2.3",
|
"version": "0.2.4",
|
||||||
"lockfileVersion": 3,
|
"lockfileVersion": 3,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"packages": {
|
"packages": {
|
||||||
"": {
|
"": {
|
||||||
"name": "miller",
|
"name": "miller",
|
||||||
"version": "0.2.3",
|
"version": "0.2.4",
|
||||||
"license": "GPL-3.0-or-later",
|
"license": "GPL-3.0-or-later",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"chokidar": "^5.0.0",
|
"chokidar": "^5.0.0",
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
import type * as lsp from "vscode-languageserver-protocol";
|
import * as lsp from "vscode-languageserver-protocol";
|
||||||
import { Extension, TransactionSpec } from "@codemirror/state";
|
import { Extension, TransactionSpec } from "@codemirror/state";
|
||||||
import { EditorView } from "@codemirror/view";
|
import { EditorView } from "@codemirror/view";
|
||||||
|
|
||||||
|
|
@ -137,7 +137,7 @@ class OpenFileWorkspace extends Workspace {
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
openFile(uri: string, languageId: string, view: EditorView) {
|
openFile(uri: string, _languageId: string, view: EditorView) {
|
||||||
console.log("LSP: attempting to open file", uri);
|
console.log("LSP: attempting to open file", uri);
|
||||||
if (this.getFile(uri)) return;
|
if (this.getFile(uri)) return;
|
||||||
// Try to map to an existing OpenFile instance, prefer using its doc
|
// Try to map to an existing OpenFile instance, prefer using its doc
|
||||||
|
|
@ -162,7 +162,7 @@ class OpenFileWorkspace extends Workspace {
|
||||||
file.dispatch(update);
|
file.dispatch(update);
|
||||||
}
|
}
|
||||||
|
|
||||||
closeFile(uri: string, view: EditorView) {
|
closeFile(uri: string, _view: EditorView) {
|
||||||
const path = uri.replace(/^file:\/\//, "");
|
const path = uri.replace(/^file:\/\//, "");
|
||||||
const of = OpenFile.findOpenFile(path);
|
const of = OpenFile.findOpenFile(path);
|
||||||
// If OpenFile exists and still has editors, defer closing
|
// If OpenFile exists and still has editors, defer closing
|
||||||
|
|
@ -174,19 +174,32 @@ class OpenFileWorkspace extends Workspace {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function applyTextEdits(mapping: WorkspaceMapping, workspace: Workspace, uri: string, edits: lsp.TextEdit[], userEvent: string) {
|
||||||
|
const file = workspace.getFile(uri);
|
||||||
|
if(!file) return;
|
||||||
|
workspace.updateFile(uri, {
|
||||||
|
changes: edits.map(change => ({
|
||||||
|
from: mapping.mapPosition(uri, change.range.start),
|
||||||
|
to: mapping.mapPosition(uri, change.range.end),
|
||||||
|
insert: change.newText,
|
||||||
|
})),
|
||||||
|
userEvent,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
export function applyWorkspaceEdit(mapping: WorkspaceMapping, workspace: Workspace, edit: lsp.WorkspaceEdit, userEvent: string) {
|
export function applyWorkspaceEdit(mapping: WorkspaceMapping, workspace: Workspace, edit: lsp.WorkspaceEdit, userEvent: string) {
|
||||||
for (const uri in edit.changes) {
|
for (const uri in edit.changes) {
|
||||||
const lspChanges = edit.changes[uri];
|
const lspChanges = edit.changes[uri];
|
||||||
const file = workspace.getFile(uri);
|
if (!lspChanges.length) continue;
|
||||||
if (!lspChanges.length || !file) continue;
|
applyTextEdits(mapping, workspace, uri, lspChanges, userEvent);
|
||||||
workspace.updateFile(uri, {
|
}
|
||||||
changes: lspChanges.map(change => ({
|
for (const change of edit.documentChanges) {
|
||||||
from: mapping.mapPosition(uri, change.range.start),
|
if (Object.hasOwn(change, "kind")) {
|
||||||
to: mapping.mapPosition(uri, change.range.end),
|
const action = change as (lsp.CreateFile | lsp.RenameFile | lsp.DeleteFile);
|
||||||
insert: change.newText,
|
console.warn("Unsupported edit type!", action.kind);
|
||||||
})),
|
}
|
||||||
userEvent,
|
const lspChanges = change as lsp.TextDocumentEdit;
|
||||||
})
|
applyTextEdits(mapping, workspace, lspChanges.textDocument.uri, lspChanges.edits, userEvent);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue