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

55 lines
1.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 {File} from "@github/actions-workflow-parser/workflows/file";
import {Position, TextDocument} from "vscode-languageserver-textdocument";
import {Hover} from "vscode-languageserver-types";
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) {
return getHover(token);
2022-11-08 17:00:59 -08:00
}
return null;
}
function getHover(token: TemplateToken): Hover | null {
if (token.definition) {
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,
range: {
start: {
line: token.range!.start[0] - 1,
character: token.range!.start[1] - 1
2022-11-08 17:00:59 -08:00
},
end: {
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;
}