Move hover logic from parser to language service

This commit is contained in:
Jacob Wallraff
2023-01-23 16:22:18 -08:00
parent 86401f1f54
commit 6be2bd1cd7
2 changed files with 31 additions and 5 deletions
+31 -4
View File
@@ -1,12 +1,16 @@
import {parseWorkflow} from "@github/actions-workflow-parser";
import {TemplateToken} from "@github/actions-workflow-parser/templates/tokens/template-token";
import {TokenResult} from "./utils/find-token";
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";
import {Position, TextDocument} from "vscode-languageserver-textdocument";
import {Hover} from "vscode-languageserver-types";
import {info} from "./log";
import {nullTrace} from "./nulltrace";
import {findToken} from "./utils/find-token";
import {mapRange} from "./utils/range";
import {getSentence} from "@github/actions-workflow-parser/model/converter/cron";
// Render value description and Context when hovering over a key in a MappingToken
export async function hover(document: TextDocument, position: Position): Promise<Hover | null> {
@@ -16,15 +20,34 @@ export async function hover(document: TextDocument, position: Position): Promise
};
const result = parseWorkflow(file.name, [file], nullTrace);
const {token} = findToken(position, result.value);
const tokenResult = findToken(position, result.value);
if (result.value && token) {
return getHover(token);
if (result.value) {
return getHover(tokenResult);
}
return null;
}
function getHover(token: TemplateToken): Hover | 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) {
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) {
info(`Calculating hover for token with definition ${token.definition.key}`);
@@ -47,3 +70,7 @@ function getHover(token: TemplateToken): Hover | null {
}
return null;
}
function isCronMapping(token: TemplateToken): boolean {
return token.definition?.key === "cron-mapping"
}
@@ -1,6 +1,5 @@
// template-reader *just* does schema validation
import {getSentence} from "../model/converter/cron";
import {ObjectReader} from "./object-reader";
import {TemplateSchema} from "./schema";
import {DefinitionInfo} from "./schema/definition-info";