All files / src/fix codeActionProvider.ts

18.6% Statements 8/43
100% Branches 2/2
66.66% Functions 2/3
18.6% Lines 8/43

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 441x 1x 1x 1x 1x 1x 1x                                                                       1x  
import * as vscode from 'vscode';
import type { Orchestrator } from '../orchestrator/orchestrator';
 
export class FixCodeActionProvider implements vscode.CodeActionProvider {
  constructor(private orchestrator: Orchestrator) {}
 
  provideCodeActions(
    document: vscode.TextDocument,
    _range: vscode.Range,
    context: vscode.CodeActionContext,
    _token: vscode.CancellationToken
  ): vscode.CodeAction[] {
    const cached = this.orchestrator.getAnalysisResult(document.uri);
    if (!cached) { return []; }

    const actions: vscode.CodeAction[] = [];
    for (const diag of cached.diagnostics) {
      if (!diag.fix) { continue; }
      const overlapsContext = context.diagnostics.some(d =>
        diag.range.intersection(d.range)
      );
      if (!overlapsContext) { continue; }

      const action = new vscode.CodeAction(
        `Code Purifier: 修复 ${diag.ruleId}`,
        vscode.CodeActionKind.QuickFix
      );
      action.command = {
        command: 'codeReviewer.fixIssue',
        title: '修复',
        arguments: [{
          line: diag.range.start.line,
          ruleId: diag.ruleId,
          source: 'linter',
          origin: 'hover',
        }],
      };
      action.diagnostics = [...context.diagnostics];
      actions.push(action);
    }
    return actions;
  }
}