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 {File} from "@github/actions-workflow-parser/workflows/file";
|
|
|
|
|
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-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);
|
|
|
|
|
|
2022-12-01 11:37:50 -05:00
|
|
|
const {token} = findToken(position, result.value);
|
2022-11-21 17:21:40 -05:00
|
|
|
|
|
|
|
|
if (result.value && token) {
|
2022-11-29 17:01:43 -05:00
|
|
|
return getHover(token);
|
2022-11-08 17:00:59 -08:00
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-29 17:01:43 -05:00
|
|
|
function getHover(token: TemplateToken): Hover | null {
|
|
|
|
|
if (token.definition) {
|
2022-12-05 10:05:41 -08:00
|
|
|
info(`Calculation hover for token with definition ${token.definition.key}`);
|
|
|
|
|
|
2022-11-08 17:00:59 -08:00
|
|
|
let description = "";
|
2022-11-29 17:01:43 -05:00
|
|
|
if (token.description) {
|
|
|
|
|
description = token.description;
|
2022-11-08 17:00:59 -08:00
|
|
|
}
|
|
|
|
|
|
2022-11-29 17:01:43 -05:00
|
|
|
if (token.definition.evaluatorContext.length > 0) {
|
2022-11-08 17:00:59 -08:00
|
|
|
// Only add padding if there is a description
|
2022-11-29 17:01:43 -05:00
|
|
|
description += `${description.length > 0 ? `\n\n` : ""}**Context:** ${token.definition.evaluatorContext.join(
|
|
|
|
|
", "
|
|
|
|
|
)}`;
|
2022-11-08 17:00:59 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
contents: description,
|
|
|
|
|
range: {
|
|
|
|
|
start: {
|
2022-11-29 17:01:43 -05:00
|
|
|
line: token.range!.start[0] - 1,
|
|
|
|
|
character: token.range!.start[1] - 1
|
2022-11-08 17:00:59 -08:00
|
|
|
},
|
|
|
|
|
end: {
|
2022-11-29 17:01:43 -05:00
|
|
|
line: token.range!.end[0] - 1,
|
|
|
|
|
character: token.range!.end[1] - 1
|
2022-11-15 14:08:12 -05:00
|
|
|
}
|
|
|
|
|
}
|
2022-11-08 17:00:59 -08:00
|
|
|
} as Hover;
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|