All files / src/diagnostics diagnosticMarkers.ts

90.56% Statements 48/53
100% Branches 13/13
75% Functions 6/8
90.56% Lines 48/53

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 542x 2x 2x 2x 2x       2x 7x 7x 7x 6x 6x 6x 6x 1x 1x 2x 6x 6x 7x 7x 7x 7x 5x 7x 7x 7x 7x 7x 6x 6x 2x 2x 2x 2x 2x 1x 1x 2x 2x     2x 2x 37x 37x 2x 2x 2x 2x 2x  
import * as vscode from 'vscode';
import type { LinterDiagnostic } from '../types';
 
const PLUGIN_NAME = 'Code Purifier';
 
export function isMarkersEnabled(): boolean {
  return vscode.workspace.getConfiguration('vscode-code-reviewer').get<boolean>('markers.enabled', true);
}
 
function formatDiagnosticMessage(d: LinterDiagnostic): string {
  const sepIndex = d.ruleId.indexOf(':');
  if (sepIndex > 0) {
    const linter = d.ruleId.slice(0, sepIndex);
    const rule = d.ruleId.slice(sepIndex + 1);
    return `[${PLUGIN_NAME} ยท ${linter}] ${rule}: ${d.message}`;
  }
  return `[${PLUGIN_NAME}] ${d.ruleId}: ${d.message}`;
}
 
export function toVscodeDiagnostics(diagnostics: LinterDiagnostic[]): vscode.Diagnostic[] {
  return diagnostics.map(d => {
    const severity =
      d.severity === 'error'
        ? vscode.DiagnosticSeverity.Error
        : d.severity === 'warning'
          ? vscode.DiagnosticSeverity.Warning
          : vscode.DiagnosticSeverity.Information;
    const diag = new vscode.Diagnostic(d.range, formatDiagnosticMessage(d), severity);
    diag.source = PLUGIN_NAME;
    diag.code = d.ruleId;
    return diag;
  });
}
 
export class DiagnosticMarkers {
  private collection: vscode.DiagnosticCollection;
 
  constructor() {
    this.collection = vscode.languages.createDiagnosticCollection('codeReviewer');
  }
 
  apply(uri: vscode.Uri, diagnostics: LinterDiagnostic[]): void {
    this.collection.set(uri, toVscodeDiagnostics(diagnostics));
  }
 
  clear(uri: vscode.Uri): void {
    this.collection.delete(uri);
  }
 
  dispose(): void {
    this.collection.dispose();
  }
}