All files / vscode-extension/src languageProviders.ts

100% Statements 62/62
95% Branches 19/20
100% Functions 16/16
100% Lines 54/54

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 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132          1x         1x         51x 1x 24x       12x 12x 16x 16x 16x 12x 12x 12x   12x   12x 12x 9x 8x 1x 1x   7x     7x         7x 7x 7x       12x 12x   12x         4x 4x 4x     1x 1x         1x 1x 1x 1x 1x     5x 3x 3x   1x     5x 4x 4x 3x               2x 44x 44x                         4x 3x 3x 3x                           12x    
import * as vscode from 'vscode';
import type { PhotometrySourceDescription } from '@ies-tools/core';
import { analyze, fieldAt, hoverMarkdown, lensSummary, outline, toProblems, type Span } from './languageFeatures.js';
 
/** Works whether or not the `ies`/`eulumdat` language IDs are registered for a file. */
const selector: vscode.DocumentSelector = [
  { language: 'ies' },
  { language: 'eulumdat' },
  { pattern: '**/*.{ies,IES,ldt,LDT}' },
];
const severities = {
  error: vscode.DiagnosticSeverity.Error,
  warning: vscode.DiagnosticSeverity.Warning,
  info: vscode.DiagnosticSeverity.Information,
};
const toRange = (span: Span) => new vscode.Range(span.startLine, span.startCharacter, span.endLine, span.endCharacter);
const setting = (name: string, fallback: boolean) =>
  vscode.workspace.getConfiguration('iesTools').get<boolean>(name, fallback);
 
/** Problems, hover, outline and CodeLens for photometric text documents. Read-only; safe in untrusted workspaces. */
export function registerLanguageFeatures(context: vscode.ExtensionContext): void {
  const cache = new Map<string, { version: number; result: PhotometrySourceDescription }>();
  const analysis = (document: vscode.TextDocument): PhotometrySourceDescription => {
    const key = document.uri.toString();
    const cached = cache.get(key);
    if (cached?.version === document.version) return cached.result;
    const result = analyze(document.getText(), document.uri.path, document.languageId);
    cache.set(key, { version: document.version, result });
    return result;
  };
  const matches = (document: vscode.TextDocument) => vscode.languages.match(selector, document) > 0;
 
  const collection = vscode.languages.createDiagnosticCollection('ies-tools');
  const validate = (document: vscode.TextDocument): void => {
    if (!matches(document)) return;
    if (!setting('validation.enabled', true)) {
      collection.delete(document.uri);
      return;
    }
    collection.set(
      document.uri,
      toProblems(analysis(document).diagnostics, setting('validation.strict', false)).map((problem) => {
        const diagnostic = new vscode.Diagnostic(
          problem.span ? toRange(problem.span) : document.lineAt(0).range,
          problem.message,
          severities[problem.severity],
        );
        diagnostic.source = 'ies-tools';
        diagnostic.code = problem.code;
        return diagnostic;
      }),
    );
  };
  const timers = new Map<string, ReturnType<typeof setTimeout>>();
  const lensesChanged = new vscode.EventEmitter<void>();
 
  context.subscriptions.push(
    collection,
    lensesChanged,
    vscode.workspace.onDidOpenTextDocument(validate),
    vscode.workspace.onDidChangeTextDocument(({ document }) => {
      const key = document.uri.toString();
      clearTimeout(timers.get(key));
      timers.set(
        key,
        setTimeout(() => {
          timers.delete(key);
          validate(document);
        }, 250),
      );
    }),
    vscode.workspace.onDidCloseTextDocument((document) => {
      const key = document.uri.toString();
      clearTimeout(timers.get(key));
      timers.delete(key);
      cache.delete(key);
      collection.delete(document.uri);
    }),
    vscode.workspace.onDidChangeConfiguration((event) => {
      if (!event.affectsConfiguration('iesTools')) return;
      vscode.workspace.textDocuments.forEach(validate);
      lensesChanged.fire();
    }),
    { dispose: () => timers.forEach(clearTimeout) },
    vscode.languages.registerHoverProvider(selector, {
      provideHover(document, position) {
        if (!setting('hover.enabled', true)) return undefined;
        const field = fieldAt(analysis(document).fields, position.line, position.character);
        if (!field) return undefined;
        return new vscode.Hover(
          new vscode.MarkdownString(hoverMarkdown(field)),
          new vscode.Range(field.line, field.start, field.line, field.end),
        );
      },
    }),
    vscode.languages.registerDocumentSymbolProvider(selector, {
      provideDocumentSymbols(document) {
        return outline(analysis(document).fields).map((entry) => {
          const range = toRange(entry.span);
          return new vscode.DocumentSymbol(
            entry.name,
            entry.detail,
            entry.array ? vscode.SymbolKind.Array : vscode.SymbolKind.Field,
            range,
            range,
          );
        });
      },
    }),
    vscode.languages.registerCodeLensProvider(selector, {
      onDidChangeCodeLenses: lensesChanged.event,
      provideCodeLenses(document) {
        if (!setting('codeLens.enabled', true)) return [];
        const range = new vscode.Range(0, 0, 0, 0);
        const summary = lensSummary(analysis(document));
        return [
          new vscode.CodeLens(range, {
            title: summary.title,
            command: summary.problems ? 'workbench.actions.view.problems' : '',
          }),
          new vscode.CodeLens(range, {
            title: 'Open Light Preview',
            command: 'ies.openPreview',
            arguments: [document.uri],
          }),
        ];
      },
    }),
  );
  vscode.workspace.textDocuments.forEach(validate);
}