Skip to content
This repository was archived by the owner on Jul 31, 2023. It is now read-only.
Merged
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
13 changes: 12 additions & 1 deletion client/src/WorkspaceRubyEnvironment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
LanguageClient,
RequestType,
StaticFeature,
InitializeParams,
} from 'vscode-languageclient';
import { Uri, workspace, WorkspaceFolder } from 'vscode';
import { loadEnv, IEnvironment } from './util/env';
Expand Down Expand Up @@ -33,8 +34,18 @@ type WorkspaceRubyEnvironmentCapability = {
type ClientCapabilitiesWithRubyEnvironment = ClientCapabilities &
WorkspaceRubyEnvironmentCapability;

export enum NodeRuntime {
Electron = 1,
Node = 2
}

export class WorkspaceRubyEnvironmentFeature implements StaticFeature {
constructor(private client: LanguageClient) {}
constructor(private client: LanguageClient, private runtime: NodeRuntime) { }

public fillInitializeParams(params: InitializeParams): void {
params.initializationOptions = params.initializationOptions || {};
params.initializationOptions.runtime = this.runtime;
}

public fillClientCapabilities(capabilities: ClientCapabilitiesWithRubyEnvironment): void {
capabilities.workspace = capabilities.workspace || {};
Expand Down
5 changes: 3 additions & 2 deletions client/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
TransportKind,
WorkspaceMiddleware,
} from 'vscode-languageclient';
import { WorkspaceRubyEnvironmentFeature } from './WorkspaceRubyEnvironment';
import { WorkspaceRubyEnvironmentFeature, NodeRuntime } from './WorkspaceRubyEnvironment';

const RUBOCOP_ABSOLUTE_PATH_KEYS = ['require'];
let client: LanguageClient;
Expand Down Expand Up @@ -95,7 +95,8 @@ export function activate(context: ExtensionContext): void {
// Create the language client and start the client.
client = new LanguageClient('ruby', 'Ruby', serverOptions, clientOptions);
client.registerProposedFeatures();
client.registerFeature(new WorkspaceRubyEnvironmentFeature(client));
const nodeRuntime = (context as any).executionContext === 1 ? NodeRuntime.Electron : NodeRuntime.Node;
client.registerFeature(new WorkspaceRubyEnvironmentFeature(client, nodeRuntime));

// Push the disposable to the context's subscriptions so that the
// client can be deactivated on extension deactivation
Expand Down
10 changes: 5 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions server/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,17 @@ import {
} from 'vscode-languageserver';

import { ILanguageServer } from './Server';
import { rebuildTreeSitter } from './util/rebuilder';
import { rebuildTreeSitter, NodeRuntime } from './util/rebuilder';

const connection: IConnection = createConnection(ProposedFeatures.all);
let server: ILanguageServer;

connection.onInitialize(async (params: InitializeParams) => {
connection.console.info('Initializing Ruby language server...');

connection.console.info('Rebuilding tree-sitter for local Electron version');
const rebuildResult: [void | Error, void | Error] = await rebuildTreeSitter();
let nodeRuntime = params.initializationOptions ? params.initializationOptions.runtime : NodeRuntime.Electron;
connection.console.info(`Rebuilding tree-sitter for local ${nodeRuntime === NodeRuntime.Electron ? 'Electron' : 'Node.js'} version`);
const rebuildResult: [void | Error, void | Error] = await rebuildTreeSitter(nodeRuntime);
for (const result of rebuildResult) {
if (result) {
connection.console.error('Rebuild failed!');
Expand Down
19 changes: 12 additions & 7 deletions server/src/util/rebuilder.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import * as path from 'path';
import * as prebuildInstall from 'prebuild-install';

export enum NodeRuntime {
Electron = 1,
Node = 2
}

function packageToGithubRepo(name: string): string {
let repo: string;
switch (name) {
Expand All @@ -14,20 +19,20 @@ function packageToGithubRepo(name: string): string {
return repo;
}

function downloadUrl(name: string, version: string): string {
function downloadUrl(name: string, version: string, nodeRuntime: NodeRuntime): string {
const repo: string = packageToGithubRepo(name);
const urlBase: string = `https://github.com/tree-sitter/${repo}/releases/download/v${version}/`;
const prebuild: string = `${name}-v${version}-electron-v${process.versions.modules}-${
const prebuild: string = `${name}-v${version}-${nodeRuntime === NodeRuntime.Electron ? 'electron' : 'node'}-v${process.versions.modules}-${
process.platform
}-${process.arch}.tar.gz`;
}-${process.arch}.tar.gz`;

return `${urlBase}${prebuild}`;
}

function fetchPrebuild(name: string): Promise<void | Error> {
function fetchPrebuild(name: string, nodeRuntime: NodeRuntime): Promise<void | Error> {
const pkgRoot: string = path.resolve(path.join(__dirname, '../../node_modules', name));
const pkg: { name: string; version: string } = require(`${pkgRoot}/package.json`);
const url: string = downloadUrl(pkg.name, pkg.version);
const url: string = downloadUrl(pkg.name, pkg.version, nodeRuntime);

return new Promise((res, rej) => {
prebuildInstall.download(url, { pkg, path: pkgRoot }, (err: Error) => {
Expand All @@ -36,6 +41,6 @@ function fetchPrebuild(name: string): Promise<void | Error> {
});
}

export function rebuildTreeSitter(): Promise<[void | Error, void | Error]> {
return Promise.all([fetchPrebuild('tree-sitter'), fetchPrebuild('tree-sitter-ruby')]);
export function rebuildTreeSitter(nodeRuntime: NodeRuntime): Promise<[void | Error, void | Error]> {
return Promise.all([fetchPrebuild('tree-sitter', nodeRuntime), fetchPrebuild('tree-sitter-ruby', nodeRuntime)]);
}