All files / src extension.ts

76.38% Statements 110/144
46.66% Branches 7/15
80% Functions 4/5
76.38% Lines 110/144

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 133 134 135 136 137 138 139 140 141 142 143 144 1451x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x                           1x 1x 1x 26x 26x                       1x 1x 1x 1x 1x 1x 1x     1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 11x 11x 11x 11x 1x 1x 1x 1x 1x 31x   1x 1x 1x 1x 1x 26x 26x 1x 1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x         1x 1x 1x 1x 1x 1x     1x 1x 1x 1x  
import * as vscode from 'vscode';
import { Orchestrator } from './orchestrator/orchestrator';
import { registerCommands } from './activation/commands';
import { SetupViewProvider } from './views/setupView';
import { setLanguage, t, type Language } from './i18n/messages';
import { getAIOutputLanguage } from './config';
import { ReviewStatusCache } from './scope/status-cache';
import { MethodCodeLensProvider } from './views/codeLensProvider';
import { DiagnosticMarkers, isMarkersEnabled } from './diagnostics/diagnosticMarkers';
import { FixCodeActionProvider } from './fix/codeActionProvider';
import { FixSessionManager } from './fix/fixSession';
import { FixPendingStore } from './fix/fixPending';
import { registerFixPreviewProvider } from './fix/fixPreview';
 
let orchestrator: Orchestrator;
let markers: DiagnosticMarkers;
 
async function runStaticAndApply(document: vscode.TextDocument): Promise<void> {
  try {
    const version = document.version;
    const workspaceRoot = vscode.workspace.workspaceFolders?.[0]?.uri.fsPath ?? '';
    const workingDir = workspaceRoot || vscode.Uri.joinPath(document.uri, '..').fsPath;
    const result = await orchestrator.runStaticAnalysis(document, workingDir);
    const current = vscode.workspace.textDocuments.find(d => d.uri.toString() === document.uri.toString());
    if (!current || current.version !== version) { return; }
    markers.apply(document.uri, result.diagnostics);
  } catch (err) {
    console.error('[code-reviewer] static analysis failed:', err);
  }
}
 
const analysisTimers = new Map<string, NodeJS.Timeout>();
 
function scheduleAnalysis(document: vscode.TextDocument, delay: number): void {
  if (document.uri.scheme !== 'file' || !isMarkersEnabled()) { return; }
  const key = document.uri.toString();
  const existing = analysisTimers.get(key);
  if (existing) { clearTimeout(existing); }

  const timer = setTimeout(async () => {
    analysisTimers.delete(key);
    await runStaticAndApply(document);
  }, delay);

  analysisTimers.set(key, timer);
}
 
async function analyzeOpenDocuments(): Promise<void> {
  const docs = vscode.workspace.textDocuments;
  const active = vscode.window.activeTextEditor?.document;
  const ordered = [...docs].sort((a, b) => a === active ? -1 : b === active ? 1 : 0);
  for (const doc of ordered) {
    if (doc.uri.scheme !== 'file' || !isMarkersEnabled()) { continue; }
    await runStaticAndApply(doc);
  }
}
 
export function activate(context: vscode.ExtensionContext) {
  const lang = getAIOutputLanguage() as Language;
  setLanguage(lang);
  console.log(t('extension.activated'));
 
  orchestrator = new Orchestrator();
  markers = new DiagnosticMarkers();
  context.subscriptions.push(markers);
 
  const setupProvider = new SetupViewProvider(context);
  context.subscriptions.push(
    vscode.window.registerWebviewViewProvider('codeReviewer.setupView', setupProvider)
  );
 
  const statusCache = new ReviewStatusCache();
  const codeLensProvider = new MethodCodeLensProvider(statusCache);
 
  const fixSession = new FixSessionManager();
  const pendingStore = new FixPendingStore();
  registerFixPreviewProvider(context);
 
  context.subscriptions.push(
    vscode.languages.registerCodeActionsProvider(
      { scheme: 'file' },
      new FixCodeActionProvider(orchestrator),
      { providedCodeActionKinds: [vscode.CodeActionKind.QuickFix] }
    )
  );
 
  void analyzeOpenDocuments();
 
  context.subscriptions.push(
    vscode.languages.registerCodeLensProvider(
      { scheme: 'file' },
      codeLensProvider
    )
  );
 
  context.subscriptions.push(
    vscode.workspace.onDidCloseTextDocument((document) => {
      statusCache.clearDocument(document.uri);
      markers.clear(document.uri);
      fixSession.clear(document.uri);
      pendingStore.clear(document.fileName);
    })
  );
 
  context.subscriptions.push(
    vscode.workspace.onDidOpenTextDocument((document) => {
      if (document.uri.scheme !== 'file' || !isMarkersEnabled()) { return; }
      void runStaticAndApply(document);
    })
  );
 
  context.subscriptions.push(
    vscode.workspace.onDidChangeTextDocument((event) => {
      markers.clear(event.document.uri);
      scheduleAnalysis(event.document, 1000);
    })
  );
 
  registerCommands(context, orchestrator, codeLensProvider, statusCache, markers, fixSession, pendingStore);
 
  context.subscriptions.push(
    vscode.workspace.onDidSaveTextDocument((document) => {
      scheduleAnalysis(document, 500);
    })
  );
 
  context.subscriptions.push(
    vscode.workspace.onDidChangeConfiguration(e => {
      if (e.affectsConfiguration('vscode-code-reviewer.ai.outputLanguage')) {
        const newLang = getAIOutputLanguage() as Language;
        setLanguage(newLang);
      }
    })
  );
}
 
export function deactivate() {
  for (const timer of analysisTimers.values()) {
    clearTimeout(timer);
  }
  analysisTimers.clear();
  markers?.dispose();
  orchestrator = undefined!;
}