Small cleanup
This commit is contained in:
parent
c0fed59548
commit
c44971ff23
|
|
@ -86,10 +86,6 @@ export class OpenFile implements WorkspaceFile {
|
||||||
await window.electronAPI.saveFile(doc, this.filePath.val);
|
await window.electronAPI.saveFile(doc, this.filePath.val);
|
||||||
this.lastSaved.val = this.rootState.val.doc;
|
this.lastSaved.val = this.rootState.val.doc;
|
||||||
this.expectedDiskContent.val = doc;
|
this.expectedDiskContent.val = doc;
|
||||||
// Notify LSP clients that the file was saved. The lsp plugin typically
|
|
||||||
// listens to EditorView changes and save events; nudging the views
|
|
||||||
// ensures any listeners pick up the final document state.
|
|
||||||
this.notifyLspSave();
|
|
||||||
} else {
|
} else {
|
||||||
await this.saveAs();
|
await this.saveAs();
|
||||||
}
|
}
|
||||||
|
|
@ -221,20 +217,6 @@ export class OpenFile implements WorkspaceFile {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Lightweight helper to nudge LSP plugins on views after a save. This
|
|
||||||
// triggers a no-op dispatch on each view so that any view-bound listeners
|
|
||||||
// (including lsp-client's save/didSave handling) can observe the new state.
|
|
||||||
notifyLspSave() {
|
|
||||||
this.editors.forEach((e) => {
|
|
||||||
try {
|
|
||||||
// dispatch an empty transaction to trigger plugin observers
|
|
||||||
e.view.dispatch({});
|
|
||||||
} catch (err) {
|
|
||||||
console.warn("Failed to notify LSP of save for view:", err);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
notifyLspClose() {
|
notifyLspClose() {
|
||||||
// Some language clients respond to EditorView disposal/transactions; to be
|
// Some language clients respond to EditorView disposal/transactions; to be
|
||||||
// conservative, dispatch a no-op and then attempt to remove the LSP
|
// conservative, dispatch a no-op and then attempt to remove the LSP
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
// Minimal LSP integration helper for the editor.
|
// Minimal LSP integration helper for the editor.
|
||||||
// Keeps all LSP-specific logic in one place so it's easy to review.
|
// Keeps all LSP-specific logic in one place so it's easy to review.
|
||||||
|
|
||||||
import { Extension, ChangeSet, Text } from "@codemirror/state";
|
import { Extension, ChangeSet, TransactionSpec } from "@codemirror/state";
|
||||||
import { EditorView } from "@codemirror/view";
|
import { EditorView } from "@codemirror/view";
|
||||||
|
|
||||||
import {
|
import {
|
||||||
|
|
@ -85,7 +85,7 @@ export function inferLanguageFromPath(
|
||||||
// expectations. This supports multiple views per OpenFile by using the
|
// expectations. This supports multiple views per OpenFile by using the
|
||||||
// OpenFile.getView method.
|
// OpenFile.getView method.
|
||||||
class OpenFileWorkspace extends Workspace {
|
class OpenFileWorkspace extends Workspace {
|
||||||
files: WorkspaceFile[] = [];
|
files: OpenFile[] = [];
|
||||||
private fileVersions: { [uri: string]: number } = Object.create(null);
|
private fileVersions: { [uri: string]: number } = Object.create(null);
|
||||||
|
|
||||||
nextFileVersion(uri: string) {
|
nextFileVersion(uri: string) {
|
||||||
|
|
@ -98,6 +98,7 @@ class OpenFileWorkspace extends Workspace {
|
||||||
|
|
||||||
// Look through known workspace files and update their docs/versions
|
// Look through known workspace files and update their docs/versions
|
||||||
// based on the editor views or the OpenFile state when no view exists.
|
// based on the editor views or the OpenFile state when no view exists.
|
||||||
|
// TODO: fix (cause vibe coding is useless)
|
||||||
syncFiles() {
|
syncFiles() {
|
||||||
let result: any[] = [];
|
let result: any[] = [];
|
||||||
for (let file of this.files) {
|
for (let file of this.files) {
|
||||||
|
|
@ -129,35 +130,36 @@ class OpenFileWorkspace extends Workspace {
|
||||||
}
|
}
|
||||||
|
|
||||||
openFile(uri: string, languageId: string, view: EditorView) {
|
openFile(uri: string, languageId: string, view: EditorView) {
|
||||||
|
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
|
||||||
const path = uri.replace(/^file:\/\//, "");
|
const path = uri.replace(/^file:\/\//, "");
|
||||||
const of = OpenFile.findOpenFile(path);
|
const of = OpenFile.findOpenFile(path);
|
||||||
const file: WorkspaceFile = of
|
if (!of) {
|
||||||
? {
|
console.warn("LSP: attempted to open unknown file", uri);
|
||||||
uri,
|
return;
|
||||||
languageId: of.languageId || languageId,
|
|
||||||
version: of.version,
|
|
||||||
doc: of.doc,
|
|
||||||
getView: (main?: EditorView) => of.getView(main ?? view),
|
|
||||||
}
|
}
|
||||||
: {
|
this.files.push(of);
|
||||||
uri,
|
this.client.didOpen(of);
|
||||||
languageId,
|
}
|
||||||
version: this.nextFileVersion(uri),
|
|
||||||
doc: view.state.doc,
|
updateFile(uri: string, update: TransactionSpec): void {
|
||||||
getView: () => view,
|
const file = this.getFile(uri) as OpenFile;
|
||||||
};
|
if (!file) {
|
||||||
this.files.push(file);
|
console.warn("LSP: attempted to update unknown file", uri);
|
||||||
this.client.didOpen(file);
|
return;
|
||||||
|
}
|
||||||
|
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
|
||||||
|
console.log("LSP: attempting to close file", uri, of);
|
||||||
if (of && of.editors.length > 0) return;
|
if (of && of.editors.length > 0) return;
|
||||||
this.files = this.files.filter((f) => f.uri !== uri);
|
this.files = this.files.filter((f) => f.uri !== uri);
|
||||||
|
console.log("LSP: closing file", uri);
|
||||||
this.client.didClose(uri);
|
this.client.didClose(uri);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue