Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | 10x 9x 9x 9x 1x 8x 8x 11x 11x 11x 11x 11x 10x 8x | import * as vscode from 'vscode';
import { WorkspaceUri } from './workspace-uri';
/**
* Provides document links for URIs with the 'workspace:' scheme.
*/
export class WorkspaceUriLinkProvider implements vscode.DocumentLinkProvider {
constructor(context: vscode.ExtensionContext) {
// Self-register with the extension context for automatic disposal
context.subscriptions.push(
vscode.languages.registerDocumentLinkProvider({ scheme: 'file' }, this)
);
}
provideDocumentLinks(document: vscode.TextDocument): vscode.DocumentLink[] {
const links: vscode.DocumentLink[] = [];
const text = document.getText();
// The provider runs for every file in every language; skip the regex scan when the
// scheme does not occur at all, which is the case for almost every document.
if (!text.includes(`${WorkspaceUri.uriScheme}://`)) {
return links;
}
const regex = new RegExp(WorkspaceUri.uriRegex, 'g');
for (const match of text.matchAll(regex)) {
const start = document.positionAt(match.index);
const end = document.positionAt(match.index + match[0].length);
const range = new vscode.Range(start, end);
const uri = vscode.Uri.parse(match[0]);
// Only offer the link if it resolves to a file inside the workspace root. This stops a
// crafted document from turning `workspace:///../../etc/passwd` into a clickable link that
// would read (or, on save, write) a file outside the workspace.
if (uri && WorkspaceUri.tryToFileUri(uri)) {
links.push(new vscode.DocumentLink(range, uri));
}
}
return links;
}
} |