Skip to content
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

Add "Replace ALL" option to Quick Fix menu #5

Merged
merged 5 commits into from
May 22, 2021
Merged
Changes from 4 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
42 changes: 37 additions & 5 deletions src/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,17 +142,50 @@ export class WokeProvider implements vscode.CodeActionProvider {
actions.push(this.createFix(document, diagnostic.range, alt));
});
}
for (const diagnostic of context.diagnostics) {
this.alternatives.get(diagnostic.code).forEach((alt: string) => {
const action = this.createFixAll(document, diagnostic.code, alt);
if (action !== undefined) {
actions.push(action);
}
});
}

return actions;
}

private createFix(document: vscode.TextDocument, range: vscode.Range, replacement: string): vscode.CodeAction {
const msg = `[woke] Click to replace with '${replacement}'`;
const fix = new vscode.CodeAction(msg, vscode.CodeActionKind.QuickFix);
private getFixEdit(msg: string): vscode.CodeAction {
const fix: vscode.CodeAction = new vscode.CodeAction(msg, vscode.CodeActionKind.QuickFix);
fix.edit = new vscode.WorkspaceEdit();
fix.edit.replace(document.uri, range, replacement);
return fix;
}

private createFix(document: vscode.TextDocument, range: vscode.Range, replacement: string): vscode.CodeAction {
const fix = this.getFixEdit(`[woke] Click to replace with '${replacement}'`);
fix?.edit?.replace(document.uri, range, replacement);
return fix;
}

private createFixAll(document: vscode.TextDocument, code: any, replacement: string): vscode.CodeAction | undefined{
const textEdits = new Array<vscode.TextEdit>();
const diagnosticCollection = this.diagnosticCollection.get(document.uri);
if (diagnosticCollection !== undefined) {
for (const diagnostic of diagnosticCollection) {
if (diagnostic.code === code) {
textEdits.push(new vscode.TextEdit(diagnostic.range, replacement));
}
}

if (textEdits.length > 1) {
const fix = this.getFixEdit(`[woke] Click to replace ALL with '${replacement}'`);
fix?.edit?.set(document.uri, textEdits);
return fix;
}
}

return undefined;
}

private showMessage(msg: string, severity: MessageSeverity): void {
const showMsg = `Woke: ${msg}`;

Expand All @@ -177,7 +210,6 @@ export class WokeProvider implements vscode.CodeActionProvider {

private async runWoke(textDocument: vscode.TextDocument): Promise<vscode.Diagnostic[]> {
return new Promise<vscode.Diagnostic[]>((resolve) => {

const diagnostics: vscode.Diagnostic[] = [];
const useBufferArgs = textDocument.isUntitled || this.settings.trigger !== RunTrigger.onSave;

Expand Down