Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support file-based cancellation #135

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,206 changes: 1,118 additions & 88 deletions client/package-lock.json

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"lint": "eslint -c .eslintrc.js --ext .ts src"
},
"dependencies": {
"vscode-languageclient": "^6.1.3"
"@elm-tooling/elm-language-server": "^1.13.2",
"vscode-languageclient": "^7.0.0-next.14"
},
"devDependencies": {
"@types/request": "^2.48.5",
Expand Down
96 changes: 96 additions & 0 deletions client/src/cancellation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import { randomBytes } from "crypto";
import * as fs from "fs";
import * as os from "os";
import { Disposable } from "vscode";
import {
CancellationId,
CancellationReceiverStrategy,
CancellationSenderStrategy,
CancellationStrategy,
MessageConnection,
} from "vscode-languageclient";
import path = require("path");

function getCancellationFolderPath(folderName: string) {
return path.join(os.tmpdir(), "elm-language-server-cancellation", folderName);
}

function getCancellationFilePath(
folderName: string,
id: CancellationId,
): string {
return path.join(
getCancellationFolderPath(folderName),
`cancellation-${String(id)}.tmp`,
);
}

function tryRun(callback: () => void) {
try {
callback();
} catch (e) {
/* empty */
}
}

class FileCancellationSenderStrategy implements CancellationSenderStrategy {
constructor(readonly folderName: string) {
const folder = getCancellationFolderPath(folderName);
tryRun(() => fs.mkdirSync(folder, { recursive: true }));
}

sendCancellation(_: MessageConnection, id: CancellationId): void {
const file = getCancellationFilePath(this.folderName, id);
tryRun(() => fs.writeFileSync(file, "", { flag: "w" }));
}

cleanup(id: CancellationId): void {
tryRun(() => fs.unlinkSync(getCancellationFilePath(this.folderName, id)));
}

dispose(): void {
const folder = getCancellationFolderPath(this.folderName);
tryRun(() => rimraf(folder));

function rimraf(location: string) {
const stat = fs.lstatSync(location);
if (stat) {
if (stat.isDirectory() && !stat.isSymbolicLink()) {
for (const dir of fs.readdirSync(location)) {
rimraf(path.join(location, dir));
}

fs.rmdirSync(location);
} else {
fs.unlinkSync(location);
}
}
}
}
}

export class FileBasedCancellationStrategy
implements CancellationStrategy, Disposable {
private _sender: FileCancellationSenderStrategy;

constructor() {
const folderName = randomBytes(21).toString("hex");
this._sender = new FileCancellationSenderStrategy(folderName);
}

get receiver(): CancellationReceiverStrategy {
return CancellationReceiverStrategy.Message;
}

get sender(): CancellationSenderStrategy {
return this._sender;
}

getCommandLineArguments(): string[] {
return [`--cancellationReceive=file:${this._sender.folderName}`];
}

dispose(): void {
this._sender.dispose();
}
}
20 changes: 8 additions & 12 deletions client/src/exposeUnexposeAction.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import { LanguageClient } from "vscode-languageclient";
import { LanguageClient } from "vscode-languageclient/node";
import { ExtensionContext, commands } from "vscode";
import {
ExposeRequest,
UnexposeRequest,
IExposeUnexposeParams,
} from "./protocol";
import { Protocol } from "@elm-tooling/elm-language-server";

export function registerCommands(
languageClient: LanguageClient,
Expand All @@ -13,7 +9,7 @@ export function registerCommands(
context.subscriptions.push(
commands.registerCommand(
"elm.expose",
async (params: IExposeUnexposeParams) => {
async (params: Protocol.IExposeUnexposeParams) => {
await expose(languageClient, params);
},
),
Expand All @@ -22,7 +18,7 @@ export function registerCommands(
context.subscriptions.push(
commands.registerCommand(
"elm.unexpose",
async (params: IExposeUnexposeParams) => {
async (params: Protocol.IExposeUnexposeParams) => {
await unexpose(languageClient, params);
},
),
Expand All @@ -31,14 +27,14 @@ export function registerCommands(

async function expose(
languageClient: LanguageClient,
params: IExposeUnexposeParams,
params: Protocol.IExposeUnexposeParams,
) {
await languageClient.sendRequest(ExposeRequest, params);
await languageClient.sendRequest(Protocol.ExposeRequest, params);
}

async function unexpose(
languageClient: LanguageClient,
params: IExposeUnexposeParams,
params: Protocol.IExposeUnexposeParams,
) {
await languageClient.sendRequest(UnexposeRequest, params);
await languageClient.sendRequest(Protocol.UnexposeRequest, params);
}
35 changes: 28 additions & 7 deletions client/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,25 @@ import {
WorkspaceFolder,
} from "vscode";
import {
LanguageClient,
LanguageClientOptions,
Middleware,
ResolveCodeLensSignature,
RevealOutputChannelOn,
TransportKind,
ProvideCodeLensesSignature,
DidChangeConfigurationNotification,
Disposable,
} from "vscode-languageclient";
import {
LanguageClient,
ServerOptions,
TransportKind,
} from "vscode-languageclient/node";
import * as Package from "./elmPackage";
import * as RefactorAction from "./refactorAction";
import * as ExposeUnexposeAction from "./exposeUnexposeAction";
import * as Restart from "./restart";
import { OnDidCreateFilesRequest, OnDidRenameFilesRequest } from "./protocol";
import { Protocol } from "@elm-tooling/elm-language-server";
import { FileBasedCancellationStrategy } from "./cancellation";

export type ElmAnalyseTrigger = "change" | "save" | "never";

Expand Down Expand Up @@ -83,6 +88,8 @@ function getOuterMostWorkspaceFolder(
return folder;
}

const disposables: Disposable[] = [];

export function activate(context: ExtensionContext): void {
const module = context.asAbsolutePath(path.join("server", "out", "index.js"));

Expand Down Expand Up @@ -114,16 +121,24 @@ export function activate(context: ExtensionContext): void {
relativeWorkspace.length > 0 ? `Elm (${relativeWorkspace})` : "Elm",
);

const cancellationStrategy = new FileBasedCancellationStrategy();
disposables.push(cancellationStrategy);

const debugOptions = {
execArgv: ["--nolazy", `--inspect=${6010 + clients.size}`],
};
const serverOptions = {
const serverOptions: ServerOptions = {
debug: {
module,
options: debugOptions,
transport: TransportKind.ipc,
args: cancellationStrategy.getCommandLineArguments(),
},
run: {
module,
transport: TransportKind.ipc,
args: cancellationStrategy.getCommandLineArguments(),
},
run: { module, transport: TransportKind.ipc },
};
const clientOptions: LanguageClientOptions = {
diagnosticCollectionName: "Elm",
Expand All @@ -143,6 +158,9 @@ export function activate(context: ExtensionContext): void {
progressOnInitialization: true,
revealOutputChannelOn: RevealOutputChannelOn.Never,
workspaceFolder: folder,
connectionOptions: {
cancellationStrategy,
},
};
const client = new LanguageClient(
"elmLS",
Expand Down Expand Up @@ -173,15 +191,17 @@ export function activate(context: ExtensionContext): void {
Workspace.onDidCreateFiles((e) => {
if (e.files.some((file) => file.toString().endsWith(".elm"))) {
clients.forEach(
(client) => void client.sendRequest(OnDidCreateFilesRequest, e),
(client) =>
void client.sendRequest(Protocol.OnDidCreateFilesRequest, e),
);
}
});

Workspace.onDidRenameFiles((e) => {
if (e.files.some(({ newUri }) => newUri.toString().endsWith(".elm"))) {
clients.forEach(
(client) => void client.sendRequest(OnDidRenameFilesRequest, e),
(client) =>
void client.sendRequest(Protocol.OnDidRenameFilesRequest, e),
);
}
});
Expand Down Expand Up @@ -223,6 +243,7 @@ export function activate(context: ExtensionContext): void {
}

export function deactivate(): Thenable<void> | undefined {
disposables.forEach((d) => d.dispose());
const promises: Thenable<void>[] = [];
for (const client of clients.values()) {
promises.push(client.stop());
Expand Down
66 changes: 0 additions & 66 deletions client/src/protocol.ts

This file was deleted.

9 changes: 5 additions & 4 deletions client/src/refactorAction.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { commands, ExtensionContext, window } from "vscode";
import { CodeActionParams, LanguageClient } from "vscode-languageclient";
import { GetMoveDestinationRequest, MoveRequest } from "./protocol";
import { CodeActionParams } from "vscode-languageclient";
import { LanguageClient } from "vscode-languageclient/node";
import { Protocol } from "@elm-tooling/elm-language-server";

export function registerCommands(
languageClient: LanguageClient,
Expand Down Expand Up @@ -28,7 +29,7 @@ async function moveFunction(
commandInfo: string,
) {
const moveDestinations = await languageClient.sendRequest(
GetMoveDestinationRequest,
Protocol.GetMoveDestinationRequest,
{
sourceUri: params.textDocument.uri,
params,
Expand Down Expand Up @@ -65,7 +66,7 @@ async function moveFunction(
return;
}

await languageClient.sendRequest(MoveRequest, {
await languageClient.sendRequest(Protocol.MoveRequest, {
sourceUri: params.textDocument.uri,
params,
destination: selected.destination,
Expand Down
2 changes: 1 addition & 1 deletion client/src/restart.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use strict";
import * as vscode from "vscode";
import { LanguageClient } from "vscode-languageclient";
import { LanguageClient } from "vscode-languageclient/node";

export function registerCommand(
langClients: Map<string, LanguageClient>,
Expand Down