Skip to content

Commit

Permalink
Refactor collecting documentSelector paths
Browse files Browse the repository at this point in the history
  • Loading branch information
d-lebed committed Nov 28, 2024
1 parent c8a1869 commit b5b2464
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 54 deletions.
83 changes: 29 additions & 54 deletions vscode/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,45 +157,22 @@ function collectClientOptions(
const supportedSchemes = ["file", "git"];

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

// For each workspace, the language client is responsible for handling requests for:
// 1. Files inside of the workspace itself
// 2. Bundled gems
// 3. Default gems
let documentSelector: DocumentSelector = SUPPORTED_LANGUAGE_IDS.flatMap(
(language) => {
return supportedSchemes.map((scheme) => {
return { scheme, language, pattern: `${fsPath}/**/*` };
return pathConverter.alternativePaths(fsPath).flatMap((pathVariant) => {
return supportedSchemes.map((scheme) => {
return { scheme, language, pattern: `${pathVariant}/**/*` };
});
});
},
);

const pathConverter = ruby.pathConverter;

const pushAlternativePaths = (
path: string,
schemes: string[] = supportedSchemes,
) => {
schemes.forEach((scheme) => {
[
pathConverter.toLocalPath(path),
pathConverter.toRemotePath(path),
].forEach((convertedPath) => {
if (convertedPath !== path) {
SUPPORTED_LANGUAGE_IDS.forEach((language) => {
documentSelector.push({
scheme,
language,
pattern: `${convertedPath}/**/*`,
});
});
}
});
});
};

pushAlternativePaths(fsPath);

// Only the first language server we spawn should handle unsaved files, otherwise requests will be duplicated across
// all workspaces
if (isMainWorkspace) {
Expand All @@ -209,37 +186,35 @@ function collectClientOptions(

ruby.gemPath.forEach((gemPath) => {
supportedSchemes.forEach((scheme) => {
documentSelector.push({
scheme,
language: "ruby",
pattern: `${gemPath}/**/*`,
});

pushAlternativePaths(gemPath, [scheme]);

// Because of how default gems are installed, the gemPath location is actually not exactly where the files are
// located. With the regex, we are correcting the default gem path from this (where the files are not located)
// /opt/rubies/3.3.1/lib/ruby/gems/3.3.0
//
// to this (where the files are actually stored)
// /opt/rubies/3.3.1/lib/ruby/3.3.0
//
// Notice that we still need to add the regular path to the selector because some version managers will install
// gems under the non-corrected path
if (/lib\/ruby\/gems\/(?=\d)/.test(gemPath)) {
const correctedPath = gemPath.replace(
/lib\/ruby\/gems\/(?=\d)/,
"lib/ruby/",
);

pathConverter.alternativePaths(gemPath).forEach((pathVariant) => {
documentSelector.push({
scheme,
language: "ruby",
pattern: `${correctedPath}/**/*`,
pattern: `${pathVariant}/**/*`,
});

pushAlternativePaths(correctedPath, [scheme]);
}
// Because of how default gems are installed, the gemPath location is actually not exactly where the files are
// located. With the regex, we are correcting the default gem path from this (where the files are not located)
// /opt/rubies/3.3.1/lib/ruby/gems/3.3.0
//
// to this (where the files are actually stored)
// /opt/rubies/3.3.1/lib/ruby/3.3.0
//
// Notice that we still need to add the regular path to the selector because some version managers will install
// gems under the non-corrected path
if (/lib\/ruby\/gems\/(?=\d)/.test(pathVariant)) {
const correctedPath = pathVariant.replace(
/lib\/ruby\/gems\/(?=\d)/,
"lib/ruby/",
);

documentSelector.push({
scheme,
language: "ruby",
pattern: `${correctedPath}/**/*`,
});
}
});
});
});

Expand Down
5 changes: 5 additions & 0 deletions vscode/src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ export interface PathConverterInterface {
toRemotePath: (localPath: string) => string;
toLocalPath: (remotePath: string) => string;
toRemoteUri: (localUri: vscode.Uri) => vscode.Uri;
alternativePaths: (path: string) => string[];
}

export class PathConverter implements PathConverterInterface {
Expand All @@ -85,6 +86,10 @@ export class PathConverter implements PathConverterInterface {
toRemoteUri(localUri: vscode.Uri) {
return localUri;
}

alternativePaths(path: string) {
return [path];
}
}

// Event emitter used to signal that the language status items need to be refreshed
Expand Down
10 changes: 10 additions & 0 deletions vscode/src/docker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,16 @@ export class ContainerPathConverter implements PathConverterInterface {
const remotePath = this.toRemotePath(localUri.fsPath);
return vscode.Uri.file(remotePath);
}

alternativePaths(path: string) {
const alternatives = [
this.toRemotePath(path),
this.toLocalPath(path),
path,
];

return Array.from(new Set(alternatives));
}
}

function fetchComposeBindings(
Expand Down

0 comments on commit b5b2464

Please sign in to comment.