|
| 1 | +import {isMapping} from "@actions/workflow-parser"; |
| 2 | +import {MappingToken} from "@actions/workflow-parser/templates/tokens/mapping-token"; |
| 3 | +import {ScalarToken} from "@actions/workflow-parser/templates/tokens/scalar-token"; |
| 4 | +import {TemplateToken} from "@actions/workflow-parser/templates/tokens/template-token"; |
| 5 | +import {CodeAction, Position, TextEdit} from "vscode-languageserver-types"; |
| 6 | +import {error} from "../../log.js"; |
| 7 | +import {findToken} from "../../utils/find-token.js"; |
| 8 | +import {getOrParseWorkflow} from "../../utils/workflow-cache.js"; |
| 9 | +import {DiagnosticCode, MissingInputsDiagnosticData} from "../../validate-action-reference.js"; |
| 10 | +import {CodeActionContext, CodeActionProvider} from "../types.js"; |
| 11 | + |
| 12 | +/** |
| 13 | + * Information extracted from a step token needed to generate edits |
| 14 | + */ |
| 15 | +interface StepInfo { |
| 16 | + /** Column where step keys start (1-indexed), e.g., the column of "uses:" */ |
| 17 | + stepKeyColumn: number; |
| 18 | + /** End line of the step (1-indexed) */ |
| 19 | + stepEndLine: number; |
| 20 | + /** Detected indent size (spaces per level) */ |
| 21 | + indentSize: number; |
| 22 | + /** Information about existing with: block, if present */ |
| 23 | + withInfo?: { |
| 24 | + keyColumn: number; |
| 25 | + keyEndLine: number; |
| 26 | + valueEndLine: number; |
| 27 | + hasChildren: boolean; |
| 28 | + /** Column of first child input (1-indexed), for indentation detection */ |
| 29 | + firstChildColumn?: number; |
| 30 | + }; |
| 31 | +} |
| 32 | + |
| 33 | +export const addMissingInputsProvider: CodeActionProvider = { |
| 34 | + diagnosticCodes: [DiagnosticCode.MissingRequiredInputs], |
| 35 | + |
| 36 | + createCodeAction(context: CodeActionContext, diagnostic): CodeAction | undefined { |
| 37 | + const data = diagnostic.data as MissingInputsDiagnosticData | undefined; |
| 38 | + if (!data) { |
| 39 | + return undefined; |
| 40 | + } |
| 41 | + |
| 42 | + // Parse the document to get the step token |
| 43 | + const stepInfo = getStepInfo(context, diagnostic.range.start); |
| 44 | + if (!stepInfo) { |
| 45 | + return undefined; |
| 46 | + } |
| 47 | + |
| 48 | + const edits = createInputEdits(data.missingInputs, stepInfo); |
| 49 | + if (!edits || edits.length === 0) { |
| 50 | + return undefined; |
| 51 | + } |
| 52 | + |
| 53 | + const inputNames = data.missingInputs.map(i => i.name).join(", "); |
| 54 | + |
| 55 | + return { |
| 56 | + title: `Add missing input${data.missingInputs.length > 1 ? "s" : ""}: ${inputNames}`, |
| 57 | + edit: { |
| 58 | + changes: { |
| 59 | + [context.uri]: edits |
| 60 | + } |
| 61 | + } |
| 62 | + }; |
| 63 | + } |
| 64 | +}; |
| 65 | + |
| 66 | +/** |
| 67 | + * Parse the document and extract step information needed for generating edits. |
| 68 | + * Returns undefined if parsing fails or the step token cannot be found. |
| 69 | + */ |
| 70 | +function getStepInfo(context: CodeActionContext, diagnosticPosition: Position): StepInfo | undefined { |
| 71 | + // Parse the document (uses cache if available from validation) |
| 72 | + const file = {name: context.uri, content: context.documentContent}; |
| 73 | + const parseResult = getOrParseWorkflow(file, context.uri); |
| 74 | + |
| 75 | + if (!parseResult.value) { |
| 76 | + error("Failed to parse workflow for missing inputs quickfix"); |
| 77 | + return undefined; |
| 78 | + } |
| 79 | + |
| 80 | + // Find the token at the diagnostic position |
| 81 | + const {path} = findToken(diagnosticPosition, parseResult.value); |
| 82 | + |
| 83 | + // Walk up the path to find the step token (regular-step) |
| 84 | + const stepToken = findStepInPath(path); |
| 85 | + if (!stepToken) { |
| 86 | + error("Could not find step token for missing inputs quickfix"); |
| 87 | + return undefined; |
| 88 | + } |
| 89 | + |
| 90 | + return extractStepInfo(stepToken); |
| 91 | +} |
| 92 | + |
| 93 | +/** |
| 94 | + * Find the step token (regular-step) in the token path |
| 95 | + */ |
| 96 | +function findStepInPath(path: TemplateToken[]): MappingToken | undefined { |
| 97 | + // Walk backwards through path to find the step |
| 98 | + for (let i = path.length - 1; i >= 0; i--) { |
| 99 | + if (path[i].definition?.key === "regular-step" && isMapping(path[i])) { |
| 100 | + return path[i] as MappingToken; |
| 101 | + } |
| 102 | + } |
| 103 | + return undefined; |
| 104 | +} |
| 105 | + |
| 106 | +/** |
| 107 | + * Extract position and indentation info from a step token |
| 108 | + */ |
| 109 | +function extractStepInfo(stepToken: MappingToken): StepInfo | undefined { |
| 110 | + if (!stepToken.range) { |
| 111 | + return undefined; |
| 112 | + } |
| 113 | + |
| 114 | + // Get the column of the first key in the step |
| 115 | + let stepKeyColumn = stepToken.range.start.column; |
| 116 | + if (stepToken.count > 0) { |
| 117 | + const firstEntry = stepToken.get(0); |
| 118 | + if (firstEntry?.key.range) { |
| 119 | + stepKeyColumn = firstEntry.key.range.start.column; |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + // Find the with: block if present |
| 124 | + let withKey: ScalarToken | undefined; |
| 125 | + let withToken: TemplateToken | undefined; |
| 126 | + for (const {key, value} of stepToken) { |
| 127 | + if (key.toString() === "with") { |
| 128 | + withKey = key; |
| 129 | + withToken = value; |
| 130 | + break; |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + // Calculate indent size |
| 135 | + let indentSize = 2; // Default |
| 136 | + let withInfo: StepInfo["withInfo"]; |
| 137 | + |
| 138 | + if (withKey?.range && withToken?.range) { |
| 139 | + // Has with: block - extract its info |
| 140 | + const hasChildren = isMapping(withToken) && withToken.count > 0; |
| 141 | + let firstChildColumn: number | undefined; |
| 142 | + |
| 143 | + if (hasChildren) { |
| 144 | + const firstChild = (withToken as MappingToken).get(0); |
| 145 | + if (firstChild?.key.range) { |
| 146 | + firstChildColumn = firstChild.key.range.start.column; |
| 147 | + // Detect indent size from with: children |
| 148 | + indentSize = firstChildColumn - withKey.range.start.column; |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + withInfo = { |
| 153 | + keyColumn: withKey.range.start.column, |
| 154 | + keyEndLine: withKey.range.end.line, |
| 155 | + valueEndLine: withToken.range.end.line, |
| 156 | + hasChildren, |
| 157 | + firstChildColumn |
| 158 | + }; |
| 159 | + } else { |
| 160 | + // No with: block - detect indent size using heuristics |
| 161 | + // Based on the step key column position, estimate indent size |
| 162 | + // 2-space indent files typically have step keys at column 7 |
| 163 | + // 4-space indent files typically have step keys at column 15 |
| 164 | + const zeroIndexedCol = stepKeyColumn - 1; |
| 165 | + if (zeroIndexedCol >= 10) { |
| 166 | + indentSize = 4; |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + return { |
| 171 | + stepKeyColumn, |
| 172 | + stepEndLine: stepToken.range.end.line, |
| 173 | + indentSize, |
| 174 | + withInfo |
| 175 | + }; |
| 176 | +} |
| 177 | + |
| 178 | +/** |
| 179 | + * Generate text edits to add missing inputs |
| 180 | + */ |
| 181 | +function createInputEdits(missingInputs: MissingInputsDiagnosticData["missingInputs"], stepInfo: StepInfo): TextEdit[] { |
| 182 | + const formatInputLines = (indent: string) => |
| 183 | + missingInputs.map(input => { |
| 184 | + const value = input.default ?? '""'; |
| 185 | + return `${indent}${input.name}: ${value}`; |
| 186 | + }); |
| 187 | + |
| 188 | + if (stepInfo.withInfo) { |
| 189 | + // `with:` exists - add inputs to existing block |
| 190 | + const withIndent = stepInfo.withInfo.keyColumn - 1; // 0-indexed |
| 191 | + const inputIndentSize = stepInfo.withInfo.firstChildColumn |
| 192 | + ? stepInfo.withInfo.firstChildColumn - stepInfo.withInfo.keyColumn |
| 193 | + : stepInfo.indentSize; |
| 194 | + |
| 195 | + const inputIndent = " ".repeat(withIndent + inputIndentSize); |
| 196 | + const inputLines = formatInputLines(inputIndent); |
| 197 | + |
| 198 | + // Calculate insert position |
| 199 | + let insertLine: number; |
| 200 | + if (stepInfo.withInfo.hasChildren) { |
| 201 | + // Insert after the last child (at end of with: block) |
| 202 | + // valueEndLine is 1-indexed, we want 0-indexed for Position |
| 203 | + insertLine = stepInfo.withInfo.valueEndLine - 1; |
| 204 | + } else { |
| 205 | + // Empty with: block - insert on the next line after with: |
| 206 | + // keyEndLine is 1-indexed, convert to 0-indexed and go to next line |
| 207 | + insertLine = stepInfo.withInfo.keyEndLine; |
| 208 | + } |
| 209 | + |
| 210 | + const insertPosition: Position = { |
| 211 | + line: insertLine, |
| 212 | + character: 0 |
| 213 | + }; |
| 214 | + |
| 215 | + return [ |
| 216 | + { |
| 217 | + range: {start: insertPosition, end: insertPosition}, |
| 218 | + newText: inputLines.map(line => line + "\n").join("") |
| 219 | + } |
| 220 | + ]; |
| 221 | + } else { |
| 222 | + // No `with:` key - add `with:` at the same level as other step keys |
| 223 | + const withKeyIndent = stepInfo.stepKeyColumn - 1; // 0-indexed (columns are 1-based) |
| 224 | + |
| 225 | + const withIndent = " ".repeat(withKeyIndent); |
| 226 | + const inputIndent = " ".repeat(withKeyIndent + stepInfo.indentSize); |
| 227 | + const inputLines = formatInputLines(inputIndent); |
| 228 | + |
| 229 | + const newText = `${withIndent}with:\n` + inputLines.map(line => `${line}\n`).join(""); |
| 230 | + |
| 231 | + // Insert at end of step |
| 232 | + // stepEndLine is 1-indexed, we want 0-indexed and insert before the line after |
| 233 | + const insertPosition: Position = { |
| 234 | + line: stepInfo.stepEndLine - 1, |
| 235 | + character: 0 |
| 236 | + }; |
| 237 | + |
| 238 | + return [ |
| 239 | + { |
| 240 | + range: {start: insertPosition, end: insertPosition}, |
| 241 | + newText |
| 242 | + } |
| 243 | + ]; |
| 244 | + } |
| 245 | +} |
0 commit comments