Skip to content

Commit f6276dd

Browse files
committed
Refactor collecting documentSelector paths
1 parent a3a5930 commit f6276dd

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
@@ -156,45 +156,22 @@ function collectClientOptions(
156156
const supportedSchemes = ["file", "git"];
157157

158158
const fsPath = workspaceFolder.uri.fsPath.replace(/\/$/, "");
159+
const pathConverter = ruby.pathConverter;
159160

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

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

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

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

vscode/src/common.ts

+5
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ export interface PathConverterInterface {
7171
toRemotePath: (localPath: string) => string;
7272
toLocalPath: (remotePath: string) => string;
7373
toRemoteUri: (localUri: vscode.Uri) => vscode.Uri;
74+
alternativePaths: (path: string) => string[];
7475
}
7576

7677
export class PathConverter implements PathConverterInterface {
@@ -87,6 +88,10 @@ export class PathConverter implements PathConverterInterface {
8788
toRemoteUri(localUri: vscode.Uri) {
8889
return localUri;
8990
}
91+
92+
alternativePaths(path: string) {
93+
return [path];
94+
}
9095
}
9196

9297
// 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)