Merge branch 'main' into cschleiden/find-token-tests

This commit is contained in:
Josh Gross
2022-11-23 14:20:44 -05:00
10 changed files with 102 additions and 38 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@github/actions-languageservice",
"version": "0.1.11",
"version": "0.1.12",
"description": "Language service for GitHub Actions",
"license": "MIT",
"type": "module",
@@ -12,6 +12,21 @@ describe("completion", () => {
expect(result[0].label).toEqual("macos-10.13");
});
it("needs", async () => {
const input = `on: push
jobs:
build:
runs-on: ubuntu-latest
build2:
runs-on: ubuntu-latest
needs: bu|`;
const result = await complete(...getPositionFromCursor(input));
expect(result).not.toBeUndefined();
expect(result.length).toEqual(1);
expect(result[0].label).toEqual("build");
});
it("empty workflow", async () => {
const input = "|";
const result = await complete(...getPositionFromCursor(input));
+35 -19
View File
@@ -1,5 +1,11 @@
import {complete as completeExpression} from "@github/actions-expressions";
import {isSequence, isString, parseWorkflow} from "@github/actions-workflow-parser";
import {
convertWorkflowTemplate,
isMapping,
isSequence,
isString,
parseWorkflow,
} from "@github/actions-workflow-parser";
import {CLOSE_EXPRESSION, OPEN_EXPRESSION} from "@github/actions-workflow-parser/templates/template-constants";
import {TemplateToken} from "@github/actions-workflow-parser/templates/tokens/index";
import {MappingToken} from "@github/actions-workflow-parser/templates/tokens/mapping-token";
@@ -13,7 +19,7 @@ import {getContext} from "./context-providers/default";
import {nullTrace} from "./nulltrace";
import {findToken} from "./utils/find-token";
import {transform} from "./utils/transform";
import {Value, ValueProviderConfig} from "./value-providers/config";
import {Value, ValueProviderConfig, WorkflowContext} from "./value-providers/config";
import {defaultValueProviders} from "./value-providers/default";
import {definitionValues} from "./value-providers/definition";
@@ -57,8 +63,12 @@ export async function complete(
content: newDoc.getText()
};
const result = parseWorkflow(file.name, [file], nullTrace);
if (!result.value) {
return [];
}
const {token, keyToken, parent} = findToken(newPos, result.value);
const {token, keyToken, parent, parentKey} = findToken(newPos, result.value);
const template = convertWorkflowTemplate(result.context, result.value);
// If we are inside an expression, take a different code-path. The workflow parser does not correctly create
// expression nodes for invalid expressions and during editing expressions are invalid most of the time.
@@ -81,32 +91,34 @@ export async function complete(
}
}
const values = await getValues(token, parent, textDocument.uri, valueProviderConfig);
const workflowContext = {uri: textDocument.uri, template: template};
const values = await getValues(token, parent, parentKey, valueProviderConfig, workflowContext);
return values.map(value => CompletionItem.create(value.label));
}
async function getValues(
token: TemplateToken | null,
parent: TemplateToken | null,
workflowUri: string,
valueProviderConfig: ValueProviderConfig | undefined
parentKey: TemplateToken | null,
valueProviderConfig: ValueProviderConfig | undefined,
workflowContext: WorkflowContext
): Promise<Value[]> {
if (!parent) {
return [];
}
const existingValues = getExistingValues(token, parent);
const existingValues = getExistingValues(token, parent, parentKey);
let customValues: Value[] | undefined = undefined;
if (token?.definition?.key) {
customValues = await valueProviderConfig?.getCustomValues(token.definition.key, {uri: workflowUri});
customValues = await valueProviderConfig?.getCustomValues(token.definition.key, workflowContext);
}
if (customValues !== undefined) {
return filterAndSortCompletionOptions(customValues, existingValues);
}
const valueProviders = defaultValueProviders();
const valueProviders = defaultValueProviders(workflowContext);
// Use the value provider from the parent if we don't have a value provider for the current key
const valueProvider =
@@ -128,23 +140,27 @@ async function getValues(
return filterAndSortCompletionOptions(values, existingValues);
}
function getExistingValues(token: TemplateToken | null, parent: TemplateToken) {
function getExistingValues(token: TemplateToken | null, parent: TemplateToken, parentKey: TemplateToken | null) {
// For incomplete YAML, we may only have a parent token
if (token) {
if (!isString(token) || !isSequence(parent)) {
if (!isString(token)) {
return;
}
const sequenceValues = new Set<string>();
const seqToken = parent as SequenceToken;
for (let i = 0; i < seqToken.count; i++) {
const t = seqToken.get(i);
if (t.isLiteral && isString(t)) {
// Should we support other literal values here?
sequenceValues.add(t.value);
if (isMapping(parent) && parentKey && isString(parentKey)) {
return new Set<string>([parentKey.value]);
}
if (isSequence(parent)) {
const sequenceValues = new Set<string>();
for (let i = 0; i < parent.count; i++) {
const t = parent.get(i);
if (isString(t)) {
// Should we support other literal values here?
sequenceValues.add(t.value);
}
}
return sequenceValues;
}
return sequenceValues;
}
if (parent.templateTokenType === TokenType.Mapping) {
@@ -14,6 +14,7 @@ export type TokenResult = {
token: TemplateToken | null;
keyToken: TemplateToken | null;
parent: TemplateToken | null;
parentKey: TemplateToken | null;
};
/**
@@ -33,7 +34,8 @@ export function findToken(pos: Position, root?: TemplateToken): TokenResult {
return {
token: null,
keyToken: null,
parent: null
parent: null,
parentKey: null
};
}
@@ -43,12 +45,13 @@ export function findToken(pos: Position, root?: TemplateToken): TokenResult {
{
token: root,
keyToken: null,
parent: null
parent: null,
parentKey: null
}
];
while (s.length > 0) {
const {parent, token, keyToken} = s.shift()!;
const {parent, token, keyToken, parentKey} = s.shift()!;
if (!token) {
break;
}
@@ -72,7 +75,8 @@ export function findToken(pos: Position, root?: TemplateToken): TokenResult {
return {
parent: mappingToken,
keyToken: null,
token: key
token: key,
parentKey: keyToken
};
}
@@ -82,14 +86,16 @@ export function findToken(pos: Position, root?: TemplateToken): TokenResult {
return {
parent: mappingToken,
keyToken: key,
token: value
token: value,
parentKey: keyToken
};
}
s.push({
parent: mappingToken,
keyToken: key,
token: value
token: value,
parentKey: keyToken
});
}
continue;
@@ -100,7 +106,8 @@ export function findToken(pos: Position, root?: TemplateToken): TokenResult {
s.push({
parent: sequenceToken,
keyToken: null,
token: sequenceToken.get(i)
token: sequenceToken.get(i),
parentKey: null
});
}
continue;
@@ -109,7 +116,8 @@ export function findToken(pos: Position, root?: TemplateToken): TokenResult {
return {
token,
keyToken,
parent
parent,
parentKey
};
}
@@ -117,7 +125,8 @@ export function findToken(pos: Position, root?: TemplateToken): TokenResult {
return {
token: null,
parent: lastMatchingToken,
keyToken: null
keyToken: null,
parentKey: null
};
}
@@ -1,3 +1,5 @@
import {WorkflowTemplate} from "@github/actions-workflow-parser/.";
export interface Value {
label: string;
description?: string;
@@ -7,6 +9,7 @@ export type ValueProvider = () => Value[];
export interface WorkflowContext {
uri: string;
template: WorkflowTemplate | undefined;
}
export interface ValueProviderConfig {
getCustomValues: (key: string, context: WorkflowContext) => Promise<Value[] | undefined>;
@@ -1,7 +1,9 @@
import {Value, ValueProvider} from "./config";
import {Value, ValueProvider, WorkflowContext} from "./config";
import {getJobNames} from "./needs";
export function defaultValueProviders(): {[key: string]: ValueProvider} {
export function defaultValueProviders(workflowContext: WorkflowContext): {[key: string]: ValueProvider} {
return {
needs: () => getJobNames(workflowContext.template),
"runs-on": () =>
stringsToValues([
"ubuntu-latest",
@@ -0,0 +1,19 @@
import {Value} from "./config";
import {WorkflowTemplate} from "@github/actions-workflow-parser/model/workflow-template";
export function getJobNames(template: WorkflowTemplate | undefined): Value[] {
if (!template) {
return [];
}
const jobNames = new Set<string>();
const jobList = template.jobs;
for (const job of jobList) {
const name = job.id;
if (name && !jobNames.has(name)) {
jobNames.add(name);
}
}
return Array.from(jobNames).map(label => ({label}));
}