-
-
Notifications
You must be signed in to change notification settings - Fork 132
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Support Quick Fixes with custom decorators (#2897)
- Loading branch information
Showing
10 changed files
with
152 additions
and
90 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
2 changes: 1 addition & 1 deletion
2
.../client/src/vscode-languageclient.test.ts → .../src/client/vscode-languageclient.test.ts
This file contains 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
This file contains 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 |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import type { Suggestion } from 'code-spell-checker-server/api'; | ||
import type { CodeActionContext, CodeActionProvider, Command, Range, Selection, TextDocument } from 'vscode'; | ||
import { CodeAction, CodeActionKind, languages } from 'vscode'; | ||
|
||
import { requestSpellingSuggestions } from './codeActions/actionSuggestSpellingCorrections'; | ||
import { createTextEditCommand } from './commands'; | ||
import { filterDiags } from './diags'; | ||
import type { IssueTracker, SpellingDiagnostic } from './issueTracker'; | ||
|
||
export class SpellCheckerCodeActionProvider implements CodeActionProvider { | ||
public static readonly providedCodeActionKinds = [CodeActionKind.QuickFix]; | ||
|
||
constructor(readonly issueTracker: IssueTracker) {} | ||
|
||
async provideCodeActions( | ||
document: TextDocument, | ||
range: Range | Selection, | ||
context: CodeActionContext, | ||
// token: CancellationToken, | ||
): Promise<(CodeAction | Command)[]> { | ||
const contextDiags = filterDiags(context.diagnostics); | ||
if (contextDiags.length) { | ||
// Already handled by the language server. | ||
return []; | ||
} | ||
|
||
const diags = this.issueTracker.getDiagnostics(document.uri).filter((diag) => diag.range.contains(range)); | ||
if (diags.length !== 1) return []; | ||
const pendingDiags = diags.map((diag) => this.diagToAction(document, diag)); | ||
return (await Promise.all(pendingDiags)).flatMap((action) => action); | ||
} | ||
|
||
private async diagToAction(doc: TextDocument, diag: SpellingDiagnostic): Promise<(CodeAction | Command)[]> { | ||
const suggestions = diag.data?.suggestions; | ||
if (!suggestions?.length) { | ||
// fetch the result from the server. | ||
const actionsFromServer = await requestSpellingSuggestions(doc, diag.range, [diag]); | ||
return actionsFromServer; | ||
} | ||
return suggestions.map((sug) => suggestionToAction(doc, diag.range, sug)); | ||
} | ||
} | ||
|
||
function suggestionToAction(doc: TextDocument, range: Range, sug: Suggestion): CodeAction { | ||
const title = `Replace with: ${sug.word}`; | ||
const action = new CodeAction(title, CodeActionKind.QuickFix); | ||
action.isPreferred = sug.isPreferred; | ||
action.command = createTextEditCommand(title, doc.uri, doc.version, [{ range, newText: sug.word }]); | ||
return action; | ||
} | ||
|
||
export function registerSpellCheckerCodeActionProvider(issueTracker: IssueTracker) { | ||
return languages.registerCodeActionsProvider('*', new SpellCheckerCodeActionProvider(issueTracker), { | ||
providedCodeActionKinds: SpellCheckerCodeActionProvider.providedCodeActionKinds, | ||
}); | ||
} |
51 changes: 51 additions & 0 deletions
51
packages/client/src/codeActions/actionSuggestSpellingCorrections.ts
This file contains 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 |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import type { CodeAction, Diagnostic, QuickPickItem, Range, TextDocument, Uri } from 'vscode'; | ||
import { commands, window } from 'vscode'; | ||
|
||
import * as di from '../di'; | ||
import { extractMatchingDiagRanges, getCSpellDiags } from '../diags'; | ||
import { toRange } from '../languageServer/clientHelpers'; | ||
import type { RangeLike } from '../languageServer/models'; | ||
import { ConfigFields, getSettingFromVSConfig } from '../settings'; | ||
import { findEditor } from '../util/findEditor'; | ||
import { pVoid } from '../util/pVoid'; | ||
|
||
interface SuggestionQuickPickItem extends QuickPickItem { | ||
_action: CodeAction; | ||
} | ||
|
||
export async function actionSuggestSpellingCorrections(docUri?: Uri, rangeLike?: RangeLike, _text?: string): Promise<void> { | ||
// console.log('Args: %o', { docUri, range: rangeLike, _text }); | ||
const editor = findEditor(docUri); | ||
const document = editor?.document; | ||
const selection = editor?.selection; | ||
const range = (rangeLike && toRange(rangeLike)) || (selection && document?.getWordRangeAtPosition(selection.active)); | ||
const diags = document ? getCSpellDiags(document.uri) : undefined; | ||
const matchingRanges = extractMatchingDiagRanges(document, selection, diags); | ||
const r = matchingRanges?.[0] || range; | ||
const matchingDiags = r && diags?.filter((d) => !!d.range.intersection(r)); | ||
|
||
if (!document || !selection || !r || !matchingDiags) { | ||
return pVoid(window.showInformationMessage('Nothing to suggest.'), 'actionSuggestSpellingCorrections'); | ||
} | ||
|
||
const menu = getSettingFromVSConfig(ConfigFields.suggestionMenuType, document); | ||
if (menu === 'quickFix') { | ||
return await commands.executeCommand('editor.action.quickFix'); | ||
} | ||
|
||
const actions = await requestSpellingSuggestions(document, r, matchingDiags); | ||
if (!actions || !actions.length) { | ||
return pVoid(window.showInformationMessage(`No Suggestions Found for ${document.getText(r)}`), 'actionSuggestSpellingCorrections'); | ||
} | ||
|
||
const items: SuggestionQuickPickItem[] = actions.map((a) => ({ label: a.title, _action: a })); | ||
const picked = await window.showQuickPick(items); | ||
if (picked && picked._action.command) { | ||
const { command: cmd, arguments: args = [] } = picked._action.command; | ||
commands.executeCommand(cmd, ...args); | ||
} | ||
} | ||
|
||
export function requestSpellingSuggestions(document: TextDocument, range: Range, diags: Diagnostic[]) { | ||
return di.get('client').requestSpellingSuggestions(document, range, diags); | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import type { TextEditor, Uri } from 'vscode'; | ||
import { window } from 'vscode'; | ||
|
||
export function findEditor(uri?: Uri): TextEditor | undefined { | ||
if (!uri) return window.activeTextEditor; | ||
|
||
const uriStr = uri.toString(); | ||
|
||
for (const editor of window.visibleTextEditors) { | ||
if (editor.document.uri.toString() === uriStr) { | ||
return editor; | ||
} | ||
} | ||
|
||
return undefined; | ||
} |
This file contains 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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import type { OnErrorResolver } from './errors'; | ||
import { handleErrors, ignoreError } from './errors'; | ||
|
||
export function pVoid<T>(p: Promise<T> | Thenable<T>, context: string, onErrorHandler: OnErrorResolver = ignoreError): Promise<void> { | ||
const v = Promise.resolve(p).then(() => undefined); | ||
return handleErrors(v, context, onErrorHandler); | ||
} |