All files / vscode-extension/src languageFeatures.ts

100% Statements 46/46
96.15% Branches 50/52
100% Functions 13/13
100% Lines 35/35

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                                                                    24x 24x     8x                 10x                   10x 10x 10x 25x 25x 25x 13x 6x   4x     8x     4x         3x 3x 66x 66x 66x 66x 66x 66x           66x   3x     3x       10x 10x 7x 7x 15x 14x   7x 7x               10x              
import {
  describePhotometrySource,
  inspectParsedPhotometry,
  inputFormat,
  type PhotometryDiagnostic,
  type PhotometrySourceDescription,
  type SourceField,
} from '@ies-tools/core';
 
/** Editor-neutral mapping from core results to what the VS Code providers display. No `vscode` import. */
export interface Span {
  startLine: number;
  startCharacter: number;
  endLine: number;
  endCharacter: number;
}
 
export interface Problem {
  /** Null when core has no location; the caller uses the first line. */
  span: Span | null;
  severity: 'error' | 'warning' | 'info';
  message: string;
  code: string;
}
 
export interface OutlineEntry {
  name: string;
  detail: string;
  span: Span;
  /** True when the entry collapses a repeated array such as candela values. */
  array: boolean;
}
 
export function analyze(text: string, path: string, languageId?: string): PhotometrySourceDescription {
  const hint = languageId === 'eulumdat' ? 'ldt' : languageId === 'ies' ? 'ies' : undefined;
  return describePhotometrySource(text, inputFormat(text, path, hint));
}
 
const span = (range: { line: number; start: number; end: number }): Span => ({
  startLine: range.line,
  startCharacter: range.start,
  endLine: range.line,
  endCharacter: range.end,
});
 
/** Strict mode reports warnings as errors, matching the preview's strict validation. */
export function toProblems(diagnostics: PhotometryDiagnostic[], strict = false): Problem[] {
  return diagnostics.map((diagnostic) => ({
    span: diagnostic.range ? span(diagnostic.range) : null,
    severity: strict && diagnostic.severity === 'warning' ? 'error' : diagnostic.severity,
    message: diagnostic.message,
    code: diagnostic.code,
  }));
}
 
/** Fields are in document order, so a binary search finds the one under the cursor. */
export function fieldAt(fields: SourceField[], line: number, character: number): SourceField | undefined {
  let lo = 0;
  let hi = fields.length - 1;
  while (lo <= hi) {
    const mid = (lo + hi) >>> 1;
    const field = fields[mid]!;
    if (field.line < line || (field.line === line && field.end <= character)) lo = mid + 1;
    else if (field.line > line || field.start > character) hi = mid - 1;
    else return field;
  }
  return undefined;
}
 
const escapeMarkdown = (text: string) => text.replace(/[\\`*_{}[\]()#+\-.!|<>]/g, '\\$&');
 
export function hoverMarkdown(field: SourceField): string {
  return `**${escapeMarkdown(field.name)}**\n\n${escapeMarkdown(field.description)}`;
}
 
/** One entry per header field; consecutive fields with the same name collapse into "Name (count)". */
export function outline(fields: SourceField[]): OutlineEntry[] {
  const entries: OutlineEntry[] = [];
  for (let i = 0; i < fields.length;) {
    const first = fields[i]!;
    let j = i + 1;
    while (j < fields.length && fields[j]!.name === first.name) j++;
    const last = fields[j - 1]!;
    const count = j - i;
    entries.push({
      name: count > 1 ? `${first.name} (${count})` : first.name,
      detail: count > 1 ? '' : first.description,
      span: { startLine: first.line, startCharacter: first.start, endLine: last.line, endCharacter: last.end },
      array: count > 1,
    });
    i = j;
  }
  return entries;
}
 
const plural = (count: number, word: string) => `${count} ${word}${count === 1 ? '' : 's'}`;
 
/** Title of the line-0 summary lens, and whether it should link to the Problems panel. */
export function lensSummary(result: PhotometrySourceDescription): { title: string; problems: boolean } {
  const errors = result.diagnostics.filter((item) => item.severity === 'error').length;
  if (errors || !result.profile) return { title: plural(Math.max(errors, 1), 'error'), problems: true };
  const inspection = inspectParsedPhotometry(result.profile);
  const beam = (plane: number) => {
    const value = inspection.beamAngles.find((item) => item.horizontalDegrees === plane)?.beamAngleDegrees;
    return value == null ? null : `${value.toFixed(1)}°`;
  };
  const [c0, c90] = [beam(0), beam(90)];
  const parts = [
    inspection.integratedFluxLumens == null ? null : `Φ ${Math.round(inspection.integratedFluxLumens)} lm`,
    inspection.peakCandela == null ? null : `Imax ${Math.round(inspection.peakCandela)} cd`,
    c0 || c90 ? `beam ${c0 ?? '–'}/${c90 ?? '–'}` : null,
    inspection.luminousEfficacyLumensPerWatt == null
      ? null
      : `${inspection.luminousEfficacyLumensPerWatt.toFixed(1)} lm/W`,
  ].filter(Boolean);
  return {
    title: parts.length
      ? parts.join(' · ')
      : (inspection.metricsUnsupportedReason ?? 'Photometric summary unavailable'),
    problems: false,
  };
}