initial changes for value provider

This commit is contained in:
Laura Yu
2022-11-14 22:34:57 +00:00
parent cadaeee4a1
commit 3f7cfbfdb1
6 changed files with 79 additions and 13 deletions
+21 -6
View File
@@ -1,4 +1,4 @@
import { parseWorkflow } from "@github/actions-workflow-parser";
import { convertWorkflowTemplate, parseWorkflow, WorkflowTemplate } from "@github/actions-workflow-parser";
import {
SEQUENCE_TYPE,
STRING_TYPE,
@@ -31,16 +31,24 @@ export async function complete(
content: newDoc.getText(),
};
const result = parseWorkflow(file.name, [file], nullTrace);
const [innerToken, parent] = findInnerTokenAndParent(newPos, result.value);
let template: WorkflowTemplate | undefined = undefined;
const valueToRemove = parent?.definition?.keyname;
if (result.value) {
template = convertWorkflowTemplate(result.context, result.value);
console.log("template", template);
}
const values = await getValues(
innerToken,
parent,
newPos,
textDocument.uri,
valueProviderConfig
valueProviderConfig,
template,
);
return values.map((value) => CompletionItem.create(value.label));
const valuesFiltered = values.filter((x) => x.label !== valueToRemove);
return valuesFiltered.map((value) => CompletionItem.create(value.label));
}
async function getValues(
@@ -48,7 +56,8 @@ async function getValues(
parent: TemplateToken | null,
position: Position,
workflowUri: string,
valueProviderConfig: ValueProviderConfig | undefined
valueProviderConfig: ValueProviderConfig | undefined,
template: WorkflowTemplate | undefined,
): Promise<Value[]> {
if (!parent) {
return [];
@@ -62,16 +71,21 @@ async function getValues(
}
const existingValues = getExistingValues(token, parent);
console.log("Parent", parent);
console.log("Token", token);
let customValues: Value[] | undefined = undefined;
if (token?.definition?.key) {
customValues = await valueProviderConfig?.getCustomValues(
token.definition.key,
{ uri: workflowUri }
{ uri: workflowUri },
template,
);
// add to custom values?
}
if (customValues !== undefined) {
console.log("filterAndSortCompletionOptions with customValues")
return filterAndSortCompletionOptions(customValues, existingValues);
}
@@ -87,6 +101,7 @@ async function getValues(
}
const values = valueProvider();
console.log("filterAndSortCompletionOptions with values")
return filterAndSortCompletionOptions(values, existingValues);
}
@@ -47,7 +47,10 @@ export function findInnerTokenAndParent(
if (nullNodeOnLine(pos, key, value)) {
return [value, key];
}
//value.definition?.keyname = key.value;
if (value.definition){
value.definition.keyname = key.toString();
}
s.push(value);
}
continue;
@@ -74,8 +77,8 @@ function posInToken(pos: Position, token: TemplateToken): boolean {
const r = token.range;
// TokenRange is one-based, Position is zero-based
const tokenLine = pos.line + 1;
const tokenChar = pos.character + 1;
const tokenLine = pos.line //+ 1;
const tokenChar = pos.character //+ 1;
// Check lines
if (r.start[0] > tokenLine || tokenLine > r.end[0]) {
@@ -1,3 +1,5 @@
import { WorkflowTemplate } from "@github/actions-workflow-parser/model/workflow-template";
export interface Value {
label: string;
description?: string;
@@ -11,7 +13,8 @@ export interface WorkflowContext {
export interface ValueProviderConfig {
getCustomValues: (
key: string,
context: WorkflowContext
context: WorkflowContext,
template: WorkflowTemplate | undefined
) => Promise<Value[] | undefined>;
getActionInputs?: (
owner: string,
@@ -0,0 +1,19 @@
import { Value } from "./config";
import { WorkflowTemplate } from "@github/actions-workflow-parser/model/workflow-template";
export async function getJobNames(
template: WorkflowTemplate
): Promise<Value[]> {
const labels = new Set<string>([
"dummy-job",
]);
const jobsValues = template.jobs.jobNames;
if (jobsValues) {
for (const job of jobsValues?.values() || []) {
labels.add(job);
}
}
return Array.from(labels).map((label) => ({ label }));
}