Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 82 additions & 2 deletions Public/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,9 @@ export class App {
case "diagnostics":
this.updateLanguageServerStatus(true);
this.editor.clearMarkers();
// Reset the fix-its surfaced as Quick Fix code actions; repopulated
// below from the new diagnostics (sourcekit-lsp ships them inline).
this.codeActionEntries = [];

if (!response.value) {
return;
Expand All @@ -181,7 +184,7 @@ export class App {
const startLineNumber = start.line + 1;
const startColumn = start.character + 1;
const endLineNumber = end.line + 1;
const endColumn = start.character + 1;
const endColumn = end.character + 1;

let severity = languageServer.convertDiagnosticSeverity(
diagnostic.severity
Expand All @@ -194,10 +197,23 @@ export class App {
endColumn: endColumn,
message: diagnostic.message,
severity: severity,
source: diagnostic.source,
// Fall back to a stable source so LSP markers are always
// distinguishable from runner markers (which set no source) when
// matching fix-its below.
source: diagnostic.source ?? "sourcekit-lsp",
};
});

this.codeActionEntries = diagnostics
.filter((diagnostic) => diagnostic.codeActions?.length)
.map((diagnostic) => ({
startLineNumber: diagnostic.range.start.line + 1,
startColumn: diagnostic.range.start.character + 1,
Comment on lines +207 to +211

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in the latest commit: the lookup now also keys on the marker source (LSP diagnostics get a stable sourcekit-lsp fallback), so runner markers — which set no source — can't pick up a stale fix-it.

message: diagnostic.message,
source: diagnostic.source ?? "sourcekit-lsp",
actions: diagnostic.codeActions,
}));

this.editor.updateMarkers(markers);
break;
case "format":
Expand Down Expand Up @@ -289,6 +305,70 @@ export class App {
});
};

// Quick Fix: turn the fix-its sourcekit-lsp ships on each diagnostic into
// Monaco code actions for the markers under the cursor. No round-trip —
// the data already arrived with the diagnostics notification.
this.editor.oncodeaction = (model, _range, context) => {
const entries = this.codeActionEntries || [];
const markers = (context && context.markers) || [];

const toEdits = (lspAction) => {
const changes = lspAction.edit && lspAction.edit.changes;
if (!changes) {
return null;
}
const edits = [];
// The document URI is the server's temp path; apply every edit to the
// single local model regardless of the key.
for (const uri of Object.keys(changes)) {
for (const textEdit of changes[uri]) {
edits.push({
resource: model.uri,
versionId: undefined,
textEdit: {
range: {
startLineNumber: textEdit.range.start.line + 1,
startColumn: textEdit.range.start.character + 1,
endLineNumber: textEdit.range.end.line + 1,
endColumn: textEdit.range.end.character + 1,
},
text: textEdit.newText,
},
});
}
}
return edits.length ? edits : null;
};

const actions = [];
for (const marker of markers) {
const entry = entries.find(
(e) =>
e.source === marker.source &&
e.startLineNumber === marker.startLineNumber &&
e.startColumn === marker.startColumn &&
e.message === marker.message
);
Comment on lines +346 to +351

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in the latest commit: the lookup now also keys on the marker source (LSP diagnostics get a stable sourcekit-lsp fallback), so runner markers — which set no source — can't pick up a stale fix-it.

if (!entry) {
continue;
}
for (const lspAction of entry.actions) {
const edits = toEdits(lspAction);
if (!edits) {
continue;
}
actions.push({
title: lspAction.title,
kind: lspAction.kind || "quickfix",
diagnostics: [marker],
edit: { edits: edits },
isPreferred: lspAction.isPreferred,
});
}
}
return { actions: actions, dispose: () => {} };
};

this.editor.focus();
this.editor.scrollToBottm();

Expand Down
9 changes: 9 additions & 0 deletions Public/js/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ export class Editor {
},
});

// Quick Fix (lightbulb) for diagnostics. sourcekit-lsp ships the fix-its
// inline on each diagnostic, so this just surfaces them as code actions.
monaco.languages.registerCodeActionProvider("swift", {
provideCodeActions: (model, range, context) => {
return this.oncodeaction(model, range, context);
},
});

// Explicit Ctrl+Space to trigger completion, like Xcode. (Cmd+Space is
// taken by Spotlight on macOS, so bind WinCtrl = the Control key.)
this.editor.addAction({
Expand All @@ -70,6 +78,7 @@ export class Editor {
this.onhover = () => {};
this.oncompletion = () => {};
this.onsignaturehelp = () => {};
this.oncodeaction = () => ({ actions: [], dispose() {} });
this.onaction = () => {};
}

Expand Down