Run prettier on language server
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
import { validate } from "@github/actions-languageservice";
|
import {validate} from "@github/actions-languageservice";
|
||||||
import { TextDocument } from "vscode-languageserver-textdocument";
|
import {TextDocument} from "vscode-languageserver-textdocument";
|
||||||
|
|
||||||
describe("simple test", () => {
|
describe("simple test", () => {
|
||||||
it("should work", async () => {
|
it("should work", async () => {
|
||||||
|
|||||||
@@ -8,18 +8,15 @@ import {
|
|||||||
ProposedFeatures,
|
ProposedFeatures,
|
||||||
TextDocumentPositionParams,
|
TextDocumentPositionParams,
|
||||||
TextDocuments,
|
TextDocuments,
|
||||||
TextDocumentSyncKind,
|
TextDocumentSyncKind
|
||||||
} from "vscode-languageserver/node";
|
} from "vscode-languageserver/node";
|
||||||
|
|
||||||
import { hover, validate } from "@github/actions-languageservice";
|
import {hover, validate} from "@github/actions-languageservice";
|
||||||
import { TextDocument } from "vscode-languageserver-textdocument";
|
import {TextDocument} from "vscode-languageserver-textdocument";
|
||||||
import {
|
import {InitializationOptions, RepositoryContext} from "./initializationOptions";
|
||||||
InitializationOptions,
|
import {onCompletion} from "./on-completion";
|
||||||
RepositoryContext,
|
import {TTLCache} from "./utils/cache";
|
||||||
} from "./initializationOptions";
|
import {valueProviders} from "./value-providers";
|
||||||
import { onCompletion } from "./on-completion";
|
|
||||||
import { TTLCache } from "./utils/cache";
|
|
||||||
import { valueProviders } from "./value-providers";
|
|
||||||
|
|
||||||
// Create a connection for the server, using Node's IPC as a transport.
|
// Create a connection for the server, using Node's IPC as a transport.
|
||||||
// Also include all preview / proposed LSP features.
|
// Also include all preview / proposed LSP features.
|
||||||
@@ -39,9 +36,7 @@ let hasDiagnosticRelatedInformationCapability = false;
|
|||||||
connection.onInitialize((params: InitializeParams) => {
|
connection.onInitialize((params: InitializeParams) => {
|
||||||
const capabilities = params.capabilities;
|
const capabilities = params.capabilities;
|
||||||
|
|
||||||
hasWorkspaceFolderCapability = !!(
|
hasWorkspaceFolderCapability = !!(capabilities.workspace && !!capabilities.workspace.workspaceFolders);
|
||||||
capabilities.workspace && !!capabilities.workspace.workspaceFolders
|
|
||||||
);
|
|
||||||
hasDiagnosticRelatedInformationCapability = !!(
|
hasDiagnosticRelatedInformationCapability = !!(
|
||||||
capabilities.textDocument &&
|
capabilities.textDocument &&
|
||||||
capabilities.textDocument.publishDiagnostics &&
|
capabilities.textDocument.publishDiagnostics &&
|
||||||
@@ -59,17 +54,17 @@ connection.onInitialize((params: InitializeParams) => {
|
|||||||
textDocumentSync: TextDocumentSyncKind.Full,
|
textDocumentSync: TextDocumentSyncKind.Full,
|
||||||
completionProvider: {
|
completionProvider: {
|
||||||
resolveProvider: false,
|
resolveProvider: false,
|
||||||
triggerCharacters: [":", "."],
|
triggerCharacters: [":", "."]
|
||||||
},
|
},
|
||||||
hoverProvider: true,
|
hoverProvider: true
|
||||||
},
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
if (hasWorkspaceFolderCapability) {
|
if (hasWorkspaceFolderCapability) {
|
||||||
result.capabilities.workspace = {
|
result.capabilities.workspace = {
|
||||||
workspaceFolders: {
|
workspaceFolders: {
|
||||||
supported: true,
|
supported: true
|
||||||
},
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -78,7 +73,7 @@ connection.onInitialize((params: InitializeParams) => {
|
|||||||
|
|
||||||
connection.onInitialized(() => {
|
connection.onInitialized(() => {
|
||||||
if (hasWorkspaceFolderCapability) {
|
if (hasWorkspaceFolderCapability) {
|
||||||
connection.workspace.onDidChangeWorkspaceFolders((_event) => {
|
connection.workspace.onDidChangeWorkspaceFolders(_event => {
|
||||||
connection.console.log("Workspace folder change event received.");
|
connection.console.log("Workspace folder change event received.");
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -86,7 +81,7 @@ connection.onInitialized(() => {
|
|||||||
|
|
||||||
// The content of a text document has changed. This event is emitted
|
// The content of a text document has changed. This event is emitted
|
||||||
// when the text document first opened or when its content has changed.
|
// when the text document first opened or when its content has changed.
|
||||||
documents.onDidChangeContent((change) => {
|
documents.onDidChangeContent(change => {
|
||||||
validateTextDocument(change.document);
|
validateTextDocument(change.document);
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -95,41 +90,34 @@ async function validateTextDocument(textDocument: TextDocument): Promise<void> {
|
|||||||
textDocument,
|
textDocument,
|
||||||
valueProviders(
|
valueProviders(
|
||||||
sessionToken,
|
sessionToken,
|
||||||
repos.find((repo) => textDocument.uri.startsWith(repo.workspaceUri)),
|
repos.find(repo => textDocument.uri.startsWith(repo.workspaceUri)),
|
||||||
cache
|
cache
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
connection.sendDiagnostics({ uri: textDocument.uri, diagnostics: result });
|
connection.sendDiagnostics({uri: textDocument.uri, diagnostics: result});
|
||||||
}
|
}
|
||||||
|
|
||||||
connection.onDidChangeWatchedFiles((_change) => {
|
connection.onDidChangeWatchedFiles(_change => {
|
||||||
// Monitored files have change in VSCode
|
// Monitored files have change in VSCode
|
||||||
connection.console.log("We received an file change event");
|
connection.console.log("We received an file change event");
|
||||||
});
|
});
|
||||||
|
|
||||||
// This handler provides the initial list of the completion items.
|
// This handler provides the initial list of the completion items.
|
||||||
connection.onCompletion(
|
connection.onCompletion(async ({position, textDocument}: TextDocumentPositionParams): Promise<CompletionItem[]> => {
|
||||||
async ({
|
return await onCompletion(
|
||||||
position,
|
position,
|
||||||
textDocument,
|
documents.get(textDocument.uri)!,
|
||||||
}: TextDocumentPositionParams): Promise<CompletionItem[]> => {
|
sessionToken,
|
||||||
return await onCompletion(
|
repos.find(repo => textDocument.uri.startsWith(repo.workspaceUri)),
|
||||||
position,
|
cache
|
||||||
documents.get(textDocument.uri)!,
|
);
|
||||||
sessionToken,
|
});
|
||||||
repos.find((repo) => textDocument.uri.startsWith(repo.workspaceUri)),
|
|
||||||
cache
|
|
||||||
);
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
connection.onHover(
|
connection.onHover(async ({position, textDocument}: HoverParams): Promise<Hover | null> => {
|
||||||
async ({ position, textDocument }: HoverParams): Promise<Hover | null> => {
|
const r = await hover(documents.get(textDocument.uri)!, position);
|
||||||
const r = await hover(documents.get(textDocument.uri)!, position);
|
return r;
|
||||||
return r;
|
});
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
// Make the text document manager listen on the connection
|
// Make the text document manager listen on the connection
|
||||||
// for open, change and close text document events
|
// for open, change and close text document events
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
import { complete } from "@github/actions-languageservice/complete";
|
import {complete} from "@github/actions-languageservice/complete";
|
||||||
import { CompletionItem, Position } from "vscode-languageserver";
|
import {CompletionItem, Position} from "vscode-languageserver";
|
||||||
import { TextDocument } from "vscode-languageserver-textdocument";
|
import {TextDocument} from "vscode-languageserver-textdocument";
|
||||||
import { contextProviders } from "./context-providers";
|
import {contextProviders} from "./context-providers";
|
||||||
import { RepositoryContext } from "./initializationOptions";
|
import {RepositoryContext} from "./initializationOptions";
|
||||||
import { TTLCache } from "./utils/cache";
|
import {TTLCache} from "./utils/cache";
|
||||||
import { valueProviders } from "./value-providers";
|
import {valueProviders} from "./value-providers";
|
||||||
|
|
||||||
export async function onCompletion(
|
export async function onCompletion(
|
||||||
position: Position,
|
position: Position,
|
||||||
|
|||||||
@@ -15,18 +15,10 @@ export class TTLCache {
|
|||||||
* @param ttlInMS How long is the content valid. If optional, default value will be used
|
* @param ttlInMS How long is the content valid. If optional, default value will be used
|
||||||
* @param getter Function to retrieve content if not in cache
|
* @param getter Function to retrieve content if not in cache
|
||||||
*/
|
*/
|
||||||
async get<T>(
|
async get<T>(key: string, ttlInMS: number | undefined, getter: () => Promise<T>): Promise<T> {
|
||||||
key: string,
|
|
||||||
ttlInMS: number | undefined,
|
|
||||||
getter: () => Promise<T>
|
|
||||||
): Promise<T> {
|
|
||||||
const hasEntry = this.cache.has(key);
|
const hasEntry = this.cache.has(key);
|
||||||
const e = hasEntry && this.cache.get(key);
|
const e = hasEntry && this.cache.get(key);
|
||||||
if (
|
if (hasEntry && e && e.cachedAt > Date.now() - (ttlInMS || this.defaultTTLinMS)) {
|
||||||
hasEntry &&
|
|
||||||
e &&
|
|
||||||
e.cachedAt > Date.now() - (ttlInMS || this.defaultTTLinMS)
|
|
||||||
) {
|
|
||||||
return e.content as T;
|
return e.content as T;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -35,7 +27,7 @@ export class TTLCache {
|
|||||||
|
|
||||||
this.cache.set(key, {
|
this.cache.set(key, {
|
||||||
cachedAt: Date.now(),
|
cachedAt: Date.now(),
|
||||||
content,
|
content
|
||||||
});
|
});
|
||||||
|
|
||||||
return content;
|
return content;
|
||||||
|
|||||||
@@ -1,11 +1,11 @@
|
|||||||
import { ValueProviderConfig } from "@github/actions-languageservice";
|
import {ValueProviderConfig} from "@github/actions-languageservice";
|
||||||
import { WorkflowContext } from "@github/actions-languageservice/context/workflow-context";
|
import {WorkflowContext} from "@github/actions-languageservice/context/workflow-context";
|
||||||
import { ValueProviderKind } from "@github/actions-languageservice/value-providers/config";
|
import {ValueProviderKind} from "@github/actions-languageservice/value-providers/config";
|
||||||
import { Octokit } from "@octokit/rest";
|
import {Octokit} from "@octokit/rest";
|
||||||
import { RepositoryContext } from "./initializationOptions";
|
import {RepositoryContext} from "./initializationOptions";
|
||||||
import { TTLCache } from "./utils/cache";
|
import {TTLCache} from "./utils/cache";
|
||||||
import { getEnvironments } from "./value-providers/job-environment";
|
import {getEnvironments} from "./value-providers/job-environment";
|
||||||
import { getRunnerLabels } from "./value-providers/runs-on";
|
import {getRunnerLabels} from "./value-providers/runs-on";
|
||||||
|
|
||||||
export function valueProviders(
|
export function valueProviders(
|
||||||
sessionToken: string | undefined,
|
sessionToken: string | undefined,
|
||||||
@@ -17,19 +17,17 @@ export function valueProviders(
|
|||||||
}
|
}
|
||||||
|
|
||||||
const octokit = new Octokit({
|
const octokit = new Octokit({
|
||||||
auth: sessionToken,
|
auth: sessionToken
|
||||||
});
|
});
|
||||||
|
|
||||||
return {
|
return {
|
||||||
"job-environment": {
|
"job-environment": {
|
||||||
kind: ValueProviderKind.AllowedValues,
|
kind: ValueProviderKind.AllowedValues,
|
||||||
get: (_: WorkflowContext) =>
|
get: (_: WorkflowContext) => getEnvironments(octokit, cache, repo.owner, repo.name)
|
||||||
getEnvironments(octokit, cache, repo.owner, repo.name),
|
|
||||||
},
|
},
|
||||||
"runs-on": {
|
"runs-on": {
|
||||||
kind: ValueProviderKind.SuggestedValues,
|
kind: ValueProviderKind.SuggestedValues,
|
||||||
get: (_: WorkflowContext) =>
|
get: (_: WorkflowContext) => getRunnerLabels(octokit, cache, repo.owner, repo.name)
|
||||||
getRunnerLabels(octokit, cache, repo.owner, repo.name),
|
}
|
||||||
},
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,31 +1,24 @@
|
|||||||
import { Value } from "@github/actions-languageservice/value-providers/config";
|
import {Value} from "@github/actions-languageservice/value-providers/config";
|
||||||
import { Octokit } from "@octokit/rest";
|
import {Octokit} from "@octokit/rest";
|
||||||
import { TTLCache } from "../utils/cache";
|
import {TTLCache} from "../utils/cache";
|
||||||
|
|
||||||
export async function getEnvironments(
|
export async function getEnvironments(client: Octokit, cache: TTLCache, owner: string, name: string): Promise<Value[]> {
|
||||||
client: Octokit,
|
const environments = await cache.get(`${owner}/${name}/environments`, undefined, () =>
|
||||||
cache: TTLCache,
|
fetchEnvironments(client, owner, name)
|
||||||
owner: string,
|
);
|
||||||
name: string
|
return Array.from(environments).map(env => ({label: env}));
|
||||||
): Promise<Value[]> {
|
|
||||||
const environments = await cache.get(`${owner}/${name}/environments`, undefined, () => fetchEnvironments(client, owner, name));
|
|
||||||
return Array.from(environments).map((env) => ({ label: env }));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function fetchEnvironments(
|
async function fetchEnvironments(client: Octokit, owner: string, name: string): Promise<string[]> {
|
||||||
client: Octokit,
|
|
||||||
owner: string,
|
|
||||||
name: string
|
|
||||||
): Promise<string[]> {
|
|
||||||
let environments: string[] = [];
|
let environments: string[] = [];
|
||||||
try {
|
try {
|
||||||
const response = await client.repos.getAllEnvironments({
|
const response = await client.repos.getAllEnvironments({
|
||||||
owner,
|
owner,
|
||||||
repo: name,
|
repo: name
|
||||||
});
|
});
|
||||||
|
|
||||||
if (response.data.environments) {
|
if (response.data.environments) {
|
||||||
environments = response.data.environments.map((env) => env.name);
|
environments = response.data.environments.map(env => env.name);
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.log("Failure to retrieve environments: ", e);
|
console.log("Failure to retrieve environments: ", e);
|
||||||
|
|||||||
@@ -1,13 +1,8 @@
|
|||||||
import { Value } from "@github/actions-languageservice/value-providers/config";
|
import {Value} from "@github/actions-languageservice/value-providers/config";
|
||||||
import { Octokit } from "@octokit/rest";
|
import {Octokit} from "@octokit/rest";
|
||||||
import { TTLCache } from "../utils/cache";
|
import {TTLCache} from "../utils/cache";
|
||||||
|
|
||||||
export async function getRunnerLabels(
|
export async function getRunnerLabels(client: Octokit, cache: TTLCache, owner: string, name: string): Promise<Value[]> {
|
||||||
client: Octokit,
|
|
||||||
cache: TTLCache,
|
|
||||||
owner: string,
|
|
||||||
name: string
|
|
||||||
): Promise<Value[]> {
|
|
||||||
const defaultLabels = [
|
const defaultLabels = [
|
||||||
"ubuntu-latest",
|
"ubuntu-latest",
|
||||||
"ubuntu-22.04",
|
"ubuntu-22.04",
|
||||||
@@ -21,32 +16,26 @@ export async function getRunnerLabels(
|
|||||||
"macos-12",
|
"macos-12",
|
||||||
"macos-11",
|
"macos-11",
|
||||||
"macos-10.15",
|
"macos-10.15",
|
||||||
"self-hosted",
|
"self-hosted"
|
||||||
];
|
];
|
||||||
|
|
||||||
const repoLabels = await cache.get(
|
const repoLabels = await cache.get(`${owner}/${name}/runner-labels`, undefined, () =>
|
||||||
`${owner}/${name}/runner-labels`,
|
fetchRunnerLabels(client, owner, name)
|
||||||
undefined,
|
|
||||||
() => fetchRunnerLabels(client, owner, name)
|
|
||||||
);
|
);
|
||||||
|
|
||||||
for (const label of defaultLabels) {
|
for (const label of defaultLabels) {
|
||||||
repoLabels.add(label);
|
repoLabels.add(label);
|
||||||
}
|
}
|
||||||
|
|
||||||
return Array.from(repoLabels).map((label) => ({ label }));
|
return Array.from(repoLabels).map(label => ({label}));
|
||||||
}
|
}
|
||||||
|
|
||||||
async function fetchRunnerLabels(
|
async function fetchRunnerLabels(client: Octokit, owner: string, name: string): Promise<Set<string>> {
|
||||||
client: Octokit,
|
|
||||||
owner: string,
|
|
||||||
name: string
|
|
||||||
): Promise<Set<string>> {
|
|
||||||
const labels = new Set<string>();
|
const labels = new Set<string>();
|
||||||
try {
|
try {
|
||||||
const response = await client.actions.listSelfHostedRunnersForRepo({
|
const response = await client.actions.listSelfHostedRunnersForRepo({
|
||||||
owner,
|
owner,
|
||||||
repo: name,
|
repo: name
|
||||||
});
|
});
|
||||||
|
|
||||||
for (const runner of response.data.runners) {
|
for (const runner of response.data.runners) {
|
||||||
|
|||||||
Reference in New Issue
Block a user