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

Eagerly activate workspaces for opened files #2283

Merged
merged 2 commits into from
Jul 12, 2024
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
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) => ({
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be plural? documentSelectors?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I matched the type name, which is DocumentSelector and is an array of entries.

language,
}));

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

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