From 81356cbe42d6a4be284a7935064d0b1978dc1e69 Mon Sep 17 00:00:00 2001 From: Daniel Imms <2193314+Tyriar@users.noreply.github.com> Date: Fri, 24 Jun 2022 12:42:40 -0700 Subject: [PATCH] Match word links without the cwd if the cwd resolved text wasn't found (#153141) --- .../browser/links/terminalLinkOpeners.ts | 58 ++++++++++++------- 1 file changed, 38 insertions(+), 20 deletions(-) diff --git a/src/vs/workbench/contrib/terminal/browser/links/terminalLinkOpeners.ts b/src/vs/workbench/contrib/terminal/browser/links/terminalLinkOpeners.ts index 835b57f934e..bfc044ba133 100644 --- a/src/vs/workbench/contrib/terminal/browser/links/terminalLinkOpeners.ts +++ b/src/vs/workbench/contrib/terminal/browser/links/terminalLinkOpeners.ts @@ -154,30 +154,24 @@ export class TerminalSearchLinkOpener implements ITerminalLinkOpener { return; } }); - let matchLink = text; + let cwdResolvedText = text; if (this._capabilities.has(TerminalCapability.CommandDetection)) { - matchLink = updateLinkWithRelativeCwd(this._capabilities, link.bufferRange.start.y, text, pathSeparator) || text; + cwdResolvedText = updateLinkWithRelativeCwd(this._capabilities, link.bufferRange.start.y, text, pathSeparator) || text; } - const sanitizedLink = matchLink.replace(/:\d+(:\d+)?$/, ''); - try { - const result = await this._getExactMatch(sanitizedLink); - if (result) { - const { uri, isDirectory } = result; - const linkToOpen = { - // Use the absolute URI's path here so the optional line/col get detected - text: result.uri.fsPath + (matchLink.match(/:\d+(:\d+)?$/)?.[0] || ''), - uri, - bufferRange: link.bufferRange, - type: link.type - }; - if (uri) { - return isDirectory ? this._localFolderInWorkspaceOpener.open(linkToOpen) : this._localFileOpener.open(linkToOpen); - } + + // Try open the cwd resolved link first + if (await this._tryOpenExactLink(cwdResolvedText, link)) { + return; + } + + // If the cwd resolved text didn't match, try find the link without the cwd resolved, for + // example when a command prints paths in a sub-directory of the current cwd + if (text !== cwdResolvedText) { + if (await this._tryOpenExactLink(text, link)) { + return; } - } catch { - // Fallback to searching quick access - return this._quickInputService.quickAccess.show(text); } + // Fallback to searching quick access return this._quickInputService.quickAccess.show(text); } @@ -221,6 +215,30 @@ export class TerminalSearchLinkOpener implements ITerminalLinkOpener { } return resourceMatch; } + + private async _tryOpenExactLink(text: string, link: ITerminalSimpleLink): Promise { + const sanitizedLink = text.replace(/:\d+(:\d+)?$/, ''); + try { + const result = await this._getExactMatch(sanitizedLink); + if (result) { + const { uri, isDirectory } = result; + const linkToOpen = { + // Use the absolute URI's path here so the optional line/col get detected + text: result.uri.fsPath + (text.match(/:\d+(:\d+)?$/)?.[0] || ''), + uri, + bufferRange: link.bufferRange, + type: link.type + }; + if (uri) { + await (isDirectory ? this._localFolderInWorkspaceOpener.open(linkToOpen) : this._localFileOpener.open(linkToOpen)); + return true; + } + } + } catch { + return false; + } + return false; + } } interface IResourceMatch {