-
-
Notifications
You must be signed in to change notification settings - Fork 20
Add Quick Fix (code actions) for diagnostics #1237
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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; | ||
|
|
@@ -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 | ||
|
|
@@ -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, | ||
| message: diagnostic.message, | ||
| source: diagnostic.source ?? "sourcekit-lsp", | ||
| actions: diagnostic.codeActions, | ||
| })); | ||
|
|
||
| this.editor.updateMarkers(markers); | ||
| break; | ||
| case "format": | ||
|
|
@@ -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
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
| 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(); | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 stablesourcekit-lspfallback), so runner markers — which set no source — can't pick up a stale fix-it.