Merge pull request #167957 from microsoft/tyriar/164687

Consider all links when common directories are removed
This commit is contained in:
Daniel Imms 2022-12-02 12:39:22 -08:00 committed by GitHub
commit 0dc7e497dc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 11 additions and 6 deletions

View file

@ -156,27 +156,32 @@ export function getXtermLineContent(buffer: IBuffer, lineStart: number, lineEnd:
* For shells with the CommandDetection capability, the cwd for a command relative to the line of
* the particular link can be used to narrow down the result for an exact file match.
*/
export function updateLinkWithRelativeCwd(capabilities: ITerminalCapabilityStore, y: number, text: string, pathSeparator: string): string | undefined {
export function updateLinkWithRelativeCwd(capabilities: ITerminalCapabilityStore, y: number, text: string, pathSeparator: string): string[] | undefined {
const cwd = capabilities.get(TerminalCapability.CommandDetection)?.getCwdForLine(y);
if (!cwd) {
return undefined;
}
const result: string[] = [];
if (!text.includes(pathSeparator)) {
text = cwd + pathSeparator + text;
result.push(cwd + pathSeparator + text);
} else {
let commonDirs = 0;
let i = 0;
const cwdPath = cwd.split(pathSeparator).reverse();
const linkPath = text.split(pathSeparator);
// Get all results as candidates, prioritizing the link with the most common directories.
// For example if in the directory /home/common and the link is common/file, the result
// should be: `['/home/common/common/file', '/home/common/file']`. The first is the most
// likely as cwd detection is active.
while (i < cwdPath.length) {
result.push(cwd + pathSeparator + linkPath.slice(commonDirs).join(pathSeparator));
if (cwdPath[i] === linkPath[i]) {
commonDirs++;
}
i++;
}
text = cwd + pathSeparator + linkPath.slice(commonDirs).join(pathSeparator);
}
return text;
return result;
}
export function osPathModule(os: OperatingSystem): IPath {

View file

@ -173,7 +173,7 @@ export class TerminalSearchLinkOpener implements ITerminalLinkOpener {
});
let cwdResolvedText = text;
if (this._capabilities.has(TerminalCapability.CommandDetection)) {
cwdResolvedText = updateLinkWithRelativeCwd(this._capabilities, link.bufferRange.start.y, text, pathSeparator) || text;
cwdResolvedText = updateLinkWithRelativeCwd(this._capabilities, link.bufferRange.start.y, text, pathSeparator)?.[0] || text;
}
// Try open the cwd resolved link first

View file

@ -157,7 +157,7 @@ export class TerminalLocalLinkDetector implements ITerminalLinkDetector {
// not exist. Doing otherwise could cause unexpected results where handling via the
// word link detector is preferable.
if (absolutePath) {
linkCandidates.push(absolutePath);
linkCandidates.push(...absolutePath);
}
} else {
// Fallback to resolving against the initial cwd, removing any relative directory prefixes