Show description and context on key hover

This commit is contained in:
Beth Brennan
2022-11-21 17:21:40 -05:00
parent eb9daaccd2
commit 2e8f4ea076
3 changed files with 72 additions and 33 deletions
+29 -19
View File
@@ -1,13 +1,14 @@
import {parseWorkflow, ParseWorkflowResult} from "@github/actions-workflow-parser";
import {parseWorkflow} from "@github/actions-workflow-parser";
import {TemplateToken} from "@github/actions-workflow-parser/templates/tokens/template-token";
import {MappingToken} from "@github/actions-workflow-parser/templates/tokens/mapping-token";
import {SequenceToken} from "@github/actions-workflow-parser/templates/tokens/sequence-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";
import {findInnerToken} from "./utils/find-token";
import {findInnerToken, findToken} from "./utils/find-token";
import {TokenType} from "@github/actions-workflow-parser/templates/tokens/types";
// Render value description and Context when hovering over a key in a MappingToken
export async function hover(document: TextDocument, position: Position): Promise<Hover | null> {
const file: File = {
name: document.uri,
@@ -15,38 +16,47 @@ export async function hover(document: TextDocument, position: Position): Promise
};
const result = parseWorkflow(file.name, [file], nullTrace);
// Find inner token returns null if position is not in a token
const innerToken = findInnerToken(position, result.value);
if (result.value && innerToken) {
return getHover(innerToken);
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
if (parent?.templateTokenType === TokenType.Mapping && !keyToken) {
const mappingToken = parent as MappingToken;
const value = mappingToken.find(token.toString());
if (value) {
return getHover(token, value);
}
}
}
return null;
}
function getHover(innerToken: TemplateToken): Hover | null {
if (innerToken.definition) {
// 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) {
let description = "";
if (innerToken.description) {
description = innerToken.description;
if (descriptionToken.description) {
description = descriptionToken.description;
}
if (innerToken.definition.evaluatorContext.length > 0) {
if (descriptionToken.definition.evaluatorContext.length > 0) {
// Only add padding if there is a description
description += `${description.length > 0 ? `\n\n` : ""}**Context:** ${innerToken.definition.evaluatorContext.join(
", "
)}`;
description += `${
description.length > 0 ? `\n\n` : ""
}**Context:** ${descriptionToken.definition.evaluatorContext.join(", ")}`;
}
return {
contents: description,
range: {
start: {
line: innerToken.range!.start[0],
character: innerToken.range!.start[1]
line: positionToken.range!.start[0] - 1,
character: positionToken.range!.start[1] - 1
},
end: {
line: innerToken.range!.end[0],
character: innerToken.range!.end[1]
line: positionToken.range!.end[0] - 1,
character: positionToken.range!.end[1] - 1
}
}
} as Hover;