Skip to content

Commit

Permalink
Resolve links to workspace files in terminal
Browse files Browse the repository at this point in the history
Try and provide links for workspace files in the terminal, by searching
for matching files.

Contributed on behalf of STMicroelectronics
  • Loading branch information
AlexandraBuzila committed Mar 18, 2024
1 parent 2c0c211 commit 799f702
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 7 deletions.
1 change: 1 addition & 0 deletions packages/terminal/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"@theia/process": "1.47.0",
"@theia/variable-resolver": "1.47.0",
"@theia/workspace": "1.47.0",
"@theia/file-search": "1.47.0",
"tslib": "^2.6.2",
"xterm": "^5.3.0",
"xterm-addon-fit": "^0.8.0",
Expand Down
49 changes: 42 additions & 7 deletions packages/terminal/src/browser/terminal-file-link-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@ import { FileService } from '@theia/filesystem/lib/browser/file-service';
import { TerminalWidget } from './base/terminal-widget';
import { TerminalLink, TerminalLinkProvider } from './terminal-link-provider';
import { TerminalWidgetImpl } from './terminal-widget-impl';

import { FileSearchService } from '@theia/file-search/lib/common/file-search-service';
@injectable()
export class FileLinkProvider implements TerminalLinkProvider {

@inject(OpenerService) protected readonly openerService: OpenerService;
@inject(FileService) protected fileService: FileService;
@inject(FileSearchService) private searchService: FileSearchService;

async provideLinks(line: string, terminal: TerminalWidget): Promise<TerminalLink[]> {
const links: TerminalLink[] = [];
Expand All @@ -42,6 +43,32 @@ export class FileLinkProvider implements TerminalLinkProvider {
length: match.length,
handle: () => this.open(match, terminal)
});
} else {
const cwd = await this.getCwd(terminal);
let searchTerm = await this.extractPath(match);
if (searchTerm) {
// remove any leading ./, ../ etc. as they can't be searched
searchTerm = searchTerm.replace(/^(\.+[\\/])+/, '');
// try and find a matching file in the workspace
const files = (await this.searchService.find(searchTerm, {
rootUris: [cwd.toString()],
fuzzyMatch: true,
limit: 1
}));
if (files.length) {
const fileUri = new URI(files[0]);
const valid = await this.isValidFileURI(fileUri);
if (valid) {
const position = await this.extractPosition(match);
links.push({
startIndex: regExp.lastIndex - match.length,
length: match.length,
handle: () => this.openURI(fileUri, position)
});
}

}
}
}
}
return links;
Expand All @@ -57,17 +84,22 @@ export class FileLinkProvider implements TerminalLinkProvider {
const toOpen = await this.toURI(match, await this.getCwd(terminal));
if (toOpen) {
// TODO: would be better to ask the opener service, but it returns positively even for unknown files.
try {
const stat = await this.fileService.resolve(toOpen);
return !stat.isDirectory;
} catch { }
return this.isValidFileURI(toOpen);
}
} catch (err) {
console.trace('Error validating ' + match, err);
}
return false;
}

protected async isValidFileURI(uri: URI): Promise<boolean> {
try {
const stat = await this.fileService.resolve(uri);
return !stat.isDirectory;
} catch { }
return false;
}

protected async toURI(match: string, cwd: URI): Promise<URI | undefined> {
const path = await this.extractPath(match);
if (!path) {
Expand Down Expand Up @@ -97,8 +129,11 @@ export class FileLinkProvider implements TerminalLinkProvider {
if (!toOpen) {
return;
}

const position = await this.extractPosition(match);
return this.openURI(toOpen, position);
}

async openURI(toOpen: URI, position: Position): Promise<void> {
let options = {};
if (position) {
options = { selection: { start: position } };
Expand All @@ -108,7 +143,7 @@ export class FileLinkProvider implements TerminalLinkProvider {
const opener = await this.openerService.getOpener(toOpen, options);
opener.open(toOpen, options);
} catch (err) {
console.error('Cannot open link ' + match, err);
console.error('Cannot open link ' + toOpen, err);
}
}

Expand Down
3 changes: 3 additions & 0 deletions packages/terminal/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
{
"path": "../editor"
},
{
"path": "../file-search"
},
{
"path": "../filesystem"
},
Expand Down

0 comments on commit 799f702

Please sign in to comment.