All files / src/views codeLensProvider.ts

30.35% Statements 17/56
100% Branches 1/1
25% Functions 1/4
30.35% Lines 17/56

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 571x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x     1x 1x                                                           1x 1x                 1x  
import * as vscode from 'vscode';
import { getMethodSymbols } from '../scope/method-extractor';
import { ReviewStatusCache, type ReviewStatus } from '../scope/status-cache';
import { t } from '../i18n/messages';
 
export class MethodCodeLensProvider implements vscode.CodeLensProvider {
  private _onDidChangeCodeLenses: vscode.EventEmitter<void> = new vscode.EventEmitter<void>();
  readonly onDidChangeCodeLenses: vscode.Event<void> = this._onDidChangeCodeLenses.event;
 
  constructor(private statusCache: ReviewStatusCache) {}
 
  refresh(): void {
    this._onDidChangeCodeLenses.fire();
  }
 
  async provideCodeLenses(
    document: vscode.TextDocument,
    token: vscode.CancellationToken
  ): Promise<vscode.CodeLens[]> {
    const config = vscode.workspace.getConfiguration('vscode-code-reviewer');
    const enabled = config.get<boolean>('codelens.enabled', true);
    if (!enabled) { return []; }

    const languages = config.get<string[]>('codelens.languages', [
      'typescript', 'javascript', 'java', 'python'
    ]);
    if (!languages.includes(document.languageId)) { return []; }

    const symbols = await getMethodSymbols(document);
    if (symbols.length === 0) { return []; }
    if (symbols.length > 50) { return []; }

    const lenses: vscode.CodeLens[] = [];
    for (const symbol of symbols) {
      const status = this.statusCache.get(document.uri, symbol.name);
      const title = this.buildLensTitle(status);
      const line = symbol.range.start.line;
      lenses.push(new vscode.CodeLens(new vscode.Range(line, 0, line, 0), {
        command: 'codeReviewer.reviewMethod',
        title,
        arguments: [symbol.range],
      }));
    }
    return lenses;
  }
 
  private buildLensTitle(status: ReviewStatus | null): string {
    if (!status) {
      return t('codelens.reviewMethod');
    }
    if (status.issueCount === 0) {
      return t('codelens.reviewedClean');
    }
    return t('codelens.reviewedWithIssues', { 0: String(status.issueCount) });
  }
}