56 lines
1.8 KiB
Markdown
56 lines
1.8 KiB
Markdown
# The Hashline Technique
|
|
|
|
Hashline is AnchorCli's unique approach to safe, precise file editing.
|
|
|
|
## How It Works
|
|
|
|
Every line returned by file tools is prefixed with a content-derived hash anchor:
|
|
|
|
```
|
|
function hello() {
|
|
return "world";
|
|
}
|
|
```
|
|
|
|
The format is `lineNumber:hash|content` where:
|
|
- `lineNumber` is the current line number in the file
|
|
- `hash` is an Adler-8 checksum derived from the line content and position
|
|
- `content` is the actual line text
|
|
|
|
## Editing with Anchors
|
|
|
|
When editing, you reference these `line:hash` anchors instead of reproducing old content. Before any mutation, both the line number **and** hash are validated — stale anchors are rejected immediately.
|
|
|
|
### Example
|
|
|
|
To replace lines 2-3 from the example above:
|
|
|
|
```json
|
|
{
|
|
"tool": "replace_lines",
|
|
"path": "example.js",
|
|
"startAnchor": "2:f1",
|
|
"endAnchor": "3:0e",
|
|
"newLines": [" return \"hello world\";"]
|
|
}
|
|
```
|
|
|
|
## Benefits
|
|
|
|
This eliminates:
|
|
|
|
- **Whitespace/indentation reproduction errors** — No need to match exact spacing
|
|
- **Silent edits to the wrong line** — Hash validation ensures you're editing the right content
|
|
- **Entire-file rewrites** — Change just one line without touching the rest
|
|
- **Line drift in batch operations** — BatchEdit validates all anchors upfront and applies changes bottom-to-top
|
|
|
|
## Key Principles
|
|
|
|
1. **Never include anchors in your content** — When using `replace_lines`, `insert_after`, or similar tools, the `newLines` parameter should contain raw source code only, WITHOUT the `lineNumber:hash|` prefix
|
|
2. **Always validate before editing** — Re-read files to get fresh anchors if your previous anchors fail validation
|
|
3. **Use BatchEdit for multiple changes** — This validates all anchors upfront and applies operations atomically
|
|
|
|
---
|
|
|
|
*Back to [README.md](../README.md)* | *See [TOOLS.md](TOOLS.md) for all available tools*
|