Skip to content

Commit 4fb8426

Browse files
committed
Consider all links when common directories are removed
Fixes microsoft#164687
1 parent dc83ca1 commit 4fb8426

3 files changed

Lines changed: 11 additions & 6 deletions

File tree

src/vs/workbench/contrib/terminal/browser/links/terminalLinkHelpers.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -156,27 +156,32 @@ export function positionIsInRange(position: IBufferCellPosition, range: IBufferR
156156
* For shells with the CommandDetection capability, the cwd for a command relative to the line of
157157
* the particular link can be used to narrow down the result for an exact file match.
158158
*/
159-
export function updateLinkWithRelativeCwd(capabilities: ITerminalCapabilityStore, y: number, text: string, pathSeparator: string): string | undefined {
159+
export function updateLinkWithRelativeCwd(capabilities: ITerminalCapabilityStore, y: number, text: string, pathSeparator: string): string[] | undefined {
160160
const cwd = capabilities.get(TerminalCapability.CommandDetection)?.getCwdForLine(y);
161161
if (!cwd) {
162162
return undefined;
163163
}
164+
const result: string[] = [];
164165
if (!text.includes(pathSeparator)) {
165-
text = cwd + pathSeparator + text;
166+
result.push(cwd + pathSeparator + text);
166167
} else {
167168
let commonDirs = 0;
168169
let i = 0;
169170
const cwdPath = cwd.split(pathSeparator).reverse();
170171
const linkPath = text.split(pathSeparator);
172+
// Get all results as candidates, prioritizing the link with the most common directories.
173+
// For example if in the directory /home/common and the link is common/file, the result
174+
// should be: `['/home/common/common/file', '/home/common/file']`. The first is the most
175+
// likely as cwd detection is active.
171176
while (i < cwdPath.length) {
177+
result.push(cwd + pathSeparator + linkPath.slice(commonDirs).join(pathSeparator));
172178
if (cwdPath[i] === linkPath[i]) {
173179
commonDirs++;
174180
}
175181
i++;
176182
}
177-
text = cwd + pathSeparator + linkPath.slice(commonDirs).join(pathSeparator);
178183
}
179-
return text;
184+
return result;
180185
}
181186

182187
export function osPathModule(os: OperatingSystem): IPath {

src/vs/workbench/contrib/terminal/browser/links/terminalLinkOpeners.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ export class TerminalSearchLinkOpener implements ITerminalLinkOpener {
173173
});
174174
let cwdResolvedText = text;
175175
if (this._capabilities.has(TerminalCapability.CommandDetection)) {
176-
cwdResolvedText = updateLinkWithRelativeCwd(this._capabilities, link.bufferRange.start.y, text, pathSeparator) || text;
176+
cwdResolvedText = updateLinkWithRelativeCwd(this._capabilities, link.bufferRange.start.y, text, pathSeparator)?.[0] || text;
177177
}
178178

179179
// Try open the cwd resolved link first

src/vs/workbench/contrib/terminal/browser/links/terminalLocalLinkDetector.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ export class TerminalLocalLinkDetector implements ITerminalLinkDetector {
157157
// not exist. Doing otherwise could cause unexpected results where handling via the
158158
// word link detector is preferable.
159159
if (absolutePath) {
160-
linkCandidates.push(absolutePath);
160+
linkCandidates.push(...absolutePath);
161161
}
162162
} else {
163163
// Fallback to resolving against the initial cwd, removing any relative directory prefixes

0 commit comments

Comments
 (0)