Skip to content

Commit

Permalink
feat(nextls): to-pipe and from-pipe (#80)
Browse files Browse the repository at this point in the history
  • Loading branch information
mhanberg committed Mar 4, 2024
1 parent b18f0d5 commit 673e808
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 31 deletions.
4 changes: 0 additions & 4 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
// A launch configuration that compiles the extension and then opens it inside a new window
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
{
"version": "0.2.0",
"configurations": [
Expand Down
4 changes: 1 addition & 3 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
{
"version": "2.0.0",
"tasks": [
{
"type": "npm",
"script": "build",
"script": "compile-dist",
"problemMatcher": "$ts-webpack-watch",
"isBackground": true,
"presentation": {
Expand Down
11 changes: 10 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,14 @@
{
"command": "elixir-tools.uninstall-nextls",
"title": "elixir-tools: Uninstall Next LS"
},
{
"command": "elixir-tools.toPipe",
"title": "Convert to pipe (Next LS)"
},
{
"command": "elixir-tools.fromPipe",
"title": "Convert from pipe (Next LS)"
}
],
"grammars": [
Expand All @@ -182,6 +190,7 @@
"vscode:prepublish": "yarn run build-base --minify",
"package": "vsce package",
"compile-tests": "tsc -p . --outDir out",
"compile-dist": "esbuild ./src/extension.ts --bundle --outfile=dist/extension.js --external:vscode --format=cjs --platform=node --target=node16 --sourcemap",
"watch-tests": "tsc -p . -w --outDir out",
"lint": "eslint src --ext ts",
"fix": "eslint src --ext ts --fix",
Expand Down Expand Up @@ -216,4 +225,4 @@
"sinon": "^17.0.1",
"typescript": "^4.9.5"
}
}
}
33 changes: 33 additions & 0 deletions src/commands/from-pipe.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import * as vscode from "vscode";

import {
LanguageClient,
ExecuteCommandRequest,
} from "vscode-languageclient/node";

export const run = async (client: LanguageClient) => {
const position = vscode.window.activeTextEditor?.selection.start;

client.sendRequest(ExecuteCommandRequest.type, {
command: "from-pipe",
arguments: [
{
uri: vscode.window.activeTextEditor?.document.uri.toString(),
position: position,
},
],
});
};

function registerFromPipeCommand(
client: LanguageClient,
context: vscode.ExtensionContext
) {
const fromPipeCommand = "elixir-tools.fromPipe";
const fromPipe = async () => run(client);
context.subscriptions.push(
vscode.commands.registerCommand(fromPipeCommand, fromPipe)
);
}

export default registerFromPipeCommand;
33 changes: 33 additions & 0 deletions src/commands/to-pipe.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import * as vscode from "vscode";

import {
LanguageClient,
ExecuteCommandRequest,
} from "vscode-languageclient/node";

export const run = async (client: LanguageClient) => {
const position = vscode.window.activeTextEditor?.selection.start;

client.sendRequest(ExecuteCommandRequest.type, {
command: "to-pipe",
arguments: [
{
uri: vscode.window.activeTextEditor?.document.uri.toString(),
position: position,
},
],
});
};

function registerToPipeCommand(
client: LanguageClient,
context: vscode.ExtensionContext
) {
const toPipeCommand = "elixir-tools.toPipe";
const toPipe = async () => run(client);
context.subscriptions.push(
vscode.commands.registerCommand(toPipeCommand, toPipe)
);
}

export default registerToPipeCommand;
6 changes: 3 additions & 3 deletions src/commands/uninstall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ export const run = async (cacheDir: string) => {
);
};

const registerUninstallCommand = (
function registerUninstallCommand(
config: vscode.WorkspaceConfiguration,
context: vscode.ExtensionContext
) => {
) {
const uninstallCommand = "elixir-tools.uninstall-nextls";

const uninstall = async () =>
Expand All @@ -36,6 +36,6 @@ const registerUninstallCommand = (
context.subscriptions.push(
vscode.commands.registerCommand(uninstallCommand, uninstall)
);
};
}

export default registerUninstallCommand;
37 changes: 21 additions & 16 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,16 @@ import {
} from "vscode-languageclient/node";

import registerUninstallCommand from "./commands/uninstall";
import registerToPipeCommand from "./commands/to-pipe";
import registerFromPipeCommand from "./commands/from-pipe";

let credoClient: LanguageClient;
let nextLSClient: LanguageClient;

const channel = vscode.window.createOutputChannel("elixir-tools.vscode", {
log: true,
});

async function latestRelease(project: string): Promise<string> {
return fetch(
`https://api.github.com/repos/elixir-tools/${project}/releases/latest`,
Expand Down Expand Up @@ -101,8 +107,6 @@ async function activateNextLS(
) {
let config = vscode.workspace.getConfiguration("elixir-tools.nextLS");

registerUninstallCommand(config, context);

if (config.get("enable")) {
let serverOptions: ServerOptions;

Expand Down Expand Up @@ -163,11 +167,15 @@ async function activateNextLS(

nextLSClient = new LanguageClient(
"elixir-tools.nextLS",
"NextLS",
"Next LS",
serverOptions,
clientOptions
);

registerToPipeCommand(nextLSClient, context);
registerFromPipeCommand(nextLSClient, context);
registerUninstallCommand(config, context);

// Start the nextLSClient. This will also launch the server
nextLSClient.start();
}
Expand Down Expand Up @@ -206,21 +214,14 @@ export async function ensureNextLSDownloaded(
const shouldDownload = opts.force || (await isBinaryMissing(bin));

if (shouldDownload) {
channel.info("Next LS needs to be downloaded");
await fsp.mkdir(cacheDir, { recursive: true });

const platform = getPlatform();
const exe = getExe(platform);
const url = `https://github.com/elixir-tools/next-ls/releases/latest/download/${exe}`;

const shouldInstall = await vscode.window.showInformationMessage(
"Install Next LS?",
{ modal: true, detail: `Downloading to '${cacheDir}'` },
{ title: "Yes" }
);

if (shouldInstall?.title !== "Yes") {
throw new Error("Could not activate Next LS");
}
channel.info(`Starting download from ${url}`);

await fetch(url).then((res) => {
if (res.ok) {
Expand All @@ -230,10 +231,12 @@ export async function ensureNextLSDownloaded(
file.on("close", resolve);
file.on("error", reject);
})
.then(() => console.log("Downloaded NextLS!!"))
.catch(() => console.log("Failed to download NextLS!!"));
.then(() => channel.info("Downloaded NextLS!"))
.catch(() =>
channel.error("Failed to write downloaded executable to a file")
);
} else {
throw new Error(`Download failed (${url}, status=${res.status})`);
channel.error(`Failed to write download Next LS: status=${res.status}`);
}
});
await fsp.chmod(bin, "755");
Expand All @@ -245,8 +248,10 @@ export async function ensureNextLSDownloaded(
async function isBinaryMissing(bin: string) {
try {
await fsp.access(bin, fs.constants.X_OK);
channel.info(`Found Next LS executable at ${bin}`);
return false;
} catch {
channel.warn(`Did not find Next LS executable at ${bin}`);
return true;
}
}
Expand All @@ -266,7 +271,7 @@ function getExe(platform: string) {
return "next_ls_darwin_amd64";

case "arm64":
return "next_ls_darwin_amd64";
return "next_ls_darwin_arm64";
}

case "linux":
Expand Down
13 changes: 9 additions & 4 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@
"compilerOptions": {
"target": "es2020",
"module": "commonjs",
"lib": ["ES2020", "DOM"],
"lib": [
"ES2020",
"DOM"
],
"outDir": "out",
"sourceMap": true,
"strict": false
},
"exclude": ["node_modules", ".vscode-test", ]
}

"exclude": [
"node_modules",
".vscode-test",
]
}

0 comments on commit 673e808

Please sign in to comment.