Merge pull request #21 from github/joshmgross/prettier-again
Run prettier on language server
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
node_modules
|
||||
dist
|
||||
*.md
|
||||
*.js
|
||||
*.json
|
||||
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"tabWidth": 2,
|
||||
"useTabs": false,
|
||||
"printWidth": 120,
|
||||
"singleQuote": false,
|
||||
"bracketSpacing": false,
|
||||
"trailingComma": "none",
|
||||
"arrowParens": "avoid"
|
||||
}
|
||||
@@ -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"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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 () => {
|
||||
|
||||
@@ -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<void> {
|
||||
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<CompletionItem[]> => {
|
||||
return await onCompletion(
|
||||
position,
|
||||
textDocument,
|
||||
}: TextDocumentPositionParams): Promise<CompletionItem[]> => {
|
||||
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<Hover | null> => {
|
||||
const r = await hover(documents.get(textDocument.uri)!, position);
|
||||
return r;
|
||||
}
|
||||
);
|
||||
connection.onHover(async ({position, textDocument}: HoverParams): Promise<Hover | null> => {
|
||||
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
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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<T>(
|
||||
key: string,
|
||||
ttlInMS: number | undefined,
|
||||
getter: () => Promise<T>
|
||||
): Promise<T> {
|
||||
async get<T>(key: string, ttlInMS: number | undefined, getter: () => Promise<T>): Promise<T> {
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@@ -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<Value[]> {
|
||||
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<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(
|
||||
client: Octokit,
|
||||
owner: string,
|
||||
name: string
|
||||
): Promise<string[]> {
|
||||
async function fetchEnvironments(client: Octokit, owner: string, name: string): Promise<string[]> {
|
||||
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);
|
||||
|
||||
@@ -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<Value[]> {
|
||||
export async function getRunnerLabels(client: Octokit, cache: TTLCache, owner: string, name: string): Promise<Value[]> {
|
||||
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<Set<string>> {
|
||||
async function fetchRunnerLabels(client: Octokit, owner: string, name: string): Promise<Set<string>> {
|
||||
const labels = new Set<string>();
|
||||
try {
|
||||
const response = await client.actions.listSelfHostedRunnersForRepo({
|
||||
owner,
|
||||
repo: name,
|
||||
repo: name
|
||||
});
|
||||
|
||||
for (const runner of response.data.runners) {
|
||||
|
||||
Reference in New Issue
Block a user