This repository has been archived by the owner on Nov 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement fix all using
Source.fixAll
instead of onWillSave
Follow up on #43
- Loading branch information
Showing
5 changed files
with
80 additions
and
69 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
|
||
import * as vscode from 'vscode'; | ||
import { isTypeScriptDocument, isEnabledForJavaScriptDocument } from './utils'; | ||
|
||
function executeCodeActionProvider(uri: vscode.Uri, range: vscode.Range) { | ||
return vscode.commands.executeCommand<vscode.CodeAction[]>('vscode.executeCodeActionProvider', uri, range); | ||
} | ||
|
||
|
||
async function getTsLintFixAllCodeAction(document: vscode.TextDocument): Promise<vscode.CodeAction | undefined> { | ||
const diagnostics = vscode.languages | ||
.getDiagnostics(document.uri) | ||
.filter(diagnostic => diagnostic.source === 'tslint'); | ||
|
||
for (const diagnostic of diagnostics) { | ||
const codeActions = await executeCodeActionProvider(document.uri, diagnostic.range); | ||
if (codeActions) { | ||
const fixAll = codeActions.filter(action => action.title === 'Fix all auto-fixable tslint failures'); | ||
if (fixAll.length > 0) { | ||
return fixAll[0]; | ||
} | ||
} | ||
} | ||
return undefined; | ||
} | ||
|
||
|
||
const fixAllCodeActionKind = vscode.CodeActionKind.Source.append('fixAll').append('tslint'); | ||
|
||
export class FixAllProvider implements vscode.CodeActionProvider { | ||
public static metadata: vscode.CodeActionProviderMetadata = { | ||
providedCodeActionKinds: [fixAllCodeActionKind], | ||
}; | ||
|
||
async provideCodeActions( | ||
document: vscode.TextDocument, | ||
_range: vscode.Range | vscode.Selection, | ||
context: vscode.CodeActionContext, | ||
_token: vscode.CancellationToken | ||
): Promise<vscode.CodeAction[]> { | ||
if (!context.only) { | ||
return []; | ||
} | ||
|
||
if (!context.only.contains(fixAllCodeActionKind) && !fixAllCodeActionKind.contains(context.only)) { | ||
return []; | ||
} | ||
|
||
if (!isTypeScriptDocument(document) && !isEnabledForJavaScriptDocument(document)) { | ||
return []; | ||
} | ||
|
||
const fixAllAction = await getTsLintFixAllCodeAction(document); | ||
if (!fixAllAction) { | ||
return []; | ||
} | ||
|
||
return [{ | ||
...fixAllAction, | ||
title: 'Fix All TSLint', | ||
kind: fixAllCodeActionKind, | ||
}]; | ||
} | ||
} |
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