Files
languageservices/languageserver/src/description-provider.ts
T

42 lines
1.4 KiB
TypeScript
Raw Normal View History

2023-01-23 16:02:13 -05:00
import {DescriptionProvider} from "@github/actions-languageservice/hover";
import {Octokit} from "@octokit/rest";
import {getActionInputDescription} from "./description-providers/action-input";
2023-01-30 17:05:32 -08:00
import {TTLCache} from "./utils/cache";
2023-03-06 14:44:18 -05:00
import {isString} from "@github/actions-workflow-parser/templates/tokens/type-guards";
import {TemplateToken} from "@github/actions-workflow-parser/templates/tokens/template-token";
import {getActionDescription} from "./description-providers/action-description";
2023-01-23 16:02:13 -05:00
2023-01-30 17:05:32 -08:00
export function descriptionProvider(client: Octokit | undefined, cache: TTLCache): DescriptionProvider {
2023-01-23 16:02:13 -05:00
const getDescription: DescriptionProvider["getDescription"] = async (context, token, path) => {
2023-03-06 14:44:18 -05:00
if (!client || !context.step) {
2023-01-23 16:02:13 -05:00
return undefined;
}
2023-01-30 17:05:32 -08:00
2023-03-06 14:44:18 -05:00
if (isStepInput(path)) {
2023-01-30 17:05:32 -08:00
return await getActionInputDescription(client, cache, context.step, token);
2023-01-23 16:02:13 -05:00
}
2023-03-06 14:44:18 -05:00
if (isStepUses(path)) {
return await getActionDescription(client, cache, context.step);
}
2023-01-23 16:02:13 -05:00
};
return {
getDescription
};
}
2023-03-06 14:44:18 -05:00
function isStepInput(path: TemplateToken[]): boolean {
const parent = path[path.length - 1];
return parent.definition?.key === "step-with";
}
function isStepUses(path: TemplateToken[]): boolean {
if (path.length < 2) {
return false;
}
const parent = path[path.length - 1];
const grandparent = path[path.length - 2];
return isString(parent) && parent.value === "uses" && grandparent.definition?.key === "regular-step";
}