From 3b9c6711eb176a9def4eb175af1d0f1b5baa3a52 Mon Sep 17 00:00:00 2001 From: Josh Gross Date: Tue, 29 Nov 2022 17:41:13 -0500 Subject: [PATCH 1/2] Add prettier config to language server --- actions-languageserver/.prettierignore | 5 +++++ actions-languageserver/.prettierrc.json | 9 +++++++++ actions-languageserver/package.json | 6 ++++-- 3 files changed, 18 insertions(+), 2 deletions(-) create mode 100644 actions-languageserver/.prettierignore create mode 100644 actions-languageserver/.prettierrc.json diff --git a/actions-languageserver/.prettierignore b/actions-languageserver/.prettierignore new file mode 100644 index 0000000..ce98533 --- /dev/null +++ b/actions-languageserver/.prettierignore @@ -0,0 +1,5 @@ +node_modules +dist +*.md +*.js +*.json diff --git a/actions-languageserver/.prettierrc.json b/actions-languageserver/.prettierrc.json new file mode 100644 index 0000000..e932aad --- /dev/null +++ b/actions-languageserver/.prettierrc.json @@ -0,0 +1,9 @@ +{ + "tabWidth": 2, + "useTabs": false, + "printWidth": 120, + "singleQuote": false, + "bracketSpacing": false, + "trailingComma": "none", + "arrowParens": "avoid" +} diff --git a/actions-languageserver/package.json b/actions-languageserver/package.json index d9e63ec..bbed9df 100644 --- a/actions-languageserver/package.json +++ b/actions-languageserver/package.json @@ -33,7 +33,9 @@ "prepublishOnly": "npm run build && npm run test", "test": "NODE_OPTIONS=\"--experimental-vm-modules\" jest", "test-watch": "NODE_OPTIONS=\"--experimental-vm-modules\" jest --watch", - "watch": "tsc --build tsconfig.build.json --watch" + "watch": "tsc --build tsconfig.build.json --watch", + "prettier": "prettier .", + "prettier-fix": "prettier --write ." }, "dependencies": { "@github/actions-languageservice": "^0.1.18", @@ -55,4 +57,4 @@ "ts-jest": "^29.0.3", "typescript": "^4.8.4" } -} +} \ No newline at end of file From 9b40ba1887dd80786df750b172d120534cf1545e Mon Sep 17 00:00:00 2001 From: Josh Gross Date: Tue, 29 Nov 2022 17:41:48 -0500 Subject: [PATCH 2/2] Run prettier on language server --- actions-languageserver/src/index.test.ts | 4 +- actions-languageserver/src/index.ts | 72 ++++++++----------- actions-languageserver/src/on-completion.ts | 14 ++-- actions-languageserver/src/utils/cache.ts | 16 ++--- actions-languageserver/src/value-providers.ts | 26 ++++--- .../src/value-providers/job-environment.ts | 29 +++----- .../src/value-providers/runs-on.ts | 31 +++----- 7 files changed, 76 insertions(+), 116 deletions(-) diff --git a/actions-languageserver/src/index.test.ts b/actions-languageserver/src/index.test.ts index 4b26421..8f3c889 100644 --- a/actions-languageserver/src/index.test.ts +++ b/actions-languageserver/src/index.test.ts @@ -1,5 +1,5 @@ -import { validate } from "@github/actions-languageservice"; -import { TextDocument } from "vscode-languageserver-textdocument"; +import {validate} from "@github/actions-languageservice"; +import {TextDocument} from "vscode-languageserver-textdocument"; describe("simple test", () => { it("should work", async () => { diff --git a/actions-languageserver/src/index.ts b/actions-languageserver/src/index.ts index 19839e2..4605ef5 100644 --- a/actions-languageserver/src/index.ts +++ b/actions-languageserver/src/index.ts @@ -8,18 +8,15 @@ import { ProposedFeatures, TextDocumentPositionParams, TextDocuments, - TextDocumentSyncKind, + TextDocumentSyncKind } from "vscode-languageserver/node"; -import { hover, validate } from "@github/actions-languageservice"; -import { TextDocument } from "vscode-languageserver-textdocument"; -import { - InitializationOptions, - RepositoryContext, -} from "./initializationOptions"; -import { onCompletion } from "./on-completion"; -import { TTLCache } from "./utils/cache"; -import { valueProviders } from "./value-providers"; +import {hover, validate} from "@github/actions-languageservice"; +import {TextDocument} from "vscode-languageserver-textdocument"; +import {InitializationOptions, RepositoryContext} from "./initializationOptions"; +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. // Also include all preview / proposed LSP features. @@ -39,9 +36,7 @@ let hasDiagnosticRelatedInformationCapability = false; connection.onInitialize((params: InitializeParams) => { const capabilities = params.capabilities; - hasWorkspaceFolderCapability = !!( - capabilities.workspace && !!capabilities.workspace.workspaceFolders - ); + hasWorkspaceFolderCapability = !!(capabilities.workspace && !!capabilities.workspace.workspaceFolders); hasDiagnosticRelatedInformationCapability = !!( capabilities.textDocument && capabilities.textDocument.publishDiagnostics && @@ -59,17 +54,17 @@ connection.onInitialize((params: InitializeParams) => { textDocumentSync: TextDocumentSyncKind.Full, completionProvider: { resolveProvider: false, - triggerCharacters: [":", "."], + triggerCharacters: [":", "."] }, - hoverProvider: true, - }, + hoverProvider: true + } }; if (hasWorkspaceFolderCapability) { result.capabilities.workspace = { workspaceFolders: { - supported: true, - }, + supported: true + } }; } @@ -78,7 +73,7 @@ connection.onInitialize((params: InitializeParams) => { connection.onInitialized(() => { if (hasWorkspaceFolderCapability) { - connection.workspace.onDidChangeWorkspaceFolders((_event) => { + connection.workspace.onDidChangeWorkspaceFolders(_event => { 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 // when the text document first opened or when its content has changed. -documents.onDidChangeContent((change) => { +documents.onDidChangeContent(change => { validateTextDocument(change.document); }); @@ -95,41 +90,34 @@ async function validateTextDocument(textDocument: TextDocument): Promise { textDocument, valueProviders( sessionToken, - repos.find((repo) => textDocument.uri.startsWith(repo.workspaceUri)), + repos.find(repo => textDocument.uri.startsWith(repo.workspaceUri)), 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 connection.console.log("We received an file change event"); }); // This handler provides the initial list of the completion items. -connection.onCompletion( - async ({ +connection.onCompletion(async ({position, textDocument}: TextDocumentPositionParams): Promise => { + return await onCompletion( position, - textDocument, - }: TextDocumentPositionParams): Promise => { - return await onCompletion( - position, - documents.get(textDocument.uri)!, - sessionToken, - repos.find((repo) => textDocument.uri.startsWith(repo.workspaceUri)), - cache - ); - } -); + documents.get(textDocument.uri)!, + sessionToken, + repos.find(repo => textDocument.uri.startsWith(repo.workspaceUri)), + cache + ); +}); -connection.onHover( - async ({ position, textDocument }: HoverParams): Promise => { - const r = await hover(documents.get(textDocument.uri)!, position); - return r; - } -); +connection.onHover(async ({position, textDocument}: HoverParams): Promise => { + const r = await hover(documents.get(textDocument.uri)!, position); + return r; +}); // Make the text document manager listen on the connection // for open, change and close text document events diff --git a/actions-languageserver/src/on-completion.ts b/actions-languageserver/src/on-completion.ts index cf93539..43bd70f 100644 --- a/actions-languageserver/src/on-completion.ts +++ b/actions-languageserver/src/on-completion.ts @@ -1,10 +1,10 @@ -import { complete } from "@github/actions-languageservice/complete"; -import { CompletionItem, Position } from "vscode-languageserver"; -import { TextDocument } from "vscode-languageserver-textdocument"; -import { contextProviders } from "./context-providers"; -import { RepositoryContext } from "./initializationOptions"; -import { TTLCache } from "./utils/cache"; -import { valueProviders } from "./value-providers"; +import {complete} from "@github/actions-languageservice/complete"; +import {CompletionItem, Position} from "vscode-languageserver"; +import {TextDocument} from "vscode-languageserver-textdocument"; +import {contextProviders} from "./context-providers"; +import {RepositoryContext} from "./initializationOptions"; +import {TTLCache} from "./utils/cache"; +import {valueProviders} from "./value-providers"; export async function onCompletion( position: Position, diff --git a/actions-languageserver/src/utils/cache.ts b/actions-languageserver/src/utils/cache.ts index 7bd791a..1e4de37 100644 --- a/actions-languageserver/src/utils/cache.ts +++ b/actions-languageserver/src/utils/cache.ts @@ -15,18 +15,10 @@ export class TTLCache { * @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 */ - async get( - key: string, - ttlInMS: number | undefined, - getter: () => Promise - ): Promise { + async get(key: string, ttlInMS: number | undefined, getter: () => Promise): Promise { const hasEntry = this.cache.has(key); const e = hasEntry && this.cache.get(key); - if ( - hasEntry && - e && - e.cachedAt > Date.now() - (ttlInMS || this.defaultTTLinMS) - ) { + if (hasEntry && e && e.cachedAt > Date.now() - (ttlInMS || this.defaultTTLinMS)) { return e.content as T; } @@ -35,7 +27,7 @@ export class TTLCache { this.cache.set(key, { cachedAt: Date.now(), - content, + content }); return content; @@ -44,4 +36,4 @@ export class TTLCache { throw e; } } -} \ No newline at end of file +} diff --git a/actions-languageserver/src/value-providers.ts b/actions-languageserver/src/value-providers.ts index 9e3fc8e..0faf01d 100644 --- a/actions-languageserver/src/value-providers.ts +++ b/actions-languageserver/src/value-providers.ts @@ -1,11 +1,11 @@ -import { ValueProviderConfig } from "@github/actions-languageservice"; -import { WorkflowContext } from "@github/actions-languageservice/context/workflow-context"; -import { ValueProviderKind } from "@github/actions-languageservice/value-providers/config"; -import { Octokit } from "@octokit/rest"; -import { RepositoryContext } from "./initializationOptions"; -import { TTLCache } from "./utils/cache"; -import { getEnvironments } from "./value-providers/job-environment"; -import { getRunnerLabels } from "./value-providers/runs-on"; +import {ValueProviderConfig} from "@github/actions-languageservice"; +import {WorkflowContext} from "@github/actions-languageservice/context/workflow-context"; +import {ValueProviderKind} from "@github/actions-languageservice/value-providers/config"; +import {Octokit} from "@octokit/rest"; +import {RepositoryContext} from "./initializationOptions"; +import {TTLCache} from "./utils/cache"; +import {getEnvironments} from "./value-providers/job-environment"; +import {getRunnerLabels} from "./value-providers/runs-on"; export function valueProviders( sessionToken: string | undefined, @@ -17,19 +17,17 @@ export function valueProviders( } const octokit = new Octokit({ - auth: sessionToken, + auth: sessionToken }); return { "job-environment": { kind: ValueProviderKind.AllowedValues, - get: (_: WorkflowContext) => - getEnvironments(octokit, cache, repo.owner, repo.name), + get: (_: WorkflowContext) => getEnvironments(octokit, cache, repo.owner, repo.name) }, "runs-on": { kind: ValueProviderKind.SuggestedValues, - get: (_: WorkflowContext) => - getRunnerLabels(octokit, cache, repo.owner, repo.name), - }, + get: (_: WorkflowContext) => getRunnerLabels(octokit, cache, repo.owner, repo.name) + } }; } diff --git a/actions-languageserver/src/value-providers/job-environment.ts b/actions-languageserver/src/value-providers/job-environment.ts index 9f20011..0bf0bbc 100644 --- a/actions-languageserver/src/value-providers/job-environment.ts +++ b/actions-languageserver/src/value-providers/job-environment.ts @@ -1,31 +1,24 @@ -import { Value } from "@github/actions-languageservice/value-providers/config"; -import { Octokit } from "@octokit/rest"; -import { TTLCache } from "../utils/cache"; +import {Value} from "@github/actions-languageservice/value-providers/config"; +import {Octokit} from "@octokit/rest"; +import {TTLCache} from "../utils/cache"; -export async function getEnvironments( - client: Octokit, - cache: TTLCache, - owner: string, - name: string -): Promise { - const environments = await cache.get(`${owner}/${name}/environments`, undefined, () => fetchEnvironments(client, owner, name)); - return Array.from(environments).map((env) => ({ label: env })); +export async function getEnvironments(client: Octokit, cache: TTLCache, owner: string, name: string): Promise { + const environments = await cache.get(`${owner}/${name}/environments`, undefined, () => + fetchEnvironments(client, owner, name) + ); + return Array.from(environments).map(env => ({label: env})); } -async function fetchEnvironments( - client: Octokit, - owner: string, - name: string -): Promise { +async function fetchEnvironments(client: Octokit, owner: string, name: string): Promise { let environments: string[] = []; try { const response = await client.repos.getAllEnvironments({ owner, - repo: name, + repo: name }); if (response.data.environments) { - environments = response.data.environments.map((env) => env.name); + environments = response.data.environments.map(env => env.name); } } catch (e) { console.log("Failure to retrieve environments: ", e); diff --git a/actions-languageserver/src/value-providers/runs-on.ts b/actions-languageserver/src/value-providers/runs-on.ts index ed869fb..0de79f4 100644 --- a/actions-languageserver/src/value-providers/runs-on.ts +++ b/actions-languageserver/src/value-providers/runs-on.ts @@ -1,13 +1,8 @@ -import { Value } from "@github/actions-languageservice/value-providers/config"; -import { Octokit } from "@octokit/rest"; -import { TTLCache } from "../utils/cache"; +import {Value} from "@github/actions-languageservice/value-providers/config"; +import {Octokit} from "@octokit/rest"; +import {TTLCache} from "../utils/cache"; -export async function getRunnerLabels( - client: Octokit, - cache: TTLCache, - owner: string, - name: string -): Promise { +export async function getRunnerLabels(client: Octokit, cache: TTLCache, owner: string, name: string): Promise { const defaultLabels = [ "ubuntu-latest", "ubuntu-22.04", @@ -21,32 +16,26 @@ export async function getRunnerLabels( "macos-12", "macos-11", "macos-10.15", - "self-hosted", + "self-hosted" ]; - const repoLabels = await cache.get( - `${owner}/${name}/runner-labels`, - undefined, - () => fetchRunnerLabels(client, owner, name) + const repoLabels = await cache.get(`${owner}/${name}/runner-labels`, undefined, () => + fetchRunnerLabels(client, owner, name) ); for (const label of defaultLabels) { repoLabels.add(label); } - return Array.from(repoLabels).map((label) => ({ label })); + return Array.from(repoLabels).map(label => ({label})); } -async function fetchRunnerLabels( - client: Octokit, - owner: string, - name: string -): Promise> { +async function fetchRunnerLabels(client: Octokit, owner: string, name: string): Promise> { const labels = new Set(); try { const response = await client.actions.listSelfHostedRunnersForRepo({ owner, - repo: name, + repo: name }); for (const runner of response.data.runners) {