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

63 lines
2.2 KiB
TypeScript
Raw Normal View History

2022-11-22 16:03:01 -05:00
import {isMapping, 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-11-21 17:21:40 -05:00
const {token, keyToken, parent} = findToken(position, result.value);
if (result.value && token) {
// If the parent is a MappingToken and no keyToken was returned, our token is the key
2022-11-22 16:03:01 -05:00
if (parent && isMapping(parent) && !keyToken) {
const value = parent.find(token.toString());
2022-11-21 17:21:40 -05:00
if (value) {
return getHover(token, value);
}
}
2022-11-08 17:00:59 -08:00
}
return null;
}
2022-11-21 17:21:40 -05:00
// PositionToken is the token that the cursor is on
// DescriptionToken may differ if the description is stored on an associated token, such as when hovering over a key in a mapping
function getHover(positionToken: TemplateToken, descriptionToken: TemplateToken): Hover | null {
if (descriptionToken.definition) {
2022-11-08 17:00:59 -08:00
let description = "";
2022-11-21 17:21:40 -05:00
if (descriptionToken.description) {
description = descriptionToken.description;
2022-11-08 17:00:59 -08:00
}
2022-11-21 17:21:40 -05:00
if (descriptionToken.definition.evaluatorContext.length > 0) {
2022-11-08 17:00:59 -08:00
// Only add padding if there is a description
2022-11-21 17:21:40 -05:00
description += `${
description.length > 0 ? `\n\n` : ""
}**Context:** ${descriptionToken.definition.evaluatorContext.join(", ")}`;
2022-11-08 17:00:59 -08:00
}
return {
contents: description,
range: {
start: {
2022-11-21 17:21:40 -05:00
line: positionToken.range!.start[0] - 1,
character: positionToken.range!.start[1] - 1
2022-11-08 17:00:59 -08:00
},
end: {
2022-11-21 17:21:40 -05:00
line: positionToken.range!.end[0] - 1,
character: positionToken.range!.end[1] - 1
2022-11-15 14:08:12 -05:00
}
}
2022-11-08 17:00:59 -08:00
} as Hover;
}
return null;
}