Files
languageservices/actions-languageservice/src/hover.ts
T

78 lines
2.6 KiB
TypeScript
Raw Normal View History

2022-12-01 11:37:50 -05:00
import {parseWorkflow} from "@github/actions-workflow-parser";
2022-11-15 14:08:12 -05:00
import {TemplateToken} from "@github/actions-workflow-parser/templates/tokens/template-token";
import {TokenResult} from "./utils/find-token";
2022-11-15 14:08:12 -05:00
import {File} from "@github/actions-workflow-parser/workflows/file";
import {isString} from "@github/actions-workflow-parser/templates/tokens/type-guards";
import {StringToken} from "@github/actions-workflow-parser/templates/tokens/string-token";
2022-11-15 14:08:12 -05:00
import {Position, TextDocument} from "vscode-languageserver-textdocument";
import {Hover} from "vscode-languageserver-types";
2022-12-05 10:05:41 -08:00
import {info} from "./log";
2022-11-15 14:08:12 -05:00
import {nullTrace} from "./nulltrace";
2022-11-22 16:03:01 -05:00
import {findToken} from "./utils/find-token";
2022-12-07 10:51:44 -05:00
import {mapRange} from "./utils/range";
import {getSentence} from "@github/actions-workflow-parser/model/converter/cron";
2022-11-08 17:00:59 -08:00
2022-11-21 17:21:40 -05:00
// Render value description and Context when hovering over a key in a MappingToken
2022-11-15 14:08:12 -05:00
export async function hover(document: TextDocument, position: Position): Promise<Hover | null> {
2022-11-08 17:00:59 -08:00
const file: File = {
name: document.uri,
2022-11-15 14:08:12 -05:00
content: document.getText()
2022-11-08 17:00:59 -08:00
};
const result = parseWorkflow(file.name, [file], nullTrace);
const tokenResult = findToken(position, result.value);
2022-11-21 17:21:40 -05:00
if (result.value) {
return getHover(tokenResult);
2022-11-08 17:00:59 -08:00
}
return null;
}
function getHover(tokenResult: TokenResult): Hover | null {
const token = tokenResult.token;
if (!token) {
return null;
}
if (tokenResult.parent && isCronMapping(tokenResult.parent) && isString(token)) {
let description = getSentence((token as StringToken).value);
if (description) {
2023-01-23 16:25:30 -08:00
description +=
"\n\nActions schedules run at most every 5 minutes." +
" [Learn more](https://docs.github.com/actions/using-workflows/workflow-syntax-for-github-actions#onschedule)";
return {
contents: description,
range: mapRange(token.range)
} as Hover;
}
return {
contents: "Invalid cron expression",
range: mapRange(token.range)
} as Hover;
}
if (token.definition) {
2022-12-05 12:08:05 -08:00
info(`Calculating hover for token with definition ${token.definition.key}`);
2022-12-05 10:05:41 -08:00
2022-11-08 17:00:59 -08:00
let description = "";
if (token.description) {
description = token.description;
2022-11-08 17:00:59 -08:00
}
if (token.definition.evaluatorContext.length > 0) {
2022-11-08 17:00:59 -08:00
// Only add padding if there is a description
description += `${description.length > 0 ? `\n\n` : ""}**Context:** ${token.definition.evaluatorContext.join(
", "
)}`;
2022-11-08 17:00:59 -08:00
}
return {
contents: description,
2022-12-07 10:51:44 -05:00
range: mapRange(token.range)
2022-11-08 17:00:59 -08:00
} as Hover;
}
return null;
}
function isCronMapping(token: TemplateToken): boolean {
2023-01-23 16:25:30 -08:00
return token.definition?.key === "cron-mapping";
}