-
-
Notifications
You must be signed in to change notification settings - Fork 38
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
feat: autofix feature for common linting errors #187
Closed
Closed
Changes from all commits
Commits
Show all changes
4 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 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,76 @@ | ||
import * as vscode from 'vscode'; | ||
import * as path from 'path'; | ||
import * as yaml from 'js-yaml'; | ||
import * as fs from 'fs'; | ||
import { createFix } from './createFix'; | ||
|
||
interface QuickFixOption { | ||
message: string; | ||
errorCheck: string; | ||
quickfix: { | ||
text: string; | ||
type: string; | ||
}; | ||
} | ||
|
||
interface QuickFixData { | ||
[errorCode: string]: QuickFixOption[]; | ||
} | ||
|
||
export function activate(context: vscode.ExtensionContext) { | ||
// Register code action provider | ||
const codeActionProvider = new DiagnosticFixProvider(); | ||
context.subscriptions.push( | ||
vscode.languages.registerCodeActionsProvider({ scheme: 'file', language: 'yaml' }, codeActionProvider) | ||
); | ||
} | ||
|
||
export class DiagnosticFixProvider implements vscode.CodeActionProvider { | ||
private quickFixes: QuickFixData = {}; | ||
|
||
constructor() { | ||
// Load quick fixes from external YAML file | ||
const yamlFilePath = path.join(__dirname, '../src/quickfixes.yaml'); | ||
const yamlContent = fs.readFileSync(yamlFilePath, 'utf8'); | ||
this.quickFixes = yaml.load(yamlContent) as QuickFixData; | ||
} | ||
|
||
provideCodeActions( | ||
document: vscode.TextDocument, | ||
range: vscode.Range, | ||
context: vscode.CodeActionContext | ||
): vscode.CodeAction[] | undefined { | ||
const codeActions: vscode.CodeAction[] = []; | ||
|
||
// Check if the file is YAML or YML | ||
const extension = path.extname(document.fileName); | ||
if (extension !== '.yaml' && extension !== '.yml') { | ||
return codeActions; | ||
} | ||
|
||
for (const diagnostic of context.diagnostics) { | ||
const errorCode = diagnostic.code?.toString(); | ||
if (errorCode && this.quickFixes[errorCode]) { | ||
const options = this.quickFixes[errorCode]; | ||
for (const option of options) { | ||
if (errorCode === 'asyncapi-schema'){ | ||
if(diagnostic.message.includes(option.errorCheck)) { | ||
const fix = createFix(document, range, errorCode, option, this.quickFixes); | ||
if (fix) { | ||
codeActions.push(fix); | ||
} | ||
} | ||
}else{ | ||
const fix = createFix(document, range, errorCode, option, this.quickFixes); | ||
if (fix) { | ||
codeActions.push(fix); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
return codeActions; | ||
} | ||
} | ||
|
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,98 @@ | ||
import * as vscode from 'vscode'; | ||
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. I don't think this file can work as a generic framework to add different autofixes to the
|
||
|
||
interface QuickFixOption { | ||
message: string; | ||
quickfix: { | ||
text: string; | ||
type: string; | ||
}; | ||
} | ||
|
||
interface CustomCodeAction extends vscode.CodeAction { | ||
[key: string]: any; | ||
} | ||
|
||
export function createFix( | ||
document: vscode.TextDocument, | ||
range: vscode.Range, | ||
errorCode: string, | ||
option: QuickFixOption, | ||
quickFixes: { [errorCode: string]: QuickFixOption[] } | ||
): vscode.CodeAction | undefined { | ||
const { message, quickfix } = option; | ||
|
||
const fix: CustomCodeAction = new vscode.CodeAction( | ||
quickfix.text, | ||
vscode.CodeActionKind.QuickFix | ||
); | ||
fix.edit = new vscode.WorkspaceEdit(); | ||
|
||
if (quickfix.type === 'update') { | ||
// Replace the version within the range | ||
fix.edit.replace(document.uri, range, quickfix.text); | ||
} else if (quickfix.type === 'delete') { | ||
// Delete the text within the range | ||
const lineToDelete = range.start.line; | ||
if (lineToDelete >= 0) { | ||
const line = document.lineAt(lineToDelete); | ||
const deleteRange = new vscode.Range(line.range.start, line.range.end); | ||
fix.edit.delete(document.uri, deleteRange); | ||
} | ||
}else if (quickfix.type === 'append') { | ||
// Append '-1' to the duplicate name value | ||
const line = document.lineAt(range.start.line); | ||
const lineText = line.text; | ||
|
||
// Find the position of the duplicate name value | ||
const nameStartIndex = lineText.indexOf(': "') + 3; | ||
const nameEndIndex = lineText.lastIndexOf('"'); | ||
|
||
if (nameStartIndex !== -1 && nameEndIndex !== -1) { | ||
const existingNameValue = lineText.substring(nameStartIndex, nameEndIndex); | ||
const newNameValue = `${existingNameValue}-1`; | ||
//vscode.window.showInformationMessage(`Existing Name: ${existingNameValue}, New Name: ${newNameValue}`); | ||
// Replace the existing name value with the appended one | ||
const nameValueRange = new vscode.Range( | ||
range.start.line, | ||
nameStartIndex, | ||
range.start.line, | ||
nameEndIndex | ||
); | ||
fix.edit.replace(document.uri, nameValueRange, newNameValue); | ||
} | ||
} else if (quickfix.type === 'add') { | ||
const line = document.lineAt(range.start.line); | ||
const lineText = line.text; | ||
|
||
// Calculate the indentation based on the line before the error | ||
const previousLine = document.lineAt(range.start.line - 1); | ||
const previousIndentation = previousLine.text.match(/^\s*/)?.[0] || ''; | ||
|
||
// Check if the current line has an error, and then add the indentation | ||
if (errorCode === 'asyncapi-schema') { | ||
const insertionPosition = line.range.end; | ||
|
||
// Insert the quick fix text after the ':' | ||
const insertionText = ` ${quickfix.text}`; | ||
fix.edit.insert(document.uri, insertionPosition, insertionText); | ||
} else { | ||
// Insert the quick fix text with the calculated indentation | ||
const shouldAddIndentation = lineText.trim().length > 0; | ||
const indentation = shouldAddIndentation ? previousIndentation : ''; | ||
|
||
const insertionPosition = line.range.end; | ||
const insertionText = `\n${indentation}${quickfix.text}`; | ||
fix.edit.insert(document.uri, insertionPosition, insertionText); | ||
} | ||
|
||
|
||
} | ||
|
||
// Set the error message as the code action's title | ||
fix.title = message; | ||
|
||
// Add type field to code action metadata | ||
fix.type = quickfix.type; | ||
|
||
return fix; | ||
} |
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,62 @@ | ||
asyncapi-latest-version: | ||
- message: "The latest version is not used. You should update to the '2.4.0' version." | ||
errorCheck: "version" | ||
quickfix: | ||
text: "2.4.0" | ||
type: "update" | ||
- message: "The latest version is not used. You should update to the '2.5.0' version." | ||
errorCheck: "version" | ||
quickfix: | ||
text: "2.5.0" | ||
type: "update" | ||
|
||
asyncapi-schema: | ||
- message: "'title' property type must be string" | ||
errorCheck: "title" | ||
quickfix: | ||
text: "Example API" | ||
type: "add" | ||
- message: "'version' property type must be string" | ||
errorCheck: "version" | ||
quickfix: | ||
text: "1.0.0" | ||
type: "add" | ||
- message: "'name' property type must be string" | ||
errorCheck: "name" | ||
quickfix: | ||
text: "Dummy Name" | ||
type: "add" | ||
- message: "'url' property type must be string" | ||
errorCheck: "url" | ||
quickfix: | ||
text: "https://example.com" | ||
type: "add" | ||
- message: "'description' property type must be string" | ||
errorCheck: "description" | ||
quickfix: | ||
text: "Dummy description" | ||
type: "add" | ||
|
||
|
||
asyncapi-info-contact-properties: | ||
- message: "Add a 'name'" | ||
quickfix: | ||
text: "name: Dummy Name" | ||
type: "add" | ||
- message: "Add a 'email'" | ||
quickfix: | ||
text: "email: [email protected]" | ||
type: "add" | ||
- message: "Add a 'url'" | ||
quickfix: | ||
text: "url: https://example.com" | ||
type: "add" | ||
|
||
asyncapi-tags-uniqueness: | ||
- message: "Delete the same value object" | ||
quickfix: | ||
type: "delete" | ||
- message: "Update the same value object" | ||
quickfix: | ||
type: "append" | ||
text: "-1" |
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.
there is something magic about 'asyncapi-schema' to be harcoded here in the
provideCodeActions
method?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.
Many errors have the same error code i.e.
asyncapi-schema
So for that specific error code I'm checking if errorMessage contains errorCheck word (which is present in the yaml file) and showing autofix directly
The errors can't be distinguish by just error codes so had to add one more condition for just asyncapi-schema errors