# Enhanced Edit Extension for Pi

Replaces the built-in `edit` and `write` tools with enhanced versions featuring 9-level fuzzy matching and automatic LSP diagnostics injection. Also adds a `multi_edit` tool for atomic batch editing.

## Features

### 9-Level Fuzzy Matching

When the exact `oldText` isn't found, the extension tries progressively fuzzier matching strategies:

1. **SimpleReplacer** - Exact match
2. **LineTrimmedReplacer** - Ignores trailing whitespace per line
3. **BlockAnchorReplacer** - Uses first/last lines as anchors with Levenshtein similarity
4. **WhitespaceNormalizedReplacer** - Normalizes all whitespace to single spaces
5. **IndentationFlexibleReplacer** - Ignores indentation differences
6. **EscapeNormalizedReplacer** - Handles escape sequences (`\n`, `\t`, etc.)
7. **TrimmedBoundaryReplacer** - Handles trimmed boundaries
8. **ContextAwareReplacer** - Uses context lines for matching (50% threshold)
9. **MultiOccurrenceReplacer** - Yields all exact matches (for replaceAll)

### Multi-Edit (Atomic Batch Editing)

The `multi_edit` tool applies multiple edits atomically across one or more files.
If any single edit fails, **no real files are written** (preflight on a virtual filesystem).

Two input modes:

- **`edits` array**: `{ path, oldText, newText, replaceAll? }[]` — uses the same 9-level fuzzy matching chain. Multiple edits to the same file are applied in sequence against an in-memory buffer.
- **Codex `patch` string**: `*** Begin Patch` / `*** End Patch` with `*** Add File:`, `*** Delete File:`, and `*** Update File:` sections. Update hunks use `@@` context markers and `+`/`-` line markers. Fuzzy matching (exact → rstrip → trim → unicode-normalized) is used to locate hunks.

LSP diagnostics run **once** at the end across all touched files (not per-edit).

```
multi_edit(edits: [
  { path: "src/a.ts", oldText: "foo", newText: "bar" },
  { path: "src/b.ts", oldText: "old", newText: "new" },
])

multi_edit(patch: "*** Begin Patch\n*** Update File: src/app.ts\n@@\n-old\n+new\n*** End Patch")
```

### LSP Diagnostics Injection

After every edit (or batch of edits), the extension:
1. Notifies the LSP server of the change
2. Waits for diagnostics (up to 3 seconds)
3. Appends any errors to the tool output

This creates a tight feedback loop where the LLM sees errors immediately.

### replaceAll Parameter

Use `replaceAll: true` to replace all occurrences of `oldText`:

```
edit(path: "src/app.ts", oldText: "oldName", newText: "newName", replaceAll: true)
```

## Line Ending Handling

- Auto-detects CRLF vs LF
- Normalizes for matching
- Restores original line endings in output
- Handles BOM (Byte Order Mark)

## Riced Visual Rendering

All tools use the shared design system (`shared/design-system.ts`) for rendering:
- Box-drawing characters for diff previews
- Color-coded add/remove counts
- animated spinners for pending states
- Truncated paths with line counts

## Example Output

Single edit success:
```
Edit applied successfully.
```

Multi-edit success:
```
Applied 3 operation(s) across 2 file(s).
1. Edited src/app.ts.
2. Edited src/util.ts.
3. Created src/new.ts.
```

With errors:
```
Edit applied successfully.

LSP errors detected in this file, please fix:
<diagnostics file="src/app.ts">
ERROR [42:10] Property 'foo' does not exist on type 'Bar'
ERROR [55:3] Cannot find name 'undefined_var'
</diagnostics>
```

## Integration with LSP Extension

This extension reads from the global `__piLspManager` set by the LSP extension. If the LSP extension is not loaded, diagnostics injection is skipped (edit still works).

Load order doesn't matter - the enhanced-edit extension checks for the manager at execution time.

## Files

- `src/index.ts` - Main extension: overrides edit/write, registers multi_edit tool
- `src/replacers.ts` - 9-level fuzzy matching chain
- `src/multi-edit.ts` - Atomic batch editing: classic edits + codex-patch parser + virtual/real workspaces

## Dependencies

- `diff` - For generating unified diffs

## Comparison with Built-in Edit

| Feature | Built-in | Enhanced |
|---------|----------|----------|
| Exact match | ✓ | ✓ |
| Basic fuzzy (whitespace) | ✓ | ✓ |
| Levenshtein matching | ✗ | ✓ |
| Block anchors | ✗ | ✓ |
| Indentation flexible | ✗ | ✓ |
| replaceAll | ✗ | ✓ |
| LSP diagnostics | ✗ | ✓ |
| CRLF handling | Basic | Full |
| BOM handling | ✗ | ✓ |
| Multi-edit batching | ✗ | ✓ |
| Codex-patch format | ✗ | ✓ |
| Atomic preflight | ✗ | ✓ |
| Riced visual rendering | ✗ | ✓ |
