Skip to content

Commit

Permalink
Eagerly activate workspaces for opened files (#2283)
Browse files Browse the repository at this point in the history
* Eagerly activate workspaces for opened files

* Use SUPPORTED_LANGUAGE_IDS everywhere we need to handle document languages
  • Loading branch information
vinistock authored Jul 12, 2024
1 parent 37ec812 commit a6ddd5b
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 26 deletions.
34 changes: 15 additions & 19 deletions vscode/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,12 @@ import {
State,
} from "vscode-languageclient/node";

import { LSP_NAME, ClientInterface, Addon } from "./common";
import {
LSP_NAME,
ClientInterface,
Addon,
SUPPORTED_LANGUAGE_IDS,
} from "./common";
import { Ruby } from "./ruby";
import { WorkspaceChannel } from "./workspaceChannel";

Expand Down Expand Up @@ -114,30 +119,21 @@ function collectClientOptions(
const enabledFeatures = Object.keys(features).filter((key) => features[key]);

const fsPath = workspaceFolder.uri.fsPath.replace(/\/$/, "");
const documentSelector: DocumentSelector = [
{
language: "ruby",
pattern: `${fsPath}/**/*`,
},
{
language: "erb",
pattern: `${fsPath}/**/*`,
const documentSelector: DocumentSelector = SUPPORTED_LANGUAGE_IDS.map(
(language) => {
return { language, pattern: `${fsPath}/**/*` };
},
];
);

// Only the first language server we spawn should handle unsaved files, otherwise requests will be duplicated across
// all workspaces
if (isMainWorkspace) {
documentSelector.push(
{
language: "ruby",
scheme: "untitled",
},
{
language: "erb",
SUPPORTED_LANGUAGE_IDS.forEach((language) => {
documentSelector.push({
language,
scheme: "untitled",
},
);
});
});
}

// For each workspace, the language client is responsible for handling requests for:
Expand Down
1 change: 1 addition & 0 deletions vscode/src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export const LSP_NAME = "Ruby LSP";
export const LOG_CHANNEL = vscode.window.createOutputChannel(LSP_NAME, {
log: true,
});
export const SUPPORTED_LANGUAGE_IDS = ["ruby", "erb"];

// Creates a debounced version of a function with the specified delay. If the function is invoked before the delay runs
// out, then the previous invocation of the function gets cancelled and a new one is scheduled.
Expand Down
26 changes: 24 additions & 2 deletions vscode/src/rubyLsp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ import { Range } from "vscode-languageclient/node";

import DocumentProvider from "./documentProvider";
import { Workspace } from "./workspace";
import { Command, LOG_CHANNEL, STATUS_EMITTER } from "./common";
import {
Command,
LOG_CHANNEL,
STATUS_EMITTER,
SUPPORTED_LANGUAGE_IDS,
} from "./common";
import { ManagerIdentifier, ManagerConfiguration } from "./ruby";
import { StatusItems } from "./status";
import { TestController } from "./testController";
Expand Down Expand Up @@ -61,7 +66,7 @@ export class RubyLsp {
}),
// Lazily activate workspaces that do not contain a lockfile
vscode.workspace.onDidOpenTextDocument(async (document) => {
if (document.languageId !== "ruby") {
if (!SUPPORTED_LANGUAGE_IDS.includes(document.languageId)) {
return;
}

Expand Down Expand Up @@ -98,6 +103,23 @@ export class RubyLsp {
await this.activateWorkspace(firstWorkspace, true);
}

// If the user has the editor already opened on a Ruby file and that file does not belong to the first workspace,
// eagerly activate the workspace for that file too
const activeDocument = vscode.window.activeTextEditor?.document;

if (
activeDocument &&
SUPPORTED_LANGUAGE_IDS.includes(activeDocument.languageId)
) {
const workspaceFolder = vscode.workspace.getWorkspaceFolder(
activeDocument.uri,
);

if (workspaceFolder && workspaceFolder !== firstWorkspace) {
await this.activateWorkspace(workspaceFolder, true);
}
}

this.context.subscriptions.push(
vscode.workspace.registerTextDocumentContentProvider(
"ruby-lsp",
Expand Down
16 changes: 11 additions & 5 deletions vscode/src/status.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import * as vscode from "vscode";
import { State } from "vscode-languageclient";

import { Command, STATUS_EMITTER, WorkspaceInterface } from "./common";
import {
Command,
STATUS_EMITTER,
WorkspaceInterface,
SUPPORTED_LANGUAGE_IDS,
} from "./common";

const STOPPED_SERVER_OPTIONS = [
{ label: "Ruby LSP: Start", description: Command.Start },
Expand All @@ -17,10 +22,11 @@ export abstract class StatusItem {
public item: vscode.LanguageStatusItem;

constructor(id: string) {
this.item = vscode.languages.createLanguageStatusItem(id, {
scheme: "file",
language: "ruby",
});
const documentSelector = SUPPORTED_LANGUAGE_IDS.map((language) => ({
language,
}));

this.item = vscode.languages.createLanguageStatusItem(id, documentSelector);
}

abstract refresh(workspace: WorkspaceInterface): void;
Expand Down

0 comments on commit a6ddd5b

Please sign in to comment.