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

92.85% Statements 13/14
50% Branches 2/4
100% Functions 2/2
92.85% Lines 13/14

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                    5x           4x 4x       4x       4x   4x 3x 3x   3x 3x   3x 3x       4x    
import * as vscode from 'vscode';
import { InferenceUri } from '@src/providers/inference-uri';
 
/**
 * Provides document links for URIs with the 'inference:' scheme.
 */
export class InferenceUriLinkProvider 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 no
    // inference URI can occur, which is the case for almost every document.
    Iif (!text.includes(InferenceUri.queryAppendix)) {
      return links;
    }
 
    const regex = new RegExp(InferenceUri.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;
  }
}