Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: improve terminal local link parser #4302

Merged
merged 7 commits into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/e2e.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ jobs:
- name: Checkout
uses: actions/checkout@v4

- name: Install libsecret-1
run: sudo apt-get update && sudo apt-get install -y libsecret-1-dev

- name: Use Node.js "20.x"
uses: actions/setup-node@v4
with:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,4 +286,16 @@ describe('Workbench - TerminalValidatedLocalLinkProvider', () => {
},
]);
});

test('should support single file path', async () => {
await assertLink('foo', false, [
{
range: [
[0, 1],
[3, 1],
],
text: 'foo',
},
]);
});
});
7 changes: 1 addition & 6 deletions packages/terminal-next/src/browser/links/link-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -375,13 +375,8 @@ export class TerminalLinkManager extends Disposable {
return undefined;
}

const linkUrl = this.extractLinkUrl(preprocessedLink);
if (!linkUrl) {
return undefined;
}

try {
const uri = URI.file(linkUrl);
const uri = URI.file(preprocessedLink);
const stat = await this._fileService.getFileStat(uri.toString());
if (stat) {
return { uri, isDirectory: stat.isDirectory };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,6 @@ export class TerminalValidatedLocalLinkProvider extends TerminalBaseLinkProvider
let match;
let stringIndex = -1;
while ((match = rex.exec(text)) !== null) {
// const link = match[typeof matcher.matchIndex !== 'number' ? 0 : matcher.matchIndex];
let link = match[0];
if (!link) {
// something matched but does not comply with the given matchIndex
Expand Down Expand Up @@ -169,60 +168,81 @@ export class TerminalValidatedLocalLinkProvider extends TerminalBaseLinkProvider
link = link.substring(2);
stringIndex += 2;
}
const validatedLinks = await this.detectLocalLink(link, lines, startLine, stringIndex, 1);

// Convert the link text's string index into a wrapped buffer range
const bufferRange = convertLinkRangeToBuffer(
lines,
this._xterm.cols,
{
startColumn: stringIndex + 1,
startLineNumber: 1,
endColumn: stringIndex + link.length + 1,
endLineNumber: 1,
},
startLine,
);

const validatedLink = await new Promise<TerminalLink | undefined>((r) => {
this._validationCallback(link, async (result) => {
if (result) {
const label = result.isDirectory
? (await this._isDirectoryInsideWorkspace(result.uri))
? FOLDER_IN_WORKSPACE_LABEL
: FOLDER_NOT_IN_WORKSPACE_LABEL
: OPEN_FILE_LABEL;
const activateCallback = this._wrapLinkHandler((event: MouseEvent | undefined, text: string) => {
if (result.isDirectory) {
this._handleLocalFolderLink(result.uri);
} else {
this._activateFileCallback(event, text);
}
});
r(
this.injector.get(TerminalLink, [
this._xterm,
bufferRange,
link,
this._xterm.buffer.active.viewportY,
activateCallback,
this._tooltipCallback,
true,
label,
]),
);
} else {
r(undefined);
}
});
});
if (validatedLink) {
result.push(validatedLink);
if (validatedLinks.length > 0) {
result.push(...validatedLinks);
}
}

if (result.length === 0) {
const validatedLinks = await this.detectLocalLink(text, lines, startLine, stringIndex, 2);
if (validatedLinks.length > 0) {
result.push(...validatedLinks);
}
}

return result;
}

private async detectLocalLink(
text: string,
bufferLines: IBufferLine[],
startLine: number,
stringIndex: number,
offset,
) {
const result: TerminalLink[] = [];
const validatedLink = await new Promise<TerminalLink | undefined>((r) => {
this._validationCallback(text, async (result) => {
if (result) {
const label = result.isDirectory
? (await this._isDirectoryInsideWorkspace(result.uri))
? FOLDER_IN_WORKSPACE_LABEL
: FOLDER_NOT_IN_WORKSPACE_LABEL
: OPEN_FILE_LABEL;
const activateCallback = this._wrapLinkHandler((event: MouseEvent | undefined, text: string) => {
if (result.isDirectory) {
this._handleLocalFolderLink(result.uri);
} else {
this._activateFileCallback(event, text);
}
});
// Convert the link text's string index into a wrapped buffer range
const bufferRange = convertLinkRangeToBuffer(
bufferLines,
this._xterm.cols,
{
startColumn: stringIndex + 1,
startLineNumber: 1,
endColumn: stringIndex + text.length + offset,
endLineNumber: 1,
},
startLine,
);
r(
this.injector.get(TerminalLink, [
this._xterm,
bufferRange,
text,
this._xterm.buffer.active.viewportY,
activateCallback,
this._tooltipCallback,
true,
label,
]),
);
} else {
r(undefined);
}
});
});
if (validatedLink) {
result.push(validatedLink);
}
return result;
}

protected get _localLinkRegex(): RegExp {
const baseLocalLinkClause = this._client.os === OperatingSystem.Windows ? winLocalLinkClause : unixLocalLinkClause;
// Append line and column number regex
Expand Down
Loading