diff --git a/vscode/src/client.ts b/vscode/src/client.ts index 5779dbc1a..665ec7231 100644 --- a/vscode/src/client.ts +++ b/vscode/src/client.ts @@ -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"; @@ -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: diff --git a/vscode/src/common.ts b/vscode/src/common.ts index 8af0332b8..48b76768d 100644 --- a/vscode/src/common.ts +++ b/vscode/src/common.ts @@ -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. diff --git a/vscode/src/rubyLsp.ts b/vscode/src/rubyLsp.ts index ad9a75a55..c53d3fa28 100644 --- a/vscode/src/rubyLsp.ts +++ b/vscode/src/rubyLsp.ts @@ -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"; @@ -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; } @@ -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", diff --git a/vscode/src/status.ts b/vscode/src/status.ts index 0e9fea69b..28f7e3c94 100644 --- a/vscode/src/status.ts +++ b/vscode/src/status.ts @@ -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 }, @@ -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;