-
Notifications
You must be signed in to change notification settings - Fork 52
Setup CodeActions and add quickfix for missing inputs #254
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
Open
rentziass
wants to merge
7
commits into
main
Choose a base branch
from
rentziass/codeactions2
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
f35d847
Setup CodeActions and add quickfix for missing inputs
rentziass 6498a99
Merge branch 'main' into rentziass/codeactions2
rentziass 2d9b787
Remove CodeActionConfig
rentziass b95219e
PR feedback
rentziass 514a119
Update languageservice/src/code-actions/quickfix/add-missing-inputs.ts
rentziass 106aef0
Merge branch 'main' into rentziass/codeactions2
rentziass a1bd7e8
Merge branch 'main' into rentziass/codeactions2
rentziass 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
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 |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| import {CodeAction, CodeActionKind, Diagnostic} from "vscode-languageserver-types"; | ||
| import {CodeActionContext, CodeActionProvider} from "./types"; | ||
| import {quickfixProviders} from "./quickfix"; | ||
|
|
||
| // Aggregate all providers by kind | ||
| const providersByKind: Map<string, CodeActionProvider[]> = new Map([ | ||
| [CodeActionKind.QuickFix, quickfixProviders] | ||
| // [CodeActionKind.Refactor, refactorProviders], | ||
| // [CodeActionKind.Source, sourceProviders], | ||
| // etc | ||
| ]); | ||
|
|
||
| export interface CodeActionParams { | ||
| uri: string; | ||
| diagnostics: Diagnostic[]; | ||
| only?: string[]; | ||
| } | ||
|
|
||
| export function getCodeActions(params: CodeActionParams): CodeAction[] { | ||
| const actions: CodeAction[] = []; | ||
| const context: CodeActionContext = { | ||
| uri: params.uri | ||
| }; | ||
|
|
||
| // Filter to requested kinds, or use all if none specified | ||
| const requestedKinds = params.only; | ||
| const kindsToCheck = requestedKinds | ||
| ? [...providersByKind.keys()].filter(kind => requestedKinds.some(requested => kind.startsWith(requested))) | ||
| : [...providersByKind.keys()]; | ||
|
|
||
| for (const diagnostic of params.diagnostics) { | ||
| for (const kind of kindsToCheck) { | ||
| const providers = providersByKind.get(kind) ?? []; | ||
| for (const provider of providers) { | ||
| if (provider.diagnosticCodes.includes(diagnostic.code)) { | ||
| const action = provider.createCodeAction(context, diagnostic); | ||
| if (action) { | ||
| action.kind = kind; | ||
| action.diagnostics = [diagnostic]; | ||
| actions.push(action); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return actions; | ||
| } | ||
|
|
||
| export type {CodeActionContext, CodeActionProvider} from "./types"; | ||
65 changes: 65 additions & 0 deletions
65
languageservice/src/code-actions/quickfix/add-missing-inputs.ts
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 |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| import {CodeAction, TextEdit} from "vscode-languageserver-types"; | ||
| import {CodeActionProvider} from "../types"; | ||
| import {DiagnosticCode, MissingInputsDiagnosticData} from "../../validate-action"; | ||
|
|
||
| export const addMissingInputsProvider: CodeActionProvider = { | ||
| diagnosticCodes: [DiagnosticCode.MissingRequiredInputs], | ||
|
|
||
| createCodeAction(context, diagnostic): CodeAction | undefined { | ||
| const data = diagnostic.data as MissingInputsDiagnosticData | undefined; | ||
| if (!data) { | ||
| return undefined; | ||
| } | ||
|
|
||
| const edits = createInputEdits(data); | ||
| if (!edits) { | ||
| return undefined; | ||
| } | ||
|
|
||
| const inputNames = data.missingInputs.map(i => i.name).join(", "); | ||
|
|
||
| return { | ||
| title: `Add missing input${data.missingInputs.length > 1 ? "s" : ""}: ${inputNames}`, | ||
| edit: { | ||
| changes: { | ||
| [context.uri]: edits | ||
| } | ||
| } | ||
| }; | ||
| } | ||
| }; | ||
|
|
||
| function createInputEdits(data: MissingInputsDiagnosticData): TextEdit[] { | ||
| const edits: TextEdit[] = []; | ||
|
|
||
| const formatInputLines = (indent: string) => | ||
| data.missingInputs.map(input => { | ||
| const value = input.default ?? '""'; | ||
| return `${indent}${input.name}: ${value}`; | ||
| }); | ||
|
|
||
| if (data.hasWithKey && data.withIndent !== undefined) { | ||
| // `with:` exists - use its indentation + 2 for inputs | ||
| const inputIndent = " ".repeat(data.withIndent + data.indentSize); | ||
| const inputLines = formatInputLines(inputIndent); | ||
|
|
||
| edits.push({ | ||
| range: {start: data.insertPosition, end: data.insertPosition}, | ||
| newText: inputLines.map(line => line + "\n").join("") | ||
| }); | ||
| } else { | ||
| // No `with:` key - `with:` at step indentation, inputs at step indentation + 2 | ||
| const withIndent = " ".repeat(data.stepIndent); | ||
| const inputIndent = " ".repeat(data.stepIndent + data.indentSize); | ||
| const inputLines = formatInputLines(inputIndent); | ||
|
|
||
| const newText = `${withIndent}with:\n` + inputLines.map(line => `${line}\n`).join(""); | ||
|
|
||
| edits.push({ | ||
| range: {start: data.insertPosition, end: data.insertPosition}, | ||
| newText | ||
| }); | ||
| } | ||
|
|
||
| return edits; | ||
| } |
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 |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| import {CodeActionProvider} from "../types"; | ||
| import {addMissingInputsProvider} from "./add-missing-inputs"; | ||
|
|
||
| export const quickfixProviders: CodeActionProvider[] = [addMissingInputsProvider]; |
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 |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| import * as path from "path"; | ||
| import {fileURLToPath} from "url"; | ||
| import {loadTestCases, runTestCase} from "./runner"; | ||
| import {ValidationConfig} from "../../validate"; | ||
| import {ActionMetadata, ActionReference} from "../../action"; | ||
| import {clearCache} from "../../utils/workflow-cache"; | ||
|
|
||
| // ESM-compatible __dirname | ||
| const __filename = fileURLToPath(import.meta.url); | ||
| const __dirname = path.dirname(__filename); | ||
|
|
||
| // Mock action metadata provider for tests | ||
| const validationConfig: ValidationConfig = { | ||
| actionsMetadataProvider: { | ||
| fetchActionMetadata: (ref: ActionReference): Promise<ActionMetadata | undefined> => { | ||
| const key = `${ref.owner}/${ref.name}@${ref.ref}`; | ||
|
|
||
| const metadata: Record<string, ActionMetadata> = { | ||
| "actions/cache@v1": { | ||
| name: "Cache", | ||
| description: "Cache dependencies", | ||
| inputs: { | ||
| path: { | ||
| description: "A list of files to cache", | ||
| required: true | ||
| }, | ||
| key: { | ||
| description: "Cache key", | ||
| required: true | ||
| }, | ||
| "restore-keys": { | ||
| description: "Restore keys", | ||
| required: false | ||
| } | ||
| } | ||
| }, | ||
| "actions/setup-node@v3": { | ||
| name: "Setup Node", | ||
| description: "Setup Node.js", | ||
| inputs: { | ||
| "node-version": { | ||
| description: "Node version", | ||
| required: true, | ||
| default: "16" | ||
| } | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| return Promise.resolve(metadata[key]); | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| // Point to the source testdata directory | ||
| const testdataDir = path.join(__dirname, "testdata"); | ||
|
|
||
| beforeEach(() => { | ||
| clearCache(); | ||
| }); | ||
|
|
||
| describe("code action golden tests", () => { | ||
| const testCases = loadTestCases(testdataDir); | ||
|
|
||
| if (testCases.length === 0) { | ||
| it.todo("no test cases found - add .yml files to testdata/"); | ||
| return; | ||
| } | ||
|
|
||
| for (const testCase of testCases) { | ||
| it(testCase.name, async () => { | ||
| const result = await runTestCase(testCase, validationConfig); | ||
|
|
||
| if (!result.passed) { | ||
| let errorMessage = result.error || "Test failed"; | ||
|
|
||
| if (result.expected !== undefined && result.actual !== undefined) { | ||
| errorMessage += "\n\n"; | ||
| errorMessage += "=== EXPECTED (golden file) ===\n"; | ||
| errorMessage += result.expected; | ||
| errorMessage += "\n\n"; | ||
| errorMessage += "=== ACTUAL ===\n"; | ||
| errorMessage += result.actual; | ||
| } | ||
|
|
||
| throw new Error(errorMessage); | ||
| } | ||
| }); | ||
| } | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.