-
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
12
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.
+845
−9
Open
Changes from 10 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
5517920
Setup CodeActions and add quickfix for missing inputs
rentziass 5d05f29
PR feedback
rentziass f6750a6
Update languageservice/src/code-actions/quickfix/add-missing-inputs.ts
rentziass fa9be15
Fix indentSize detection for code actions after rebase
rentziass 745ad89
Merge branch 'main' into rentziass/codeactions2
rentziass a186d7a
update typescript
rentziass 576419e
formatting
rentziass df2127b
linting
rentziass 612cfdb
Merge branch 'main' into rentziass/codeactions2
rentziass 1ba0f24
Gate missing inputs quickfix behind feature flag
rentziass 1116f79
Address PR review: rename files, move position calculation to quickfix
rentziass 1efba34
wip
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,53 @@ | ||
| import {FeatureFlags} from "@actions/expressions"; | ||
| import {CodeAction, CodeActionKind, Diagnostic} from "vscode-languageserver-types"; | ||
| import {CodeActionContext, CodeActionProvider} from "./types.js"; | ||
| import {getQuickfixProviders} from "./quickfix/index.js"; | ||
|
|
||
| export interface CodeActionParams { | ||
| uri: string; | ||
| diagnostics: Diagnostic[]; | ||
| only?: string[]; | ||
| featureFlags?: FeatureFlags; | ||
| } | ||
|
|
||
| export function getCodeActions(params: CodeActionParams): CodeAction[] { | ||
| const actions: CodeAction[] = []; | ||
| const context: CodeActionContext = { | ||
| uri: params.uri, | ||
| featureFlags: params.featureFlags | ||
| }; | ||
|
|
||
| // Build providers map based on feature flags | ||
| const providersByKind: Map<string, CodeActionProvider[]> = new Map([ | ||
| [CodeActionKind.QuickFix, getQuickfixProviders(params.featureFlags)] | ||
| // [CodeActionKind.Refactor, getRefactorProviders(params.featureFlags)], | ||
| // [CodeActionKind.Source, getSourceProviders(params.featureFlags)], | ||
| // etc | ||
| ]); | ||
|
|
||
| // 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.js"; |
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.js"; | ||
| import {DiagnosticCode, MissingInputsDiagnosticData} from "../../validate-action-reference.js"; | ||
|
|
||
| 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,13 @@ | ||
| import {FeatureFlags} from "@actions/expressions"; | ||
| import {CodeActionProvider} from "../types.js"; | ||
| import {addMissingInputsProvider} from "./add-missing-inputs.js"; | ||
|
|
||
| export function getQuickfixProviders(featureFlags?: FeatureFlags): CodeActionProvider[] { | ||
| const providers: CodeActionProvider[] = []; | ||
|
|
||
| if (featureFlags?.isEnabled("missingInputsQuickfix")) { | ||
| providers.push(addMissingInputsProvider); | ||
| } | ||
|
|
||
| return providers; | ||
| } |
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.js"; | ||
| import {ValidationConfig} from "../../validate.js"; | ||
| import {ActionMetadata, ActionReference} from "../../action.js"; | ||
| import {clearCache} from "../../utils/workflow-cache.js"; | ||
|
|
||
| // 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.
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.
Should we use the latest version?
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.
These are just static actions that are part of test data (also added in this PR):
languageservices/languageservice/src/code-actions/tests/runner.test.ts
Lines 18 to 47 in 1ba0f24