Add first default value provider
This commit is contained in:
@@ -1,31 +1,19 @@
|
|||||||
import {data} from "@github/actions-expressions/.";
|
import {data} from "@github/actions-expressions";
|
||||||
import {complete, getExpressionInput} from "./complete";
|
import {complete, getExpressionInput} from "./complete";
|
||||||
import {ContextProviderConfig} from "./context-providers/config";
|
import {ContextProviderConfig} from "./context-providers/config";
|
||||||
import {getPositionFromCursor} from "./test-utils/cursor-position";
|
import {getPositionFromCursor} from "./test-utils/cursor-position";
|
||||||
|
|
||||||
const contextProviderConfig: ContextProviderConfig = {
|
const contextProviderConfig: ContextProviderConfig = {
|
||||||
async getContext(contexts: string[]): Promise<data.Dictionary | undefined> {
|
getContext: async (context: string) => {
|
||||||
const context = new data.Dictionary();
|
switch (context) {
|
||||||
|
case "github":
|
||||||
for (const contextName of contexts) {
|
return new data.Dictionary({
|
||||||
switch (contextName) {
|
key: "event",
|
||||||
case "github":
|
value: new data.StringData("push")
|
||||||
context.add(
|
});
|
||||||
"github",
|
|
||||||
new data.Dictionary({
|
|
||||||
key: "event",
|
|
||||||
value: new data.StringData("push")
|
|
||||||
})
|
|
||||||
);
|
|
||||||
break;
|
|
||||||
|
|
||||||
default:
|
|
||||||
context.add(contextName, new data.Dictionary());
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return context;
|
return undefined;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -102,5 +90,13 @@ describe("expressions", () => {
|
|||||||
|
|
||||||
expect(result.map(x => x.label)).toEqual(["event"]);
|
expect(result.map(x => x.label)).toEqual(["event"]);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("using default context provider", async () => {
|
||||||
|
const input =
|
||||||
|
"on: push\njobs:\n build:\n runs-on: ubuntu-latest\n environment:\n url: ${{ runner.| }}\n steps:\n - run: echo";
|
||||||
|
const result = await complete(...getPositionFromCursor(input), undefined, contextProviderConfig);
|
||||||
|
|
||||||
|
expect(result.map(x => x.label)).toEqual(["arch", "name", "os", "temp", "tool_cache"]);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import {complete as completeExpression, data} from "@github/actions-expressions";
|
import {complete as completeExpression} from "@github/actions-expressions";
|
||||||
import {isSequence, isString, parseWorkflow} from "@github/actions-workflow-parser";
|
import {isSequence, isString, parseWorkflow} from "@github/actions-workflow-parser";
|
||||||
import {CLOSE_EXPRESSION, OPEN_EXPRESSION} from "@github/actions-workflow-parser/templates/template-constants";
|
import {CLOSE_EXPRESSION, OPEN_EXPRESSION} from "@github/actions-workflow-parser/templates/template-constants";
|
||||||
import {TemplateToken} from "@github/actions-workflow-parser/templates/tokens/index";
|
import {TemplateToken} from "@github/actions-workflow-parser/templates/tokens/index";
|
||||||
@@ -9,6 +9,7 @@ import {File} from "@github/actions-workflow-parser/workflows/file";
|
|||||||
import {Position, TextDocument} from "vscode-languageserver-textdocument";
|
import {Position, TextDocument} from "vscode-languageserver-textdocument";
|
||||||
import {CompletionItem} from "vscode-languageserver-types";
|
import {CompletionItem} from "vscode-languageserver-types";
|
||||||
import {ContextProviderConfig} from "./context-providers/config";
|
import {ContextProviderConfig} from "./context-providers/config";
|
||||||
|
import {getContext} from "./context-providers/default";
|
||||||
import {nullTrace} from "./nulltrace";
|
import {nullTrace} from "./nulltrace";
|
||||||
import {findInnerTokenAndParent} from "./utils/find-token";
|
import {findInnerTokenAndParent} from "./utils/find-token";
|
||||||
import {transform} from "./utils/transform";
|
import {transform} from "./utils/transform";
|
||||||
@@ -61,8 +62,7 @@ export async function complete(
|
|||||||
|
|
||||||
const expressionInput = (getExpressionInput(currentInput, relCharPos) || "").trim();
|
const expressionInput = (getExpressionInput(currentInput, relCharPos) || "").trim();
|
||||||
|
|
||||||
const context =
|
const context = await getContext(innerToken.definition?.readerContext || [], contextProviderConfig);
|
||||||
(await contextProviderConfig?.getContext(innerToken.definition!.readerContext)) || new data.Dictionary();
|
|
||||||
|
|
||||||
return completeExpression(expressionInput, context, []);
|
return completeExpression(expressionInput, context, []);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,9 +2,9 @@ import {data} from "@github/actions-expressions";
|
|||||||
import {Dictionary} from "@github/actions-expressions/data/dictionary";
|
import {Dictionary} from "@github/actions-expressions/data/dictionary";
|
||||||
import {ExpressionData, Pair} from "@github/actions-expressions/data/expressiondata";
|
import {ExpressionData, Pair} from "@github/actions-expressions/data/expressiondata";
|
||||||
|
|
||||||
export interface ContextProviderConfig {
|
export type ContextProviderConfig = {
|
||||||
getContext(contexts: string[]): Promise<data.Dictionary | undefined>;
|
getContext: (name: string) => Promise<data.Dictionary | undefined>;
|
||||||
}
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* DynamicDictionary is a dictionary that returns an empty DynamicDictionary for
|
* DynamicDictionary is a dictionary that returns an empty DynamicDictionary for
|
||||||
|
|||||||
@@ -0,0 +1,48 @@
|
|||||||
|
import {data} from "@github/actions-expressions";
|
||||||
|
import {ContextProviderConfig} from "./config";
|
||||||
|
|
||||||
|
export async function getContext(names: string[], config: ContextProviderConfig | undefined): Promise<data.Dictionary> {
|
||||||
|
const context = new data.Dictionary();
|
||||||
|
|
||||||
|
for (const contextName of names) {
|
||||||
|
let value: data.Dictionary | undefined;
|
||||||
|
|
||||||
|
value = await getDefaultContext(contextName);
|
||||||
|
|
||||||
|
if (!value) {
|
||||||
|
value = await config?.getContext(contextName);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!value) {
|
||||||
|
value = new data.Dictionary();
|
||||||
|
}
|
||||||
|
|
||||||
|
context.add(contextName, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
return context;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function getDefaultContext(name: string): Promise<data.Dictionary | undefined> {
|
||||||
|
switch (name) {
|
||||||
|
case "runner":
|
||||||
|
return objectToDictionary({
|
||||||
|
os: "Linux",
|
||||||
|
arch: "X64",
|
||||||
|
name: "GitHub Actions 2",
|
||||||
|
tool_cache: "/opt/hostedtoolcache",
|
||||||
|
temp: "/home/runner/work/_temp"
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
function objectToDictionary(object: {[key: string]: string}): data.Dictionary {
|
||||||
|
const dictionary = new data.Dictionary();
|
||||||
|
for (const key in object) {
|
||||||
|
dictionary.add(key, new data.StringData(object[key]));
|
||||||
|
}
|
||||||
|
|
||||||
|
return dictionary;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user