curd-editor 0.5.0

A small terminal code editor with language-server support, folding and snippets
# curd

A tiny terminal code editor written in Rust with `crossterm`, built step by step as a learning project.

Package name: `curd-editor` (`curd` is taken on crates.io). Command name: `curd`.

## Install

One command, no Rust needed. It downloads the latest release for your system.

Linux and macOS:

```sh
curl -fsSL https://raw.githubusercontent.com/vikram5258/crud-editor/main/install.sh | sh
```

Windows (PowerShell):

```powershell
irm https://raw.githubusercontent.com/vikram5258/crud-editor/main/install.ps1 | iex
```

Then run `curd some-file.txt`.

Other ways to get it:

- With Rust installed: `cargo install curd-editor`
- By hand: download a binary from the [releases page]https://github.com/vikram5258/crud-editor/releases

## Run

```sh
cargo run -- src/main.rs
```

## Keys

| Key | What it does |
|---|---|
| Typing, Enter, Tab | insert text, a new line, four spaces |
| Backspace / Delete | delete before / under the cursor (joins lines at a line edge) |
| Arrows, Home, End | move the cursor; the view scrolls in both directions |
| Ctrl+S | save |
| Alt+K or Ctrl+K | fold / unfold the block on, or around, the cursor line |
| Alt+A | fold every top-level block, or unfold everything |
| Alt+H or F2 | hover: show what the language server knows about the thing under the cursor |
| Alt+G or F12 | go to definition |
| Tab after a trigger word | expand a snippet (see below) |
| Ctrl+Q | quit (asks for a second press if there are unsaved changes) |

Some terminals keep certain Ctrl keys for themselves (VS Code's terminal takes Ctrl+K), which is why the commands above also have Alt or function-key forms.

## Language servers (LSP)

`curd` gets colours, errors, warnings, hover and go-to-definition from the same language servers VS Code and other editors use. It starts the server for the file's type, if it is installed:

| Language | Server | Install |
|---|---|---|
| Rust | `rust-analyzer` | `rustup component add rust-analyzer` |
| Python | `pyright-langserver` | `npm install -g pyright` |
| JavaScript / TypeScript | `typescript-language-server` | `npm install -g typescript typescript-language-server` |
| C / C++ | `clangd` | your system's `clangd` package |
| Go | `gopls` | `go install golang.org/x/tools/gopls@latest` |

- **Colours:** the server says what each piece of code is (keyword, function, type, string, comment and so on) and `curd` colours it. It uses your terminal's own sixteen colours, so it matches your terminal theme. How much gets coloured depends on the server: `rust-analyzer` colours everything, while some servers only report names and leave keywords and strings plain.
- **Underlines:** the exact code an error or warning is about is underlined. Unused code is shown faded and deprecated code struck through.
- Lines with a problem get a red `E` or yellow `W` beside the line number; move the cursor onto the line to read the message in the status bar. The status bar also shows a count, like `E:1 W:2`.
- Without a server installed, `curd` works as a plain editor and says so once in the status bar.
- `CURD_LSP=off curd file.rs` turns servers off. `CURD_LSP="my-server --stdio" curd file.rs` uses a different one.

How it works: `curd` runs the server as a child process and talks to it over stdin/stdout. Edits are sent as small changes (not the whole file), batched while you type, and a background thread reads the answers, so typing never waits on the server.

## Code editing

- **Line numbers** in a column on the left.
- **Folding:** lines that start a block have a `-` beside their line number. Alt+K (or Ctrl+K) collapses the block into one row, shown as `fn main() { ... }` with a `+`; the same key opens it again. Alt+A folds or unfolds everything. Blocks are `{ ... }` in most languages and indentation in Python. Folding only changes the view, never the file.
- **Auto-indent:** Enter keeps the current indentation and adds a level after `{`, `(` or `[` (and after `:` in Python). Typing `}` on a blank line steps back a level.
- **Paste:** pasted text goes in exactly as copied, without auto-indent.
- **Snippets:** type a trigger word and press Tab. Which ones exist depends on the file extension:

| Language | Triggers |
|---|---|
| Rust (`.rs`) | `fn` `main` `struct` `enum` `impl` `match` `if` `for` `while` `loop` `test` `tests` `pl` |
| Python (`.py`) | `def` `class` `if` `for` `while` `main` `try` |
| JavaScript / TypeScript | `fn` `afn` `if` `for` `while` `log` |
| C / C++ | `main` `if` `for` `while` `inc` |
| Go (`.go`) | `fn` `main` `if` `for` `err` |

For example, in a `.rs` file `main` + Tab becomes:

```rust
fn main() {
    |
}
```

`curd new-file.txt` starts an empty document and creates the file on the first save. The bottom row is a status bar with the file name, a `[modified]` marker and the cursor position.

## Test

```sh
cargo test                # unit tests + end-to-end tests
cargo llvm-cov            # same, with a coverage report (cargo install cargo-llvm-cov)
```

- Unit tests sit next to the code in a `tests.rs` file per module, such as `src/editor/tests.rs`.
- End-to-end tests in `tests/e2e.rs` start the real `curd` in a pseudo-terminal, send keys and check the screen (Linux/macOS only).
- The language-server tests use a small fake server, `tests/fake_lsp.py` (needs `python3`). To also test against the real `rust-analyzer`: `cargo test --test e2e -- --ignored`.

Test files are left out of the published crate (see `include` in `Cargo.toml`).

CI runs the tests on every push and pull request and saves a JUnit test report and a coverage report as artifacts on the run. Pushing a `v*` tag runs the same checks first and only publishes a release (GitHub binaries, then crates.io) if they pass.

## Layout

- `src/main.rs` — starts everything up
- `src/terminal.rs` — raw mode, alternate screen, screen size (restored on drop)
- `src/document.rs` — the text (lines), editing it, opening and saving files
- `src/editor.rs` — main loop, keys, scrolling, drawing, status bar
- `src/folds.rs` — finding `{ ... }` blocks and tracking which are collapsed
- `src/snippets.rs` — snippet tables per language
- `src/lsp.rs` — language server client: starting servers, the wire format, requests and replies
- `src/highlight.rs` — colours from the server, kept per line and moved along as you type

## Progress

- [x] Step 1: raw mode, read keys, Ctrl+Q
- [x] Step 2: draw the screen with `~` and a welcome message
- [x] Step 3: open a file and show it (modular refactor)
- [x] Step 4: cursor movement and vertical scrolling
- [x] Step 5: typing and deleting
- [x] Step 6: save with Ctrl+S, unsaved-changes warning, new files
- [x] Status bar and horizontal scrolling
- [x] Line numbers, code folding, snippets, auto-indent, paste
- [x] LSP: diagnostics, hover, go to definition
- [x] Colours, underlines and other decorations from the language server
- [ ] LSP: completion
- [ ] Challenge: PageUp / PageDown
- [ ] Save as (name a file started without one)
- [ ] Search, undo