All files / src/fix fixSession.ts

91.6% Statements 120/131
76.19% Branches 16/21
100% Functions 10/10
91.6% Lines 120/131

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 1322x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 3x 3x 3x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x     1x 1x   1x                 2x 2x 2x 2x 2x 6x 6x 2x 2x 3x 3x 2x 2x 3x 3x 2x 2x 3x 3x 3x 5x 5x 3x 3x 2x 2x 12x 12x 2x 2x 12x 2x 2x 3x 3x 3x 3x 1x 3x 2x 2x 2x 2x 2x 2x 2x 2x 3x 3x 2x 2x 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 2x  
import * as vscode from 'vscode';
import type { AppliedFix } from './fixEngine';
 
export interface FixedEntry {
  key: string;
  ruleId: string;
  line: number;
  fixes: AppliedFix[];
  source: 'linter' | 'custom' | 'ai';
}
 
function keyOf(ruleId: string, line: number): string {
  return `${ruleId}@${line}`;
}
 
interface LocatedEdit {
  start: number;
  end: number;
  text: string;
}
 
function locateNewText(document: vscode.TextDocument, fix: AppliedFix): number {
  const text = document.getText();
  const lines = text.split('\n');
  let index = -1;
 
  if (fix.newText !== '') {
    const firstLineOfNew = fix.newText.split('\n')[0];
    if (fix.line >= 0 && fix.line < lines.length && lines[fix.line].includes(firstLineOfNew)) {
      const offset = document.offsetAt(new vscode.Position(fix.line, 0));
      index = text.indexOf(fix.newText, offset);
    }
    if (index === -1) {
      index = text.indexOf(fix.newText);
    }
    return index;
  }

  if (fix.line >= 0 && fix.line < lines.length) {
    const offset = document.offsetAt(new vscode.Position(fix.line, 0));
    const lineEnd = text.indexOf('\n', offset);
    const end = lineEnd === -1 ? text.length : lineEnd;
    index = offset + lines[fix.line].search(/\S|$/);
    if (index > end) { index = offset; }
  }
  return index;
}
 
export class FixSessionManager {
  private fixedEntries = new Map<string, FixedEntry>();
 
  add(uri: vscode.Uri, entry: FixedEntry): void {
    this.fixedEntries.set(uri.toString() + '|' + entry.key, entry);
  }
 
  get(uri: vscode.Uri, key: string): FixedEntry | undefined {
    return this.fixedEntries.get(uri.toString() + '|' + key);
  }
 
  has(uri: vscode.Uri, key: string): boolean {
    return this.fixedEntries.has(uri.toString() + '|' + key);
  }
 
  getEntries(uri: vscode.Uri): FixedEntry[] {
    const prefix = uri.toString() + '|';
    const result: FixedEntry[] = [];
    for (const [k, v] of this.fixedEntries) {
      if (k.startsWith(prefix)) { result.push(v); }
    }
    return result;
  }
 
  clear(uri: vscode.Uri): void {
    const prefix = uri.toString() + '|';
    for (const k of this.fixedEntries.keys()) {
      if (k.startsWith(prefix)) { this.fixedEntries.delete(k); }
    }
  }
 
  recordFixes(uri: vscode.Uri, ruleId: string, line: number, fixes: AppliedFix[], source: 'linter' | 'custom' | 'ai' = 'linter'): string {
    const key = keyOf(ruleId, line);
    const fullKey = uri.toString() + '|' + key;
    const existing = this.fixedEntries.get(fullKey);
    if (existing) {
      existing.fixes.push(...fixes);
    } else {
      this.fixedEntries.set(fullKey, {
        key,
        ruleId,
        line,
        fixes: [...fixes],
        source,
      });
    }
    return key;
  }
 
  async undo(document: vscode.TextDocument, key: string): Promise<boolean> {
    const entry = this.fixedEntries.get(document.uri.toString() + '|' + key);
    if (!entry || entry.fixes.length === 0) { return false; }
 
    const edits: LocatedEdit[] = [];
    for (let i = entry.fixes.length - 1; i >= 0; i--) {
      const fix = entry.fixes[i];
      const index = locateNewText(document, fix);
      if (index === -1) { return false; }
      edits.push({
        start: index,
        end: index + fix.newText.length,
        text: fix.originalText,
      });
    }
 
    const workspaceEdit = new vscode.WorkspaceEdit();
    for (const e of edits) {
      workspaceEdit.replace(
        document.uri,
        new vscode.Range(
          document.positionAt(e.start),
          document.positionAt(e.end)
        ),
        e.text
      );
    }
    const applied = await vscode.workspace.applyEdit(workspaceEdit);
    if (applied) {
      this.fixedEntries.delete(document.uri.toString() + '|' + key);
    }
    return applied;
  }
}