| 1 | |
| 2 | | Crate | Description | |
| 3 | | ----------- | ----------- | |
| 4 | | helix-stdx | Extensions to the standard library (similar to [`rust-analyzer`'s](https://github.com/rust-lang/rust-analyzer/blob/ea413f67a8f730b4211c09e103f8207c62e7dbc3/crates/stdx/Cargo.toml#L5)) | |
| 5 | | helix-core | Core editing primitives, functional. | |
| 6 | | helix-lsp | Language server client | |
| 7 | | helix-lsp-types | Language Server Protocol type definitions | |
| 8 | | helix-dap | Debug Adapter Protocol (DAP) client | |
| 9 | | helix-event | Primitives for defining and handling events within the editor | |
| 10 | | helix-loader | Functions for building, fetching, and loading external resources | |
| 11 | | helix-view | UI abstractions for use in backends, imperative shell. | |
| 12 | | helix-term | Terminal UI | |
| 13 | | helix-tui | TUI primitives, forked from tui-rs, inspired by Cursive | |
| 14 | |
| 15 | |
| 16 | This document contains a high-level overview of Helix internals. |
| 17 | |
| 18 | > NOTE: Use `cargo doc --open` for API documentation as well as dependency |
| 19 | > documentation. |
| 20 | |
| 21 | ## Core |
| 22 | |
| 23 | The core contains basic building blocks used to construct the editor. It is |
| 24 | heavily based on [CodeMirror 6](https://codemirror.net/6/docs/). The primitives |
| 25 | are functional: most operations won't modify data in place but instead return |
| 26 | a new copy. |
| 27 | |
| 28 | The main data structure used for representing buffers is a `Rope`. We re-export |
| 29 | the excellent [ropey](https://github.com/cessen/ropey) library. Ropes are cheap |
| 30 | to clone, and allow us to easily make snapshots of a text state. |
| 31 | |
| 32 | Multiple selections are a core editing primitive. Document selections are |
| 33 | represented by a `Selection`. Each `Range` in the selection consists of a moving |
| 34 | `head` and an immovable `anchor`. A single cursor in the editor is simply |
| 35 | a selection with a single range, with the head and the anchor in the same |
| 36 | position. |
| 37 | |
| 38 | Ropes are modified by constructing an OT-like `Transaction`. It represents |
| 39 | a single coherent change to the document and can be applied to the rope. |
| 40 | A transaction can be inverted to produce an undo. Selections and marks can be |
| 41 | mapped over a transaction to translate to a position in the new text state after |
| 42 | applying the transaction. |
| 43 | |
| 44 | > NOTE: `Transaction::change`/`Transaction::change_by_selection` is the main |
| 45 | > interface used to generate text edits. |
| 46 | |
| 47 | `Syntax` is the interface used to interact with tree-sitter ASTs for syntax |
| 48 | highlighting and other features. |
| 49 | |
| 50 | ## View |
| 51 | |
| 52 | The `view` layer was supposed to be a frontend-agnostic imperative library that |
| 53 | would build on top of `core` to provide the common editor logic. Currently it's |
| 54 | tied to the terminal UI. |
| 55 | |
| 56 | A `Document` ties together the `Rope`, `Selection`(s), `Syntax`, document |
| 57 | `History`, language server (etc.) into a comprehensive representation of an open |
| 58 | file. |
| 59 | |
| 60 | A `View` represents an open split in the UI. It holds the currently open |
| 61 | document ID and other related state. Views encapsulate the gutter, status line, |
| 62 | diagnostics, and the inner area where the code is displayed. |
| 63 | |
| 64 | > NOTE: Multiple views are able to display the same document, so the document |
| 65 | > contains selections for each view. To retrieve, `document.selection()` takes |
| 66 | > a `ViewId`. |
| 67 | |
| 68 | `Info` is the autoinfo box that shows hints when awaiting another key with bindings |
| 69 | like `g` and `m`. It is attached to the viewport as a whole. |
| 70 | |
| 71 | `Surface` is like a buffer to which widgets draw themselves to, and the |
| 72 | surface is then rendered on the screen on each cycle. |
| 73 | |
| 74 | `Rect`s are areas (simply an x and y coordinate with the origin at the |
| 75 | screen top left and then a height and width) which are part of a |
| 76 | `Surface`. They can be used to limit the area to which a `Component` can |
| 77 | render. For example if we wrap a `Markdown` component in a `Popup` |
| 78 | (think the documentation popup with space+k), Markdown's render method |
| 79 | will get a Rect that is the exact size of the popup. |
| 80 | |
| 81 | Widgets are called `Component`s internally, and you can see most of them |
| 82 | in `helix-term/src/ui`. Some components like `Popup` and `Overlay` can take |
| 83 | other components as children. |
| 84 | |
| 85 | `Layer`s are how multiple components are displayed, and is simply a |
| 86 | `Vec<Component>`. Layers are managed by the `Compositor`. On each top |
| 87 | level render call, the compositor renders each component in the order |
| 88 | they were pushed into the stack. This makes multiple components "layer" |
| 89 | on top of one another. Hence we get a file picker displayed over the |
| 90 | editor, etc. |
| 91 | |
| 92 | The `Editor` holds the global state: all the open documents, a tree |
| 93 | representation of all the view splits, the configuration, and a registry of |
| 94 | language servers. To open or close files, interact with the editor. |
| 95 | |
| 96 | ## LSP |
| 97 | |
| 98 | A language server protocol client. |
| 99 | |
| 100 | ## Term |
| 101 | |
| 102 | The terminal frontend. |
| 103 | |
| 104 | The `main` function sets up a new `Application` that runs the event loop. |
| 105 | |
| 106 | `commands.rs` is probably the most interesting file. It contains all commands |
| 107 | (actions tied to keybindings). |
| 108 | |
| 109 | `keymap.rs` links commands to key combinations. |
| 110 | |
| 111 | |
| 112 | ## TUI / Term |
| 113 | |
| 114 | TODO: document Component and rendering related stuff |
| 115 | |
| 116 | ## Event |
| 117 | |
| 118 | The `helix-event` crate defines primitives for defining and acting on events |
| 119 | within the editor. "Events" cover things like opening, changing and closing of |
| 120 | documents, starting and stopping of language servers and more. |
| 121 | |
| 122 | `helix-event` has tools for defining events and registering _hooks_ which run |
| 123 | any time an event is emitted. `helix-event` also provides `AsyncHook` - a tool |
| 124 | for running cancellable tasks which run after events with _debouncing_. |
| 125 | |
| 126 | See the `AsyncHook` type for more information. Events can be created within the |
| 127 | `events!` macro. Synchronous hooks can be created with `register_hook!`. And |
| 128 | editor-wide events can be sent to hooks with `helix_event::dispatch`. |