All files / providers workspace-uri-link-provider.ts

100% Statements 13/13
50% Branches 1/2
100% Functions 2/2
100% Lines 13/13

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                        9x 9x           8x 8x 8x   8x 9x 9x   9x 9x   9x 9x       8x    
import * as vscode from 'vscode';
import { container } from 'tsyringe';
import { ServiceToken } from '@src/services/tokens';
import { WorkspaceUri } from './workspace-uri';
 
/**
 * Provides document links for URIs with the 'workspace:' scheme.
 */
export class WorkspaceUriLinkProvider implements vscode.DocumentLinkProvider {
 
  constructor() {
    // Self-register with the extension context for automatic disposal
    const context = container.resolve<vscode.ExtensionContext>(ServiceToken.ExtensionContext);
    context.subscriptions.push(
      vscode.languages.registerDocumentLinkProvider({ scheme: 'file' }, this)
    );
  }
 
  provideDocumentLinks(document: vscode.TextDocument): vscode.DocumentLink[] {
    const links: vscode.DocumentLink[] = [];
    const text = document.getText();
    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]);
 
      Eif (uri) {
        links.push(new vscode.DocumentLink(range, uri));
      }
    }
 
    return links;
  }
}