Skip to content

Commit b5b2464

Browse files
committed
Refactor collecting documentSelector paths
1 parent c8a1869 commit b5b2464

File tree

3 files changed

+44
-54
lines changed

3 files changed

+44
-54
lines changed

vscode/src/client.ts

+29-54
Original file line numberDiff line numberDiff line change
@@ -157,45 +157,22 @@ function collectClientOptions(
157157
const supportedSchemes = ["file", "git"];
158158

159159
const fsPath = workspaceFolder.uri.fsPath.replace(/\/$/, "");
160+
const pathConverter = ruby.pathConverter;
160161

161162
// For each workspace, the language client is responsible for handling requests for:
162163
// 1. Files inside of the workspace itself
163164
// 2. Bundled gems
164165
// 3. Default gems
165166
let documentSelector: DocumentSelector = SUPPORTED_LANGUAGE_IDS.flatMap(
166167
(language) => {
167-
return supportedSchemes.map((scheme) => {
168-
return { scheme, language, pattern: `${fsPath}/**/*` };
168+
return pathConverter.alternativePaths(fsPath).flatMap((pathVariant) => {
169+
return supportedSchemes.map((scheme) => {
170+
return { scheme, language, pattern: `${pathVariant}/**/*` };
171+
});
169172
});
170173
},
171174
);
172175

173-
const pathConverter = ruby.pathConverter;
174-
175-
const pushAlternativePaths = (
176-
path: string,
177-
schemes: string[] = supportedSchemes,
178-
) => {
179-
schemes.forEach((scheme) => {
180-
[
181-
pathConverter.toLocalPath(path),
182-
pathConverter.toRemotePath(path),
183-
].forEach((convertedPath) => {
184-
if (convertedPath !== path) {
185-
SUPPORTED_LANGUAGE_IDS.forEach((language) => {
186-
documentSelector.push({
187-
scheme,
188-
language,
189-
pattern: `${convertedPath}/**/*`,
190-
});
191-
});
192-
}
193-
});
194-
});
195-
};
196-
197-
pushAlternativePaths(fsPath);
198-
199176
// Only the first language server we spawn should handle unsaved files, otherwise requests will be duplicated across
200177
// all workspaces
201178
if (isMainWorkspace) {
@@ -209,37 +186,35 @@ function collectClientOptions(
209186

210187
ruby.gemPath.forEach((gemPath) => {
211188
supportedSchemes.forEach((scheme) => {
212-
documentSelector.push({
213-
scheme,
214-
language: "ruby",
215-
pattern: `${gemPath}/**/*`,
216-
});
217-
218-
pushAlternativePaths(gemPath, [scheme]);
219-
220-
// Because of how default gems are installed, the gemPath location is actually not exactly where the files are
221-
// located. With the regex, we are correcting the default gem path from this (where the files are not located)
222-
// /opt/rubies/3.3.1/lib/ruby/gems/3.3.0
223-
//
224-
// to this (where the files are actually stored)
225-
// /opt/rubies/3.3.1/lib/ruby/3.3.0
226-
//
227-
// Notice that we still need to add the regular path to the selector because some version managers will install
228-
// gems under the non-corrected path
229-
if (/lib\/ruby\/gems\/(?=\d)/.test(gemPath)) {
230-
const correctedPath = gemPath.replace(
231-
/lib\/ruby\/gems\/(?=\d)/,
232-
"lib/ruby/",
233-
);
234-
189+
pathConverter.alternativePaths(gemPath).forEach((pathVariant) => {
235190
documentSelector.push({
236191
scheme,
237192
language: "ruby",
238-
pattern: `${correctedPath}/**/*`,
193+
pattern: `${pathVariant}/**/*`,
239194
});
240195

241-
pushAlternativePaths(correctedPath, [scheme]);
242-
}
196+
// Because of how default gems are installed, the gemPath location is actually not exactly where the files are
197+
// located. With the regex, we are correcting the default gem path from this (where the files are not located)
198+
// /opt/rubies/3.3.1/lib/ruby/gems/3.3.0
199+
//
200+
// to this (where the files are actually stored)
201+
// /opt/rubies/3.3.1/lib/ruby/3.3.0
202+
//
203+
// Notice that we still need to add the regular path to the selector because some version managers will install
204+
// gems under the non-corrected path
205+
if (/lib\/ruby\/gems\/(?=\d)/.test(pathVariant)) {
206+
const correctedPath = pathVariant.replace(
207+
/lib\/ruby\/gems\/(?=\d)/,
208+
"lib/ruby/",
209+
);
210+
211+
documentSelector.push({
212+
scheme,
213+
language: "ruby",
214+
pattern: `${correctedPath}/**/*`,
215+
});
216+
}
217+
});
243218
});
244219
});
245220

vscode/src/common.ts

+5
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ export interface PathConverterInterface {
6969
toRemotePath: (localPath: string) => string;
7070
toLocalPath: (remotePath: string) => string;
7171
toRemoteUri: (localUri: vscode.Uri) => vscode.Uri;
72+
alternativePaths: (path: string) => string[];
7273
}
7374

7475
export class PathConverter implements PathConverterInterface {
@@ -85,6 +86,10 @@ export class PathConverter implements PathConverterInterface {
8586
toRemoteUri(localUri: vscode.Uri) {
8687
return localUri;
8788
}
89+
90+
alternativePaths(path: string) {
91+
return [path];
92+
}
8893
}
8994

9095
// Event emitter used to signal that the language status items need to be refreshed

vscode/src/docker.ts

+10
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,16 @@ export class ContainerPathConverter implements PathConverterInterface {
101101
const remotePath = this.toRemotePath(localUri.fsPath);
102102
return vscode.Uri.file(remotePath);
103103
}
104+
105+
alternativePaths(path: string) {
106+
const alternatives = [
107+
this.toRemotePath(path),
108+
this.toLocalPath(path),
109+
path,
110+
];
111+
112+
return Array.from(new Set(alternatives));
113+
}
104114
}
105115

106116
function fetchComposeBindings(

0 commit comments

Comments
 (0)