Merge pull request #97 from github/joshmgross/action-input-descriptions
Show action input descriptions on hover
This commit is contained in:
@@ -1,6 +1,25 @@
|
||||
import {hover} from "./hover";
|
||||
import {isString} from "@github/actions-workflow-parser/.";
|
||||
import {StringToken} from "@github/actions-workflow-parser/templates/tokens/string-token";
|
||||
import {DescriptionProvider, hover, HoverConfig} from "./hover";
|
||||
import {getPositionFromCursor} from "./test-utils/cursor-position";
|
||||
|
||||
function testHoverConfig(tokenValue: string, tokenKey: string, description?: string) {
|
||||
return {
|
||||
descriptionProvider: {
|
||||
getDescription: async (_, token, __) => {
|
||||
if (!isString(token)) {
|
||||
throw new Error("Test provider only supports string tokens");
|
||||
}
|
||||
|
||||
expect((token as StringToken).value).toEqual(tokenValue);
|
||||
expect(token.definition!.key).toEqual(tokenKey);
|
||||
|
||||
return description;
|
||||
}
|
||||
} satisfies DescriptionProvider
|
||||
} satisfies HoverConfig
|
||||
}
|
||||
|
||||
describe("hover", () => {
|
||||
it("on a key", async () => {
|
||||
const input = `o|n: push
|
||||
@@ -77,3 +96,37 @@ jobs:
|
||||
expect(result?.contents).toEqual("Runs your workflow when you push a commit or tag.");
|
||||
});
|
||||
});
|
||||
|
||||
describe("hover with description provider", () => {
|
||||
it("uses the description provider", async () => {
|
||||
const input = `
|
||||
on: push
|
||||
jobs:
|
||||
build:
|
||||
runs-on: [self-hosted]
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
with:
|
||||
ref|: main
|
||||
`;
|
||||
|
||||
const result = await hover(...getPositionFromCursor(input), testHoverConfig("ref", "string", "The branch, tag or SHA to checkout."));
|
||||
expect(result).not.toBeUndefined();
|
||||
expect(result?.contents).toEqual("The branch, tag or SHA to checkout.");
|
||||
});
|
||||
|
||||
it("falls back to the token description", async () => {
|
||||
const input = `
|
||||
on: push
|
||||
jobs:
|
||||
build:
|
||||
runs-on: [self-hosted]
|
||||
steps:
|
||||
- uses|: actions/checkout@v2
|
||||
`;
|
||||
|
||||
const result = await hover(...getPositionFromCursor(input), testHoverConfig("uses", "non-empty-string", undefined));
|
||||
expect(result).not.toBeUndefined();
|
||||
expect(result?.contents).toEqual("Selects an action to run as part of a step in your job. An action is a reusable unit of code. You can use an action defined in the same repository as the workflow, a public repository, or in a published Docker container image.");
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,49 +1,67 @@
|
||||
import {parseWorkflow} from "@github/actions-workflow-parser";
|
||||
import {convertWorkflowTemplate, parseWorkflow, ParseWorkflowResult} from "@github/actions-workflow-parser";
|
||||
import {ErrorPolicy} from "@github/actions-workflow-parser/model/convert";
|
||||
import {TemplateToken} from "@github/actions-workflow-parser/templates/tokens/template-token";
|
||||
import {File} from "@github/actions-workflow-parser/workflows/file";
|
||||
import {Position, TextDocument} from "vscode-languageserver-textdocument";
|
||||
import {Hover} from "vscode-languageserver-types";
|
||||
import {getWorkflowContext, WorkflowContext} from "./context/workflow-context";
|
||||
import {info} from "./log";
|
||||
import {nullTrace} from "./nulltrace";
|
||||
import {findToken} from "./utils/find-token";
|
||||
import {mapRange} from "./utils/range";
|
||||
|
||||
export type DescriptionProvider = {
|
||||
getDescription(context: WorkflowContext, token: TemplateToken, path: TemplateToken[]): Promise<string | undefined>;
|
||||
};
|
||||
|
||||
export type HoverConfig = {
|
||||
descriptionProvider?: DescriptionProvider;
|
||||
};
|
||||
|
||||
// Render value description and Context when hovering over a key in a MappingToken
|
||||
export async function hover(document: TextDocument, position: Position): Promise<Hover | null> {
|
||||
export async function hover(document: TextDocument, position: Position, config?: HoverConfig): Promise<Hover | null> {
|
||||
const file: File = {
|
||||
name: document.uri,
|
||||
content: document.getText()
|
||||
};
|
||||
const result = parseWorkflow(file.name, [file], nullTrace);
|
||||
|
||||
const {token} = findToken(position, result.value);
|
||||
|
||||
if (result.value && token) {
|
||||
return getHover(token);
|
||||
const {token, path} = findToken(position, result.value);
|
||||
if (!token?.definition) {
|
||||
return null;
|
||||
}
|
||||
return null;
|
||||
|
||||
info(`Calculating hover for token with definition ${token.definition.key}`);
|
||||
|
||||
let description = await getDescription(document, config, result, token, path);
|
||||
|
||||
if (token.definition.evaluatorContext.length > 0) {
|
||||
// Only add padding if there is a description
|
||||
description += `${description.length > 0 ? `\n\n` : ""}**Context:** ${token.definition.evaluatorContext.join(
|
||||
", "
|
||||
)}`;
|
||||
}
|
||||
|
||||
return {
|
||||
contents: description,
|
||||
range: mapRange(token.range)
|
||||
} satisfies Hover;
|
||||
}
|
||||
|
||||
function getHover(token: TemplateToken): Hover | null {
|
||||
if (token.definition) {
|
||||
info(`Calculating hover for token with definition ${token.definition.key}`);
|
||||
|
||||
let description = "";
|
||||
if (token.description) {
|
||||
description = token.description;
|
||||
}
|
||||
|
||||
if (token.definition.evaluatorContext.length > 0) {
|
||||
// Only add padding if there is a description
|
||||
description += `${description.length > 0 ? `\n\n` : ""}**Context:** ${token.definition.evaluatorContext.join(
|
||||
", "
|
||||
)}`;
|
||||
}
|
||||
|
||||
return {
|
||||
contents: description,
|
||||
range: mapRange(token.range)
|
||||
} as Hover;
|
||||
async function getDescription(
|
||||
document: TextDocument,
|
||||
config: HoverConfig | undefined,
|
||||
result: ParseWorkflowResult | undefined,
|
||||
token: TemplateToken,
|
||||
path: TemplateToken[]
|
||||
) {
|
||||
const defaultDescription = token.description || "";
|
||||
if (!result?.value || !config?.descriptionProvider) {
|
||||
return defaultDescription;
|
||||
}
|
||||
return null;
|
||||
|
||||
const template = convertWorkflowTemplate(result.context, result.value, ErrorPolicy.TryConversion);
|
||||
const workflowContext = getWorkflowContext(document.uri, template, path);
|
||||
const description = await config.descriptionProvider.getDescription(workflowContext, token, path);
|
||||
return description || defaultDescription;
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@ export interface Value {
|
||||
/** Label of this value */
|
||||
label: string;
|
||||
|
||||
/** Optional description to show when auto-completing or hovering */
|
||||
/** Optional description to show when auto-completing */
|
||||
description?: string;
|
||||
|
||||
/** Whether this value is deprecated */
|
||||
|
||||
Reference in New Issue
Block a user