Merge branch 'master' into mwp-steel-integration

pull/8675/merge^2
mattwparas 1 year ago
commit 34144490ec

@ -14,7 +14,7 @@ jobs:
uses: actions/checkout@v3 uses: actions/checkout@v3
- name: Install nix - name: Install nix
uses: cachix/install-nix-action@v20 uses: cachix/install-nix-action@v22
- name: Authenticate with Cachix - name: Authenticate with Cachix
uses: cachix/cachix-action@v12 uses: cachix/cachix-action@v12

@ -1,3 +1,120 @@
# 23.05 (2023-05-18)
23.05 is a smaller release focusing on fixes. There were 88 contributors in this release. Thank you all!
Features:
- Add a config option to exclude declaration from LSP references request ([#6886](https://github.com/helix-editor/helix/pull/6886))
- Enable injecting languages based on their file extension and shebang ([#3970](https://github.com/helix-editor/helix/pull/3970))
- Sort the buffer picker by most recent access ([#2980](https://github.com/helix-editor/helix/pull/2980))
- Perform syntax highlighting in the picker asynchronously ([#7028](https://github.com/helix-editor/helix/pull/7028))
Commands:
- `:update` is now aliased as `:u` ([#6835](https://github.com/helix-editor/helix/pull/6835))
- Add `extend_to_first_nonwhitespace` which acts the same as `goto_first_nonwhitespace` but always extends ([#6837](https://github.com/helix-editor/helix/pull/6837))
- Add `:clear-register` for clearing the given register or all registers ([#5695](https://github.com/helix-editor/helix/pull/5695))
- Add `:write-buffer-close` and `:write-buffer-close!` ([#6947](https://github.com/helix-editor/helix/pull/6947))
Fixes:
- Normalize LSP workspace paths ([#6517](https://github.com/helix-editor/helix/pull/6517))
- Robustly handle invalid LSP ranges ([#6512](https://github.com/helix-editor/helix/pull/6512))
- Fix line number display for LSP goto pickers ([#6559](https://github.com/helix-editor/helix/pull/6559))
- Fix toggling of `soft-wrap.enable` option ([#6656](https://github.com/helix-editor/helix/pull/6656), [58e457a](https://github.com/helix-editor/helix/commit/58e457a), [#6742](https://github.com/helix-editor/helix/pull/6742))
- Handle `workspace/configuration` requests from stopped language servers ([#6693](https://github.com/helix-editor/helix/pull/6693))
- Fix possible crash from opening the jumplist picker ([#6672](https://github.com/helix-editor/helix/pull/6672))
- Fix theme preview returning to current theme on line and word deletions ([#6694](https://github.com/helix-editor/helix/pull/6694))
- Re-run crate build scripts on changes to revision and grammar repositories ([#6743](https://github.com/helix-editor/helix/pull/6743))
- Fix crash on opening from suspended state ([#6764](https://github.com/helix-editor/helix/pull/6764))
- Fix unwrap bug in DAP ([#6786](https://github.com/helix-editor/helix/pull/6786))
- Always build tree-sitter parsers with C++14 and C11 ([#6792](https://github.com/helix-editor/helix/pull/6792), [#6834](https://github.com/helix-editor/helix/pull/6834), [#6845](https://github.com/helix-editor/helix/pull/6845))
- Exit with a non-zero statuscode when tree-sitter parser builds fail ([#6795](https://github.com/helix-editor/helix/pull/6795))
- Flip symbol range in LSP goto commands ([#6794](https://github.com/helix-editor/helix/pull/6794))
- Fix runtime toggling of the `mouse` option ([#6675](https://github.com/helix-editor/helix/pull/6675))
- Fix panic in inlay hint computation when view anchor is out of bounds ([#6883](https://github.com/helix-editor/helix/pull/6883))
- Significantly improve performance of git discovery on slow file systems ([#6890](https://github.com/helix-editor/helix/pull/6890))
- Downgrade gix log level to info ([#6915](https://github.com/helix-editor/helix/pull/6915))
- Conserve BOM and properly support saving UTF16 files ([#6497](https://github.com/helix-editor/helix/pull/6497))
- Correctly handle completion re-request ([#6594](https://github.com/helix-editor/helix/pull/6594))
- Fix offset encoding in LSP `didChange` notifications ([#6921](https://github.com/helix-editor/helix/pull/6921))
- Change `gix` logging level to info ([#6915](https://github.com/helix-editor/helix/pull/6915))
- Improve error message when writes fail because parent directories do not exist ([#7014](https://github.com/helix-editor/helix/pull/7014))
- Replace DAP variables popup instead of pushing more popups ([#7034](https://github.com/helix-editor/helix/pull/7034))
- Disable tree-sitter for files after parsing for 500ms ([#7028](https://github.com/helix-editor/helix/pull/7028))
- Fix crash when deleting with multiple cursors ([#6024](https://github.com/helix-editor/helix/pull/6024))
- Fix selection sliding when deleting forwards in append mode ([#6024](https://github.com/helix-editor/helix/pull/6024))
- Fix completion on paths containing spaces ([#6779](https://github.com/helix-editor/helix/pull/6779))
Themes:
- Style inlay hints in `dracula` theme ([#6515](https://github.com/helix-editor/helix/pull/6515))
- Style inlay hints in `onedark` theme ([#6503](https://github.com/helix-editor/helix/pull/6503))
- Style inlay hints and the soft-wrap indicator in `varua` ([#6568](https://github.com/helix-editor/helix/pull/6568), [#6589](https://github.com/helix-editor/helix/pull/6589))
- Style inlay hints in `emacs` theme ([#6569](https://github.com/helix-editor/helix/pull/6569))
- Update `base16_transparent` and `dark_high_contrast` themes ([#6577](https://github.com/helix-editor/helix/pull/6577))
- Style inlay hints for `mellow` and `rasmus` themes ([#6583](https://github.com/helix-editor/helix/pull/6583))
- Dim pane divider for `base16_transparent` theme ([#6534](https://github.com/helix-editor/helix/pull/6534))
- Style inlay hints in `zenburn` theme ([#6593](https://github.com/helix-editor/helix/pull/6593))
- Style inlay hints in `boo_berry` theme ([#6625](https://github.com/helix-editor/helix/pull/6625))
- Add `ferra` theme ([#6619](https://github.com/helix-editor/helix/pull/6619), [#6776](https://github.com/helix-editor/helix/pull/6776))
- Style inlay hints in `nightfox` theme ([#6655](https://github.com/helix-editor/helix/pull/6655))
- Fix `ayu` theme family markup code block background ([#6538](https://github.com/helix-editor/helix/pull/6538))
- Improve whitespace and search match colors in `rose_pine` theme ([#6679](https://github.com/helix-editor/helix/pull/6679))
- Highlight selected items in `base16_transparent` theme ([#6716](https://github.com/helix-editor/helix/pull/6716))
- Adjust everforest to resemble original more closely ([#5866](https://github.com/helix-editor/helix/pull/5866))
- Refactor `dracula` theme ([#6552](https://github.com/helix-editor/helix/pull/6552), [#6767](https://github.com/helix-editor/helix/pull/6767), [#6855](https://github.com/helix-editor/helix/pull/6855), [#6987](https://github.com/helix-editor/helix/pull/6987))
- Style inlay hints in `darcula` theme ([#6732](https://github.com/helix-editor/helix/pull/6732))
- Style inlay hints in `kanagawa` theme ([#6773](https://github.com/helix-editor/helix/pull/6773))
- Improve `ayu_dark` theme ([#6622](https://github.com/helix-editor/helix/pull/6622))
- Refactor `noctis` theme multiple cursor highlighting ([96720e7](https://github.com/helix-editor/helix/commit/96720e7))
- Refactor `noctis` theme whitespace rendering and indent guides ([f2ccc03](https://github.com/helix-editor/helix/commit/f2ccc03))
- Add `amberwood` theme ([#6924](https://github.com/helix-editor/helix/pull/6924))
- Update `nightfox` theme ([#7061](https://github.com/helix-editor/helix/pull/7061))
Language support:
- R language server: use the `--no-echo` flag to silence output ([#6570](https://github.com/helix-editor/helix/pull/6570))
- Recognize CUDA files as C++ ([#6521](https://github.com/helix-editor/helix/pull/6521))
- Add support for Hurl ([#6450](https://github.com/helix-editor/helix/pull/6450))
- Add textobject queries for Julia ([#6588](https://github.com/helix-editor/helix/pull/6588))
- Update Ruby highlight queries ([#6587](https://github.com/helix-editor/helix/pull/6587))
- Add xsd to XML file-types ([#6631](https://github.com/helix-editor/helix/pull/6631))
- Support Robot Framework ([#6611](https://github.com/helix-editor/helix/pull/6611))
- Update Gleam tree-sitter parser ([#6641](https://github.com/helix-editor/helix/pull/6641))
- Update git-commit tree-sitter parser ([#6692](https://github.com/helix-editor/helix/pull/6692))
- Update Haskell tree-sitter parser ([#6317](https://github.com/helix-editor/helix/pull/6317))
- Add injection queries for Haskell quasiquotes ([#6474](https://github.com/helix-editor/helix/pull/6474))
- Highlight C/C++ escape sequences ([#6724](https://github.com/helix-editor/helix/pull/6724))
- Support Markdoc ([#6432](https://github.com/helix-editor/helix/pull/6432))
- Support OpenCL ([#6473](https://github.com/helix-editor/helix/pull/6473))
- Support DTD ([#6644](https://github.com/helix-editor/helix/pull/6644))
- Fix constant highlighting in Python queries ([#6751](https://github.com/helix-editor/helix/pull/6751))
- Support Just ([#6453](https://github.com/helix-editor/helix/pull/6453))
- Fix Go locals query for `var_spec` identifiers ([#6763](https://github.com/helix-editor/helix/pull/6763))
- Update Markdown tree-sitter parser ([#6785](https://github.com/helix-editor/helix/pull/6785))
- Fix Haskell workspace root for cabal projects ([#6828](https://github.com/helix-editor/helix/pull/6828))
- Avoid extra indentation in Go switches ([#6817](https://github.com/helix-editor/helix/pull/6817))
- Fix Go workspace roots ([#6884](https://github.com/helix-editor/helix/pull/6884))
- Set PerlNavigator as the default Perl language server ([#6860](https://github.com/helix-editor/helix/pull/6860))
- Highlight more sqlx macros in Rust ([#6793](https://github.com/helix-editor/helix/pull/6793))
- Switch Odin tree-sitter grammar ([#6766](https://github.com/helix-editor/helix/pull/6766))
- Recognize `poetry.lock` as TOML ([#6928](https://github.com/helix-editor/helix/pull/6928))
- Recognize Jupyter notebooks as JSON ([#6927](https://github.com/helix-editor/helix/pull/6927))
- Add language server configuration for Crystal ([#6948](https://github.com/helix-editor/helix/pull/6948))
- Add `build.gradle.kts` to Java and Scala roots ([#6970](https://github.com/helix-editor/helix/pull/6970))
- Recognize `sty` and `cls` files as latex ([#6986](https://github.com/helix-editor/helix/pull/6986))
- Update Dockerfile tree-sitter grammar ([#6895](https://github.com/helix-editor/helix/pull/6895))
- Add comment injections for Odin ([#7027](https://github.com/helix-editor/helix/pull/7027))
- Recognize `gml` as XML ([#7055](https://github.com/helix-editor/helix/pull/7055))
- Recognize `geojson` as JSON ([#7054](https://github.com/helix-editor/helix/pull/7054))
Packaging:
- Update the Nix flake dependencies, remove a deprecated option ([#6546](https://github.com/helix-editor/helix/pull/6546))
- Fix and re-enable aarch64-macos release binary builds ([#6504](https://github.com/helix-editor/helix/pull/6504))
- The git dependency on `tree-sitter` has been replaced with a regular crates.io dependency ([#6608](https://github.com/helix-editor/helix/pull/6608))
# 23.03 (2023-03-31) # 23.03 (2023-03-31)
23.03 brings some long-awaited and exciting features. Thank you to everyone involved! This release saw changes from 102 contributors. 23.03 brings some long-awaited and exciting features. Thank you to everyone involved! This release saw changes from 102 contributors.
@ -1479,7 +1596,7 @@ to distinguish it in bug reports..
- The `runtime/` directory is now properly detected on binary releases and - The `runtime/` directory is now properly detected on binary releases and
on cargo run. `~/.config/helix/runtime` can also be used. on cargo run. `~/.config/helix/runtime` can also be used.
- Registers can now be selected via " (for example `"ay`) - Registers can now be selected via " (for example, `"ay`)
- Support for Nix files was added - Support for Nix files was added
- Movement is now fully tested and matches Kakoune implementation - Movement is now fully tested and matches Kakoune implementation
- A per-file LSP symbol picker was added to space+s - A per-file LSP symbol picker was added to space+s

997
Cargo.lock generated

File diff suppressed because it is too large Load Diff

@ -1 +1 @@
23.03 23.05

@ -3,10 +3,10 @@ authors = ["Blaž Hrastnik"]
language = "en" language = "en"
multilingual = false multilingual = false
src = "src" src = "src"
edit-url-template = "https://github.com/helix-editor/helix/tree/master/book/{path}?mode=edit"
[output.html] [output.html]
cname = "docs.helix-editor.com" cname = "docs.helix-editor.com"
default-theme = "colibri" default-theme = "colibri"
preferred-dark-theme = "colibri" preferred-dark-theme = "colibri"
git-repository-url = "https://github.com/helix-editor/helix" git-repository-url = "https://github.com/helix-editor/helix"
edit-url-template = "https://github.com/helix-editor/helix/edit/master/book/{path}"

@ -52,6 +52,7 @@ Its settings will be merged with the configuration directory `config.toml` and t
| `auto-format` | Enable automatic formatting on save | `true` | | `auto-format` | Enable automatic formatting on save | `true` |
| `auto-save` | Enable automatic saving on the focus moving away from Helix. Requires [focus event support](https://github.com/helix-editor/helix/wiki/Terminal-Support) from your terminal | `false` | | `auto-save` | Enable automatic saving on the focus moving away from Helix. Requires [focus event support](https://github.com/helix-editor/helix/wiki/Terminal-Support) from your terminal | `false` |
| `idle-timeout` | Time in milliseconds since last keypress before idle timers trigger. Used for autocompletion, set to 0 for instant | `400` | | `idle-timeout` | Time in milliseconds since last keypress before idle timers trigger. Used for autocompletion, set to 0 for instant | `400` |
| `preview-completion-insert` | Whether to apply completion item instantly when selected | `true` |
| `completion-trigger-len` | The min-length of word under cursor to trigger autocompletion | `2` | | `completion-trigger-len` | The min-length of word under cursor to trigger autocompletion | `2` |
| `completion-replace` | Set to `true` to make completions always replace the entire word and not just the part before the cursor | `false` | | `completion-replace` | Set to `true` to make completions always replace the entire word and not just the part before the cursor | `false` |
| `auto-info` | Whether to display info boxes | `true` | | `auto-info` | Whether to display info boxes | `true` |
@ -62,6 +63,7 @@ Its settings will be merged with the configuration directory `config.toml` and t
| `color-modes` | Whether to color the mode indicator with different colors depending on the mode itself | `false` | | `color-modes` | Whether to color the mode indicator with different colors depending on the mode itself | `false` |
| `text-width` | Maximum line length. Used for the `:reflow` command and soft-wrapping if `soft-wrap.wrap-at-text-width` is set | `80` | | `text-width` | Maximum line length. Used for the `:reflow` command and soft-wrapping if `soft-wrap.wrap-at-text-width` is set | `80` |
| `workspace-lsp-roots` | Directories relative to the workspace root that are treated as LSP roots. Should only be set in `.helix/config.toml` | `[]` | | `workspace-lsp-roots` | Directories relative to the workspace root that are treated as LSP roots. Should only be set in `.helix/config.toml` | `[]` |
| `default-line-ending` | The line ending to use for new documents. Can be `native`, `lf`, `crlf`, `ff`, `cr` or `nel`. `native` uses the platform's native line ending (`crlf` on Windows, otherwise `lf`). | `native` |
### `[editor.statusline]` Section ### `[editor.statusline]` Section
@ -117,6 +119,7 @@ The following statusline elements can be configured:
| `separator` | The string defined in `editor.statusline.separator` (defaults to `"│"`) | | `separator` | The string defined in `editor.statusline.separator` (defaults to `"│"`) |
| `spacer` | Inserts a space between elements (multiple/contiguous spacers may be specified) | | `spacer` | Inserts a space between elements (multiple/contiguous spacers may be specified) |
| `version-control` | The current branch name or detached commit hash of the opened workspace | | `version-control` | The current branch name or detached commit hash of the opened workspace |
| `register` | The current selected register |
### `[editor.lsp]` Section ### `[editor.lsp]` Section
@ -131,8 +134,8 @@ The following statusline elements can be configured:
| `goto-reference-include-declaration` | Include declaration in the goto references popup. | `true` | | `goto-reference-include-declaration` | Include declaration in the goto references popup. | `true` |
[^1]: By default, a progress spinner is shown in the statusline beside the file path. [^1]: By default, a progress spinner is shown in the statusline beside the file path.
[^2]: You may also have to activate them in the LSP config for them to appear, not just in Helix.
Inlay hints in Helix are still being improved on and may be a little bit laggy/janky under some circumstances, please report any bugs you see so we can fix them! [^2]: You may also have to activate them in the LSP config for them to appear, not just in Helix. Inlay hints in Helix are still being improved on and may be a little bit laggy/janky under some circumstances. Please report any bugs you see so we can fix them!
### `[editor.cursor-shape]` Section ### `[editor.cursor-shape]` Section

@ -7,10 +7,11 @@
| beancount | ✓ | | | | | beancount | ✓ | | | |
| bibtex | ✓ | | | `texlab` | | bibtex | ✓ | | | `texlab` |
| bicep | ✓ | | | `bicep-langserver` | | bicep | ✓ | | | `bicep-langserver` |
| blueprint | ✓ | | | `blueprint-compiler` |
| c | ✓ | ✓ | ✓ | `clangd` | | c | ✓ | ✓ | ✓ | `clangd` |
| c-sharp | ✓ | ✓ | | `OmniSharp` | | c-sharp | ✓ | ✓ | | `OmniSharp` |
| cabal | | | | | | cabal | | | | |
| cairo | ✓ | | | | | cairo | ✓ | | | `cairo-language-server` |
| capnp | ✓ | | ✓ | | | capnp | ✓ | | ✓ | |
| clojure | ✓ | | | `clojure-lsp` | | clojure | ✓ | | | `clojure-lsp` |
| cmake | ✓ | ✓ | ✓ | `cmake-language-server` | | cmake | ✓ | ✓ | ✓ | `cmake-language-server` |
@ -18,7 +19,7 @@
| common-lisp | ✓ | | | `cl-lsp` | | common-lisp | ✓ | | | `cl-lsp` |
| cpon | ✓ | | ✓ | | | cpon | ✓ | | ✓ | |
| cpp | ✓ | ✓ | ✓ | `clangd` | | cpp | ✓ | ✓ | ✓ | `clangd` |
| crystal | ✓ | ✓ | | | | crystal | ✓ | ✓ | | `crystalline` |
| css | ✓ | | | `vscode-css-language-server` | | css | ✓ | | | `vscode-css-language-server` |
| cue | ✓ | | | `cuelsp` | | cue | ✓ | | | `cuelsp` |
| d | ✓ | ✓ | ✓ | `serve-d` | | d | ✓ | ✓ | ✓ | `serve-d` |
@ -40,6 +41,7 @@
| erlang | ✓ | ✓ | | `erlang_ls` | | erlang | ✓ | ✓ | | `erlang_ls` |
| esdl | ✓ | | | | | esdl | ✓ | | | |
| fish | ✓ | ✓ | ✓ | | | fish | ✓ | ✓ | ✓ | |
| forth | ✓ | | | `forth-lsp` |
| fortran | ✓ | | ✓ | `fortls` | | fortran | ✓ | | ✓ | `fortls` |
| gdscript | ✓ | ✓ | ✓ | | | gdscript | ✓ | ✓ | ✓ | |
| git-attributes | ✓ | | | | | git-attributes | ✓ | | | |
@ -86,7 +88,7 @@
| markdoc | ✓ | | | `markdoc-ls` | | markdoc | ✓ | | | `markdoc-ls` |
| markdown | ✓ | | | `marksman` | | markdown | ✓ | | | `marksman` |
| markdown.inline | ✓ | | | | | markdown.inline | ✓ | | | |
| matlab | ✓ | | | | | matlab | ✓ | | | |
| mermaid | ✓ | | | | | mermaid | ✓ | | | |
| meson | ✓ | | ✓ | | | meson | ✓ | | ✓ | |
| mint | | | | `mint` | | mint | | | | `mint` |
@ -141,6 +143,7 @@
| svelte | ✓ | | | `svelteserver` | | svelte | ✓ | | | `svelteserver` |
| sway | ✓ | ✓ | ✓ | `forc` | | sway | ✓ | ✓ | ✓ | `forc` |
| swift | ✓ | | | `sourcekit-lsp` | | swift | ✓ | | | `sourcekit-lsp` |
| t32 | ✓ | | | |
| tablegen | ✓ | ✓ | ✓ | | | tablegen | ✓ | ✓ | ✓ | |
| task | ✓ | | | | | task | ✓ | | | |
| tfvars | ✓ | | ✓ | `terraform-ls` | | tfvars | ✓ | | ✓ | `terraform-ls` |
@ -156,9 +159,10 @@
| verilog | ✓ | ✓ | | `svlangserver` | | verilog | ✓ | ✓ | | `svlangserver` |
| vhdl | ✓ | | | `vhdl_ls` | | vhdl | ✓ | | | `vhdl_ls` |
| vhs | ✓ | | | | | vhs | ✓ | | | |
| vue | ✓ | | | `vls` | | vue | ✓ | | | `vue-language-server` |
| wast | ✓ | | | | | wast | ✓ | | | |
| wat | ✓ | | | | | wat | ✓ | | | |
| webc | ✓ | | | |
| wgsl | ✓ | | | `wgsl_analyzer` | | wgsl | ✓ | | | `wgsl_analyzer` |
| wit | ✓ | | ✓ | | | wit | ✓ | | ✓ | |
| xit | ✓ | | | | | xit | ✓ | | | |

@ -12,7 +12,9 @@
| `:buffer-next`, `:bn`, `:bnext` | Goto next buffer. | | `:buffer-next`, `:bn`, `:bnext` | Goto next buffer. |
| `:buffer-previous`, `:bp`, `:bprev` | Goto previous buffer. | | `:buffer-previous`, `:bp`, `:bprev` | Goto previous buffer. |
| `:write`, `:w` | Write changes to disk. Accepts an optional path (:write some/path.txt) | | `:write`, `:w` | Write changes to disk. Accepts an optional path (:write some/path.txt) |
| `:write!`, `:w!` | Force write changes to disk creating necessary subdirectories. Accepts an optional path (:write some/path.txt) | | `:write!`, `:w!` | Force write changes to disk creating necessary subdirectories. Accepts an optional path (:write! some/path.txt) |
| `:write-buffer-close`, `:wbc` | Write changes to disk and closes the buffer. Accepts an optional path (:write-buffer-close some/path.txt) |
| `:write-buffer-close!`, `:wbc!` | Force write changes to disk creating necessary subdirectories and closes the buffer. Accepts an optional path (:write-buffer-close! some/path.txt) |
| `:new`, `:n` | Create a new scratch buffer. | | `:new`, `:n` | Create a new scratch buffer. |
| `:format`, `:fmt` | Format the file using the LSP formatter. | | `:format`, `:fmt` | Format the file using the LSP formatter. |
| `:indent-style` | Set the indentation style for editing. ('t' for tabs or 1-8 for number of spaces.) | | `:indent-style` | Set the indentation style for editing. ('t' for tabs or 1-8 for number of spaces.) |
@ -29,6 +31,7 @@
| `:cquit`, `:cq` | Quit with exit code (default 1). Accepts an optional integer exit code (:cq 2). | | `:cquit`, `:cq` | Quit with exit code (default 1). Accepts an optional integer exit code (:cq 2). |
| `:cquit!`, `:cq!` | Force quit with exit code (default 1) ignoring unsaved changes. Accepts an optional integer exit code (:cq! 2). | | `:cquit!`, `:cq!` | Force quit with exit code (default 1) ignoring unsaved changes. Accepts an optional integer exit code (:cq! 2). |
| `:theme` | Change the editor theme (show current theme if no name specified). | | `:theme` | Change the editor theme (show current theme if no name specified). |
| `:yank-join` | Yank joined selections. A separator can be provided as first argument. Default value is newline. |
| `:clipboard-yank` | Yank main selection into system clipboard. | | `:clipboard-yank` | Yank main selection into system clipboard. |
| `:clipboard-yank-join` | Yank joined selections into system clipboard. A separator can be provided as first argument. Default value is newline. | | `:clipboard-yank-join` | Yank joined selections into system clipboard. A separator can be provided as first argument. Default value is newline. |
| `:primary-clipboard-yank` | Yank main selection into system primary clipboard. | | `:primary-clipboard-yank` | Yank main selection into system primary clipboard. |
@ -44,12 +47,12 @@
| `:show-directory`, `:pwd` | Show the current working directory. | | `:show-directory`, `:pwd` | Show the current working directory. |
| `:encoding` | Set encoding. Based on `https://encoding.spec.whatwg.org`. | | `:encoding` | Set encoding. Based on `https://encoding.spec.whatwg.org`. |
| `:character-info`, `:char` | Get info about the character under the primary cursor. | | `:character-info`, `:char` | Get info about the character under the primary cursor. |
| `:reload` | Discard changes and reload from the source file. | | `:reload`, `:rl` | Discard changes and reload from the source file. |
| `:reload-all` | Discard changes and reload all documents from the source files. | | `:reload-all`, `:rla` | Discard changes and reload all documents from the source files. |
| `:update`, `:u` | Write changes only if the file has been modified. | | `:update`, `:u` | Write changes only if the file has been modified. |
| `:lsp-workspace-command` | Open workspace command picker | | `:lsp-workspace-command` | Open workspace command picker |
| `:lsp-restart` | Restarts the Language Server that is in use by the current doc | | `:lsp-restart` | Restarts the language servers used by the current doc |
| `:lsp-stop` | Stops the Language Server that is in use by the current doc | | `:lsp-stop` | Stops the language servers that are used by the current doc |
| `:tree-sitter-scopes` | Display tree sitter scopes, primarily for theming and development. | | `:tree-sitter-scopes` | Display tree sitter scopes, primarily for theming and development. |
| `:debug-start`, `:dbg` | Start a debug session from a given template with given parameters. | | `:debug-start`, `:dbg` | Start a debug session from a given template with given parameters. |
| `:debug-remote`, `:dbg-tcp` | Connect to a debug adapter by TCP address and start a debugging session from a given template with given parameters. | | `:debug-remote`, `:dbg-tcp` | Connect to a debug adapter by TCP address and start a debugging session from a given template with given parameters. |

@ -9,6 +9,7 @@ below.
necessary configuration for the new language. For more information on necessary configuration for the new language. For more information on
language configuration, refer to the language configuration, refer to the
[language configuration section](../languages.md) of the documentation. [language configuration section](../languages.md) of the documentation.
A new language server can be added by extending the `[language-server]` table in the same file.
2. If you are adding a new language or updating an existing language server 2. If you are adding a new language or updating an existing language server
configuration, run the command `cargo xtask docgen` to update the configuration, run the command `cargo xtask docgen` to update the
[Language Support](../lang-support.md) documentation. [Language Support](../lang-support.md) documentation.

@ -8,6 +8,7 @@
- [Fedora/RHEL](#fedorarhel) - [Fedora/RHEL](#fedorarhel)
- [Arch Linux community](#arch-linux-community) - [Arch Linux community](#arch-linux-community)
- [NixOS](#nixos) - [NixOS](#nixos)
- [Flatpak](#flatpak)
- [AppImage](#appimage) - [AppImage](#appimage)
- [macOS](#macos) - [macOS](#macos)
- [Homebrew Core](#homebrew-core) - [Homebrew Core](#homebrew-core)
@ -18,6 +19,9 @@
- [MSYS2](#msys2) - [MSYS2](#msys2)
- [Building from source](#building-from-source) - [Building from source](#building-from-source)
- [Configuring Helix's runtime files](#configuring-helixs-runtime-files) - [Configuring Helix's runtime files](#configuring-helixs-runtime-files)
- [Linux and macOS](#linux-and-macos)
- [Windows](#windows)
- [Multiple runtime directories](#multiple-runtime-directories)
- [Validating the installation](#validating-the-installation) - [Validating the installation](#validating-the-installation)
- [Configure the desktop shortcut](#configure-the-desktop-shortcut) - [Configure the desktop shortcut](#configure-the-desktop-shortcut)
<!--toc:end--> <!--toc:end-->
@ -78,7 +82,10 @@ in the AUR, which builds the master branch.
### NixOS ### NixOS
Helix is available as a [flake](https://nixos.wiki/wiki/Flakes) in the project Helix is available in [nixpkgs](https://github.com/nixos/nixpkgs) through the `helix` attribute,
the unstable channel usually carries the latest release.
Helix is also available as a [flake](https://nixos.wiki/wiki/Flakes) in the project
root. Use `nix develop` to spin up a reproducible development shell. Outputs are root. Use `nix develop` to spin up a reproducible development shell. Outputs are
cached for each push to master using [Cachix](https://www.cachix.org/). The cached for each push to master using [Cachix](https://www.cachix.org/). The
flake is configured to automatically make use of this cache assuming the user flake is configured to automatically make use of this cache assuming the user
@ -88,6 +95,15 @@ If you are using a version of Nix without flakes enabled,
[install Cachix CLI](https://docs.cachix.org/installation) and use [install Cachix CLI](https://docs.cachix.org/installation) and use
`cachix use helix` to configure Nix to use cached outputs when possible. `cachix use helix` to configure Nix to use cached outputs when possible.
### Flatpak
Helix is available on [Flathub](https://flathub.org/en-GB/apps/com.helix_editor.Helix):
```sh
flatpak install flathub com.helix_editor.Helix
flatpak run com.helix_editor.Helix
```
### AppImage ### AppImage
Install Helix using the Linux [AppImage](https://appimage.org/) format. Install Helix using the Linux [AppImage](https://appimage.org/) format.
@ -143,9 +159,13 @@ pacman -S mingw-w64-ucrt-x86_64-helix
Requirements: Requirements:
Clone the Helix GitHub repository into a directory of your choice. The
examples in this documentation assume installation into either `~/src/` on
Linux and macOS, or `%userprofile%\src\` on Windows.
- The [Rust toolchain](https://www.rust-lang.org/tools/install) - The [Rust toolchain](https://www.rust-lang.org/tools/install)
- The [Git version control system](https://git-scm.com/) - The [Git version control system](https://git-scm.com/)
- A c++14 compatible compiler to build the tree-sitter grammars, for example GCC or Clang - A C++14 compatible compiler to build the tree-sitter grammars, for example GCC or Clang
If you are using the `musl-libc` standard library instead of `glibc` the following environment variable must be set during the build to ensure tree-sitter grammars can be loaded correctly: If you are using the `musl-libc` standard library instead of `glibc` the following environment variable must be set during the build to ensure tree-sitter grammars can be loaded correctly:
@ -179,18 +199,22 @@ grammars in the local `runtime` folder.
#### Linux and macOS #### Linux and macOS
Either set the `HELIX_RUNTIME` environment variable to point to the runtime files and add it to your `~/.bashrc` or equivalent: The **runtime** directory is one below the Helix source, so either set a
`HELIX_RUNTIME` environment variable to point to that directory and add it to
your `~/.bashrc` or equivalent:
```sh ```sh
HELIX_RUNTIME=/home/user-name/src/helix/runtime HELIX_RUNTIME=~/src/helix/runtime
``` ```
Or, create a symlink in `~/.config/helix` that links to the source code directory: Or, create a symbolic link:
```sh ```sh
ln -s $PWD/runtime ~/.config/helix/runtime ln -Ts $PWD/runtime ~/.config/helix/runtime
``` ```
If the above command fails to create a symbolic link because the file exists either move `~/.config/helix/runtime` to a new location or delete it, then run the symlink command above again.
#### Windows #### Windows
Either set the `HELIX_RUNTIME` environment variable to point to the runtime files using the Windows setting (search for Either set the `HELIX_RUNTIME` environment variable to point to the runtime files using the Windows setting (search for

@ -15,7 +15,7 @@
- [Popup](#popup) - [Popup](#popup)
- [Unimpaired](#unimpaired) - [Unimpaired](#unimpaired)
- [Insert mode](#insert-mode) - [Insert mode](#insert-mode)
- [Select / extend mode](#select-extend-mode) - [Select / extend mode](#select--extend-mode)
- [Picker](#picker) - [Picker](#picker)
- [Prompt](#prompt) - [Prompt](#prompt)
@ -25,6 +25,8 @@
## Normal mode ## Normal mode
Normal mode is the default mode when you launch helix. Return to it from other modes by typing `Escape`.
### Movement ### Movement
> NOTE: Unlike Vim, `f`, `F`, `t` and `T` are not confined to the current line. > NOTE: Unlike Vim, `f`, `F`, `t` and `T` are not confined to the current line.
@ -32,8 +34,8 @@
| Key | Description | Command | | Key | Description | Command |
| ----- | ----------- | ------- | | ----- | ----------- | ------- |
| `h`, `Left` | Move left | `move_char_left` | | `h`, `Left` | Move left | `move_char_left` |
| `j`, `Down` | Move down | `move_line_down` | | `j`, `Down` | Move down | `move_visual_line_down` |
| `k`, `Up` | Move up | `move_line_up` | | `k`, `Up` | Move up | `move_visual_line_up` |
| `l`, `Right` | Move right | `move_char_right` | | `l`, `Right` | Move right | `move_char_right` |
| `w` | Move next word start | `move_next_word_start` | | `w` | Move next word start | `move_next_word_start` |
| `b` | Move previous word start | `move_prev_word_start` | | `b` | Move previous word start | `move_prev_word_start` |
@ -111,6 +113,7 @@
| `s` | Select all regex matches inside selections | `select_regex` | | `s` | Select all regex matches inside selections | `select_regex` |
| `S` | Split selection into sub selections on regex matches | `split_selection` | | `S` | Split selection into sub selections on regex matches | `split_selection` |
| `Alt-s` | Split selection on newlines | `split_selection_on_newline` | | `Alt-s` | Split selection on newlines | `split_selection_on_newline` |
| `Alt-minus` | Merge selections | `merge_selections` |
| `Alt-_` | Merge consecutive selections | `merge_consecutive_selections` | | `Alt-_` | Merge consecutive selections | `merge_consecutive_selections` |
| `&` | Align selection in columns | `align_selections` | | `&` | Align selection in columns | `align_selections` |
| `_` | Trim whitespace from the selection | `trim_selections` | | `_` | Trim whitespace from the selection | `trim_selections` |
@ -218,6 +221,8 @@ Jumps to various locations.
| `n` | Go to next buffer | `goto_next_buffer` | | `n` | Go to next buffer | `goto_next_buffer` |
| `p` | Go to previous buffer | `goto_previous_buffer` | | `p` | Go to previous buffer | `goto_previous_buffer` |
| `.` | Go to last modification in current file | `goto_last_modification` | | `.` | Go to last modification in current file | `goto_last_modification` |
| `j` | Move down textual (instead of visual) line | `move_line_down` |
| `k` | Move up textual (instead of visual) line | `move_line_up` |
#### Match mode #### Match mode
@ -334,6 +339,8 @@ These mappings are in the style of [vim-unimpaired](https://github.com/tpope/vim
## Insert mode ## Insert mode
Accessed by typing `i` in [normal mode](#normal-mode).
Insert mode bindings are minimal by default. Helix is designed to Insert mode bindings are minimal by default. Helix is designed to
be a modal editor, and this is reflected in the user experience and internal be a modal editor, and this is reflected in the user experience and internal
mechanics. Changes to the text are only saved for undos when mechanics. Changes to the text are only saved for undos when
@ -387,9 +394,11 @@ end = "no_op"
## Select / extend mode ## Select / extend mode
Accessed by typing `v` in [normal mode](#normal-mode).
Select mode echoes Normal mode, but changes any movements to extend Select mode echoes Normal mode, but changes any movements to extend
selections rather than replace them. Goto motions are also changed to selections rather than replace them. Goto motions are also changed to
extend, so that `vgl` for example extends the selection to the end of extend, so that `vgl`, for example, extends the selection to the end of
the line. the line.
Search is also affected. By default, `n` and `N` will remove the current Search is also affected. By default, `n` and `N` will remove the current

@ -7,17 +7,20 @@ in `languages.toml` files.
There are three possible locations for a `languages.toml` file: There are three possible locations for a `languages.toml` file:
1. In the Helix source code, this lives in the 1. In the Helix source code, which lives in the
[Helix repository](https://github.com/helix-editor/helix/blob/master/languages.toml). [Helix repository](https://github.com/helix-editor/helix/blob/master/languages.toml).
It provides the default configurations for languages and language servers. It provides the default configurations for languages and language servers.
2. In your [configuration directory](./configuration.md). This overrides values 2. In your [configuration directory](./configuration.md). This overrides values
from the built-in language configuration. For example to disable from the built-in language configuration. For example, to disable
auto-LSP-formatting in Rust: auto-LSP-formatting in Rust:
```toml ```toml
# in <config_dir>/helix/languages.toml # in <config_dir>/helix/languages.toml
[language-server.mylang-lsp]
command = "mylang-lsp"
[[language]] [[language]]
name = "rust" name = "rust"
auto-format = false auto-format = false
@ -41,8 +44,8 @@ injection-regex = "mylang"
file-types = ["mylang", "myl"] file-types = ["mylang", "myl"]
comment-token = "#" comment-token = "#"
indent = { tab-width = 2, unit = " " } indent = { tab-width = 2, unit = " " }
language-server = { command = "mylang-lsp", args = ["--stdio"], environment = { "ENV1" = "value1", "ENV2" = "value2" } }
formatter = { command = "mylang-formatter" , args = ["--stdin"] } formatter = { command = "mylang-formatter" , args = ["--stdin"] }
language-servers = [ "mylang-lsp" ]
``` ```
These configuration keys are available: These configuration keys are available:
@ -50,6 +53,7 @@ These configuration keys are available:
| Key | Description | | Key | Description |
| ---- | ----------- | | ---- | ----------- |
| `name` | The name of the language | | `name` | The name of the language |
| `language-id` | The language-id for language servers, checkout the table at [TextDocumentItem](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocumentItem) for the right id |
| `scope` | A string like `source.js` that identifies the language. Currently, we strive to match the scope names used by popular TextMate grammars and by the Linguist library. Usually `source.<name>` or `text.<name>` in case of markup languages | | `scope` | A string like `source.js` that identifies the language. Currently, we strive to match the scope names used by popular TextMate grammars and by the Linguist library. Usually `source.<name>` or `text.<name>` in case of markup languages |
| `injection-regex` | regex pattern that will be tested against a language name in order to determine whether this language should be used for a potential [language injection][treesitter-language-injection] site. | | `injection-regex` | regex pattern that will be tested against a language name in order to determine whether this language should be used for a potential [language injection][treesitter-language-injection] site. |
| `file-types` | The filetypes of the language, for example `["yml", "yaml"]`. See the file-type detection section below. | | `file-types` | The filetypes of the language, for example `["yml", "yaml"]`. See the file-type detection section below. |
@ -59,8 +63,7 @@ These configuration keys are available:
| `diagnostic-severity` | Minimal severity of diagnostic for it to be displayed. (Allowed values: `Error`, `Warning`, `Info`, `Hint`) | | `diagnostic-severity` | Minimal severity of diagnostic for it to be displayed. (Allowed values: `Error`, `Warning`, `Info`, `Hint`) |
| `comment-token` | The token to use as a comment-token | | `comment-token` | The token to use as a comment-token |
| `indent` | The indent to use. Has sub keys `unit` (the text inserted into the document when indenting; usually set to N spaces or `"\t"` for tabs) and `tab-width` (the number of spaces rendered for a tab) | | `indent` | The indent to use. Has sub keys `unit` (the text inserted into the document when indenting; usually set to N spaces or `"\t"` for tabs) and `tab-width` (the number of spaces rendered for a tab) |
| `language-server` | The Language Server to run. See the Language Server configuration section below. | | `language-servers` | The Language Servers used for this language. See below for more information in the section [Configuring Language Servers for a language](#configuring-language-servers-for-a-language) |
| `config` | Language Server configuration |
| `grammar` | The tree-sitter grammar to use (defaults to the value of `name`) | | `grammar` | The tree-sitter grammar to use (defaults to the value of `name`) |
| `formatter` | The formatter for the language, it will take precedence over the lsp when defined. The formatter must be able to take the original file as input from stdin and write the formatted file to stdout | | `formatter` | The formatter for the language, it will take precedence over the lsp when defined. The formatter must be able to take the original file as input from stdin and write the formatted file to stdout |
| `text-width` | Maximum line length. Used for the `:reflow` command and soft-wrapping if `soft-wrap.wrap-at-text-width` is set, defaults to `editor.text-width` | | `text-width` | Maximum line length. Used for the `:reflow` command and soft-wrapping if `soft-wrap.wrap-at-text-width` is set, defaults to `editor.text-width` |
@ -92,31 +95,102 @@ with the following priorities:
replaced at runtime with the appropriate path separator for the operating replaced at runtime with the appropriate path separator for the operating
system, so this rule would match against `.git\config` files on Windows. system, so this rule would match against `.git\config` files on Windows.
### Language Server configuration ## Language Server configuration
Language servers are configured separately in the table `language-server` in the same file as the languages `languages.toml`
The `language-server` field takes the following keys: For example:
```toml
[language-server.mylang-lsp]
command = "mylang-lsp"
args = ["--stdio"]
config = { provideFormatter = true }
environment = { "ENV1" = "value1", "ENV2" = "value2" }
[language-server.efm-lsp-prettier]
command = "efm-langserver"
[language-server.efm-lsp-prettier.config]
documentFormatting = true
languages = { typescript = [ { formatCommand ="prettier --stdin-filepath ${INPUT}", formatStdin = true } ] }
```
These are the available options for a language server.
| Key | Description | | Key | Description |
| --- | ----------- | | ---- | ----------- |
| `command` | The name of the language server binary to execute. Binaries must be in `$PATH` | | `command` | The name or path of the language server binary to execute. Binaries must be in `$PATH` |
| `args` | A list of arguments to pass to the language server binary | | `args` | A list of arguments to pass to the language server binary |
| `config` | LSP initialization options |
| `timeout` | The maximum time a request to the language server may take, in seconds. Defaults to `20` | | `timeout` | The maximum time a request to the language server may take, in seconds. Defaults to `20` |
| `language-id` | The language name to pass to the language server. Some language servers support multiple languages and use this field to determine which one is being served in a buffer |
| `environment` | Any environment variables that will be used when starting the language server `{ "KEY1" = "Value1", "KEY2" = "Value2" }` | | `environment` | Any environment variables that will be used when starting the language server `{ "KEY1" = "Value1", "KEY2" = "Value2" }` |
The top-level `config` field is used to configure the LSP initialization options. A `format` A `format` sub-table within `config` can be used to pass extra formatting options to
sub-table within `config` can be used to pass extra formatting options to [Document Formatting Requests](https://github.com/microsoft/language-server-protocol/blob/gh-pages/_specifications/specification-3-17.md#document-formatting-request--leftwards_arrow_with_hook).
[Document Formatting Requests](https://github.com/microsoft/language-server-protocol/blob/gh-pages/_specifications/specification-3-16.md#document-formatting-request--leftwards_arrow_with_hook). For example, with typescript:
For example with typescript:
```toml ```toml
[[language]] [language-server.typescript-language-server]
name = "typescript"
auto-format = true
# pass format options according to https://github.com/typescript-language-server/typescript-language-server#workspacedidchangeconfiguration omitting the "[language].format." prefix. # pass format options according to https://github.com/typescript-language-server/typescript-language-server#workspacedidchangeconfiguration omitting the "[language].format." prefix.
config = { format = { "semicolons" = "insert", "insertSpaceBeforeFunctionParenthesis" = true } } config = { format = { "semicolons" = "insert", "insertSpaceBeforeFunctionParenthesis" = true } }
``` ```
### Configuring Language Servers for a language
The `language-servers` attribute in a language tells helix which language servers are used for this language.
They have to be defined in the `[language-server]` table as described in the previous section.
Different languages can use the same language server instance, e.g. `typescript-language-server` is used for javascript, jsx, tsx and typescript by default.
In case multiple language servers are specified in the `language-servers` attribute of a `language`,
it's often useful to only enable/disable certain language-server features for these language servers.
As an example, `efm-lsp-prettier` of the previous example is used only with a formatting command `prettier`,
so everything else should be handled by the `typescript-language-server` (which is configured by default).
The language configuration for typescript could look like this:
```toml
[[language]]
name = "typescript"
language-servers = [ { name = "efm-lsp-prettier", only-features = [ "format" ] }, "typescript-language-server" ]
```
or equivalent:
```toml
[[language]]
name = "typescript"
language-servers = [ { name = "typescript-language-server", except-features = [ "format" ] }, "efm-lsp-prettier" ]
```
Each requested LSP feature is prioritized in the order of the `language-servers` array.
For example, the first `goto-definition` supported language server (in this case `typescript-language-server`) will be taken for the relevant LSP request (command `goto_definition`).
The features `diagnostics`, `code-action`, `completion`, `document-symbols` and `workspace-symbols` are an exception to that rule, as they are working for all language servers at the same time and are merged together, if enabled for the language.
If no `except-features` or `only-features` is given, all features for the language server are enabled.
If a language server itself doesn't support a feature, the next language server array entry will be tried (and so on).
The list of supported features is:
- `format`
- `goto-definition`
- `goto-declaration`
- `goto-type-definition`
- `goto-reference`
- `goto-implementation`
- `signature-help`
- `hover`
- `document-highlight`
- `completion`
- `code-action`
- `workspace-command`
- `document-symbols`
- `workspace-symbols`
- `diagnostics`
- `rename-symbol`
- `inlay-hints`
## Tree-sitter grammar configuration ## Tree-sitter grammar configuration
The source for a language's tree-sitter grammar is specified in a `[[grammar]]` The source for a language's tree-sitter grammar is specified in a `[[grammar]]`

@ -296,7 +296,7 @@ These scopes are used for theming the editor interface:
| `ui.window` | Borderlines separating splits | | `ui.window` | Borderlines separating splits |
| `ui.help` | Description box for commands | | `ui.help` | Description box for commands |
| `ui.text` | Command prompts, popup text, etc. | | `ui.text` | Command prompts, popup text, etc. |
| `ui.text.focus` | | | `ui.text.focus` | The currently selected line in the picker |
| `ui.text.inactive` | Same as `ui.text` but when the text is inactive (e.g. suggestions) | | `ui.text.inactive` | Same as `ui.text` but when the text is inactive (e.g. suggestions) |
| `ui.text.info` | The key: command text in `ui.popup.info` boxes | | `ui.text.info` | The key: command text in `ui.popup.info` boxes |
| `ui.virtual.ruler` | Ruler columns (see the [`editor.rulers` config][editor-section]) | | `ui.virtual.ruler` | Ruler columns (see the [`editor.rulers` config][editor-section]) |

@ -96,13 +96,13 @@ function or block of code.
| `(`, `[`, `'`, etc. | Specified surround pairs | | `(`, `[`, `'`, etc. | Specified surround pairs |
| `m` | The closest surround pair | | `m` | The closest surround pair |
| `f` | Function | | `f` | Function |
| `c` | Class | | `t` | Type (or Class) |
| `a` | Argument/parameter | | `a` | Argument/parameter |
| `o` | Comment | | `c` | Comment |
| `t` | Test | | `T` | Test |
| `g` | Change | | `g` | Change |
> 💡 `f`, `c`, etc. need a tree-sitter grammar active for the current > 💡 `f`, `t`, etc. need a tree-sitter grammar active for the current
document and a special tree-sitter query file to work properly. [Only document and a special tree-sitter query file to work properly. [Only
some grammars][lang-support] currently have the query file implemented. some grammars][lang-support] currently have the query file implemented.
Contributions are welcome! Contributions are welcome!
@ -112,7 +112,7 @@ Contributions are welcome!
Navigating between functions, classes, parameters, and other elements is Navigating between functions, classes, parameters, and other elements is
possible using tree-sitter and textobject queries. For possible using tree-sitter and textobject queries. For
example to move to the next function use `]f`, to move to previous example to move to the next function use `]f`, to move to previous
class use `[c`, and so on. type use `[t`, and so on.
![Tree-sitter-nav-demo][tree-sitter-nav-demo] ![Tree-sitter-nav-demo][tree-sitter-nav-demo]

@ -36,6 +36,9 @@
<content_rating type="oars-1.1" /> <content_rating type="oars-1.1" />
<releases> <releases>
<release version="23.05" date="2023-05-18">
<url>https://github.com/helix-editor/helix/releases/tag/23.05</url>
</release>
<release version="23.03" date="2023-03-31"> <release version="23.03" date="2023-03-31">
<url>https://helix-editor.com/news/release-23-03-highlights/</url> <url>https://helix-editor.com/news/release-23-03-highlights/</url>
</release> </release>

@ -1,7 +1,7 @@
## Checklist ## Checklist
Helix releases are versioned in the Calendar Versioning scheme: Helix releases are versioned in the Calendar Versioning scheme:
`YY.0M(.MICRO)`, for example `22.05` for May of 2022. In these instructions `YY.0M(.MICRO)`, for example, `22.05` for May of 2022. In these instructions
we'll use `<tag>` as a placeholder for the tag being published. we'll use `<tag>` as a placeholder for the tag being published.
* Merge the changelog PR * Merge the changelog PR
@ -30,7 +30,7 @@ we'll use `<tag>` as a placeholder for the tag being published.
The changelog is currently created manually by reading through commits in the The changelog is currently created manually by reading through commits in the
log since the last release. GitHub's compare view is a nice way to approach log since the last release. GitHub's compare view is a nice way to approach
this. For example when creating the 22.07 release notes, this compare link this. For example, when creating the 22.07 release notes, this compare link
may be used may be used
``` ```

@ -20,5 +20,5 @@ Vision statements are all well and good, but are also vague and subjective. Her
* **Built-in tools** for working with code bases efficiently. Most projects aren't a single file, and an editor should handle that as a first-class use case. In Helix's case, this means (among other things) a fuzzy-search file navigator and LSP support. * **Built-in tools** for working with code bases efficiently. Most projects aren't a single file, and an editor should handle that as a first-class use case. In Helix's case, this means (among other things) a fuzzy-search file navigator and LSP support.
* **Edit anything** that comes up when coding, within reason. Whether it's a 200 MB XML file, a megabyte of minified javascript on a single line, or Japanese text encoded in ShiftJIS, you should be able to open it and edit it without problems. (Note: this doesn't mean handle every esoteric use case. Sometimes you do just need a specialized tool, and Helix isn't that.) * **Edit anything** that comes up when coding, within reason. Whether it's a 200 MB XML file, a megabyte of minified javascript on a single line, or Japanese text encoded in ShiftJIS, you should be able to open it and edit it without problems. (Note: this doesn't mean handle every esoteric use case. Sometimes you do just need a specialized tool, and Helix isn't that.)
* **Configurable**, within reason. Although the defaults should be good, not everyone will agree on what "good" is. Within the bounds of Helix's core interaction models, it should be reasonably configurable so that it can be "good" for more people. This means, for example, custom key maps among other things. * **Configurable**, within reason. Although the defaults should be good, not everyone will agree on what "good" is. Within the bounds of Helix's core interaction models, it should be reasonably configurable so that it can be "good" for more people. This means, for example, custom key maps among other things.
* **Extensible**, within reason. Although we want Helix to be productive out-of-the-box, it's not practical or desirable to cram every useful feature and use case into the core editor. The basics should be built-in, but you should be able to extend it with additional functionality as needed. Right now we're thinking Wasm-based plugins. * **Extensible**, within reason. Although we want Helix to be productive out-of-the-box, it's not practical or desirable to cram every useful feature and use case into the core editor. The basics should be built-in, but you should be able to extend it with additional functionality as needed.
* **Clean code base.** Sometimes other factors (e.g. significant performance gains, important features, correctness, etc.) will trump strict readability, but we nevertheless want to keep the code base straightforward and easy to understand to the extent we can. * **Clean code base.** Sometimes other factors (e.g. significant performance gains, important features, correctness, etc.) will trump strict readability, but we nevertheless want to keep the code base straightforward and easy to understand to the extent we can.

@ -3,15 +3,16 @@
"crane": { "crane": {
"flake": false, "flake": false,
"locked": { "locked": {
"lastModified": 1670900067, "lastModified": 1681175776,
"narHash": "sha256-VXVa+KBfukhmWizaiGiHRVX/fuk66P8dgSFfkVN4/MY=", "narHash": "sha256-7SsUy9114fryHAZ8p1L6G6YSu7jjz55FddEwa2U8XZc=",
"owner": "ipetkov", "owner": "ipetkov",
"repo": "crane", "repo": "crane",
"rev": "59b31b41a589c0a65e4a1f86b0e5eac68081468b", "rev": "445a3d222947632b5593112bb817850e8a9cf737",
"type": "github" "type": "github"
}, },
"original": { "original": {
"owner": "ipetkov", "owner": "ipetkov",
"ref": "v0.12.1",
"repo": "crane", "repo": "crane",
"type": "github" "type": "github"
} }
@ -62,11 +63,11 @@
] ]
}, },
"locked": { "locked": {
"lastModified": 1680258209, "lastModified": 1683212002,
"narHash": "sha256-lEo50RXI/17/a9aCIun8Hz62ZJ5JM5RGeTgclIP+Lgc=", "narHash": "sha256-EObtqyQsv9v+inieRY5cvyCMCUI5zuU5qu+1axlJCPM=",
"owner": "nix-community", "owner": "nix-community",
"repo": "dream2nix", "repo": "dream2nix",
"rev": "6f512b5a220fdb26bd3c659f7b55e4f052ec8b35", "rev": "fbfb09d2ab5ff761d822dd40b4a1def81651d096",
"type": "github" "type": "github"
}, },
"original": { "original": {
@ -94,11 +95,11 @@
] ]
}, },
"locked": { "locked": {
"lastModified": 1680172861, "lastModified": 1680698112,
"narHash": "sha256-QMyI338xRxaHFDlCXdLCtgelGQX2PdlagZALky4ZXJ8=", "narHash": "sha256-FgnobN/DvCjEsc0UAZEAdPLkL4IZi2ZMnu2K2bUaElc=",
"owner": "davhau", "owner": "davhau",
"repo": "drv-parts", "repo": "drv-parts",
"rev": "ced8a52f62b0a94244713df2225c05c85b416110", "rev": "e8c2ec1157dc1edb002989669a0dbd935f430201",
"type": "github" "type": "github"
}, },
"original": { "original": {
@ -124,12 +125,15 @@
} }
}, },
"flake-utils": { "flake-utils": {
"inputs": {
"systems": "systems"
},
"locked": { "locked": {
"lastModified": 1659877975, "lastModified": 1681202837,
"narHash": "sha256-zllb8aq3YO3h8B/U0/J1WBgAL8EX5yWf5pMj3G0NAmc=", "narHash": "sha256-H+Rh19JDwRtpVPAWp64F+rlEtxUWBAQW28eAi3SRSzg=",
"owner": "numtide", "owner": "numtide",
"repo": "flake-utils", "repo": "flake-utils",
"rev": "c0e246b9b83f637f4681389ecabcb2681b4f3af0", "rev": "cfacdce06f30d2b68473a46042957675eebb3401",
"type": "github" "type": "github"
}, },
"original": { "original": {
@ -141,11 +145,11 @@
"mk-naked-shell": { "mk-naked-shell": {
"flake": false, "flake": false,
"locked": { "locked": {
"lastModified": 1676572903, "lastModified": 1681286841,
"narHash": "sha256-oQoDHHUTxNVSURfkFcYLuAK+btjs30T4rbEUtCUyKy8=", "narHash": "sha256-3XlJrwlR0nBiREnuogoa5i1b4+w/XPe0z8bbrJASw0g=",
"owner": "yusdacra", "owner": "yusdacra",
"repo": "mk-naked-shell", "repo": "mk-naked-shell",
"rev": "aeca9f8aa592f5e8f71f407d081cb26fd30c5a57", "rev": "7612f828dd6f22b7fb332cc69440e839d7ffe6bd",
"type": "github" "type": "github"
}, },
"original": { "original": {
@ -167,11 +171,11 @@
] ]
}, },
"locked": { "locked": {
"lastModified": 1680329418, "lastModified": 1683699050,
"narHash": "sha256-+KN0eQLSZvL1J0kDO8/fxv0UCHTyZCADLmpIfeeiSGo=", "narHash": "sha256-UWKQpzVcSshB+sU2O8CCHjOSTQrNS7Kk9V3+UeBsJpg=",
"owner": "yusdacra", "owner": "yusdacra",
"repo": "nix-cargo-integration", "repo": "nix-cargo-integration",
"rev": "98c1d2ff5155f0fee5d290f6b982cb990839d540", "rev": "ed27173cd1b223f598343ea3c15aacb1d140feac",
"type": "github" "type": "github"
}, },
"original": { "original": {
@ -182,11 +186,11 @@
}, },
"nixpkgs": { "nixpkgs": {
"locked": { "locked": {
"lastModified": 1680213900, "lastModified": 1683408522,
"narHash": "sha256-cIDr5WZIj3EkKyCgj/6j3HBH4Jj1W296z7HTcWj1aMA=", "narHash": "sha256-9kcPh6Uxo17a3kK3XCHhcWiV1Yu1kYj22RHiymUhMkU=",
"owner": "nixos", "owner": "nixos",
"repo": "nixpkgs", "repo": "nixpkgs",
"rev": "e3652e0735fbec227f342712f180f4f21f0594f2", "rev": "897876e4c484f1e8f92009fd11b7d988a121a4e7",
"type": "github" "type": "github"
}, },
"original": { "original": {
@ -199,11 +203,11 @@
"nixpkgs-lib": { "nixpkgs-lib": {
"locked": { "locked": {
"dir": "lib", "dir": "lib",
"lastModified": 1678375444, "lastModified": 1682879489,
"narHash": "sha256-XIgHfGvjFvZQ8hrkfocanCDxMefc/77rXeHvYdzBMc8=", "narHash": "sha256-sASwo8gBt7JDnOOstnps90K1wxmVfyhsTPPNTGBPjjg=",
"owner": "NixOS", "owner": "NixOS",
"repo": "nixpkgs", "repo": "nixpkgs",
"rev": "130fa0baaa2b93ec45523fdcde942f6844ee9f6e", "rev": "da45bf6ec7bbcc5d1e14d3795c025199f28e0de0",
"type": "github" "type": "github"
}, },
"original": { "original": {
@ -237,11 +241,11 @@
] ]
}, },
"locked": { "locked": {
"lastModified": 1679737941, "lastModified": 1683560683,
"narHash": "sha256-srSD9CwsVPnUMsIZ7Kt/UegkKUEBcTyU1Rev7mO45S0=", "narHash": "sha256-XAygPMN5Xnk/W2c1aW0jyEa6lfMDZWlQgiNtmHXytPc=",
"owner": "hercules-ci", "owner": "hercules-ci",
"repo": "flake-parts", "repo": "flake-parts",
"rev": "3502ee99d6dade045bdeaf7b0cd8ec703484c25c", "rev": "006c75898cf814ef9497252b022e91c946ba8e17",
"type": "github" "type": "github"
}, },
"original": { "original": {
@ -255,11 +259,11 @@
"nixpkgs-lib": "nixpkgs-lib" "nixpkgs-lib": "nixpkgs-lib"
}, },
"locked": { "locked": {
"lastModified": 1679737941, "lastModified": 1683560683,
"narHash": "sha256-srSD9CwsVPnUMsIZ7Kt/UegkKUEBcTyU1Rev7mO45S0=", "narHash": "sha256-XAygPMN5Xnk/W2c1aW0jyEa6lfMDZWlQgiNtmHXytPc=",
"owner": "hercules-ci", "owner": "hercules-ci",
"repo": "flake-parts", "repo": "flake-parts",
"rev": "3502ee99d6dade045bdeaf7b0cd8ec703484c25c", "rev": "006c75898cf814ef9497252b022e91c946ba8e17",
"type": "github" "type": "github"
}, },
"original": { "original": {
@ -284,11 +288,11 @@
] ]
}, },
"locked": { "locked": {
"lastModified": 1680315536, "lastModified": 1683771545,
"narHash": "sha256-0AsBuKssJMbcRcw4HJQwJsUHhZxR5+gaf6xPQayhR44=", "narHash": "sha256-we0GYcKTo2jRQGmUGrzQ9VH0OYAUsJMCsK8UkF+vZUA=",
"owner": "oxalica", "owner": "oxalica",
"repo": "rust-overlay", "repo": "rust-overlay",
"rev": "5c8c151bdd639074a0051325c16df1a64ee23497", "rev": "c57e210faf68e5d5386f18f1b17ad8365d25e4ed",
"type": "github" "type": "github"
}, },
"original": { "original": {
@ -296,6 +300,21 @@
"repo": "rust-overlay", "repo": "rust-overlay",
"type": "github" "type": "github"
} }
},
"systems": {
"locked": {
"lastModified": 1681028828,
"narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=",
"owner": "nix-systems",
"repo": "default",
"rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e",
"type": "github"
},
"original": {
"owner": "nix-systems",
"repo": "default",
"type": "github"
}
} }
}, },
"root": "root", "root": "root",

@ -64,7 +64,7 @@
}; };
in in
inp.parts.lib.mkFlake {inputs = inp;} { inp.parts.lib.mkFlake {inputs = inp;} {
imports = [inp.nci.flakeModule]; imports = [inp.nci.flakeModule inp.parts.flakeModules.easyOverlay];
systems = [ systems = [
"x86_64-linux" "x86_64-linux"
"x86_64-darwin" "x86_64-darwin"
@ -146,6 +146,10 @@
packages.helix-dev = makeOverridableHelix config.packages.helix-unwrapped-dev {}; packages.helix-dev = makeOverridableHelix config.packages.helix-unwrapped-dev {};
packages.default = config.packages.helix; packages.default = config.packages.helix;
overlayAttrs = {
inherit (config.packages) helix;
};
devShells.default = config.nci.outputs."helix-project".devShell.overrideAttrs (old: { devShells.default = config.nci.outputs."helix-project".devShell.overrideAttrs (old: {
nativeBuildInputs = nativeBuildInputs =
(old.nativeBuildInputs or []) (old.nativeBuildInputs or [])

@ -26,12 +26,12 @@ unicode-general-category = "0.6"
# slab = "0.4.2" # slab = "0.4.2"
slotmap = "1.0" slotmap = "1.0"
tree-sitter = "0.20" tree-sitter = "0.20"
once_cell = "1.17" once_cell = "1.18"
arc-swap = "1" arc-swap = "1"
regex = "1" regex = "1"
bitflags = "2.2" bitflags = "2.3"
ahash = "0.8.3" ahash = "0.8.3"
hashbrown = { version = "0.13.2", features = ["raw"] } hashbrown = { version = "0.14.0", features = ["raw"] }
dunce = "1.0" dunce = "1.0"
log = "0.4" log = "0.4"
@ -45,7 +45,7 @@ encoding_rs = "0.8"
chrono = { version = "0.4", default-features = false, features = ["alloc", "std"] } chrono = { version = "0.4", default-features = false, features = ["alloc", "std"] }
etcetera = "0.7" etcetera = "0.8"
textwrap = "0.16.0" textwrap = "0.16.0"
steel-core = { path = "../../../steel/crates/steel-core", version = "0.2.0", features = ["modules", "anyhow", "blocking_requests"] } steel-core = { path = "../../../steel/crates/steel-core", version = "0.2.0", features = ["modules", "anyhow", "blocking_requests"] }

@ -43,6 +43,7 @@ pub struct Diagnostic {
pub message: String, pub message: String,
pub severity: Option<Severity>, pub severity: Option<Severity>,
pub code: Option<NumberOrString>, pub code: Option<NumberOrString>,
pub language_server_id: usize,
pub tags: Vec<DiagnosticTag>, pub tags: Vec<DiagnosticTag>,
pub source: Option<String>, pub source: Option<String>,
pub data: Option<serde_json::Value>, pub data: Option<serde_json::Value>,

@ -20,6 +20,10 @@ pub enum IndentStyle {
Spaces(u8), Spaces(u8),
} }
// 16 spaces
const INDENTS: &str = " ";
const MAX_INDENT: u8 = 16;
impl IndentStyle { impl IndentStyle {
/// Creates an `IndentStyle` from an indentation string. /// Creates an `IndentStyle` from an indentation string.
/// ///
@ -28,10 +32,10 @@ impl IndentStyle {
#[inline] #[inline]
pub fn from_str(indent: &str) -> Self { pub fn from_str(indent: &str) -> Self {
// XXX: do we care about validating the input more than this? Probably not...? // XXX: do we care about validating the input more than this? Probably not...?
debug_assert!(!indent.is_empty() && indent.len() <= 8); debug_assert!(!indent.is_empty() && indent.len() <= MAX_INDENT as usize);
if indent.starts_with(' ') { if indent.starts_with(' ') {
IndentStyle::Spaces(indent.len() as u8) IndentStyle::Spaces(indent.len().clamp(1, MAX_INDENT as usize) as u8)
} else { } else {
IndentStyle::Tabs IndentStyle::Tabs
} }
@ -41,20 +45,13 @@ impl IndentStyle {
pub fn as_str(&self) -> &'static str { pub fn as_str(&self) -> &'static str {
match *self { match *self {
IndentStyle::Tabs => "\t", IndentStyle::Tabs => "\t",
IndentStyle::Spaces(1) => " ",
IndentStyle::Spaces(2) => " ",
IndentStyle::Spaces(3) => " ",
IndentStyle::Spaces(4) => " ",
IndentStyle::Spaces(5) => " ",
IndentStyle::Spaces(6) => " ",
IndentStyle::Spaces(7) => " ",
IndentStyle::Spaces(8) => " ",
// Unsupported indentation style. This should never happen,
// but just in case fall back to two spaces.
IndentStyle::Spaces(n) => { IndentStyle::Spaces(n) => {
debug_assert!(n > 0 && n <= 8); // Always triggers. `debug_panic!()` wanted. // Unsupported indentation style. This should never happen,
" " debug_assert!(n > 0 && n <= MAX_INDENT);
// Either way, clamp to the nearest supported value
let closest_n = n.clamp(1, MAX_INDENT) as usize;
&INDENTS[0..closest_n]
} }
} }
} }
@ -76,9 +73,9 @@ pub fn auto_detect_indent_style(document_text: &Rope) -> Option<IndentStyle> {
// Build a histogram of the indentation *increases* between // Build a histogram of the indentation *increases* between
// subsequent lines, ignoring lines that are all whitespace. // subsequent lines, ignoring lines that are all whitespace.
// //
// Index 0 is for tabs, the rest are 1-8 spaces. // Index 0 is for tabs, the rest are 1-MAX_INDENT spaces.
let histogram: [usize; 9] = { let histogram: [usize; MAX_INDENT as usize + 1] = {
let mut histogram = [0; 9]; let mut histogram = [0; MAX_INDENT as usize + 1];
let mut prev_line_is_tabs = false; let mut prev_line_is_tabs = false;
let mut prev_line_leading_count = 0usize; let mut prev_line_leading_count = 0usize;
@ -137,7 +134,7 @@ pub fn auto_detect_indent_style(document_text: &Rope) -> Option<IndentStyle> {
histogram[0] += 1; histogram[0] += 1;
} else { } else {
let amount = leading_count - prev_line_leading_count; let amount = leading_count - prev_line_leading_count;
if amount <= 8 { if amount <= MAX_INDENT as usize {
histogram[amount] += 1; histogram[amount] += 1;
} }
} }
@ -1195,4 +1192,20 @@ mod test {
3 3
); );
} }
#[test]
fn test_large_indent_level() {
let tab_width = 16;
let indent_width = 16;
let line = Rope::from(" fn new"); // 16 spaces
assert_eq!(
indent_level_for_line(line.slice(..), tab_width, indent_width),
1
);
let line = Rope::from(" fn new"); // 32 spaces
assert_eq!(
indent_level_for_line(line.slice(..), tab_width, indent_width),
2
);
}
} }

@ -68,5 +68,5 @@ pub use syntax::Syntax;
pub use diagnostic::Diagnostic; pub use diagnostic::Diagnostic;
pub use line_ending::{LineEnding, DEFAULT_LINE_ENDING}; pub use line_ending::{LineEnding, NATIVE_LINE_ENDING};
pub use transaction::{Assoc, Change, ChangeSet, Operation, Transaction}; pub use transaction::{Assoc, Change, ChangeSet, Deletion, Operation, Transaction};

@ -1,9 +1,9 @@
use crate::{Rope, RopeSlice}; use crate::{Rope, RopeSlice};
#[cfg(target_os = "windows")] #[cfg(target_os = "windows")]
pub const DEFAULT_LINE_ENDING: LineEnding = LineEnding::Crlf; pub const NATIVE_LINE_ENDING: LineEnding = LineEnding::Crlf;
#[cfg(not(target_os = "windows"))] #[cfg(not(target_os = "windows"))]
pub const DEFAULT_LINE_ENDING: LineEnding = LineEnding::LF; pub const NATIVE_LINE_ENDING: LineEnding = LineEnding::LF;
/// Represents one of the valid Unicode line endings. /// Represents one of the valid Unicode line endings.
#[derive(PartialEq, Eq, Copy, Clone, Debug)] #[derive(PartialEq, Eq, Copy, Clone, Debug)]

@ -1,7 +1,15 @@
use std::iter;
use ropey::RopeSlice;
use tree_sitter::Node; use tree_sitter::Node;
use crate::{Rope, Syntax}; use crate::movement::Direction::{self, Backward, Forward};
use crate::Syntax;
const MAX_PLAINTEXT_SCAN: usize = 10000;
const MATCH_LIMIT: usize = 16;
// Limit matching pairs to only ( ) { } [ ] < > ' ' " "
const PAIRS: &[(char, char)] = &[ const PAIRS: &[(char, char)] = &[
('(', ')'), ('(', ')'),
('{', '}'), ('{', '}'),
@ -11,17 +19,17 @@ const PAIRS: &[(char, char)] = &[
('\"', '\"'), ('\"', '\"'),
]; ];
// limit matching pairs to only ( ) { } [ ] < > ' ' " " /// Returns the position of the matching bracket under cursor.
///
// Returns the position of the matching bracket under cursor. /// If the cursor is on the opening bracket, the position of
// /// the closing bracket is returned. If the cursor on the closing
// If the cursor is one the opening bracket, the position of /// bracket, the position of the opening bracket is returned.
// the closing bracket is returned. If the cursor in the closing ///
// bracket, the position of the opening bracket is returned. /// If the cursor is not on a bracket, `None` is returned.
// ///
// If the cursor is not on a bracket, `None` is returned. /// If no matching bracket is found, `None` is returned.
#[must_use] #[must_use]
pub fn find_matching_bracket(syntax: &Syntax, doc: &Rope, pos: usize) -> Option<usize> { pub fn find_matching_bracket(syntax: &Syntax, doc: RopeSlice, pos: usize) -> Option<usize> {
if pos >= doc.len_chars() || !is_valid_bracket(doc.char(pos)) { if pos >= doc.len_chars() || !is_valid_bracket(doc.char(pos)) {
return None; return None;
} }
@ -39,17 +47,23 @@ pub fn find_matching_bracket(syntax: &Syntax, doc: &Rope, pos: usize) -> Option<
// //
// If no surrounding scope is found, the function returns `None`. // If no surrounding scope is found, the function returns `None`.
#[must_use] #[must_use]
pub fn find_matching_bracket_fuzzy(syntax: &Syntax, doc: &Rope, pos: usize) -> Option<usize> { pub fn find_matching_bracket_fuzzy(syntax: &Syntax, doc: RopeSlice, pos: usize) -> Option<usize> {
find_pair(syntax, doc, pos, true) find_pair(syntax, doc, pos, true)
} }
fn find_pair(syntax: &Syntax, doc: &Rope, pos: usize, traverse_parents: bool) -> Option<usize> { fn find_pair(
syntax: &Syntax,
doc: RopeSlice,
pos_: usize,
traverse_parents: bool,
) -> Option<usize> {
let tree = syntax.tree(); let tree = syntax.tree();
let pos = doc.char_to_byte(pos); let pos = doc.char_to_byte(pos_);
let mut node = tree.root_node().named_descendant_for_byte_range(pos, pos)?; let mut node = tree.root_node().descendant_for_byte_range(pos, pos)?;
loop { loop {
if node.is_named() {
let (start_byte, end_byte) = surrounding_bytes(doc, &node)?; let (start_byte, end_byte) = surrounding_bytes(doc, &node)?;
let (start_char, end_char) = (doc.byte_to_char(start_byte), doc.byte_to_char(end_byte)); let (start_char, end_char) = (doc.byte_to_char(start_byte), doc.byte_to_char(end_byte));
@ -57,28 +71,131 @@ fn find_pair(syntax: &Syntax, doc: &Rope, pos: usize, traverse_parents: bool) ->
if end_byte == pos { if end_byte == pos {
return Some(start_char); return Some(start_char);
} }
// We return the end char if the cursor is either on the start char // We return the end char if the cursor is either on the start char
// or at some arbitrary position between start and end char. // or at some arbitrary position between start and end char.
if traverse_parents || start_byte == pos {
return Some(end_char); return Some(end_char);
} }
}
}
// this node itselt wasn't a pair but maybe its siblings are
if traverse_parents { // check if we are *on* the pair (special cased so we don't look
node = node.parent()?; // at the current node twice and to jump to the start on that case)
} else { if let Some(open) = as_close_pair(doc, &node) {
if let Some(pair_start) = find_pair_end(doc, node.prev_sibling(), open, Backward) {
return Some(pair_start);
}
}
if !traverse_parents {
// check if we are *on* the opening pair (special cased here as
// an opptimization since we only care about bracket on the cursor
// here)
if let Some(close) = as_open_pair(doc, &node) {
if let Some(pair_end) = find_pair_end(doc, node.next_sibling(), close, Forward) {
return Some(pair_end);
}
}
if node.is_named() {
break;
}
}
for close in
iter::successors(node.next_sibling(), |node| node.next_sibling()).take(MATCH_LIMIT)
{
let Some(open) = as_close_pair(doc, &close) else { continue; };
if find_pair_end(doc, Some(node), open, Backward).is_some() {
return doc.try_byte_to_char(close.start_byte()).ok();
}
}
let Some(parent) = node.parent() else { break; };
node = parent;
}
let node = tree.root_node().named_descendant_for_byte_range(pos, pos)?;
if node.child_count() != 0 {
return None;
}
let node_start = doc.byte_to_char(node.start_byte());
find_matching_bracket_plaintext(doc.byte_slice(node.byte_range()), pos_ - node_start)
.map(|pos| pos + node_start)
}
/// Returns the position of the matching bracket under cursor.
/// This function works on plain text and ignores tree-sitter grammar.
/// The search is limited to `MAX_PLAINTEXT_SCAN` characters
///
/// If the cursor is on the opening bracket, the position of
/// the closing bracket is returned. If the cursor on the closing
/// bracket, the position of the opening bracket is returned.
///
/// If the cursor is not on a bracket, `None` is returned.
///
/// If no matching bracket is found, `None` is returned.
#[must_use]
pub fn find_matching_bracket_plaintext(doc: RopeSlice, cursor_pos: usize) -> Option<usize> {
// Don't do anything when the cursor is not on top of a bracket.
let bracket = doc.char(cursor_pos);
if !is_valid_bracket(bracket) {
return None; return None;
} }
// Determine the direction of the matching.
let is_fwd = is_forward_bracket(bracket);
let chars_iter = if is_fwd {
doc.chars_at(cursor_pos + 1)
} else {
doc.chars_at(cursor_pos).reversed()
};
let mut open_cnt = 1;
for (i, candidate) in chars_iter.take(MAX_PLAINTEXT_SCAN).enumerate() {
if candidate == bracket {
open_cnt += 1;
} else if is_valid_pair(
doc,
if is_fwd {
cursor_pos
} else {
cursor_pos - i - 1
},
if is_fwd {
cursor_pos + i + 1
} else {
cursor_pos
},
) {
// Return when all pending brackets have been closed.
if open_cnt == 1 {
return Some(if is_fwd {
cursor_pos + i + 1
} else {
cursor_pos - i - 1
});
}
open_cnt -= 1;
}
} }
None
} }
fn is_valid_bracket(c: char) -> bool { fn is_valid_bracket(c: char) -> bool {
PAIRS.iter().any(|(l, r)| *l == c || *r == c) PAIRS.iter().any(|(l, r)| *l == c || *r == c)
} }
fn is_valid_pair(doc: &Rope, start_char: usize, end_char: usize) -> bool { fn is_forward_bracket(c: char) -> bool {
PAIRS.iter().any(|(l, _)| *l == c)
}
fn is_valid_pair(doc: RopeSlice, start_char: usize, end_char: usize) -> bool {
PAIRS.contains(&(doc.char(start_char), doc.char(end_char))) PAIRS.contains(&(doc.char(start_char), doc.char(end_char)))
} }
fn surrounding_bytes(doc: &Rope, node: &Node) -> Option<(usize, usize)> { fn surrounding_bytes(doc: RopeSlice, node: &Node) -> Option<(usize, usize)> {
let len = doc.len_bytes(); let len = doc.len_bytes();
let start_byte = node.start_byte(); let start_byte = node.start_byte();
@ -90,3 +207,85 @@ fn surrounding_bytes(doc: &Rope, node: &Node) -> Option<(usize, usize)> {
Some((start_byte, end_byte)) Some((start_byte, end_byte))
} }
/// Tests if this node is a pair close char and returns the expected open char
fn as_close_pair(doc: RopeSlice, node: &Node) -> Option<char> {
let close = as_char(doc, node)?.1;
PAIRS
.iter()
.find_map(|&(open, close_)| (close_ == close).then_some(open))
}
/// Checks if `node` or its siblings (at most MATCH_LIMIT nodes) is the specified closing char
///
/// # Returns
///
/// The position of the found node or `None` otherwise
fn find_pair_end(
doc: RopeSlice,
node: Option<Node>,
end_char: char,
direction: Direction,
) -> Option<usize> {
let advance = match direction {
Forward => Node::next_sibling,
Backward => Node::prev_sibling,
};
iter::successors(node, advance)
.take(MATCH_LIMIT)
.find_map(|node| {
let (pos, c) = as_char(doc, &node)?;
(end_char == c).then_some(pos)
})
}
/// Tests if this node is a pair close char and returns the expected open char
fn as_open_pair(doc: RopeSlice, node: &Node) -> Option<char> {
let open = as_char(doc, node)?.1;
PAIRS
.iter()
.find_map(|&(open_, close)| (open_ == open).then_some(close))
}
/// If node is a single char return it (and its char position)
fn as_char(doc: RopeSlice, node: &Node) -> Option<(usize, char)> {
// TODO: multi char/non ASCII pairs
if node.byte_range().len() != 1 {
return None;
}
let pos = doc.try_byte_to_char(node.start_byte()).ok()?;
Some((pos, doc.char(pos)))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_find_matching_bracket_current_line_plaintext() {
let assert = |input: &str, pos, expected| {
let input = RopeSlice::from(input);
let actual = find_matching_bracket_plaintext(input, pos);
assert_eq!(expected, actual.unwrap());
let actual = find_matching_bracket_plaintext(input, expected);
assert_eq!(pos, actual.unwrap(), "expected symmetrical behaviour");
};
assert("(hello)", 0, 6);
assert("((hello))", 0, 8);
assert("((hello))", 1, 7);
assert("(((hello)))", 2, 8);
assert("key: ${value}", 6, 12);
assert("key: ${value} # (some comment)", 16, 29);
assert("(paren (paren {bracket}))", 0, 24);
assert("(paren (paren {bracket}))", 7, 23);
assert("(paren (paren {bracket}))", 14, 22);
assert("(prev line\n ) (middle) ( \n next line)", 0, 12);
assert("(prev line\n ) (middle) ( \n next line)", 14, 21);
assert("(prev line\n ) (middle) ( \n next line)", 23, 36);
}
}

@ -1,4 +1,4 @@
use std::iter; use std::{cmp::Reverse, iter};
use ropey::iter::Chars; use ropey::iter::Chars;
use tree_sitter::{Node, QueryCursor}; use tree_sitter::{Node, QueryCursor};
@ -177,6 +177,10 @@ pub fn move_prev_word_start(slice: RopeSlice, range: Range, count: usize) -> Ran
word_move(slice, range, count, WordMotionTarget::PrevWordStart) word_move(slice, range, count, WordMotionTarget::PrevWordStart)
} }
pub fn move_prev_word_end(slice: RopeSlice, range: Range, count: usize) -> Range {
word_move(slice, range, count, WordMotionTarget::PrevWordEnd)
}
pub fn move_next_long_word_start(slice: RopeSlice, range: Range, count: usize) -> Range { pub fn move_next_long_word_start(slice: RopeSlice, range: Range, count: usize) -> Range {
word_move(slice, range, count, WordMotionTarget::NextLongWordStart) word_move(slice, range, count, WordMotionTarget::NextLongWordStart)
} }
@ -189,8 +193,8 @@ pub fn move_prev_long_word_start(slice: RopeSlice, range: Range, count: usize) -
word_move(slice, range, count, WordMotionTarget::PrevLongWordStart) word_move(slice, range, count, WordMotionTarget::PrevLongWordStart)
} }
pub fn move_prev_word_end(slice: RopeSlice, range: Range, count: usize) -> Range { pub fn move_prev_long_word_end(slice: RopeSlice, range: Range, count: usize) -> Range {
word_move(slice, range, count, WordMotionTarget::PrevWordEnd) word_move(slice, range, count, WordMotionTarget::PrevLongWordEnd)
} }
fn word_move(slice: RopeSlice, range: Range, count: usize, target: WordMotionTarget) -> Range { fn word_move(slice: RopeSlice, range: Range, count: usize, target: WordMotionTarget) -> Range {
@ -199,6 +203,7 @@ fn word_move(slice: RopeSlice, range: Range, count: usize, target: WordMotionTar
WordMotionTarget::PrevWordStart WordMotionTarget::PrevWordStart
| WordMotionTarget::PrevLongWordStart | WordMotionTarget::PrevLongWordStart
| WordMotionTarget::PrevWordEnd | WordMotionTarget::PrevWordEnd
| WordMotionTarget::PrevLongWordEnd
); );
// Special-case early-out. // Special-case early-out.
@ -377,6 +382,7 @@ pub enum WordMotionTarget {
NextLongWordStart, NextLongWordStart,
NextLongWordEnd, NextLongWordEnd,
PrevLongWordStart, PrevLongWordStart,
PrevLongWordEnd,
} }
pub trait CharHelpers { pub trait CharHelpers {
@ -393,6 +399,7 @@ impl CharHelpers for Chars<'_> {
WordMotionTarget::PrevWordStart WordMotionTarget::PrevWordStart
| WordMotionTarget::PrevLongWordStart | WordMotionTarget::PrevLongWordStart
| WordMotionTarget::PrevWordEnd | WordMotionTarget::PrevWordEnd
| WordMotionTarget::PrevLongWordEnd
); );
// Reverse the iterator if needed for the motion direction. // Reverse the iterator if needed for the motion direction.
@ -479,7 +486,7 @@ fn reached_target(target: WordMotionTarget, prev_ch: char, next_ch: char) -> boo
is_word_boundary(prev_ch, next_ch) is_word_boundary(prev_ch, next_ch)
&& (!prev_ch.is_whitespace() || char_is_line_ending(next_ch)) && (!prev_ch.is_whitespace() || char_is_line_ending(next_ch))
} }
WordMotionTarget::NextLongWordStart => { WordMotionTarget::NextLongWordStart | WordMotionTarget::PrevLongWordEnd => {
is_long_word_boundary(prev_ch, next_ch) is_long_word_boundary(prev_ch, next_ch)
&& (char_is_line_ending(next_ch) || !next_ch.is_whitespace()) && (char_is_line_ending(next_ch) || !next_ch.is_whitespace())
} }
@ -520,10 +527,10 @@ pub fn goto_treesitter_object(
let node = match dir { let node = match dir {
Direction::Forward => nodes Direction::Forward => nodes
.filter(|n| n.start_byte() > byte_pos) .filter(|n| n.start_byte() > byte_pos)
.min_by_key(|n| n.start_byte())?, .min_by_key(|n| (n.start_byte(), Reverse(n.end_byte())))?,
Direction::Backward => nodes Direction::Backward => nodes
.filter(|n| n.end_byte() < byte_pos) .filter(|n| n.end_byte() < byte_pos)
.max_by_key(|n| n.end_byte())?, .max_by_key(|n| (n.end_byte(), Reverse(n.start_byte())))?,
}; };
let len = slice.len_bytes(); let len = slice.len_bytes();
@ -1445,6 +1452,100 @@ mod test {
} }
} }
#[test]
fn test_behaviour_when_moving_to_end_of_prev_long_words() {
let tests = [
(
"Basic backward motion from the middle of a word",
vec![(1, Range::new(3, 3), Range::new(4, 0))],
),
("Starting from after boundary retreats the anchor",
vec![(1, Range::new(0, 9), Range::new(8, 0))],
),
(
"Jump to end of a word succeeded by whitespace",
vec![(1, Range::new(10, 10), Range::new(10, 4))],
),
(
" Jump to start of line from end of word preceded by whitespace",
vec![(1, Range::new(3, 4), Range::new(4, 0))],
),
("Previous anchor is irrelevant for backward motions",
vec![(1, Range::new(12, 5), Range::new(6, 0))]),
(
" Starting from whitespace moves to first space in sequence",
vec![(1, Range::new(0, 4), Range::new(4, 0))],
),
("Identifiers_with_underscores are considered a single word",
vec![(1, Range::new(0, 20), Range::new(20, 0))]),
(
"Jumping\n \nback through a newline selects whitespace",
vec![(1, Range::new(0, 13), Range::new(12, 8))],
),
(
"Jumping to start of word from the end selects the word",
vec![(1, Range::new(6, 7), Range::new(7, 0))],
),
(
"alphanumeric.!,and.?=punctuation are treated exactly the same",
vec![(1, Range::new(29, 30), Range::new(30, 0))],
),
(
"... ... punctuation and spaces behave as expected",
vec![
(1, Range::new(0, 10), Range::new(9, 3)),
(1, Range::new(10, 6), Range::new(7, 3)),
],
),
(".._.._ punctuation is joined by underscores into a single block",
vec![(1, Range::new(0, 6), Range::new(6, 0))]),
(
"Newlines\n\nare bridged seamlessly.",
vec![(1, Range::new(0, 10), Range::new(8, 0))],
),
(
"Jumping \n\n\n\n\nback from within a newline group selects previous block",
vec![(1, Range::new(0, 13), Range::new(11, 7))],
),
(
"Failed motions do not modify the range",
vec![(0, Range::new(3, 0), Range::new(3, 0))],
),
(
"Multiple motions at once resolve correctly",
vec![(3, Range::new(19, 19), Range::new(8, 0))],
),
(
"Excessive motions are performed partially",
vec![(999, Range::new(40, 40), Range::new(9, 0))],
),
(
"", // Edge case of moving backwards in empty string
vec![(1, Range::new(0, 0), Range::new(0, 0))],
),
(
"\n\n\n\n\n", // Edge case of moving backwards in all newlines
vec![(1, Range::new(5, 5), Range::new(0, 0))],
),
(" \n \nJumping back through alternated space blocks and newlines selects the space blocks",
vec![
(1, Range::new(0, 8), Range::new(7, 4)),
(1, Range::new(7, 4), Range::new(3, 0)),
]),
("ヒーリ..クス multibyte characters behave as normal characters, including when interacting with punctuation",
vec![
(1, Range::new(0, 8), Range::new(7, 0)),
]),
];
for (sample, scenario) in tests {
for (count, begin, expected_end) in scenario.into_iter() {
let range = move_prev_long_word_end(Rope::from(sample).slice(..), begin, count);
assert_eq!(range, expected_end, "Case failed: [{}]", sample);
}
}
}
#[test] #[test]
fn test_behaviour_when_moving_to_prev_paragraph_single() { fn test_behaviour_when_moving_to_prev_paragraph_single() {
let tests = [ let tests = [

@ -161,34 +161,35 @@ impl Range {
self.from() <= pos && pos < self.to() self.from() <= pos && pos < self.to()
} }
/// Map a range through a set of changes. Returns a new range representing the same position /// Map a range through a set of changes. Returns a new range representing
/// after the changes are applied. /// the same position after the changes are applied. Note that this
pub fn map(self, changes: &ChangeSet) -> Self { /// function runs in O(N) (N is number of changes) and can therefore
/// cause performance problems if run for a large number of ranges as the
/// complexity is then O(MN) (for multicuror M=N usually). Instead use
/// [Selection::map] or [ChangeSet::update_positions] instead
pub fn map(mut self, changes: &ChangeSet) -> Self {
use std::cmp::Ordering; use std::cmp::Ordering;
let (anchor, head) = match self.anchor.cmp(&self.head) { if changes.is_empty() {
Ordering::Equal => ( return self;
changes.map_pos(self.anchor, Assoc::After),
changes.map_pos(self.head, Assoc::After),
),
Ordering::Less => (
changes.map_pos(self.anchor, Assoc::After),
changes.map_pos(self.head, Assoc::Before),
),
Ordering::Greater => (
changes.map_pos(self.anchor, Assoc::Before),
changes.map_pos(self.head, Assoc::After),
),
};
// We want to return a new `Range` with `horiz == None` every time,
// even if the anchor and head haven't changed, because we don't
// know if the *visual* position hasn't changed due to
// character-width or grapheme changes earlier in the text.
Self {
anchor,
head,
old_visual_position: None,
} }
let positions_to_map = match self.anchor.cmp(&self.head) {
Ordering::Equal => [
(&mut self.anchor, Assoc::After),
(&mut self.head, Assoc::After),
],
Ordering::Less => [
(&mut self.anchor, Assoc::After),
(&mut self.head, Assoc::Before),
],
Ordering::Greater => [
(&mut self.head, Assoc::After),
(&mut self.anchor, Assoc::Before),
],
};
changes.update_positions(positions_to_map.into_iter());
self.old_visual_position = None;
self
} }
/// Extend the range to cover at least `from` `to`. /// Extend the range to cover at least `from` `to`.
@ -451,17 +452,36 @@ impl Selection {
/// Map selections over a set of changes. Useful for adjusting the selection position after /// Map selections over a set of changes. Useful for adjusting the selection position after
/// applying changes to a document. /// applying changes to a document.
pub fn map(self, changes: &ChangeSet) -> Self { pub fn map(self, changes: &ChangeSet) -> Self {
self.map_no_normalize(changes).normalize()
}
/// Map selections over a set of changes. Useful for adjusting the selection position after
/// applying changes to a document. Doesn't normalize the selection
pub fn map_no_normalize(mut self, changes: &ChangeSet) -> Self {
if changes.is_empty() { if changes.is_empty() {
return self; return self;
} }
Self::new( let positions_to_map = self.ranges.iter_mut().flat_map(|range| {
self.ranges use std::cmp::Ordering;
.into_iter() range.old_visual_position = None;
.map(|range| range.map(changes)) match range.anchor.cmp(&range.head) {
.collect(), Ordering::Equal => [
self.primary_index, (&mut range.anchor, Assoc::After),
) (&mut range.head, Assoc::After),
],
Ordering::Less => [
(&mut range.anchor, Assoc::After),
(&mut range.head, Assoc::Before),
],
Ordering::Greater => [
(&mut range.head, Assoc::After),
(&mut range.anchor, Assoc::Before),
],
}
});
changes.update_positions(positions_to_map);
self
} }
pub fn ranges(&self) -> &[Range] { pub fn ranges(&self) -> &[Range] {
@ -497,6 +517,9 @@ impl Selection {
/// Normalizes a `Selection`. /// Normalizes a `Selection`.
fn normalize(mut self) -> Self { fn normalize(mut self) -> Self {
if self.len() < 2 {
return self;
}
let mut primary = self.ranges[self.primary_index]; let mut primary = self.ranges[self.primary_index];
self.ranges.sort_unstable_by_key(Range::from); self.ranges.sort_unstable_by_key(Range::from);
@ -522,7 +545,14 @@ impl Selection {
self self
} }
// Merges all ranges that are consecutive /// Replaces ranges with one spanning from first to last range.
pub fn merge_ranges(self) -> Self {
let first = self.ranges.first().unwrap();
let last = self.ranges.last().unwrap();
Selection::new(smallvec![first.merge(*last)], 0)
}
/// Merges all ranges that are consecutive.
pub fn merge_consecutive_ranges(mut self) -> Self { pub fn merge_consecutive_ranges(mut self) -> Self {
let mut primary = self.ranges[self.primary_index]; let mut primary = self.ranges[self.primary_index];
@ -554,17 +584,12 @@ impl Selection {
assert!(!ranges.is_empty()); assert!(!ranges.is_empty());
debug_assert!(primary_index < ranges.len()); debug_assert!(primary_index < ranges.len());
let mut selection = Self { let selection = Self {
ranges, ranges,
primary_index, primary_index,
}; };
if selection.ranges.len() > 1 { selection.normalize()
// TODO: only normalize if needed (any ranges out of order)
selection = selection.normalize();
}
selection
} }
/// Takes a closure and maps each `Range` over the closure. /// Takes a closure and maps each `Range` over the closure.

@ -397,15 +397,10 @@ mod test {
let selections: SmallVec<[Range; 1]> = spec let selections: SmallVec<[Range; 1]> = spec
.match_indices('^') .match_indices('^')
.into_iter()
.map(|(i, _)| Range::point(i)) .map(|(i, _)| Range::point(i))
.collect(); .collect();
let expectations: Vec<usize> = spec let expectations: Vec<usize> = spec.match_indices('_').map(|(i, _)| i).collect();
.match_indices('_')
.into_iter()
.map(|(i, _)| i)
.collect();
(rope, Selection::new(selections, 0), expectations) (rope, Selection::new(selections, 0), expectations)
} }

@ -16,8 +16,8 @@ use slotmap::{DefaultKey as LayerId, HopSlotMap};
use std::{ use std::{
borrow::Cow, borrow::Cow,
cell::RefCell, cell::RefCell,
collections::{HashMap, VecDeque}, collections::{HashMap, HashSet, VecDeque},
fmt, fmt::{self, Display},
hash::{Hash, Hasher}, hash::{Hash, Hasher},
mem::{replace, transmute}, mem::{replace, transmute},
path::{Path, PathBuf}, path::{Path, PathBuf},
@ -26,7 +26,7 @@ use std::{
}; };
use once_cell::sync::{Lazy, OnceCell}; use once_cell::sync::{Lazy, OnceCell};
use serde::{Deserialize, Serialize}; use serde::{ser::SerializeSeq, Deserialize, Serialize};
use helix_loader::grammar::{get_language, load_runtime_file}; use helix_loader::grammar::{get_language, load_runtime_file};
@ -48,6 +48,21 @@ where
.transpose() .transpose()
} }
fn deserialize_tab_width<'de, D>(deserializer: D) -> Result<usize, D::Error>
where
D: serde::Deserializer<'de>,
{
usize::deserialize(deserializer).and_then(|n| {
if n > 0 && n <= 16 {
Ok(n)
} else {
Err(serde::de::Error::custom(
"tab width must be a value from 1 to 16 inclusive",
))
}
})
}
pub fn deserialize_auto_pairs<'de, D>(deserializer: D) -> Result<Option<AutoPairs>, D::Error> pub fn deserialize_auto_pairs<'de, D>(deserializer: D) -> Result<Option<AutoPairs>, D::Error>
where where
D: serde::Deserializer<'de>, D: serde::Deserializer<'de>,
@ -60,8 +75,11 @@ fn default_timeout() -> u64 {
} }
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct Configuration { pub struct Configuration {
pub language: Vec<LanguageConfiguration>, pub language: Vec<LanguageConfiguration>,
#[serde(default)]
pub language_server: HashMap<String, LanguageServerConfiguration>,
} }
impl Default for Configuration { impl Default for Configuration {
@ -75,7 +93,10 @@ impl Default for Configuration {
#[serde(rename_all = "kebab-case", deny_unknown_fields)] #[serde(rename_all = "kebab-case", deny_unknown_fields)]
pub struct LanguageConfiguration { pub struct LanguageConfiguration {
#[serde(rename = "name")] #[serde(rename = "name")]
pub language_id: String, // c-sharp, rust pub language_id: String, // c-sharp, rust, tsx
#[serde(rename = "language-id")]
// see the table under https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocumentItem
pub language_server_language_id: Option<String>, // csharp, rust, typescriptreact, for the language-server
pub scope: String, // source.rust pub scope: String, // source.rust
pub file_types: Vec<FileType>, // filename extension or ends_with? <Gemfile, rb, etc> pub file_types: Vec<FileType>, // filename extension or ends_with? <Gemfile, rb, etc>
#[serde(default)] #[serde(default)]
@ -85,9 +106,6 @@ pub struct LanguageConfiguration {
pub text_width: Option<usize>, pub text_width: Option<usize>,
pub soft_wrap: Option<SoftWrap>, pub soft_wrap: Option<SoftWrap>,
#[serde(default, skip_serializing, deserialize_with = "deserialize_lsp_config")]
pub config: Option<serde_json::Value>,
#[serde(default)] #[serde(default)]
pub auto_format: bool, pub auto_format: bool,
@ -107,8 +125,13 @@ pub struct LanguageConfiguration {
#[serde(skip)] #[serde(skip)]
pub(crate) highlight_config: OnceCell<Option<Arc<HighlightConfiguration>>>, pub(crate) highlight_config: OnceCell<Option<Arc<HighlightConfiguration>>>,
// tags_config OnceCell<> https://github.com/tree-sitter/tree-sitter/pull/583 // tags_config OnceCell<> https://github.com/tree-sitter/tree-sitter/pull/583
#[serde(skip_serializing_if = "Option::is_none")] #[serde(
pub language_server: Option<LanguageServerConfiguration>, default,
skip_serializing_if = "Vec::is_empty",
serialize_with = "serialize_lang_features",
deserialize_with = "deserialize_lang_features"
)]
pub language_servers: Vec<LanguageServerFeatures>,
#[serde(skip_serializing_if = "Option::is_none")] #[serde(skip_serializing_if = "Option::is_none")]
pub indent: Option<IndentationConfiguration>, pub indent: Option<IndentationConfiguration>,
@ -187,9 +210,12 @@ impl<'de> Deserialize<'de> for FileType {
M: serde::de::MapAccess<'de>, M: serde::de::MapAccess<'de>,
{ {
match map.next_entry::<String, String>()? { match map.next_entry::<String, String>()? {
Some((key, suffix)) if key == "suffix" => Ok(FileType::Suffix( Some((key, suffix)) if key == "suffix" => Ok(FileType::Suffix({
suffix.replace('/', &std::path::MAIN_SEPARATOR.to_string()), // FIXME: use `suffix.replace('/', std::path::MAIN_SEPARATOR_STR)`
)), // if MSRV is updated to 1.68
let mut separator = [0; 1];
suffix.replace('/', std::path::MAIN_SEPARATOR.encode_utf8(&mut separator))
})),
Some((key, _value)) => Err(serde::de::Error::custom(format!( Some((key, _value)) => Err(serde::de::Error::custom(format!(
"unknown key in `file-types` list: {}", "unknown key in `file-types` list: {}",
key key
@ -205,6 +231,133 @@ impl<'de> Deserialize<'de> for FileType {
} }
} }
#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq, Eq, Hash)]
#[serde(rename_all = "kebab-case")]
pub enum LanguageServerFeature {
Format,
GotoDeclaration,
GotoDefinition,
GotoTypeDefinition,
GotoReference,
GotoImplementation,
// Goto, use bitflags, combining previous Goto members?
SignatureHelp,
Hover,
DocumentHighlight,
Completion,
CodeAction,
WorkspaceCommand,
DocumentSymbols,
WorkspaceSymbols,
// Symbols, use bitflags, see above?
Diagnostics,
RenameSymbol,
InlayHints,
}
impl Display for LanguageServerFeature {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
use LanguageServerFeature::*;
let feature = match self {
Format => "format",
GotoDeclaration => "goto-declaration",
GotoDefinition => "goto-definition",
GotoTypeDefinition => "goto-type-definition",
GotoReference => "goto-type-definition",
GotoImplementation => "goto-implementation",
SignatureHelp => "signature-help",
Hover => "hover",
DocumentHighlight => "document-highlight",
Completion => "completion",
CodeAction => "code-action",
WorkspaceCommand => "workspace-command",
DocumentSymbols => "document-symbols",
WorkspaceSymbols => "workspace-symbols",
Diagnostics => "diagnostics",
RenameSymbol => "rename-symbol",
InlayHints => "inlay-hints",
};
write!(f, "{feature}",)
}
}
#[derive(Debug, Serialize, Deserialize)]
#[serde(untagged, rename_all = "kebab-case", deny_unknown_fields)]
enum LanguageServerFeatureConfiguration {
#[serde(rename_all = "kebab-case")]
Features {
#[serde(default, skip_serializing_if = "HashSet::is_empty")]
only_features: HashSet<LanguageServerFeature>,
#[serde(default, skip_serializing_if = "HashSet::is_empty")]
except_features: HashSet<LanguageServerFeature>,
name: String,
},
Simple(String),
}
#[derive(Debug, Default)]
pub struct LanguageServerFeatures {
pub name: String,
pub only: HashSet<LanguageServerFeature>,
pub excluded: HashSet<LanguageServerFeature>,
}
impl LanguageServerFeatures {
pub fn has_feature(&self, feature: LanguageServerFeature) -> bool {
(self.only.is_empty() || self.only.contains(&feature)) && !self.excluded.contains(&feature)
}
}
fn deserialize_lang_features<'de, D>(
deserializer: D,
) -> Result<Vec<LanguageServerFeatures>, D::Error>
where
D: serde::Deserializer<'de>,
{
let raw: Vec<LanguageServerFeatureConfiguration> = Deserialize::deserialize(deserializer)?;
let res = raw
.into_iter()
.map(|config| match config {
LanguageServerFeatureConfiguration::Simple(name) => LanguageServerFeatures {
name,
..Default::default()
},
LanguageServerFeatureConfiguration::Features {
only_features,
except_features,
name,
} => LanguageServerFeatures {
name,
only: only_features,
excluded: except_features,
},
})
.collect();
Ok(res)
}
fn serialize_lang_features<S>(
map: &Vec<LanguageServerFeatures>,
serializer: S,
) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
let mut serializer = serializer.serialize_seq(Some(map.len()))?;
for features in map {
let features = if features.only.is_empty() && features.excluded.is_empty() {
LanguageServerFeatureConfiguration::Simple(features.name.to_owned())
} else {
LanguageServerFeatureConfiguration::Features {
only_features: features.only.clone(),
except_features: features.excluded.clone(),
name: features.name.to_owned(),
}
};
serializer.serialize_element(&features)?;
}
serializer.end()
}
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")] #[serde(rename_all = "kebab-case")]
pub struct LanguageServerConfiguration { pub struct LanguageServerConfiguration {
@ -214,9 +367,10 @@ pub struct LanguageServerConfiguration {
pub args: Vec<String>, pub args: Vec<String>,
#[serde(default, skip_serializing_if = "HashMap::is_empty")] #[serde(default, skip_serializing_if = "HashMap::is_empty")]
pub environment: HashMap<String, String>, pub environment: HashMap<String, String>,
#[serde(default, skip_serializing, deserialize_with = "deserialize_lsp_config")]
pub config: Option<serde_json::Value>,
#[serde(default = "default_timeout")] #[serde(default = "default_timeout")]
pub timeout: u64, pub timeout: u64,
pub language_id: Option<String>,
} }
#[derive(Debug, Clone, Serialize, Deserialize)] #[derive(Debug, Clone, Serialize, Deserialize)]
@ -285,6 +439,7 @@ pub struct DebuggerQuirks {
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")] #[serde(rename_all = "kebab-case")]
pub struct IndentationConfiguration { pub struct IndentationConfiguration {
#[serde(deserialize_with = "deserialize_tab_width")]
pub tab_width: usize, pub tab_width: usize,
pub unit: String, pub unit: String,
} }
@ -602,6 +757,8 @@ pub struct Loader {
language_config_ids_by_suffix: HashMap<String, usize>, language_config_ids_by_suffix: HashMap<String, usize>,
language_config_ids_by_shebang: HashMap<String, usize>, language_config_ids_by_shebang: HashMap<String, usize>,
language_server_configs: HashMap<String, LanguageServerConfiguration>,
scopes: ArcSwap<Vec<String>>, scopes: ArcSwap<Vec<String>>,
} }
@ -609,6 +766,7 @@ impl Loader {
pub fn new(config: Configuration) -> Self { pub fn new(config: Configuration) -> Self {
let mut loader = Self { let mut loader = Self {
language_configs: Vec::new(), language_configs: Vec::new(),
language_server_configs: config.language_server,
language_config_ids_by_extension: HashMap::new(), language_config_ids_by_extension: HashMap::new(),
language_config_ids_by_suffix: HashMap::new(), language_config_ids_by_suffix: HashMap::new(),
language_config_ids_by_shebang: HashMap::new(), language_config_ids_by_shebang: HashMap::new(),
@ -733,6 +891,10 @@ impl Loader {
self.language_configs.iter() self.language_configs.iter()
} }
pub fn language_server_configs(&self) -> &HashMap<String, LanguageServerConfiguration> {
&self.language_server_configs
}
pub fn set_scopes(&self, scopes: Vec<String>) { pub fn set_scopes(&self, scopes: Vec<String>) {
self.scopes.store(Arc::new(scopes)); self.scopes.store(Arc::new(scopes));
@ -776,7 +938,11 @@ fn byte_range_to_str(range: std::ops::Range<usize>, source: RopeSlice) -> Cow<st
} }
impl Syntax { impl Syntax {
pub fn new(source: &Rope, config: Arc<HighlightConfiguration>, loader: Arc<Loader>) -> Self { pub fn new(
source: &Rope,
config: Arc<HighlightConfiguration>,
loader: Arc<Loader>,
) -> Option<Self> {
let root_layer = LanguageLayer { let root_layer = LanguageLayer {
tree: None, tree: None,
config, config,
@ -801,11 +967,13 @@ impl Syntax {
loader, loader,
}; };
syntax let res = syntax.update(source, source, &ChangeSet::new(source));
.update(source, source, &ChangeSet::new(source))
.unwrap();
syntax if res.is_err() {
log::error!("TS parser failed, disabeling TS for the current buffer: {res:?}");
return None;
}
Some(syntax)
} }
pub fn update( pub fn update(
@ -933,6 +1101,7 @@ impl Syntax {
PARSER.with(|ts_parser| { PARSER.with(|ts_parser| {
let ts_parser = &mut ts_parser.borrow_mut(); let ts_parser = &mut ts_parser.borrow_mut();
ts_parser.parser.set_timeout_micros(1000 * 500); // half a second is pretty generours
let mut cursor = ts_parser.cursors.pop().unwrap_or_else(QueryCursor::new); let mut cursor = ts_parser.cursors.pop().unwrap_or_else(QueryCursor::new);
// TODO: might need to set cursor range // TODO: might need to set cursor range
cursor.set_byte_range(0..usize::MAX); cursor.set_byte_range(0..usize::MAX);
@ -1244,7 +1413,7 @@ impl LanguageLayer {
&mut |byte, _| { &mut |byte, _| {
if byte <= source.len_bytes() { if byte <= source.len_bytes() {
let (chunk, start_byte, _, _) = source.chunk_at_byte(byte); let (chunk, start_byte, _, _) = source.chunk_at_byte(byte);
chunk[byte - start_byte..].as_bytes() &chunk.as_bytes()[byte - start_byte..]
} else { } else {
// out of range // out of range
&[] &[]
@ -2371,7 +2540,10 @@ mod test {
"#, "#,
); );
let loader = Loader::new(Configuration { language: vec![] }); let loader = Loader::new(Configuration {
language: vec![],
language_server: HashMap::new(),
});
let language = get_language("rust").unwrap(); let language = get_language("rust").unwrap();
let query = Query::new(language, query_str).unwrap(); let query = Query::new(language, query_str).unwrap();
@ -2379,7 +2551,7 @@ mod test {
let mut cursor = QueryCursor::new(); let mut cursor = QueryCursor::new();
let config = HighlightConfiguration::new(language, "", "", "").unwrap(); let config = HighlightConfiguration::new(language, "", "", "").unwrap();
let syntax = Syntax::new(&source, Arc::new(config), Arc::new(loader)); let syntax = Syntax::new(&source, Arc::new(config), Arc::new(loader)).unwrap();
let root = syntax.tree().root_node(); let root = syntax.tree().root_node();
let mut test = |capture, range| { let mut test = |capture, range| {
@ -2430,7 +2602,10 @@ mod test {
.map(String::from) .map(String::from)
.collect(); .collect();
let loader = Loader::new(Configuration { language: vec![] }); let loader = Loader::new(Configuration {
language: vec![],
language_server: HashMap::new(),
});
let language = get_language("rust").unwrap(); let language = get_language("rust").unwrap();
let config = HighlightConfiguration::new( let config = HighlightConfiguration::new(
@ -2450,7 +2625,7 @@ mod test {
fn main() {} fn main() {}
", ",
); );
let syntax = Syntax::new(&source, Arc::new(config), Arc::new(loader)); let syntax = Syntax::new(&source, Arc::new(config), Arc::new(loader)).unwrap();
let tree = syntax.tree(); let tree = syntax.tree();
let root = tree.root_node(); let root = tree.root_node();
assert_eq!(root.kind(), "source_file"); assert_eq!(root.kind(), "source_file");
@ -2533,11 +2708,14 @@ mod test {
) { ) {
let source = Rope::from_str(source); let source = Rope::from_str(source);
let loader = Loader::new(Configuration { language: vec![] }); let loader = Loader::new(Configuration {
language: vec![],
language_server: HashMap::new(),
});
let language = get_language(language_name).unwrap(); let language = get_language(language_name).unwrap();
let config = HighlightConfiguration::new(language, "", "", "").unwrap(); let config = HighlightConfiguration::new(language, "", "", "").unwrap();
let syntax = Syntax::new(&source, Arc::new(config), Arc::new(loader)); let syntax = Syntax::new(&source, Arc::new(config), Arc::new(loader)).unwrap();
let root = syntax let root = syntax
.tree() .tree()

@ -1,10 +1,11 @@
use smallvec::SmallVec; use smallvec::SmallVec;
use crate::{Range, Rope, Selection, Tendril}; use crate::{Range, Rope, Selection, Tendril};
use std::borrow::Cow; use std::{borrow::Cow, iter::once};
/// (from, to, replacement) /// (from, to, replacement)
pub type Change = (usize, usize, Option<Tendril>); pub type Change = (usize, usize, Option<Tendril>);
pub type Deletion = (usize, usize);
// TODO: pub(crate) // TODO: pub(crate)
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq)]
@ -325,20 +326,72 @@ impl ChangeSet {
self.changes.is_empty() || self.changes == [Operation::Retain(self.len)] self.changes.is_empty() || self.changes == [Operation::Retain(self.len)]
} }
/// Map a position through the changes. /// Map a (mostly) *sorted* list of positions through the changes.
/// ///
/// `assoc` indicates which size to associate the position with. `Before` will keep the /// This is equivalent to updating each position with `map_pos`:
/// position close to the character before, and will place it before insertions over that ///
/// range, or at that point. `After` will move it forward, placing it at the end of such /// ``` no-compile
/// insertions. /// for (pos, assoc) in positions {
pub fn map_pos(&self, pos: usize, assoc: Assoc) -> usize { /// *pos = changes.map_pos(*pos, assoc);
/// }
/// ```
/// However this function is significantly faster for sorted lists running
/// in `O(N+M)` instead of `O(NM)`. This function also handles unsorted/
/// partially sorted lists. However, in that case worst case complexity is
/// again `O(MN)`. For lists that are often/mostly sorted (like the end of diagnostic ranges)
/// performance is usally close to `O(N + M)`
pub fn update_positions<'a>(&self, positions: impl Iterator<Item = (&'a mut usize, Assoc)>) {
use Operation::*; use Operation::*;
let mut positions = positions.peekable();
let mut old_pos = 0; let mut old_pos = 0;
let mut new_pos = 0; let mut new_pos = 0;
let mut iter = self.changes.iter().enumerate().peekable();
let mut iter = self.changes.iter().peekable(); 'outer: loop {
macro_rules! map {
($map: expr, $i: expr) => {
loop {
let Some((pos, assoc)) = positions.peek_mut() else { return; };
if **pos < old_pos {
// Positions are not sorted, revert to the last Operation that
// contains this position and continue iterating from there.
// We can unwrap here since `pos` can not be negative
// (unsigned integer) and iterating backwards to the start
// should always move us back to the start
for (i, change) in self.changes[..$i].iter().enumerate().rev() {
match change {
Retain(i) => {
old_pos -= i;
new_pos -= i;
}
Delete(i) => {
old_pos -= i;
}
Insert(ins) => {
new_pos -= ins.chars().count();
}
}
if old_pos <= **pos {
iter = self.changes[i..].iter().enumerate().peekable();
}
}
debug_assert!(old_pos <= **pos, "Reverse Iter across changeset works");
continue 'outer;
}
let Some(new_pos) = $map(**pos, *assoc) else { break; };
**pos = new_pos;
positions.next();
}
};
}
let Some((i, change)) = iter.next() else {
map!(|pos, _| (old_pos == pos).then_some(new_pos), self.changes.len());
break;
};
while let Some(change) = iter.next() {
let len = match change { let len = match change {
Delete(i) | Retain(i) => *i, Delete(i) | Retain(i) => *i,
Insert(_) => 0, Insert(_) => 0,
@ -347,46 +400,51 @@ impl ChangeSet {
match change { match change {
Retain(_) => { Retain(_) => {
if old_end > pos { map!(
return new_pos + (pos - old_pos); |pos, _| (old_end > pos).then_some(new_pos + (pos - old_pos)),
} i
);
new_pos += len; new_pos += len;
} }
Delete(_) => { Delete(_) => {
// in range // in range
if old_end > pos { map!(|pos, _| (old_end > pos).then_some(new_pos), i);
return new_pos;
}
} }
Insert(s) => { Insert(s) => {
let ins = s.chars().count(); let ins = s.chars().count();
// a subsequent delete means a replace, consume it // a subsequent delete means a replace, consume it
if let Some(Delete(len)) = iter.peek() { if let Some((_, Delete(len))) = iter.peek() {
iter.next(); iter.next();
old_end = old_pos + len; old_end = old_pos + len;
// in range of replaced text // in range of replaced text
if old_end > pos { map!(
|pos, assoc| (old_end > pos).then(|| {
// at point or tracking before // at point or tracking before
if pos == old_pos || assoc == Assoc::Before { if pos == old_pos || assoc == Assoc::Before {
return new_pos; new_pos
} else { } else {
// place to end of insert // place to end of insert
return new_pos + ins; new_pos + ins
}
} }
}),
i
);
} else { } else {
// at insert point // at insert point
if old_pos == pos { map!(
|pos, assoc| (old_pos == pos).then(|| {
// return position before inserted text // return position before inserted text
if assoc == Assoc::Before { if assoc == Assoc::Before {
return new_pos; new_pos
} else { } else {
// after text // after text
return new_pos + ins; new_pos + ins
}
} }
}),
i
);
} }
new_pos += ins; new_pos += ins;
@ -394,14 +452,20 @@ impl ChangeSet {
} }
old_pos = old_end; old_pos = old_end;
} }
let out_of_bounds: Vec<_> = positions.collect();
if pos > old_pos { panic!("Positions {out_of_bounds:?} are out of range for changeset len {old_pos}!",)
panic!(
"Position {} is out of range for changeset len {}!",
pos, old_pos
)
} }
new_pos
/// Map a position through the changes.
///
/// `assoc` indicates which side to associate the position with. `Before` will keep the
/// position close to the character before, and will place it before insertions over that
/// range, or at that point. `After` will move it forward, placing it at the end of such
/// insertions.
pub fn map_pos(&self, mut pos: usize, assoc: Assoc) -> usize {
self.update_positions(once((&mut pos, assoc)));
pos
} }
pub fn changes_iter(&self) -> ChangeIterator { pub fn changes_iter(&self) -> ChangeIterator {
@ -534,6 +598,46 @@ impl Transaction {
Self::from(changeset) Self::from(changeset)
} }
/// Generate a transaction from a set of potentially overlapping deletions
/// by merging overlapping deletions together.
pub fn delete<I>(doc: &Rope, deletions: I) -> Self
where
I: Iterator<Item = Deletion>,
{
let len = doc.len_chars();
let (lower, upper) = deletions.size_hint();
let size = upper.unwrap_or(lower);
let mut changeset = ChangeSet::with_capacity(2 * size + 1); // rough estimate
let mut last = 0;
for (mut from, to) in deletions {
if last > to {
continue;
}
if last > from {
from = last
}
debug_assert!(
from <= to,
"Edit end must end before it starts (should {from} <= {to})"
);
// Retain from last "to" to current "from"
changeset.retain(from - last);
changeset.delete(to - from);
last = to;
}
changeset.retain(len - last);
Self::from(changeset)
}
pub fn insert_at_eof(mut self, text: Tendril) -> Transaction {
self.changes.insert(text);
self
}
/// Generate a transaction with a change per selection range. /// Generate a transaction with a change per selection range.
pub fn change_by_selection<F>(doc: &Rope, selection: &Selection, f: F) -> Self pub fn change_by_selection<F>(doc: &Rope, selection: &Selection, f: F) -> Self
where where
@ -580,6 +684,16 @@ impl Transaction {
) )
} }
/// Generate a transaction with a deletion per selection range.
/// Compared to using `change_by_selection` directly these ranges may overlap.
/// In that case they are merged
pub fn delete_by_selection<F>(doc: &Rope, selection: &Selection, f: F) -> Self
where
F: FnMut(&Range) -> Deletion,
{
Self::delete(doc, selection.iter().map(f))
}
/// Insert text at each selection head. /// Insert text at each selection head.
pub fn insert(doc: &Rope, selection: &Selection, text: Tendril) -> Self { pub fn insert(doc: &Rope, selection: &Selection, text: Tendril) -> Self {
Self::change_by_selection(doc, selection, |range| { Self::change_by_selection(doc, selection, |range| {
@ -752,6 +866,20 @@ mod test {
}; };
assert_eq!(cs.map_pos(2, Assoc::Before), 2); assert_eq!(cs.map_pos(2, Assoc::Before), 2);
assert_eq!(cs.map_pos(2, Assoc::After), 2); assert_eq!(cs.map_pos(2, Assoc::After), 2);
// unsorted selection
let cs = ChangeSet {
changes: vec![
Insert("ab".into()),
Delete(2),
Insert("cd".into()),
Delete(2),
],
len: 4,
len_after: 4,
};
let mut positions = [4, 2];
cs.update_positions(positions.iter_mut().map(|pos| (pos, Assoc::After)));
assert_eq!(positions, [4, 2]);
} }
#[test] #[test]

@ -72,7 +72,7 @@ fn test_treesitter_indent(file_name: &str, lang_scope: &str) {
let language_config = loader.language_config_for_scope(lang_scope).unwrap(); let language_config = loader.language_config_for_scope(lang_scope).unwrap();
let highlight_config = language_config.highlight_config(&[]).unwrap(); let highlight_config = language_config.highlight_config(&[]).unwrap();
let syntax = Syntax::new(&doc, highlight_config, std::sync::Arc::new(loader)); let syntax = Syntax::new(&doc, highlight_config, std::sync::Arc::new(loader)).unwrap();
let indent_query = language_config.indent_query().unwrap(); let indent_query = language_config.indent_query().unwrap();
let text = doc.slice(..); let text = doc.slice(..);

@ -17,17 +17,18 @@ path = "src/main.rs"
anyhow = "1" anyhow = "1"
serde = { version = "1.0", features = ["derive"] } serde = { version = "1.0", features = ["derive"] }
toml = "0.7" toml = "0.7"
etcetera = "0.7" etcetera = "0.8"
tree-sitter = "0.20" tree-sitter = "0.20"
once_cell = "1.17" once_cell = "1.18"
log = "0.4" log = "0.4"
which = "4.4"
# TODO: these two should be on !wasm32 only # TODO: these two should be on !wasm32 only
# cloning/compiling tree-sitter grammars # cloning/compiling tree-sitter grammars
cc = { version = "1" } cc = { version = "1" }
threadpool = { version = "1.0" } threadpool = { version = "1.0" }
tempfile = "3.5.0" tempfile = "3.6.0"
[target.'cfg(not(target_arch = "wasm32"))'.dependencies] [target.'cfg(not(target_arch = "wasm32"))'.dependencies]
libloading = "0.8" libloading = "0.8"

@ -85,7 +85,16 @@ pub fn get_language(name: &str) -> Result<Language> {
Ok(language) Ok(language)
} }
fn ensure_git_is_available() -> Result<()> {
match which::which("git") {
Ok(_cmd) => Ok(()),
Err(err) => Err(anyhow::anyhow!("'git' could not be found ({err})")),
}
}
pub fn fetch_grammars() -> Result<()> { pub fn fetch_grammars() -> Result<()> {
ensure_git_is_available()?;
// We do not need to fetch local grammars. // We do not need to fetch local grammars.
let mut grammars = get_grammar_configs()?; let mut grammars = get_grammar_configs()?;
grammars.retain(|grammar| !matches!(grammar.source, GrammarSource::Local { .. })); grammars.retain(|grammar| !matches!(grammar.source, GrammarSource::Local { .. }));
@ -145,6 +154,8 @@ pub fn fetch_grammars() -> Result<()> {
} }
pub fn build_grammars(target: Option<String>) -> Result<()> { pub fn build_grammars(target: Option<String>) -> Result<()> {
ensure_git_is_available()?;
let grammars = get_grammar_configs()?; let grammars = get_grammar_configs()?;
println!("Building {} grammars", grammars.len()); println!("Building {} grammars", grammars.len());
let results = run_parallel(grammars, move |grammar| { let results = run_parallel(grammars, move |grammar| {

@ -217,6 +217,24 @@ pub fn merge_toml_values(left: toml::Value, right: toml::Value, merge_depth: usi
} }
} }
/// Finds the current workspace folder.
/// Used as a ceiling dir for LSP root resolution, the filepicker and potentially as a future filewatching root
///
/// This function starts searching the FS upward from the CWD
/// and returns the first directory that contains either `.git` or `.helix`.
/// If no workspace was found returns (CWD, true).
/// Otherwise (workspace, false) is returned
pub fn find_workspace() -> (PathBuf, bool) {
let current_dir = std::env::current_dir().expect("unable to determine current directory");
for ancestor in current_dir.ancestors() {
if ancestor.join(".git").exists() || ancestor.join(".helix").exists() {
return (ancestor.to_owned(), false);
}
}
(current_dir, true)
}
#[cfg(test)] #[cfg(test)]
mod merge_toml_tests { mod merge_toml_tests {
use std::str; use std::str;
@ -289,21 +307,3 @@ mod merge_toml_tests {
) )
} }
} }
/// Finds the current workspace folder.
/// Used as a ceiling dir for LSP root resolution, the filepicker and potentially as a future filewatching root
///
/// This function starts searching the FS upward from the CWD
/// and returns the first directory that contains either `.git` or `.helix`.
/// If no workspace was found returns (CWD, true).
/// Otherwise (workspace, false) is returned
pub fn find_workspace() -> (PathBuf, bool) {
let current_dir = std::env::current_dir().expect("unable to determine current directory");
for ancestor in current_dir.ancestors() {
if ancestor.join(".git").exists() || ancestor.join(".helix").exists() {
return (ancestor.to_owned(), false);
}
}
(current_dir, true)
}

@ -24,7 +24,7 @@ lsp-types = { version = "0.94" }
serde = { version = "1.0", features = ["derive"] } serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0" serde_json = "1.0"
thiserror = "1.0" thiserror = "1.0"
tokio = { version = "1.27", features = ["rt", "rt-multi-thread", "io-util", "io-std", "time", "process", "macros", "fs", "parking_lot", "sync"] } tokio = { version = "1.28", features = ["rt", "rt-multi-thread", "io-util", "io-std", "time", "process", "macros", "fs", "parking_lot", "sync"] }
tokio-stream = "0.1.12" tokio-stream = "0.1.14"
which = "4.4" which = "4.4"
parking_lot = "0.12.1" parking_lot = "0.12.1"

@ -4,7 +4,7 @@ use crate::{
Call, Error, OffsetEncoding, Result, Call, Error, OffsetEncoding, Result,
}; };
use helix_core::{find_workspace, path, ChangeSet, Rope}; use helix_core::{find_workspace, path, syntax::LanguageServerFeature, ChangeSet, Rope};
use helix_loader::{self, VERSION_AND_GIT_HASH}; use helix_loader::{self, VERSION_AND_GIT_HASH};
use lsp::{ use lsp::{
notification::DidChangeWorkspaceFolders, DidChangeWorkspaceFoldersParams, OneOf, notification::DidChangeWorkspaceFolders, DidChangeWorkspaceFoldersParams, OneOf,
@ -44,6 +44,7 @@ fn workspace_for_uri(uri: lsp::Url) -> WorkspaceFolder {
#[derive(Debug)] #[derive(Debug)]
pub struct Client { pub struct Client {
id: usize, id: usize,
name: String,
_process: Child, _process: Child,
server_tx: UnboundedSender<Payload>, server_tx: UnboundedSender<Payload>,
request_counter: AtomicU64, request_counter: AtomicU64,
@ -166,8 +167,7 @@ impl Client {
tokio::spawn(self.did_change_workspace(vec![workspace_for_uri(root_uri)], Vec::new())); tokio::spawn(self.did_change_workspace(vec![workspace_for_uri(root_uri)], Vec::new()));
} }
#[allow(clippy::type_complexity)] #[allow(clippy::type_complexity, clippy::too_many_arguments)]
#[allow(clippy::too_many_arguments)]
pub fn start( pub fn start(
cmd: &str, cmd: &str,
args: &[String], args: &[String],
@ -176,6 +176,7 @@ impl Client {
root_markers: &[String], root_markers: &[String],
manual_roots: &[PathBuf], manual_roots: &[PathBuf],
id: usize, id: usize,
name: String,
req_timeout: u64, req_timeout: u64,
doc_path: Option<&std::path::PathBuf>, doc_path: Option<&std::path::PathBuf>,
) -> Result<(Self, UnboundedReceiver<(usize, Call)>, Arc<Notify>)> { ) -> Result<(Self, UnboundedReceiver<(usize, Call)>, Arc<Notify>)> {
@ -200,7 +201,7 @@ impl Client {
let stderr = BufReader::new(process.stderr.take().expect("Failed to open stderr")); let stderr = BufReader::new(process.stderr.take().expect("Failed to open stderr"));
let (server_rx, server_tx, initialize_notify) = let (server_rx, server_tx, initialize_notify) =
Transport::start(reader, writer, stderr, id); Transport::start(reader, writer, stderr, id, name.clone());
let (workspace, workspace_is_cwd) = find_workspace(); let (workspace, workspace_is_cwd) = find_workspace();
let workspace = path::get_normalized_path(&workspace); let workspace = path::get_normalized_path(&workspace);
let root = find_lsp_workspace( let root = find_lsp_workspace(
@ -225,6 +226,7 @@ impl Client {
let client = Self { let client = Self {
id, id,
name,
_process: process, _process: process,
server_tx, server_tx,
request_counter: AtomicU64::new(0), request_counter: AtomicU64::new(0),
@ -240,6 +242,10 @@ impl Client {
Ok((client, server_rx, initialize_notify)) Ok((client, server_rx, initialize_notify))
} }
pub fn name(&self) -> &str {
&self.name
}
pub fn id(&self) -> usize { pub fn id(&self) -> usize {
self.id self.id
} }
@ -270,6 +276,87 @@ impl Client {
.expect("language server not yet initialized!") .expect("language server not yet initialized!")
} }
/// Client has to be initialized otherwise this function panics
#[inline]
pub fn supports_feature(&self, feature: LanguageServerFeature) -> bool {
let capabilities = self.capabilities();
use lsp::*;
match feature {
LanguageServerFeature::Format => matches!(
capabilities.document_formatting_provider,
Some(OneOf::Left(true) | OneOf::Right(_))
),
LanguageServerFeature::GotoDeclaration => matches!(
capabilities.declaration_provider,
Some(
DeclarationCapability::Simple(true)
| DeclarationCapability::RegistrationOptions(_)
| DeclarationCapability::Options(_),
)
),
LanguageServerFeature::GotoDefinition => matches!(
capabilities.definition_provider,
Some(OneOf::Left(true) | OneOf::Right(_))
),
LanguageServerFeature::GotoTypeDefinition => matches!(
capabilities.type_definition_provider,
Some(
TypeDefinitionProviderCapability::Simple(true)
| TypeDefinitionProviderCapability::Options(_),
)
),
LanguageServerFeature::GotoReference => matches!(
capabilities.references_provider,
Some(OneOf::Left(true) | OneOf::Right(_))
),
LanguageServerFeature::GotoImplementation => matches!(
capabilities.implementation_provider,
Some(
ImplementationProviderCapability::Simple(true)
| ImplementationProviderCapability::Options(_),
)
),
LanguageServerFeature::SignatureHelp => capabilities.signature_help_provider.is_some(),
LanguageServerFeature::Hover => matches!(
capabilities.hover_provider,
Some(HoverProviderCapability::Simple(true) | HoverProviderCapability::Options(_),)
),
LanguageServerFeature::DocumentHighlight => matches!(
capabilities.document_highlight_provider,
Some(OneOf::Left(true) | OneOf::Right(_))
),
LanguageServerFeature::Completion => capabilities.completion_provider.is_some(),
LanguageServerFeature::CodeAction => matches!(
capabilities.code_action_provider,
Some(
CodeActionProviderCapability::Simple(true)
| CodeActionProviderCapability::Options(_),
)
),
LanguageServerFeature::WorkspaceCommand => {
capabilities.execute_command_provider.is_some()
}
LanguageServerFeature::DocumentSymbols => matches!(
capabilities.document_symbol_provider,
Some(OneOf::Left(true) | OneOf::Right(_))
),
LanguageServerFeature::WorkspaceSymbols => matches!(
capabilities.workspace_symbol_provider,
Some(OneOf::Left(true) | OneOf::Right(_))
),
LanguageServerFeature::Diagnostics => true, // there's no extra server capability
LanguageServerFeature::RenameSymbol => matches!(
capabilities.rename_provider,
Some(OneOf::Left(true)) | Some(OneOf::Right(_))
),
LanguageServerFeature::InlayHints => matches!(
capabilities.inlay_hint_provider,
Some(OneOf::Left(true) | OneOf::Right(InlayHintServerCapabilities::Options(_)))
),
}
}
pub fn offset_encoding(&self) -> OffsetEncoding { pub fn offset_encoding(&self) -> OffsetEncoding {
self.capabilities() self.capabilities()
.position_encoding .position_encoding
@ -645,7 +732,11 @@ impl Client {
// Calculation is therefore a bunch trickier. // Calculation is therefore a bunch trickier.
use helix_core::RopeSlice; use helix_core::RopeSlice;
fn traverse(pos: lsp::Position, text: RopeSlice) -> lsp::Position { fn traverse(
pos: lsp::Position,
text: RopeSlice,
offset_encoding: OffsetEncoding,
) -> lsp::Position {
let lsp::Position { let lsp::Position {
mut line, mut line,
mut character, mut character,
@ -662,7 +753,11 @@ impl Client {
line += 1; line += 1;
character = 0; character = 0;
} else { } else {
character += ch.len_utf16() as u32; character += match offset_encoding {
OffsetEncoding::Utf8 => ch.len_utf8() as u32,
OffsetEncoding::Utf16 => ch.len_utf16() as u32,
OffsetEncoding::Utf32 => 1,
};
} }
} }
lsp::Position { line, character } lsp::Position { line, character }
@ -683,7 +778,7 @@ impl Client {
} }
Delete(_) => { Delete(_) => {
let start = pos_to_lsp_pos(new_text, new_pos, offset_encoding); let start = pos_to_lsp_pos(new_text, new_pos, offset_encoding);
let end = traverse(start, old_text.slice(old_pos..old_end)); let end = traverse(start, old_text.slice(old_pos..old_end), offset_encoding);
// deletion // deletion
changes.push(lsp::TextDocumentContentChangeEvent { changes.push(lsp::TextDocumentContentChangeEvent {
@ -700,7 +795,8 @@ impl Client {
// a subsequent delete means a replace, consume it // a subsequent delete means a replace, consume it
let end = if let Some(Delete(len)) = iter.peek() { let end = if let Some(Delete(len)) = iter.peek() {
old_end = old_pos + len; old_end = old_pos + len;
let end = traverse(start, old_text.slice(old_pos..old_end)); let end =
traverse(start, old_text.slice(old_pos..old_end), offset_encoding);
iter.next(); iter.next();
@ -1286,21 +1382,13 @@ impl Client {
Some(self.call::<lsp::request::CodeActionRequest>(params)) Some(self.call::<lsp::request::CodeActionRequest>(params))
} }
pub fn supports_rename(&self) -> bool {
let capabilities = self.capabilities.get().unwrap();
matches!(
capabilities.rename_provider,
Some(lsp::OneOf::Left(true) | lsp::OneOf::Right(_))
)
}
pub fn rename_symbol( pub fn rename_symbol(
&self, &self,
text_document: lsp::TextDocumentIdentifier, text_document: lsp::TextDocumentIdentifier,
position: lsp::Position, position: lsp::Position,
new_name: String, new_name: String,
) -> Option<impl Future<Output = Result<lsp::WorkspaceEdit>>> { ) -> Option<impl Future<Output = Result<lsp::WorkspaceEdit>>> {
if !self.supports_rename() { if !self.supports_feature(LanguageServerFeature::RenameSymbol) {
return None; return None;
} }

@ -12,24 +12,21 @@ pub use lsp_types as lsp;
use futures_util::stream::select_all::SelectAll; use futures_util::stream::select_all::SelectAll;
use helix_core::{ use helix_core::{
path, path,
syntax::{LanguageConfiguration, LanguageServerConfiguration}, syntax::{LanguageConfiguration, LanguageServerConfiguration, LanguageServerFeatures},
}; };
use tokio::sync::mpsc::UnboundedReceiver; use tokio::sync::mpsc::UnboundedReceiver;
use std::{ use std::{
collections::{hash_map::Entry, HashMap}, collections::HashMap,
path::{Path, PathBuf}, path::{Path, PathBuf},
sync::{ sync::Arc,
atomic::{AtomicUsize, Ordering},
Arc,
},
}; };
use thiserror::Error; use thiserror::Error;
use tokio_stream::wrappers::UnboundedReceiverStream; use tokio_stream::wrappers::UnboundedReceiverStream;
pub type Result<T> = core::result::Result<T, Error>; pub type Result<T> = core::result::Result<T, Error>;
type LanguageId = String; pub type LanguageServerName = String;
#[derive(Error, Debug)] #[derive(Error, Debug)]
pub enum Error { pub enum Error {
@ -49,7 +46,7 @@ pub enum Error {
Other(#[from] anyhow::Error), Other(#[from] anyhow::Error),
} }
#[derive(Clone, Copy, Debug, Default)] #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum OffsetEncoding { pub enum OffsetEncoding {
/// UTF-8 code units aka bytes /// UTF-8 code units aka bytes
Utf8, Utf8,
@ -380,7 +377,7 @@ pub mod util {
.expect("transaction must be valid for primary selection"); .expect("transaction must be valid for primary selection");
let removed_text = text.slice(removed_start..removed_end); let removed_text = text.slice(removed_start..removed_end);
let (transaction, selection) = Transaction::change_by_selection_ignore_overlapping( let (transaction, mut selection) = Transaction::change_by_selection_ignore_overlapping(
doc, doc,
selection, selection,
|range| { |range| {
@ -423,6 +420,11 @@ pub mod util {
return transaction; return transaction;
} }
// Don't normalize to avoid merging/reording selections which would
// break the association between tabstops and selections. Most ranges
// will be replaced by tabstops anyways and the final selection will be
// normalized anyways
selection = selection.map_no_normalize(changes);
let mut mapped_selection = SmallVec::with_capacity(selection.len()); let mut mapped_selection = SmallVec::with_capacity(selection.len());
let mut mapped_primary_idx = 0; let mut mapped_primary_idx = 0;
let primary_range = selection.primary(); let primary_range = selection.primary();
@ -431,7 +433,6 @@ pub mod util {
mapped_primary_idx = mapped_selection.len() mapped_primary_idx = mapped_selection.len()
} }
let range = range.map(changes);
let tabstops = tabstops.first().filter(|tabstops| !tabstops.is_empty()); let tabstops = tabstops.first().filter(|tabstops| !tabstops.is_empty());
let Some(tabstops) = tabstops else{ let Some(tabstops) = tabstops else{
// no tabstop normal mapping // no tabstop normal mapping
@ -624,23 +625,18 @@ impl Notification {
#[derive(Debug)] #[derive(Debug)]
pub struct Registry { pub struct Registry {
inner: HashMap<LanguageId, Vec<(usize, Arc<Client>)>>, inner: HashMap<LanguageServerName, Vec<Arc<Client>>>,
syn_loader: Arc<helix_core::syntax::Loader>,
counter: AtomicUsize, counter: usize,
pub incoming: SelectAll<UnboundedReceiverStream<(usize, Call)>>, pub incoming: SelectAll<UnboundedReceiverStream<(usize, Call)>>,
} }
impl Default for Registry {
fn default() -> Self {
Self::new()
}
}
impl Registry { impl Registry {
pub fn new() -> Self { pub fn new(syn_loader: Arc<helix_core::syntax::Loader>) -> Self {
Self { Self {
inner: HashMap::new(), inner: HashMap::new(),
counter: AtomicUsize::new(0), syn_loader,
counter: 0,
incoming: SelectAll::new(), incoming: SelectAll::new(),
} }
} }
@ -649,65 +645,92 @@ impl Registry {
self.inner self.inner
.values() .values()
.flatten() .flatten()
.find(|(client_id, _)| client_id == &id) .find(|client| client.id() == id)
.map(|(_, client)| client.as_ref()) .map(|client| &**client)
} }
pub fn remove_by_id(&mut self, id: usize) { pub fn remove_by_id(&mut self, id: usize) {
self.inner.retain(|_, clients| { self.inner.retain(|_, language_servers| {
clients.retain(|&(client_id, _)| client_id != id); language_servers.retain(|ls| id != ls.id());
!clients.is_empty() !language_servers.is_empty()
}) });
} }
pub fn restart( fn start_client(
&mut self, &mut self,
language_config: &LanguageConfiguration, name: String,
ls_config: &LanguageConfiguration,
doc_path: Option<&std::path::PathBuf>, doc_path: Option<&std::path::PathBuf>,
root_dirs: &[PathBuf], root_dirs: &[PathBuf],
enable_snippets: bool, enable_snippets: bool,
) -> Result<Option<Arc<Client>>> { ) -> Result<Arc<Client>> {
let config = match &language_config.language_server { let config = self
Some(config) => config, .syn_loader
None => return Ok(None), .language_server_configs()
}; .get(&name)
.ok_or_else(|| anyhow::anyhow!("Language server '{name}' not defined"))?;
let scope = language_config.scope.clone(); let id = self.counter;
self.counter += 1;
match self.inner.entry(scope) { let NewClient(client, incoming) = start_client(
Entry::Vacant(_) => Ok(None),
Entry::Occupied(mut entry) => {
// initialize a new client
let id = self.counter.fetch_add(1, Ordering::Relaxed);
let NewClientResult(client, incoming) = start_client(
id, id,
language_config, name,
ls_config,
config, config,
doc_path, doc_path,
root_dirs, root_dirs,
enable_snippets, enable_snippets,
)?; )?;
self.incoming.push(UnboundedReceiverStream::new(incoming)); self.incoming.push(UnboundedReceiverStream::new(incoming));
Ok(client)
}
let old_clients = entry.insert(vec![(id, client.clone())]); /// If this method is called, all documents that have a reference to language servers used by the language config have to refresh their language servers,
/// as it could be that language servers of these documents were stopped by this method.
/// See helix_view::editor::Editor::refresh_language_servers
pub fn restart(
&mut self,
language_config: &LanguageConfiguration,
doc_path: Option<&std::path::PathBuf>,
root_dirs: &[PathBuf],
enable_snippets: bool,
) -> Result<Vec<Arc<Client>>> {
language_config
.language_servers
.iter()
.filter_map(|LanguageServerFeatures { name, .. }| {
if self.inner.contains_key(name) {
let client = match self.start_client(
name.clone(),
language_config,
doc_path,
root_dirs,
enable_snippets,
) {
Ok(client) => client,
error => return Some(error),
};
let old_clients = self
.inner
.insert(name.clone(), vec![client.clone()])
.unwrap();
for (_, old_client) in old_clients { for old_client in old_clients {
tokio::spawn(async move { tokio::spawn(async move {
let _ = old_client.force_shutdown().await; let _ = old_client.force_shutdown().await;
}); });
} }
Ok(Some(client)) Some(Ok(client))
} } else {
None
} }
})
.collect()
} }
pub fn stop(&mut self, language_config: &LanguageConfiguration) { pub fn stop(&mut self, name: &str) {
let scope = language_config.scope.clone(); if let Some(clients) = self.inner.remove(name) {
for client in clients {
if let Some(clients) = self.inner.remove(&scope) {
for (_, client) in clients {
tokio::spawn(async move { tokio::spawn(async move {
let _ = client.force_shutdown().await; let _ = client.force_shutdown().await;
}); });
@ -721,37 +744,34 @@ impl Registry {
doc_path: Option<&std::path::PathBuf>, doc_path: Option<&std::path::PathBuf>,
root_dirs: &[PathBuf], root_dirs: &[PathBuf],
enable_snippets: bool, enable_snippets: bool,
) -> Result<Option<Arc<Client>>> { ) -> Result<HashMap<LanguageServerName, Arc<Client>>> {
let config = match &language_config.language_server { language_config
Some(config) => config, .language_servers
None => return Ok(None), .iter()
}; .map(|LanguageServerFeatures { name, .. }| {
if let Some(clients) = self.inner.get(name) {
let clients = self.inner.entry(language_config.scope.clone()).or_default(); if let Some((_, client)) = clients.iter().enumerate().find(|(i, client)| {
// check if we already have a client for this documents root that we can reuse
if let Some((_, client)) = clients.iter_mut().enumerate().find(|(i, (_, client))| {
client.try_add_doc(&language_config.roots, root_dirs, doc_path, *i == 0) client.try_add_doc(&language_config.roots, root_dirs, doc_path, *i == 0)
}) { }) {
return Ok(Some(client.1.clone())); return Ok((name.to_owned(), client.clone()));
} }
// initialize a new client }
let id = self.counter.fetch_add(1, Ordering::Relaxed); let client = self.start_client(
name.clone(),
let NewClientResult(client, incoming) = start_client(
id,
language_config, language_config,
config,
doc_path, doc_path,
root_dirs, root_dirs,
enable_snippets, enable_snippets,
)?; )?;
clients.push((id, client.clone())); let clients = self.inner.entry(name.clone()).or_default();
self.incoming.push(UnboundedReceiverStream::new(incoming)); clients.push(client.clone());
Ok(Some(client)) Ok((name.clone(), client))
})
.collect()
} }
pub fn iter_clients(&self) -> impl Iterator<Item = &Arc<Client>> { pub fn iter_clients(&self) -> impl Iterator<Item = &Arc<Client>> {
self.inner.values().flatten().map(|(_, client)| client) self.inner.values().flatten()
} }
} }
@ -833,26 +853,28 @@ impl LspProgressMap {
} }
} }
struct NewClientResult(Arc<Client>, UnboundedReceiver<(usize, Call)>); struct NewClient(Arc<Client>, UnboundedReceiver<(usize, Call)>);
/// start_client takes both a LanguageConfiguration and a LanguageServerConfiguration to ensure that /// start_client takes both a LanguageConfiguration and a LanguageServerConfiguration to ensure that
/// it is only called when it makes sense. /// it is only called when it makes sense.
fn start_client( fn start_client(
id: usize, id: usize,
name: String,
config: &LanguageConfiguration, config: &LanguageConfiguration,
ls_config: &LanguageServerConfiguration, ls_config: &LanguageServerConfiguration,
doc_path: Option<&std::path::PathBuf>, doc_path: Option<&std::path::PathBuf>,
root_dirs: &[PathBuf], root_dirs: &[PathBuf],
enable_snippets: bool, enable_snippets: bool,
) -> Result<NewClientResult> { ) -> Result<NewClient> {
let (client, incoming, initialize_notify) = Client::start( let (client, incoming, initialize_notify) = Client::start(
&ls_config.command, &ls_config.command,
&ls_config.args, &ls_config.args,
config.config.clone(), ls_config.config.clone(),
ls_config.environment.clone(), ls_config.environment.clone(),
&config.roots, &config.roots,
config.workspace_lsp_roots.as_deref().unwrap_or(root_dirs), config.workspace_lsp_roots.as_deref().unwrap_or(root_dirs),
id, id,
name,
ls_config.timeout, ls_config.timeout,
doc_path, doc_path,
)?; )?;
@ -886,7 +908,7 @@ fn start_client(
initialize_notify.notify_one(); initialize_notify.notify_one();
}); });
Ok(NewClientResult(client, incoming)) Ok(NewClient(client, incoming))
} }
/// Find an LSP workspace of a file using the following mechanism: /// Find an LSP workspace of a file using the following mechanism:

@ -38,6 +38,7 @@ enum ServerMessage {
#[derive(Debug)] #[derive(Debug)]
pub struct Transport { pub struct Transport {
id: usize, id: usize,
name: String,
pending_requests: Mutex<HashMap<jsonrpc::Id, Sender<Result<Value>>>>, pending_requests: Mutex<HashMap<jsonrpc::Id, Sender<Result<Value>>>>,
} }
@ -47,6 +48,7 @@ impl Transport {
server_stdin: BufWriter<ChildStdin>, server_stdin: BufWriter<ChildStdin>,
server_stderr: BufReader<ChildStderr>, server_stderr: BufReader<ChildStderr>,
id: usize, id: usize,
name: String,
) -> ( ) -> (
UnboundedReceiver<(usize, jsonrpc::Call)>, UnboundedReceiver<(usize, jsonrpc::Call)>,
UnboundedSender<Payload>, UnboundedSender<Payload>,
@ -58,6 +60,7 @@ impl Transport {
let transport = Self { let transport = Self {
id, id,
name,
pending_requests: Mutex::new(HashMap::default()), pending_requests: Mutex::new(HashMap::default()),
}; };
@ -83,6 +86,7 @@ impl Transport {
async fn recv_server_message( async fn recv_server_message(
reader: &mut (impl AsyncBufRead + Unpin + Send), reader: &mut (impl AsyncBufRead + Unpin + Send),
buffer: &mut String, buffer: &mut String,
language_server_name: &str,
) -> Result<ServerMessage> { ) -> Result<ServerMessage> {
let mut content_length = None; let mut content_length = None;
loop { loop {
@ -124,7 +128,7 @@ impl Transport {
reader.read_exact(&mut content).await?; reader.read_exact(&mut content).await?;
let msg = std::str::from_utf8(&content).context("invalid utf8 from server")?; let msg = std::str::from_utf8(&content).context("invalid utf8 from server")?;
info!("<- {}", msg); info!("{language_server_name} <- {msg}");
// try parsing as output (server response) or call (server request) // try parsing as output (server response) or call (server request)
let output: serde_json::Result<ServerMessage> = serde_json::from_str(msg); let output: serde_json::Result<ServerMessage> = serde_json::from_str(msg);
@ -135,12 +139,13 @@ impl Transport {
async fn recv_server_error( async fn recv_server_error(
err: &mut (impl AsyncBufRead + Unpin + Send), err: &mut (impl AsyncBufRead + Unpin + Send),
buffer: &mut String, buffer: &mut String,
language_server_name: &str,
) -> Result<()> { ) -> Result<()> {
buffer.truncate(0); buffer.truncate(0);
if err.read_line(buffer).await? == 0 { if err.read_line(buffer).await? == 0 {
return Err(Error::StreamClosed); return Err(Error::StreamClosed);
}; };
error!("err <- {:?}", buffer); error!("{language_server_name} err <- {buffer:?}");
Ok(()) Ok(())
} }
@ -162,15 +167,17 @@ impl Transport {
Payload::Notification(value) => serde_json::to_string(&value)?, Payload::Notification(value) => serde_json::to_string(&value)?,
Payload::Response(error) => serde_json::to_string(&error)?, Payload::Response(error) => serde_json::to_string(&error)?,
}; };
self.send_string_to_server(server_stdin, json).await self.send_string_to_server(server_stdin, json, &self.name)
.await
} }
async fn send_string_to_server( async fn send_string_to_server(
&self, &self,
server_stdin: &mut BufWriter<ChildStdin>, server_stdin: &mut BufWriter<ChildStdin>,
request: String, request: String,
language_server_name: &str,
) -> Result<()> { ) -> Result<()> {
info!("-> {}", request); info!("{language_server_name} -> {request}");
// send the headers // send the headers
server_stdin server_stdin
@ -189,9 +196,13 @@ impl Transport {
&self, &self,
client_tx: &UnboundedSender<(usize, jsonrpc::Call)>, client_tx: &UnboundedSender<(usize, jsonrpc::Call)>,
msg: ServerMessage, msg: ServerMessage,
language_server_name: &str,
) -> Result<()> { ) -> Result<()> {
match msg { match msg {
ServerMessage::Output(output) => self.process_request_response(output).await?, ServerMessage::Output(output) => {
self.process_request_response(output, language_server_name)
.await?
}
ServerMessage::Call(call) => { ServerMessage::Call(call) => {
client_tx client_tx
.send((self.id, call)) .send((self.id, call))
@ -202,14 +213,18 @@ impl Transport {
Ok(()) Ok(())
} }
async fn process_request_response(&self, output: jsonrpc::Output) -> Result<()> { async fn process_request_response(
&self,
output: jsonrpc::Output,
language_server_name: &str,
) -> Result<()> {
let (id, result) = match output { let (id, result) = match output {
jsonrpc::Output::Success(jsonrpc::Success { id, result, .. }) => { jsonrpc::Output::Success(jsonrpc::Success { id, result, .. }) => {
info!("<- {}", result); info!("{language_server_name} <- {}", result);
(id, Ok(result)) (id, Ok(result))
} }
jsonrpc::Output::Failure(jsonrpc::Failure { id, error, .. }) => { jsonrpc::Output::Failure(jsonrpc::Failure { id, error, .. }) => {
error!("<- {}", error); error!("{language_server_name} <- {error}");
(id, Err(error.into())) (id, Err(error.into()))
} }
}; };
@ -240,12 +255,17 @@ impl Transport {
) { ) {
let mut recv_buffer = String::new(); let mut recv_buffer = String::new();
loop { loop {
match Self::recv_server_message(&mut server_stdout, &mut recv_buffer).await { match Self::recv_server_message(&mut server_stdout, &mut recv_buffer, &transport.name)
.await
{
Ok(msg) => { Ok(msg) => {
match transport.process_server_message(&client_tx, msg).await { match transport
.process_server_message(&client_tx, msg, &transport.name)
.await
{
Ok(_) => {} Ok(_) => {}
Err(err) => { Err(err) => {
error!("err: <- {:?}", err); error!("{} err: <- {err:?}", transport.name);
break; break;
} }
}; };
@ -270,7 +290,7 @@ impl Transport {
params: jsonrpc::Params::None, params: jsonrpc::Params::None,
})); }));
match transport match transport
.process_server_message(&client_tx, notification) .process_server_message(&client_tx, notification, &transport.name)
.await .await
{ {
Ok(_) => {} Ok(_) => {}
@ -281,20 +301,22 @@ impl Transport {
break; break;
} }
Err(err) => { Err(err) => {
error!("err: <- {:?}", err); error!("{} err: <- {err:?}", transport.name);
break; break;
} }
} }
} }
} }
async fn err(_transport: Arc<Self>, mut server_stderr: BufReader<ChildStderr>) { async fn err(transport: Arc<Self>, mut server_stderr: BufReader<ChildStderr>) {
let mut recv_buffer = String::new(); let mut recv_buffer = String::new();
loop { loop {
match Self::recv_server_error(&mut server_stderr, &mut recv_buffer).await { match Self::recv_server_error(&mut server_stderr, &mut recv_buffer, &transport.name)
.await
{
Ok(_) => {} Ok(_) => {}
Err(err) => { Err(err) => {
error!("err: <- {:?}", err); error!("{} err: <- {err:?}", transport.name);
break; break;
} }
} }
@ -331,6 +353,11 @@ impl Transport {
} }
} }
fn is_shutdown(payload: &Payload) -> bool {
use lsp_types::request::{Request, Shutdown};
matches!(payload, Payload::Request { value: jsonrpc::MethodCall { method, .. }, .. } if method == Shutdown::METHOD)
}
// TODO: events that use capabilities need to do the right thing // TODO: events that use capabilities need to do the right thing
loop { loop {
@ -348,10 +375,11 @@ impl Transport {
method: lsp_types::notification::Initialized::METHOD.to_string(), method: lsp_types::notification::Initialized::METHOD.to_string(),
params: jsonrpc::Params::None, params: jsonrpc::Params::None,
})); }));
match transport.process_server_message(&client_tx, notification).await { let language_server_name = &transport.name;
match transport.process_server_message(&client_tx, notification, language_server_name).await {
Ok(_) => {} Ok(_) => {}
Err(err) => { Err(err) => {
error!("err: <- {:?}", err); error!("{language_server_name} err: <- {err:?}");
} }
} }
@ -361,14 +389,17 @@ impl Transport {
match transport.send_payload_to_server(&mut server_stdin, msg).await { match transport.send_payload_to_server(&mut server_stdin, msg).await {
Ok(_) => {} Ok(_) => {}
Err(err) => { Err(err) => {
error!("err: <- {:?}", err); error!("{language_server_name} err: <- {err:?}");
} }
} }
} }
} }
msg = client_rx.recv() => { msg = client_rx.recv() => {
if let Some(msg) = msg { if let Some(msg) = msg {
if is_pending && !is_initialize(&msg) { if is_pending && is_shutdown(&msg) {
log::info!("Language server not initialized, shutting down");
break;
} else if is_pending && !is_initialize(&msg) {
// ignore notifications // ignore notifications
if let Payload::Notification(_) = msg { if let Payload::Notification(_) = msg {
continue; continue;
@ -380,7 +411,7 @@ impl Transport {
match transport.send_payload_to_server(&mut server_stdin, msg).await { match transport.send_payload_to_server(&mut server_stdin, msg).await {
Ok(_) => {} Ok(_) => {}
Err(err) => { Err(err) => {
error!("err: <- {:?}", err); error!("{} err: <- {err:?}", transport.name);
} }
} }
} }

@ -31,7 +31,7 @@ helix-vcs = { version = "0.6", path = "../helix-vcs" }
helix-loader = { version = "0.6", path = "../helix-loader" } helix-loader = { version = "0.6", path = "../helix-loader" }
anyhow = "1" anyhow = "1"
once_cell = "1.17" once_cell = "1.18"
which = "4.4" which = "4.4"
@ -73,7 +73,7 @@ dlopen_derive = "0.1.4"
[target.'cfg(not(windows))'.dependencies] # https://github.com/vorner/signal-hook/issues/100 [target.'cfg(not(windows))'.dependencies] # https://github.com/vorner/signal-hook/issues/100
signal-hook-tokio = { version = "0.3", features = ["futures-v0_3"] } signal-hook-tokio = { version = "0.3", features = ["futures-v0_3"] }
libc = "0.2.142" libc = "0.2.147"
[build-dependencies] [build-dependencies]
helix-loader = { version = "0.6", path = "../helix-loader" } helix-loader = { version = "0.6", path = "../helix-loader" }
@ -81,4 +81,4 @@ helix-loader = { version = "0.6", path = "../helix-loader" }
[dev-dependencies] [dev-dependencies]
smallvec = "1.10" smallvec = "1.10"
indoc = "2.0.1" indoc = "2.0.1"
tempfile = "3.4.0" tempfile = "3.6.0"

@ -30,6 +30,7 @@ use crate::{
use log::{debug, error, warn}; use log::{debug, error, warn};
use std::{ use std::{
collections::btree_map::Entry,
io::{stdin, stdout}, io::{stdin, stdout},
path::Path, path::Path,
sync::Arc, sync::Arc,
@ -230,7 +231,13 @@ impl Application {
#[cfg(windows)] #[cfg(windows)]
let signals = futures_util::stream::empty(); let signals = futures_util::stream::empty();
#[cfg(not(windows))] #[cfg(not(windows))]
let signals = Signals::new([signal::SIGTSTP, signal::SIGCONT, signal::SIGUSR1]) let signals = Signals::new([
signal::SIGTSTP,
signal::SIGCONT,
signal::SIGUSR1,
signal::SIGTERM,
signal::SIGINT,
])
.context("build signal handler")?; .context("build signal handler")?;
let mut app = Self { let mut app = Self {
@ -330,7 +337,9 @@ impl Application {
biased; biased;
Some(signal) = self.signals.next() => { Some(signal) = self.signals.next() => {
self.handle_signals(signal).await; if !self.handle_signals(signal).await {
return false;
};
} }
Some(event) = input_stream.next() => { Some(event) = input_stream.next() => {
self.handle_terminal_events(event).await; self.handle_terminal_events(event).await;
@ -459,10 +468,12 @@ impl Application {
#[cfg(windows)] #[cfg(windows)]
// no signal handling available on windows // no signal handling available on windows
pub async fn handle_signals(&mut self, _signal: ()) {} pub async fn handle_signals(&mut self, _signal: ()) -> bool {
true
}
#[cfg(not(windows))] #[cfg(not(windows))]
pub async fn handle_signals(&mut self, signal: i32) { pub async fn handle_signals(&mut self, signal: i32) -> bool {
match signal { match signal {
signal::SIGTSTP => { signal::SIGTSTP => {
self.restore_term().unwrap(); self.restore_term().unwrap();
@ -516,8 +527,14 @@ impl Application {
self.refresh_config(); self.refresh_config();
self.render().await; self.render().await;
} }
signal::SIGTERM | signal::SIGINT => {
self.restore_term().unwrap();
return false;
}
_ => unreachable!(), _ => unreachable!(),
} }
true
} }
pub async fn handle_idle_timeout(&mut self) { pub async fn handle_idle_timeout(&mut self) {
@ -582,7 +599,7 @@ impl Application {
let doc = doc_mut!(self.editor, &doc_save_event.doc_id); let doc = doc_mut!(self.editor, &doc_save_event.doc_id);
let id = doc.id(); let id = doc.id();
doc.detect_language(loader); doc.detect_language(loader);
let _ = self.editor.refresh_language_server(id); self.editor.refresh_language_servers(id);
} }
// TODO: fix being overwritten by lsp // TODO: fix being overwritten by lsp
@ -680,6 +697,18 @@ impl Application {
) { ) {
use helix_lsp::{Call, MethodCall, Notification}; use helix_lsp::{Call, MethodCall, Notification};
macro_rules! language_server {
() => {
match self.editor.language_server_by_id(server_id) {
Some(language_server) => language_server,
None => {
warn!("can't find language server with id `{}`", server_id);
return;
}
}
};
}
match call { match call {
Call::Notification(helix_lsp::jsonrpc::Notification { method, params, .. }) => { Call::Notification(helix_lsp::jsonrpc::Notification { method, params, .. }) => {
let notification = match Notification::parse(&method, params) { let notification = match Notification::parse(&method, params) {
@ -695,14 +724,7 @@ impl Application {
match notification { match notification {
Notification::Initialized => { Notification::Initialized => {
let language_server = let language_server = language_server!();
match self.editor.language_servers.get_by_id(server_id) {
Some(language_server) => language_server,
None => {
warn!("can't find language server with id `{}`", server_id);
return;
}
};
// Trigger a workspace/didChangeConfiguration notification after initialization. // Trigger a workspace/didChangeConfiguration notification after initialization.
// This might not be required by the spec but Neovim does this as well, so it's // This might not be required by the spec but Neovim does this as well, so it's
@ -711,9 +733,10 @@ impl Application {
tokio::spawn(language_server.did_change_configuration(config.clone())); tokio::spawn(language_server.did_change_configuration(config.clone()));
} }
let docs = self.editor.documents().filter(|doc| { let docs = self
doc.language_server().map(|server| server.id()) == Some(server_id) .editor
}); .documents()
.filter(|doc| doc.supports_language_server(server_id));
// trigger textDocument/didOpen for docs that are already open // trigger textDocument/didOpen for docs that are already open
for doc in docs { for doc in docs {
@ -733,7 +756,7 @@ impl Application {
)); ));
} }
} }
Notification::PublishDiagnostics(mut params) => { Notification::PublishDiagnostics(params) => {
let path = match params.uri.to_file_path() { let path = match params.uri.to_file_path() {
Ok(path) => path, Ok(path) => path,
Err(_) => { Err(_) => {
@ -741,6 +764,12 @@ impl Application {
return; return;
} }
}; };
let language_server = language_server!();
if !language_server.is_initialized() {
log::error!("Discarding publishDiagnostic notification sent by an uninitialized server: {}", language_server.name());
return;
}
let offset_encoding = language_server.offset_encoding();
let doc = self.editor.document_by_path_mut(&path).filter(|doc| { let doc = self.editor.document_by_path_mut(&path).filter(|doc| {
if let Some(version) = params.version { if let Some(version) = params.version {
if version != doc.version() { if version != doc.version() {
@ -763,18 +792,11 @@ impl Application {
use helix_core::diagnostic::{Diagnostic, Range, Severity::*}; use helix_core::diagnostic::{Diagnostic, Range, Severity::*};
use lsp::DiagnosticSeverity; use lsp::DiagnosticSeverity;
let language_server = if let Some(language_server) = doc.language_server() {
language_server
} else {
log::warn!("Discarding diagnostic because language server is not initialized: {:?}", diagnostic);
return None;
};
// TODO: convert inside server // TODO: convert inside server
let start = if let Some(start) = lsp_pos_to_pos( let start = if let Some(start) = lsp_pos_to_pos(
text, text,
diagnostic.range.start, diagnostic.range.start,
language_server.offset_encoding(), offset_encoding,
) { ) {
start start
} else { } else {
@ -782,11 +804,9 @@ impl Application {
return None; return None;
}; };
let end = if let Some(end) = lsp_pos_to_pos( let end = if let Some(end) =
text, lsp_pos_to_pos(text, diagnostic.range.end, offset_encoding)
diagnostic.range.end, {
language_server.offset_encoding(),
) {
end end
} else { } else {
log::warn!("lsp position out of bounds - {:?}", diagnostic); log::warn!("lsp position out of bounds - {:?}", diagnostic);
@ -825,14 +845,19 @@ impl Application {
None => None, None => None,
}; };
let tags = if let Some(ref tags) = diagnostic.tags { let tags = if let Some(tags) = &diagnostic.tags {
let new_tags = tags.iter().filter_map(|tag| { let new_tags = tags
match *tag { .iter()
lsp::DiagnosticTag::DEPRECATED => Some(DiagnosticTag::Deprecated), .filter_map(|tag| match *tag {
lsp::DiagnosticTag::UNNECESSARY => Some(DiagnosticTag::Unnecessary), lsp::DiagnosticTag::DEPRECATED => {
_ => None Some(DiagnosticTag::Deprecated)
}
lsp::DiagnosticTag::UNNECESSARY => {
Some(DiagnosticTag::Unnecessary)
} }
}).collect(); _ => None,
})
.collect();
new_tags new_tags
} else { } else {
@ -848,25 +873,40 @@ impl Application {
tags, tags,
source: diagnostic.source.clone(), source: diagnostic.source.clone(),
data: diagnostic.data.clone(), data: diagnostic.data.clone(),
language_server_id: server_id,
}) })
}) })
.collect(); .collect();
doc.set_diagnostics(diagnostics); doc.replace_diagnostics(diagnostics, server_id);
} }
// Sort diagnostics first by severity and then by line numbers. let mut diagnostics = params
// Note: The `lsp::DiagnosticSeverity` enum is already defined in decreasing order
params
.diagnostics .diagnostics
.sort_unstable_by_key(|d| (d.severity, d.range.start)); .into_iter()
.map(|d| (d, server_id))
.collect();
// Insert the original lsp::Diagnostics here because we may have no open document // Insert the original lsp::Diagnostics here because we may have no open document
// for diagnosic message and so we can't calculate the exact position. // for diagnosic message and so we can't calculate the exact position.
// When using them later in the diagnostics picker, we calculate them on-demand. // When using them later in the diagnostics picker, we calculate them on-demand.
self.editor match self.editor.diagnostics.entry(params.uri) {
.diagnostics Entry::Occupied(o) => {
.insert(params.uri, params.diagnostics); let current_diagnostics = o.into_mut();
// there may entries of other language servers, which is why we can't overwrite the whole entry
current_diagnostics.retain(|(_, lsp_id)| *lsp_id != server_id);
current_diagnostics.append(&mut diagnostics);
// Sort diagnostics first by severity and then by line numbers.
// Note: The `lsp::DiagnosticSeverity` enum is already defined in decreasing order
current_diagnostics
.sort_unstable_by_key(|(d, _)| (d.severity, d.range.start));
}
Entry::Vacant(v) => {
diagnostics
.sort_unstable_by_key(|(d, _)| (d.severity, d.range.start));
v.insert(diagnostics);
}
};
} }
Notification::ShowMessage(params) => { Notification::ShowMessage(params) => {
log::warn!("unhandled window/showMessage: {:?}", params); log::warn!("unhandled window/showMessage: {:?}", params);
@ -963,24 +1003,18 @@ impl Application {
Notification::Exit => { Notification::Exit => {
self.editor.set_status("Language server exited"); self.editor.set_status("Language server exited");
// Clear any diagnostics for documents with this server open. // LSPs may produce diagnostics for files that haven't been opened in helix,
let urls: Vec<_> = self // we need to clear those and remove the entries from the list if this leads to
.editor // an empty diagnostic list for said files
.documents_mut() for diags in self.editor.diagnostics.values_mut() {
.filter_map(|doc| { diags.retain(|(_, lsp_id)| *lsp_id != server_id);
if doc.language_server().map(|server| server.id())
== Some(server_id)
{
doc.set_diagnostics(Vec::new());
doc.url()
} else {
None
} }
})
.collect();
for url in urls { self.editor.diagnostics.retain(|_, diags| !diags.is_empty());
self.editor.diagnostics.remove(&url);
// Clear any diagnostics for documents with this server open.
for doc in self.editor.documents_mut() {
doc.clear_diagnostics(server_id);
} }
// Remove the language server from the registry. // Remove the language server from the registry.
@ -1031,9 +1065,12 @@ impl Application {
Ok(serde_json::Value::Null) Ok(serde_json::Value::Null)
} }
Ok(MethodCall::ApplyWorkspaceEdit(params)) => { Ok(MethodCall::ApplyWorkspaceEdit(params)) => {
let language_server = language_server!();
if language_server.is_initialized() {
let offset_encoding = language_server.offset_encoding();
let res = apply_workspace_edit( let res = apply_workspace_edit(
&mut self.editor, &mut self.editor,
helix_lsp::OffsetEncoding::Utf8, offset_encoding,
&params.edit, &params.edit,
); );
@ -1045,35 +1082,33 @@ impl Application {
.err() .err()
.map(|err| err.failed_change_idx as u32), .map(|err| err.failed_change_idx as u32),
})) }))
} else {
Err(helix_lsp::jsonrpc::Error {
code: helix_lsp::jsonrpc::ErrorCode::InvalidRequest,
message: "Server must be initialized to request workspace edits"
.to_string(),
data: None,
})
}
} }
Ok(MethodCall::WorkspaceFolders) => { Ok(MethodCall::WorkspaceFolders) => {
let language_server = Ok(json!(&*language_server!().workspace_folders().await))
self.editor.language_servers.get_by_id(server_id).unwrap();
Ok(json!(&*language_server.workspace_folders().await))
} }
Ok(MethodCall::WorkspaceConfiguration(params)) => { Ok(MethodCall::WorkspaceConfiguration(params)) => {
let language_server = language_server!();
let result: Vec<_> = params let result: Vec<_> = params
.items .items
.iter() .iter()
.map(|item| { .map(|item| {
let mut config = match &item.scope_uri { let mut config = language_server.config()?;
Some(scope) => {
let path = scope.to_file_path().ok()?;
let doc = self.editor.document_by_path(path)?;
doc.language_config()?.config.as_ref()?
}
None => self
.editor
.language_servers
.get_by_id(server_id)?
.config()?,
};
if let Some(section) = item.section.as_ref() { if let Some(section) = item.section.as_ref() {
// for some reason some lsps send an empty string (observed in 'vscode-eslint-language-server')
if !section.is_empty() {
for part in section.split('.') { for part in section.split('.') {
config = config.get(part)?; config = config.get(part)?;
} }
} }
}
Some(config) Some(config)
}) })
.collect(); .collect();
@ -1092,15 +1127,7 @@ impl Application {
} }
}; };
let language_server = match self.editor.language_servers.get_by_id(server_id) { tokio::spawn(language_server!().reply(id, reply));
Some(language_server) => language_server,
None => {
warn!("can't find language server with id `{}`", server_id);
return;
}
};
tokio::spawn(language_server.reply(id, reply));
} }
Call::Invalid { id } => log::error!("LSP invalid method call id={:?}", id), Call::Invalid { id } => log::error!("LSP invalid method call id={:?}", id),
} }

@ -28,17 +28,18 @@ use helix_core::{
regex::{self, Regex, RegexBuilder}, regex::{self, Regex, RegexBuilder},
search::{self, CharMatcher}, search::{self, CharMatcher},
selection, shellwords, surround, selection, shellwords, surround,
syntax::LanguageServerFeature,
text_annotations::TextAnnotations, text_annotations::TextAnnotations,
textobject, textobject,
tree_sitter::Node, tree_sitter::Node,
unicode::width::UnicodeWidthChar, unicode::width::UnicodeWidthChar,
visual_offset_from_block, LineEnding, Position, Range, Rope, RopeGraphemes, RopeSlice, visual_offset_from_block, Deletion, LineEnding, Position, Range, Rope, RopeGraphemes,
Selection, SmallVec, Tendril, Transaction, RopeSlice, Selection, SmallVec, Tendril, Transaction,
}; };
use helix_view::{ use helix_view::{
clipboard::ClipboardType, clipboard::ClipboardType,
document::{FormatterError, Mode, SCRATCH_BUFFER_NAME}, document::{FormatterError, Mode, SCRATCH_BUFFER_NAME},
editor::{Action, Motion}, editor::{Action, CompleteAction, Motion},
info::Info, info::Info,
input::KeyEvent, input::KeyEvent,
keyboard::KeyCode, keyboard::KeyCode,
@ -60,13 +61,13 @@ use crate::{
job::Callback, job::Callback,
keymap::ReverseKeymap, keymap::ReverseKeymap,
ui::{ ui::{
self, editor::InsertEvent, lsp::SignatureHelp, overlay::overlaid, FilePicker, Picker, self, editor::InsertEvent, lsp::SignatureHelp, overlay::overlaid, CompletionItem, Picker,
Popup, Prompt, PromptEvent, Popup, Prompt, PromptEvent,
}, },
}; };
use crate::job::{self, Jobs}; use crate::job::{self, Jobs};
use futures_util::StreamExt; use futures_util::{stream::FuturesUnordered, StreamExt, TryStreamExt};
use std::{collections::HashMap, fmt, future::Future}; use std::{collections::HashMap, fmt, future::Future};
use std::{collections::HashSet, num::NonZeroUsize}; use std::{collections::HashSet, num::NonZeroUsize};
@ -104,6 +105,13 @@ impl<'a> Context<'a> {
})); }));
} }
/// Call `replace_or_push` on the Compositor
pub fn replace_or_push_layer<T: Component>(&mut self, id: &'static str, component: T) {
self.callback = Some(Box::new(move |compositor: &mut Compositor, _| {
compositor.replace_or_push(id, component);
}));
}
#[inline] #[inline]
pub fn on_next_key( pub fn on_next_key(
&mut self, &mut self,
@ -298,6 +306,7 @@ impl MappableCommand {
move_next_long_word_start, "Move to start of next long word", move_next_long_word_start, "Move to start of next long word",
move_prev_long_word_start, "Move to start of previous long word", move_prev_long_word_start, "Move to start of previous long word",
move_next_long_word_end, "Move to end of next long word", move_next_long_word_end, "Move to end of next long word",
move_prev_long_word_end, "Move to end of previous long word",
extend_next_word_start, "Extend to start of next word", extend_next_word_start, "Extend to start of next word",
extend_prev_word_start, "Extend to start of previous word", extend_prev_word_start, "Extend to start of previous word",
extend_next_word_end, "Extend to end of next word", extend_next_word_end, "Extend to end of next word",
@ -305,6 +314,7 @@ impl MappableCommand {
extend_next_long_word_start, "Extend to start of next long word", extend_next_long_word_start, "Extend to start of next long word",
extend_prev_long_word_start, "Extend to start of previous long word", extend_prev_long_word_start, "Extend to start of previous long word",
extend_next_long_word_end, "Extend to end of next long word", extend_next_long_word_end, "Extend to end of next long word",
extend_prev_long_word_end, "Extend to end of prev long word",
find_till_char, "Move till next occurrence of char", find_till_char, "Move till next occurrence of char",
find_next_char, "Move to next occurrence of char", find_next_char, "Move to next occurrence of char",
extend_till_char, "Extend till next occurrence of char", extend_till_char, "Extend till next occurrence of char",
@ -326,6 +336,7 @@ impl MappableCommand {
select_regex, "Select all regex matches inside selections", select_regex, "Select all regex matches inside selections",
split_selection, "Split selections on regex matches", split_selection, "Split selections on regex matches",
split_selection_on_newline, "Split selection on newlines", split_selection_on_newline, "Split selection on newlines",
merge_selections, "Merge selections",
merge_consecutive_selections, "Merge consecutive selections", merge_consecutive_selections, "Merge consecutive selections",
search, "Search for regex pattern", search, "Search for regex pattern",
rsearch, "Reverse search for regex pattern", rsearch, "Reverse search for regex pattern",
@ -424,6 +435,7 @@ impl MappableCommand {
later, "Move forward in history", later, "Move forward in history",
commit_undo_checkpoint, "Commit changes to new checkpoint", commit_undo_checkpoint, "Commit changes to new checkpoint",
yank, "Yank selection", yank, "Yank selection",
yank_joined, "Join and yank selections",
yank_joined_to_clipboard, "Join and yank selections to clipboard", yank_joined_to_clipboard, "Join and yank selections to clipboard",
yank_main_selection_to_clipboard, "Yank main selection to clipboard", yank_main_selection_to_clipboard, "Yank main selection to clipboard",
yank_joined_to_primary_clipboard, "Join and yank selections to primary clipboard", yank_joined_to_primary_clipboard, "Join and yank selections to primary clipboard",
@ -454,6 +466,7 @@ impl MappableCommand {
rotate_selections_backward, "Rotate selections backward", rotate_selections_backward, "Rotate selections backward",
rotate_selection_contents_forward, "Rotate selection contents forward", rotate_selection_contents_forward, "Rotate selection contents forward",
rotate_selection_contents_backward, "Rotate selections contents backward", rotate_selection_contents_backward, "Rotate selections contents backward",
reverse_selection_contents, "Reverse selections contents",
expand_selection, "Expand selection to parent syntax node", expand_selection, "Expand selection to parent syntax node",
shrink_selection, "Shrink selection to previously expanded syntax node", shrink_selection, "Shrink selection to previously expanded syntax node",
select_next_sibling, "Select next sibling in syntax tree", select_next_sibling, "Select next sibling in syntax tree",
@ -866,10 +879,9 @@ fn extend_to_line_start(cx: &mut Context) {
} }
fn kill_to_line_start(cx: &mut Context) { fn kill_to_line_start(cx: &mut Context) {
let (view, doc) = current!(cx.editor); delete_by_selection_insert_mode(
let text = doc.text().slice(..); cx,
move |text, range| {
let selection = doc.selection(view.id).clone().transform(|range| {
let line = range.cursor_line(text); let line = range.cursor_line(text);
let first_char = text.line_to_char(line); let first_char = text.line_to_char(line);
let anchor = range.cursor(text); let anchor = range.cursor(text);
@ -888,32 +900,29 @@ fn kill_to_line_start(cx: &mut Context) {
// select until start of line // select until start of line
first_char first_char
}; };
Range::new(head, anchor) (head, anchor)
}); },
delete_selection_insert_mode(doc, view, &selection); Direction::Backward,
);
lsp::signature_help_impl(cx, SignatureHelpInvoked::Automatic);
} }
fn kill_to_line_end(cx: &mut Context) { fn kill_to_line_end(cx: &mut Context) {
let (view, doc) = current!(cx.editor); delete_by_selection_insert_mode(
let text = doc.text().slice(..); cx,
|text, range| {
let selection = doc.selection(view.id).clone().transform(|range| {
let line = range.cursor_line(text); let line = range.cursor_line(text);
let line_end_pos = line_end_char_index(&text, line); let line_end_pos = line_end_char_index(&text, line);
let pos = range.cursor(text); let pos = range.cursor(text);
let mut new_range = range.put_cursor(text, line_end_pos, true); // if the cursor is on the newline char delete that
// don't want to remove the line separator itself if the cursor doesn't reach the end of line. if pos == line_end_pos {
if pos != line_end_pos { (pos, text.line_to_char(line + 1))
new_range.head = line_end_pos; } else {
(pos, line_end_pos)
} }
new_range },
}); Direction::Forward,
delete_selection_insert_mode(doc, view, &selection); );
lsp::signature_help_impl(cx, SignatureHelpInvoked::Automatic);
} }
fn goto_first_nonwhitespace(cx: &mut Context) { fn goto_first_nonwhitespace(cx: &mut Context) {
@ -1140,6 +1149,10 @@ fn move_prev_long_word_start(cx: &mut Context) {
move_word_impl(cx, movement::move_prev_long_word_start) move_word_impl(cx, movement::move_prev_long_word_start)
} }
fn move_prev_long_word_end(cx: &mut Context) {
move_word_impl(cx, movement::move_prev_long_word_end)
}
fn move_next_long_word_end(cx: &mut Context) { fn move_next_long_word_end(cx: &mut Context) {
move_word_impl(cx, movement::move_next_long_word_end) move_word_impl(cx, movement::move_next_long_word_end)
} }
@ -1297,6 +1310,10 @@ fn extend_prev_long_word_start(cx: &mut Context) {
extend_word_impl(cx, movement::move_prev_long_word_start) extend_word_impl(cx, movement::move_prev_long_word_start)
} }
fn extend_prev_long_word_end(cx: &mut Context) {
extend_word_impl(cx, movement::move_prev_long_word_end)
}
fn extend_next_long_word_end(cx: &mut Context) { fn extend_next_long_word_end(cx: &mut Context) {
extend_word_impl(cx, movement::move_next_long_word_end) extend_word_impl(cx, movement::move_next_long_word_end)
} }
@ -1339,7 +1356,7 @@ where
find_char_impl(cx.editor, &search_fn, inclusive, extend, ch, count); find_char_impl(cx.editor, &search_fn, inclusive, extend, ch, count);
cx.editor.last_motion = Some(Motion(Box::new(move |editor: &mut Editor| { cx.editor.last_motion = Some(Motion(Box::new(move |editor: &mut Editor| {
find_char_impl(editor, &search_fn, inclusive, true, ch, 1); find_char_impl(editor, &search_fn, inclusive, extend, ch, 1);
}))); })));
}) })
} }
@ -1811,6 +1828,12 @@ fn split_selection_on_newline(cx: &mut Context) {
doc.set_selection(view.id, selection); doc.set_selection(view.id, selection);
} }
fn merge_selections(cx: &mut Context) {
let (view, doc) = current!(cx.editor);
let selection = doc.selection(view.id).clone().merge_ranges();
doc.set_selection(view.id, selection);
}
fn merge_consecutive_selections(cx: &mut Context) { fn merge_consecutive_selections(cx: &mut Context) {
let (view, doc) = current!(cx.editor); let (view, doc) = current!(cx.editor);
let selection = doc.selection(view.id).clone().merge_consecutive_ranges(); let selection = doc.selection(view.id).clone().merge_consecutive_ranges();
@ -2211,7 +2234,7 @@ fn global_search(cx: &mut Context) {
return; return;
} }
let picker = FilePicker::new( let picker = Picker::new(
all_matches, all_matches,
current_path, current_path,
move |cx, FileResult { path, line_num }, action| { move |cx, FileResult { path, line_num }, action| {
@ -2239,11 +2262,9 @@ fn global_search(cx: &mut Context) {
doc.set_selection(view.id, Selection::single(start, end)); doc.set_selection(view.id, Selection::single(start, end));
align_view(doc, view, Align::Center); align_view(doc, view, Align::Center);
}, }).with_preview(|_editor, FileResult { path, line_num }| {
|_editor, FileResult { path, line_num }| {
Some((path.clone().into(), Some((*line_num, *line_num)))) Some((path.clone().into(), Some((*line_num, *line_num))))
}, });
);
compositor.push(Box::new(overlaid(picker))); compositor.push(Box::new(overlaid(picker)));
}, },
)); ));
@ -2386,9 +2407,8 @@ fn delete_selection_impl(cx: &mut Context, op: Operation) {
}; };
// then delete // then delete
let transaction = Transaction::change_by_selection(doc.text(), selection, |range| { let transaction =
(range.from(), range.to(), None) Transaction::delete_by_selection(doc.text(), selection, |range| (range.from(), range.to()));
});
doc.apply(&transaction, view.id); doc.apply(&transaction, view.id);
match op { match op {
@ -2403,11 +2423,49 @@ fn delete_selection_impl(cx: &mut Context, op: Operation) {
} }
#[inline] #[inline]
fn delete_selection_insert_mode(doc: &mut Document, view: &mut View, selection: &Selection) { fn delete_by_selection_insert_mode(
let transaction = Transaction::change_by_selection(doc.text(), selection, |range| { cx: &mut Context,
(range.from(), range.to(), None) mut f: impl FnMut(RopeSlice, &Range) -> Deletion,
direction: Direction,
) {
let (view, doc) = current!(cx.editor);
let text = doc.text().slice(..);
let mut selection = SmallVec::new();
let mut insert_newline = false;
let text_len = text.len_chars();
let mut transaction =
Transaction::delete_by_selection(doc.text(), doc.selection(view.id), |range| {
let (start, end) = f(text, range);
if direction == Direction::Forward {
let mut range = *range;
if range.head > range.anchor {
insert_newline |= end == text_len;
// move the cursor to the right so that the selection
// doesn't shrink when deleting forward (so the text appears to
// move to left)
// += 1 is enough here as the range is normalized to grapheme boundaries
// later anyway
range.head += 1;
}
selection.push(range);
}
(start, end)
}); });
// in case we delete the last character and the cursor would be moved to the EOF char
// insert a newline, just like when entering append mode
if insert_newline {
transaction = transaction.insert_at_eof(doc.line_ending.as_str().into());
}
if direction == Direction::Forward {
doc.set_selection(
view.id,
Selection::new(selection, doc.selection(view.id).primary_index()),
);
}
doc.apply(&transaction, view.id); doc.apply(&transaction, view.id);
lsp::signature_help_impl(cx, SignatureHelpInvoked::Automatic);
} }
fn delete_selection(cx: &mut Context) { fn delete_selection(cx: &mut Context) {
@ -2597,13 +2655,10 @@ fn buffer_picker(cx: &mut Context) {
// mru // mru
items.sort_unstable_by_key(|item| std::cmp::Reverse(item.focused_at)); items.sort_unstable_by_key(|item| std::cmp::Reverse(item.focused_at));
let picker = FilePicker::new( let picker = Picker::new(items, (), |cx, meta, action| {
items,
(),
|cx, meta, action| {
cx.editor.switch(meta.id, action); cx.editor.switch(meta.id, action);
}, })
|editor, meta| { .with_preview(|editor, meta| {
let doc = &editor.documents.get(&meta.id)?; let doc = &editor.documents.get(&meta.id)?;
let &view_id = doc.selections().keys().next()?; let &view_id = doc.selections().keys().next()?;
let line = doc let line = doc
@ -2611,8 +2666,7 @@ fn buffer_picker(cx: &mut Context) {
.primary() .primary()
.cursor_line(doc.text().slice(..)); .cursor_line(doc.text().slice(..));
Some((meta.id.into(), Some((line, line)))) Some((meta.id.into(), Some((line, line))))
}, });
);
cx.push_layer(Box::new(overlaid(picker))); cx.push_layer(Box::new(overlaid(picker)));
} }
@ -2678,7 +2732,7 @@ fn jumplist_picker(cx: &mut Context) {
} }
}; };
let picker = FilePicker::new( let picker = Picker::new(
cx.editor cx.editor
.tree .tree
.views() .views()
@ -2696,12 +2750,12 @@ fn jumplist_picker(cx: &mut Context) {
doc.set_selection(view.id, meta.selection.clone()); doc.set_selection(view.id, meta.selection.clone());
view.ensure_cursor_in_view_center(doc, config.scrolloff); view.ensure_cursor_in_view_center(doc, config.scrolloff);
}, },
|editor, meta| { )
.with_preview(|editor, meta| {
let doc = &editor.documents.get(&meta.id)?; let doc = &editor.documents.get(&meta.id)?;
let line = meta.selection.primary().cursor_line(doc.text().slice(..)); let line = meta.selection.primary().cursor_line(doc.text().slice(..));
Some((meta.path.clone()?.into(), Some((line, line)))) Some((meta.id.into(), Some((line, line))))
}, });
);
cx.push_layer(Box::new(overlaid(picker))); cx.push_layer(Box::new(overlaid(picker)));
} }
@ -2735,6 +2789,9 @@ impl ui::menu::Item for MappableCommand {
} }
pub fn command_palette(cx: &mut Context) { pub fn command_palette(cx: &mut Context) {
let register = cx.register;
let count = cx.count;
cx.callback = Some(Box::new( cx.callback = Some(Box::new(
move |compositor: &mut Compositor, cx: &mut compositor::Context| { move |compositor: &mut Compositor, cx: &mut compositor::Context| {
let keymap = compositor.find::<ui::EditorView>().unwrap().keymaps.map() let keymap = compositor.find::<ui::EditorView>().unwrap().keymaps.map()
@ -2752,8 +2809,8 @@ pub fn command_palette(cx: &mut Context) {
let picker = Picker::new(commands, keymap, move |cx, command, _action| { let picker = Picker::new(commands, keymap, move |cx, command, _action| {
let mut ctx = Context { let mut ctx = Context {
register: None, register,
count: std::num::NonZeroUsize::new(1), count,
editor: cx.editor, editor: cx.editor,
callback: None, callback: None,
on_next_key_callback: None, on_next_key_callback: None,
@ -2792,24 +2849,87 @@ fn last_picker(cx: &mut Context) {
})); }));
} }
// I inserts at the first nonwhitespace character of each line with a selection /// Fallback position to use for [`insert_with_indent`].
enum IndentFallbackPos {
LineStart,
LineEnd,
}
// `I` inserts at the first nonwhitespace character of each line with a selection.
// If the line is empty, automatically indent.
fn insert_at_line_start(cx: &mut Context) { fn insert_at_line_start(cx: &mut Context) {
goto_first_nonwhitespace(cx); insert_with_indent(cx, IndentFallbackPos::LineStart);
enter_insert_mode(cx);
} }
// A inserts at the end of each line with a selection // `A` inserts at the end of each line with a selection.
// If the line is empty, automatically indent.
fn insert_at_line_end(cx: &mut Context) { fn insert_at_line_end(cx: &mut Context) {
insert_with_indent(cx, IndentFallbackPos::LineEnd);
}
// Enter insert mode and auto-indent the current line if it is empty.
// If the line is not empty, move the cursor to the specified fallback position.
fn insert_with_indent(cx: &mut Context, cursor_fallback: IndentFallbackPos) {
enter_insert_mode(cx); enter_insert_mode(cx);
let (view, doc) = current!(cx.editor); let (view, doc) = current!(cx.editor);
let selection = doc.selection(view.id).clone().transform(|range| {
let text = doc.text().slice(..); let text = doc.text().slice(..);
let line = range.cursor_line(text); let contents = doc.text();
let pos = line_end_char_index(&text, line); let selection = doc.selection(view.id);
Range::new(pos, pos)
let language_config = doc.language_config();
let syntax = doc.syntax();
let tab_width = doc.tab_width();
let mut ranges = SmallVec::with_capacity(selection.len());
let mut offs = 0;
let mut transaction = Transaction::change_by_selection(contents, selection, |range| {
let cursor_line = range.cursor_line(text);
let cursor_line_start = text.line_to_char(cursor_line);
if line_end_char_index(&text, cursor_line) == cursor_line_start {
// line is empty => auto indent
let line_end_index = cursor_line_start;
let indent = indent::indent_for_newline(
language_config,
syntax,
&doc.indent_style,
tab_width,
text,
cursor_line,
line_end_index,
cursor_line,
);
// calculate new selection ranges
let pos = offs + cursor_line_start;
let indent_width = indent.chars().count();
ranges.push(Range::point(pos + indent_width));
offs += indent_width;
(line_end_index, line_end_index, Some(indent.into()))
} else {
// move cursor to the fallback position
let pos = match cursor_fallback {
IndentFallbackPos::LineStart => {
find_first_non_whitespace_char(text.line(cursor_line))
.map(|ws_offset| ws_offset + cursor_line_start)
.unwrap_or(cursor_line_start)
}
IndentFallbackPos::LineEnd => line_end_char_index(&text, cursor_line),
};
ranges.push(range.put_cursor(text, pos + offs, cx.editor.mode == Mode::Select));
(cursor_line_start, cursor_line_start, None)
}
}); });
doc.set_selection(view.id, selection);
transaction = transaction.with_selection(Selection::new(ranges, selection.primary_index()));
doc.apply(&transaction, view.id);
} }
// Creates an LspCallback that waits for formatting changes to be computed. When they're done, // Creates an LspCallback that waits for formatting changes to be computed. When they're done,
@ -3067,7 +3187,7 @@ fn exit_select_mode(cx: &mut Context) {
fn goto_first_diag(cx: &mut Context) { fn goto_first_diag(cx: &mut Context) {
let (view, doc) = current!(cx.editor); let (view, doc) = current!(cx.editor);
let selection = match doc.diagnostics().first() { let selection = match doc.shown_diagnostics().next() {
Some(diag) => Selection::single(diag.range.start, diag.range.end), Some(diag) => Selection::single(diag.range.start, diag.range.end),
None => return, None => return,
}; };
@ -3076,7 +3196,7 @@ fn goto_first_diag(cx: &mut Context) {
fn goto_last_diag(cx: &mut Context) { fn goto_last_diag(cx: &mut Context) {
let (view, doc) = current!(cx.editor); let (view, doc) = current!(cx.editor);
let selection = match doc.diagnostics().last() { let selection = match doc.shown_diagnostics().last() {
Some(diag) => Selection::single(diag.range.start, diag.range.end), Some(diag) => Selection::single(diag.range.start, diag.range.end),
None => return, None => return,
}; };
@ -3092,10 +3212,9 @@ fn goto_next_diag(cx: &mut Context) {
.cursor(doc.text().slice(..)); .cursor(doc.text().slice(..));
let diag = doc let diag = doc
.diagnostics() .shown_diagnostics()
.iter()
.find(|diag| diag.range.start > cursor_pos) .find(|diag| diag.range.start > cursor_pos)
.or_else(|| doc.diagnostics().first()); .or_else(|| doc.shown_diagnostics().next());
let selection = match diag { let selection = match diag {
Some(diag) => Selection::single(diag.range.start, diag.range.end), Some(diag) => Selection::single(diag.range.start, diag.range.end),
@ -3113,11 +3232,10 @@ fn goto_prev_diag(cx: &mut Context) {
.cursor(doc.text().slice(..)); .cursor(doc.text().slice(..));
let diag = doc let diag = doc
.diagnostics() .shown_diagnostics()
.iter()
.rev() .rev()
.find(|diag| diag.range.start < cursor_pos) .find(|diag| diag.range.start < cursor_pos)
.or_else(|| doc.diagnostics().last()); .or_else(|| doc.shown_diagnostics().last());
let selection = match diag { let selection = match diag {
// NOTE: the selection is reversed because we're jumping to the // NOTE: the selection is reversed because we're jumping to the
@ -3272,36 +3390,32 @@ pub mod insert {
use helix_lsp::lsp; use helix_lsp::lsp;
// if ch matches completion char, trigger completion // if ch matches completion char, trigger completion
let doc = doc_mut!(cx.editor); let doc = doc_mut!(cx.editor);
let language_server = match doc.language_server() { let trigger_completion = doc
Some(language_server) => language_server, .language_servers_with_feature(LanguageServerFeature::Completion)
None => return, .any(|ls| {
}; // TODO: what if trigger is multiple chars long
matches!(&ls.capabilities().completion_provider, Some(lsp::CompletionOptions {
let capabilities = language_server.capabilities();
if let Some(lsp::CompletionOptions {
trigger_characters: Some(triggers), trigger_characters: Some(triggers),
.. ..
}) = &capabilities.completion_provider }) if triggers.iter().any(|trigger| trigger.contains(ch)))
{ });
// TODO: what if trigger is multiple chars long
if triggers.iter().any(|trigger| trigger.contains(ch)) { if trigger_completion {
cx.editor.clear_idle_timer(); cx.editor.clear_idle_timer();
super::completion(cx); super::completion(cx);
} }
} }
}
fn signature_help(cx: &mut Context, ch: char) { fn signature_help(cx: &mut Context, ch: char) {
use helix_lsp::lsp; use helix_lsp::lsp;
// if ch matches signature_help char, trigger // if ch matches signature_help char, trigger
let doc = doc_mut!(cx.editor); let doc = doc_mut!(cx.editor);
// The language_server!() macro is not used here since it will // TODO support multiple language servers (not just the first that is found), likely by merging UI somehow
// print an "LSP not active for current buffer" message on let Some(language_server) = doc
// every keypress. .language_servers_with_feature(LanguageServerFeature::SignatureHelp)
let language_server = match doc.language_server() { .next()
Some(language_server) => language_server, else {
None => return, return;
}; };
let capabilities = language_server.capabilities(); let capabilities = language_server.capabilities();
@ -3505,10 +3619,10 @@ pub mod insert {
let auto_pairs = doc.auto_pairs(cx.editor); let auto_pairs = doc.auto_pairs(cx.editor);
let transaction = let transaction =
Transaction::change_by_selection(doc.text(), doc.selection(view.id), |range| { Transaction::delete_by_selection(doc.text(), doc.selection(view.id), |range| {
let pos = range.cursor(text); let pos = range.cursor(text);
if pos == 0 { if pos == 0 {
return (pos, pos, None); return (pos, pos);
} }
let line_start_pos = text.line_to_char(range.cursor_line(text)); let line_start_pos = text.line_to_char(range.cursor_line(text));
// consider to delete by indent level if all characters before `pos` are indent units. // consider to delete by indent level if all characters before `pos` are indent units.
@ -3516,11 +3630,7 @@ pub mod insert {
if !fragment.is_empty() && fragment.chars().all(|ch| ch == ' ' || ch == '\t') { if !fragment.is_empty() && fragment.chars().all(|ch| ch == ' ' || ch == '\t') {
if text.get_char(pos.saturating_sub(1)) == Some('\t') { if text.get_char(pos.saturating_sub(1)) == Some('\t') {
// fast path, delete one char // fast path, delete one char
( (graphemes::nth_prev_grapheme_boundary(text, pos, 1), pos)
graphemes::nth_prev_grapheme_boundary(text, pos, 1),
pos,
None,
)
} else { } else {
let width: usize = fragment let width: usize = fragment
.chars() .chars()
@ -3547,7 +3657,7 @@ pub mod insert {
_ => break, _ => break,
} }
} }
(start, pos, None) // delete! (start, pos) // delete!
} }
} else { } else {
match ( match (
@ -3565,17 +3675,12 @@ pub mod insert {
( (
graphemes::nth_prev_grapheme_boundary(text, pos, count), graphemes::nth_prev_grapheme_boundary(text, pos, count),
graphemes::nth_next_grapheme_boundary(text, pos, count), graphemes::nth_next_grapheme_boundary(text, pos, count),
None,
) )
} }
_ => _ =>
// delete 1 char // delete 1 char
{ {
( (graphemes::nth_prev_grapheme_boundary(text, pos, count), pos)
graphemes::nth_prev_grapheme_boundary(text, pos, count),
pos,
None,
)
} }
} }
} }
@ -3588,50 +3693,40 @@ pub mod insert {
pub fn delete_char_forward(cx: &mut Context) { pub fn delete_char_forward(cx: &mut Context) {
let count = cx.count(); let count = cx.count();
let (view, doc) = current!(cx.editor); delete_by_selection_insert_mode(
let text = doc.text().slice(..); cx,
let transaction = |text, range| {
Transaction::change_by_selection(doc.text(), doc.selection(view.id), |range| {
let pos = range.cursor(text); let pos = range.cursor(text);
( (pos, graphemes::nth_next_grapheme_boundary(text, pos, count))
pos, },
graphemes::nth_next_grapheme_boundary(text, pos, count), Direction::Forward,
None,
) )
});
doc.apply(&transaction, view.id);
lsp::signature_help_impl(cx, SignatureHelpInvoked::Automatic);
} }
pub fn delete_word_backward(cx: &mut Context) { pub fn delete_word_backward(cx: &mut Context) {
let count = cx.count(); let count = cx.count();
let (view, doc) = current!(cx.editor); delete_by_selection_insert_mode(
let text = doc.text().slice(..); cx,
|text, range| {
let selection = doc.selection(view.id).clone().transform(|range| { let anchor = movement::move_prev_word_start(text, *range, count).from();
let anchor = movement::move_prev_word_start(text, range, count).from();
let next = Range::new(anchor, range.cursor(text)); let next = Range::new(anchor, range.cursor(text));
exclude_cursor(text, next, range) let range = exclude_cursor(text, next, *range);
}); (range.from(), range.to())
delete_selection_insert_mode(doc, view, &selection); },
Direction::Backward,
lsp::signature_help_impl(cx, SignatureHelpInvoked::Automatic); );
} }
pub fn delete_word_forward(cx: &mut Context) { pub fn delete_word_forward(cx: &mut Context) {
let count = cx.count(); let count = cx.count();
let (view, doc) = current!(cx.editor); delete_by_selection_insert_mode(
let text = doc.text().slice(..); cx,
|text, range| {
let selection = doc.selection(view.id).clone().transform(|range| { let head = movement::move_next_word_end(text, *range, count).to();
let head = movement::move_next_word_end(text, range, count).to(); (range.cursor(text), head)
Range::new(range.cursor(text), head) },
}); Direction::Forward,
);
delete_selection_insert_mode(doc, view, &selection);
lsp::signature_help_impl(cx, SignatureHelpInvoked::Automatic);
} }
} }
@ -3714,6 +3809,38 @@ fn yank(cx: &mut Context) {
exit_select_mode(cx); exit_select_mode(cx);
} }
fn yank_joined_impl(editor: &mut Editor, separator: &str, register: char) {
let (view, doc) = current!(editor);
let text = doc.text().slice(..);
let selection = doc.selection(view.id);
let joined = selection
.fragments(text)
.fold(String::new(), |mut acc, fragment| {
if !acc.is_empty() {
acc.push_str(separator);
}
acc.push_str(&fragment);
acc
});
let msg = format!(
"joined and yanked {} selection(s) to register {}",
selection.len(),
register,
);
editor.registers.write(register, vec![joined]);
editor.set_status(msg);
}
fn yank_joined(cx: &mut Context) {
let line_ending = doc!(cx.editor).line_ending;
let register = cx.register.unwrap_or('"');
yank_joined_impl(cx.editor, line_ending.as_str(), register);
exit_select_mode(cx);
}
fn yank_joined_to_clipboard_impl( fn yank_joined_to_clipboard_impl(
editor: &mut Editor, editor: &mut Editor,
separator: &str, separator: &str,
@ -4115,55 +4242,60 @@ fn format_selections(cx: &mut Context) {
use helix_lsp::{lsp, util::range_to_lsp_range}; use helix_lsp::{lsp, util::range_to_lsp_range};
let (view, doc) = current!(cx.editor); let (view, doc) = current!(cx.editor);
let view_id = view.id;
// via lsp if available // via lsp if available
// TODO: else via tree-sitter indentation calculations // TODO: else via tree-sitter indentation calculations
let language_server = match doc.language_server() { if doc.selection(view_id).len() != 1 {
Some(language_server) => language_server, cx.editor
None => return, .set_error("format_selections only supports a single selection for now");
return;
}
// TODO extra LanguageServerFeature::FormatSelections?
// maybe such that LanguageServerFeature::Format contains it as well
let Some(language_server) = doc
.language_servers_with_feature(LanguageServerFeature::Format)
.find(|ls| {
matches!(
ls.capabilities().document_range_formatting_provider,
Some(lsp::OneOf::Left(true) | lsp::OneOf::Right(_))
)
})
else {
cx.editor
.set_error("No configured language server does not support range formatting");
return;
}; };
let offset_encoding = language_server.offset_encoding();
let ranges: Vec<lsp::Range> = doc let ranges: Vec<lsp::Range> = doc
.selection(view.id) .selection(view_id)
.iter() .iter()
.map(|range| range_to_lsp_range(doc.text(), *range, language_server.offset_encoding())) .map(|range| range_to_lsp_range(doc.text(), *range, offset_encoding))
.collect(); .collect();
if ranges.len() != 1 {
cx.editor
.set_error("format_selections only supports a single selection for now");
return;
}
// TODO: handle fails // TODO: handle fails
// TODO: concurrent map over all ranges // TODO: concurrent map over all ranges
let range = ranges[0]; let range = ranges[0];
let request = match language_server.text_document_range_formatting( let future = language_server
.text_document_range_formatting(
doc.identifier(), doc.identifier(),
range, range,
lsp::FormattingOptions::default(), lsp::FormattingOptions::default(),
None, None,
) { )
Some(future) => future, .unwrap();
None => {
cx.editor
.set_error("Language server does not support range formatting");
return;
}
};
let edits = tokio::task::block_in_place(|| helix_lsp::block_on(request)).unwrap_or_default(); let edits = tokio::task::block_in_place(|| helix_lsp::block_on(future)).unwrap_or_default();
let transaction = helix_lsp::util::generate_transaction_from_edits( let transaction =
doc.text(), helix_lsp::util::generate_transaction_from_edits(doc.text(), edits, offset_encoding);
edits,
language_server.offset_encoding(),
);
doc.apply(&transaction, view.id); doc.apply(&transaction, view_id);
} }
fn join_selections_impl(cx: &mut Context, select_space: bool) { fn join_selections_impl(cx: &mut Context, select_space: bool) {
@ -4293,21 +4425,53 @@ pub fn completion(cx: &mut Context) {
let (view, doc) = current!(cx.editor); let (view, doc) = current!(cx.editor);
let language_server = match doc.language_server() { let savepoint = if let Some(CompleteAction::Selected { savepoint }) = &cx.editor.last_completion
Some(language_server) => language_server, {
None => return, savepoint.clone()
} else {
doc.savepoint(view)
}; };
let text = savepoint.text.clone();
let cursor = savepoint.cursor();
let mut seen_language_servers = HashSet::new();
let mut futures: FuturesUnordered<_> = doc
.language_servers_with_feature(LanguageServerFeature::Completion)
.filter(|ls| seen_language_servers.insert(ls.id()))
.map(|language_server| {
let language_server_id = language_server.id();
let offset_encoding = language_server.offset_encoding(); let offset_encoding = language_server.offset_encoding();
let text = doc.text().slice(..); let pos = pos_to_lsp_pos(&text, cursor, offset_encoding);
let cursor = doc.selection(view.id).primary().cursor(text); let doc_id = doc.identifier();
let completion_request = language_server.completion(doc_id, pos, None).unwrap();
let pos = pos_to_lsp_pos(doc.text(), cursor, offset_encoding); async move {
let json = completion_request.await?;
let response: Option<lsp::CompletionResponse> = serde_json::from_value(json)?;
let future = match language_server.completion(doc.identifier(), pos, None) { let items = match response {
Some(future) => future, Some(lsp::CompletionResponse::Array(items)) => items,
None => return, // TODO: do something with is_incomplete
}; Some(lsp::CompletionResponse::List(lsp::CompletionList {
is_incomplete: _is_incomplete,
items,
})) => items,
None => Vec::new(),
}
.into_iter()
.map(|item| CompletionItem {
item,
language_server_id,
resolved: false,
})
.collect();
anyhow::Ok(items)
}
})
.collect();
// setup a channel that allows the request to be canceled // setup a channel that allows the request to be canceled
let (tx, rx) = oneshot::channel(); let (tx, rx) = oneshot::channel();
@ -4316,12 +4480,20 @@ pub fn completion(cx: &mut Context) {
// and the associated request is automatically dropped // and the associated request is automatically dropped
cx.editor.completion_request_handle = Some(tx); cx.editor.completion_request_handle = Some(tx);
let future = async move { let future = async move {
let items_future = async move {
let mut items = Vec::new();
// TODO if one completion request errors, all other completion requests are discarded (even if they're valid)
while let Some(mut lsp_items) = futures.try_next().await? {
items.append(&mut lsp_items);
}
anyhow::Ok(items)
};
tokio::select! { tokio::select! {
biased; biased;
_ = rx => { _ = rx => {
Ok(serde_json::Value::Null) Ok(Vec::new())
} }
res = future => { res = items_future => {
res res
} }
} }
@ -4337,7 +4509,6 @@ pub fn completion(cx: &mut Context) {
iter.reverse(); iter.reverse();
let offset = iter.take_while(|ch| chars::char_is_word(*ch)).count(); let offset = iter.take_while(|ch| chars::char_is_word(*ch)).count();
let start_offset = cursor.saturating_sub(offset); let start_offset = cursor.saturating_sub(offset);
let savepoint = doc.savepoint(view);
let trigger_doc = doc.id(); let trigger_doc = doc.id();
let trigger_view = view.id; let trigger_view = view.id;
@ -4356,9 +4527,9 @@ pub fn completion(cx: &mut Context) {
}, },
)); ));
cx.callback( cx.jobs.callback(async move {
future, let items = future.await?;
move |editor, compositor, response: Option<lsp::CompletionResponse>| { let call = move |editor: &mut Editor, compositor: &mut Compositor| {
let (view, doc) = current_ref!(editor); let (view, doc) = current_ref!(editor);
// check if the completion request is stale. // check if the completion request is stale.
// //
@ -4369,16 +4540,6 @@ pub fn completion(cx: &mut Context) {
return; return;
} }
let items = match response {
Some(lsp::CompletionResponse::Array(items)) => items,
// TODO: do something with is_incomplete
Some(lsp::CompletionResponse::List(lsp::CompletionList {
is_incomplete: _is_incomplete,
items,
})) => items,
None => Vec::new(),
};
if items.is_empty() { if items.is_empty() {
// editor.set_error("No completion available"); // editor.set_error("No completion available");
return; return;
@ -4389,7 +4550,6 @@ pub fn completion(cx: &mut Context) {
editor, editor,
savepoint, savepoint,
items, items,
offset_encoding,
start_offset, start_offset,
trigger_offset, trigger_offset,
size, size,
@ -4403,8 +4563,9 @@ pub fn completion(cx: &mut Context) {
{ {
compositor.remove(SignatureHelp::ID); compositor.remove(SignatureHelp::ID);
} }
}, };
); Ok(Callback::EditorCompositor(Box::new(call)))
});
} }
// comments // comments
@ -4439,7 +4600,13 @@ fn rotate_selections_backward(cx: &mut Context) {
rotate_selections(cx, Direction::Backward) rotate_selections(cx, Direction::Backward)
} }
fn rotate_selection_contents(cx: &mut Context, direction: Direction) { enum ReorderStrategy {
RotateForward,
RotateBackward,
Reverse,
}
fn reorder_selection_contents(cx: &mut Context, strategy: ReorderStrategy) {
let count = cx.count; let count = cx.count;
let (view, doc) = current!(cx.editor); let (view, doc) = current!(cx.editor);
let text = doc.text().slice(..); let text = doc.text().slice(..);
@ -4457,9 +4624,10 @@ fn rotate_selection_contents(cx: &mut Context, direction: Direction) {
for chunk in fragments.chunks_mut(group) { for chunk in fragments.chunks_mut(group) {
// TODO: also modify main index // TODO: also modify main index
match direction { match strategy {
Direction::Forward => chunk.rotate_right(1), ReorderStrategy::RotateForward => chunk.rotate_right(1),
Direction::Backward => chunk.rotate_left(1), ReorderStrategy::RotateBackward => chunk.rotate_left(1),
ReorderStrategy::Reverse => chunk.reverse(),
}; };
} }
@ -4476,10 +4644,13 @@ fn rotate_selection_contents(cx: &mut Context, direction: Direction) {
} }
fn rotate_selection_contents_forward(cx: &mut Context) { fn rotate_selection_contents_forward(cx: &mut Context) {
rotate_selection_contents(cx, Direction::Forward) reorder_selection_contents(cx, ReorderStrategy::RotateForward)
} }
fn rotate_selection_contents_backward(cx: &mut Context) { fn rotate_selection_contents_backward(cx: &mut Context) {
rotate_selection_contents(cx, Direction::Backward) reorder_selection_contents(cx, ReorderStrategy::RotateBackward)
}
fn reverse_selection_contents(cx: &mut Context) {
reorder_selection_contents(cx, ReorderStrategy::Reverse)
} }
// tree sitter node selection // tree sitter node selection
@ -4561,21 +4732,24 @@ fn select_prev_sibling(cx: &mut Context) {
fn match_brackets(cx: &mut Context) { fn match_brackets(cx: &mut Context) {
let (view, doc) = current!(cx.editor); let (view, doc) = current!(cx.editor);
let is_select = cx.editor.mode == Mode::Select;
let text = doc.text();
let text_slice = text.slice(..);
if let Some(syntax) = doc.syntax() {
let text = doc.text().slice(..);
let selection = doc.selection(view.id).clone().transform(|range| { let selection = doc.selection(view.id).clone().transform(|range| {
if let Some(pos) = let pos = range.cursor(text_slice);
match_brackets::find_matching_bracket_fuzzy(syntax, doc.text(), range.cursor(text)) if let Some(matched_pos) = doc.syntax().map_or_else(
{ || match_brackets::find_matching_bracket_plaintext(text.slice(..), pos),
range.put_cursor(text, pos, cx.editor.mode == Mode::Select) |syntax| match_brackets::find_matching_bracket_fuzzy(syntax, text.slice(..), pos),
) {
range.put_cursor(text_slice, matched_pos, is_select)
} else { } else {
range range
} }
}); });
doc.set_selection(view.id, selection); doc.set_selection(view.id, selection);
} }
}
// //
@ -5201,9 +5375,10 @@ async fn shell_impl_async(
let output = if let Some(mut stdin) = process.stdin.take() { let output = if let Some(mut stdin) = process.stdin.take() {
let input_task = tokio::spawn(async move { let input_task = tokio::spawn(async move {
if let Some(input) = input { if let Some(input) = input {
helix_view::document::to_writer(&mut stdin, encoding::UTF_8, &input).await?; helix_view::document::to_writer(&mut stdin, (encoding::UTF_8, false), &input)
.await?;
} }
Ok::<_, anyhow::Error>(()) anyhow::Ok(())
}); });
let (output, _) = tokio::join! { let (output, _) = tokio::join! {
process.wait_with_output(), process.wait_with_output(),

@ -2,7 +2,7 @@ use super::{Context, Editor};
use crate::{ use crate::{
compositor::{self, Compositor}, compositor::{self, Compositor},
job::{Callback, Jobs}, job::{Callback, Jobs},
ui::{self, overlay::overlaid, FilePicker, Picker, Popup, Prompt, PromptEvent, Text}, ui::{self, overlay::overlaid, Picker, Popup, Prompt, PromptEvent, Text},
}; };
use dap::{StackFrame, Thread, ThreadStates}; use dap::{StackFrame, Thread, ThreadStates};
use helix_core::syntax::{DebugArgumentValue, DebugConfigCompletion, DebugTemplate}; use helix_core::syntax::{DebugArgumentValue, DebugConfigCompletion, DebugTemplate};
@ -73,11 +73,10 @@ fn thread_picker(
let debugger = debugger!(editor); let debugger = debugger!(editor);
let thread_states = debugger.thread_states.clone(); let thread_states = debugger.thread_states.clone();
let picker = FilePicker::new( let picker = Picker::new(threads, thread_states, move |cx, thread, _action| {
threads, callback_fn(cx.editor, thread)
thread_states, })
move |cx, thread, _action| callback_fn(cx.editor, thread), .with_preview(move |editor, thread| {
move |editor, thread| {
let frames = editor.debugger.as_ref()?.stack_frames.get(&thread.id)?; let frames = editor.debugger.as_ref()?.stack_frames.get(&thread.id)?;
let frame = frames.get(0)?; let frame = frames.get(0)?;
let path = frame.source.as_ref()?.path.clone()?; let path = frame.source.as_ref()?.path.clone()?;
@ -86,8 +85,7 @@ fn thread_picker(
frame.end_line.unwrap_or(frame.line).saturating_sub(1), frame.end_line.unwrap_or(frame.line).saturating_sub(1),
)); ));
Some((path.into(), pos)) Some((path.into(), pos))
}, });
);
compositor.push(Box::new(picker)); compositor.push(Box::new(picker));
}, },
); );
@ -580,7 +578,7 @@ pub fn dap_variables(cx: &mut Context) {
let contents = Text::from(tui::text::Text::from(variables)); let contents = Text::from(tui::text::Text::from(variables));
let popup = Popup::new("dap-variables", contents); let popup = Popup::new("dap-variables", contents);
cx.push_layer(Box::new(popup)); cx.replace_or_push_layer("dap-variables", popup);
} }
pub fn dap_terminate(cx: &mut Context) { pub fn dap_terminate(cx: &mut Context) {
@ -728,10 +726,7 @@ pub fn dap_switch_stack_frame(cx: &mut Context) {
let frames = debugger.stack_frames[&thread_id].clone(); let frames = debugger.stack_frames[&thread_id].clone();
let picker = FilePicker::new( let picker = Picker::new(frames, (), move |cx, frame, _action| {
frames,
(),
move |cx, frame, _action| {
let debugger = debugger!(cx.editor); let debugger = debugger!(cx.editor);
// TODO: this should be simpler to find // TODO: this should be simpler to find
let pos = debugger.stack_frames[&thread_id] let pos = debugger.stack_frames[&thread_id]
@ -745,8 +740,8 @@ pub fn dap_switch_stack_frame(cx: &mut Context) {
if let Some(frame) = &frame { if let Some(frame) = &frame {
jump_to_stack_frame(cx.editor, frame); jump_to_stack_frame(cx.editor, frame);
} }
}, })
move |_editor, frame| { .with_preview(move |_editor, frame| {
frame frame
.source .source
.as_ref() .as_ref()
@ -760,7 +755,6 @@ pub fn dap_switch_stack_frame(cx: &mut Context) {
)), )),
) )
}) })
}, });
);
cx.push_layer(Box::new(picker)) cx.push_layer(Box::new(picker))
} }

@ -26,7 +26,7 @@ use steel::{rvals::Custom, steel_vm::builtin::BuiltInModule};
use crate::{ use crate::{
compositor::{self, Component, Compositor}, compositor::{self, Component, Compositor},
job::{self, Callback}, job::{self, Callback},
keymap::{merge_keys, Keymap}, keymap::{merge_keys, KeyTrie},
ui::{self, menu::Item, overlay::overlaid, Popup, Prompt, PromptEvent}, ui::{self, menu::Item, overlay::overlaid, Popup, Prompt, PromptEvent},
}; };
@ -256,7 +256,7 @@ impl SharedKeyBindingsEventQueue {
.push_back(other_as_json); .push_back(other_as_json);
} }
pub fn get() -> Option<HashMap<Mode, Keymap>> { pub fn get() -> Option<HashMap<Mode, KeyTrie>> {
let mut guard = KEYBINDING_QUEUE.raw_bindings.lock().unwrap(); let mut guard = KEYBINDING_QUEUE.raw_bindings.lock().unwrap();
if let Some(initial) = guard.pop_front() { if let Some(initial) = guard.pop_front() {

File diff suppressed because it is too large Load Diff

@ -385,6 +385,36 @@ fn force_write(
write_impl(cx, args.first(), true) write_impl(cx, args.first(), true)
} }
fn write_buffer_close(
cx: &mut compositor::Context,
args: &[Cow<str>],
event: PromptEvent,
) -> anyhow::Result<()> {
if event != PromptEvent::Validate {
return Ok(());
}
write_impl(cx, args.first(), false)?;
let document_ids = buffer_gather_paths_impl(cx.editor, args);
buffer_close_by_ids_impl(cx, &document_ids, false)
}
fn force_write_buffer_close(
cx: &mut compositor::Context,
args: &[Cow<str>],
event: PromptEvent,
) -> anyhow::Result<()> {
if event != PromptEvent::Validate {
return Ok(());
}
write_impl(cx, args.first(), true)?;
let document_ids = buffer_gather_paths_impl(cx.editor, args);
buffer_close_by_ids_impl(cx, &document_ids, false)
}
fn new_file( fn new_file(
cx: &mut compositor::Context, cx: &mut compositor::Context,
_args: &[Cow<str>], _args: &[Cow<str>],
@ -868,6 +898,25 @@ fn yank_main_selection_to_clipboard(
yank_main_selection_to_clipboard_impl(cx.editor, ClipboardType::Clipboard) yank_main_selection_to_clipboard_impl(cx.editor, ClipboardType::Clipboard)
} }
fn yank_joined(
cx: &mut compositor::Context,
args: &[Cow<str>],
event: PromptEvent,
) -> anyhow::Result<()> {
if event != PromptEvent::Validate {
return Ok(());
}
ensure!(args.len() <= 1, ":yank-join takes at most 1 argument");
let doc = doc!(cx.editor);
let default_sep = Cow::Borrowed(doc.line_ending.as_str());
let separator = args.first().unwrap_or(&default_sep);
let register = cx.editor.selected_register.unwrap_or('"');
yank_joined_impl(cx.editor, separator, register);
Ok(())
}
fn yank_joined_to_clipboard( fn yank_joined_to_clipboard(
cx: &mut compositor::Context, cx: &mut compositor::Context,
args: &[Cow<str>], args: &[Cow<str>],
@ -1302,26 +1351,22 @@ fn lsp_workspace_command(
if event != PromptEvent::Validate { if event != PromptEvent::Validate {
return Ok(()); return Ok(());
} }
let doc = doc!(cx.editor);
let (_, doc) = current!(cx.editor); let Some((language_server_id, options)) = doc
.language_servers_with_feature(LanguageServerFeature::WorkspaceCommand)
let language_server = match doc.language_server() { .find_map(|ls| {
Some(language_server) => language_server, ls.capabilities()
None => { .execute_command_provider
cx.editor .as_ref()
.set_status("Language server not active for current buffer"); .map(|options| (ls.id(), options))
})
else {
cx.editor.set_status(
"No active language servers for this document support workspace commands",
);
return Ok(()); return Ok(());
}
}; };
let options = match &language_server.capabilities().execute_command_provider {
Some(options) => options,
None => {
cx.editor
.set_status("Workspace commands are not supported for this language server");
return Ok(());
}
};
if args.is_empty() { if args.is_empty() {
let commands = options let commands = options
.commands .commands
@ -1335,8 +1380,8 @@ fn lsp_workspace_command(
let callback = async move { let callback = async move {
let call: job::Callback = Callback::EditorCompositor(Box::new( let call: job::Callback = Callback::EditorCompositor(Box::new(
move |_editor: &mut Editor, compositor: &mut Compositor| { move |_editor: &mut Editor, compositor: &mut Compositor| {
let picker = ui::Picker::new(commands, (), |cx, command, _action| { let picker = ui::Picker::new(commands, (), move |cx, command, _action| {
execute_lsp_command(cx.editor, command.clone()); execute_lsp_command(cx.editor, language_server_id, command.clone());
}); });
compositor.push(Box::new(overlaid(picker))) compositor.push(Box::new(overlaid(picker)))
}, },
@ -1349,6 +1394,7 @@ fn lsp_workspace_command(
if options.commands.iter().any(|c| c == &command) { if options.commands.iter().any(|c| c == &command) {
execute_lsp_command( execute_lsp_command(
cx.editor, cx.editor,
language_server_id,
helix_lsp::lsp::Command { helix_lsp::lsp::Command {
title: command.clone(), title: command.clone(),
arguments: None, arguments: None,
@ -1380,7 +1426,6 @@ fn lsp_restart(
.language_config() .language_config()
.context("LSP not defined for the current document")?; .context("LSP not defined for the current document")?;
let scope = config.scope.clone();
cx.editor.language_servers.restart( cx.editor.language_servers.restart(
config, config,
doc.path(), doc.path(),
@ -1393,13 +1438,22 @@ fn lsp_restart(
.editor .editor
.documents() .documents()
.filter_map(|doc| match doc.language_config() { .filter_map(|doc| match doc.language_config() {
Some(config) if config.scope.eq(&scope) => Some(doc.id()), Some(config)
if config.language_servers.iter().any(|ls| {
config
.language_servers
.iter()
.any(|restarted_ls| restarted_ls.name == ls.name)
}) =>
{
Some(doc.id())
}
_ => None, _ => None,
}) })
.collect(); .collect();
for document_id in document_ids_to_refresh { for document_id in document_ids_to_refresh {
cx.editor.refresh_language_server(document_id); cx.editor.refresh_language_servers(document_id);
} }
Ok(()) Ok(())
@ -1414,22 +1468,18 @@ fn lsp_stop(
return Ok(()); return Ok(());
} }
let doc = doc!(cx.editor); let ls_shutdown_names = doc!(cx.editor)
.language_servers()
let ls_id = doc .map(|ls| ls.name().to_string())
.language_server() .collect::<Vec<_>>();
.map(|ls| ls.id())
.context("LSP not running for the current document")?;
let config = doc for ls_name in &ls_shutdown_names {
.language_config() cx.editor.language_servers.stop(ls_name);
.context("LSP not defined for the current document")?;
cx.editor.language_servers.stop(config);
for doc in cx.editor.documents_mut() { for doc in cx.editor.documents_mut() {
if doc.language_server().map_or(false, |ls| ls.id() == ls_id) { if let Some(client) = doc.remove_language_server_by_name(ls_name) {
doc.set_language_server(None); doc.clear_diagnostics(client.id());
doc.set_diagnostics(Default::default()); }
} }
} }
@ -1737,7 +1787,7 @@ fn set_option(
*value = if value.is_string() { *value = if value.is_string() {
// JSON strings require quotes, so we can't .parse() directly // JSON strings require quotes, so we can't .parse() directly
serde_json::Value::String(arg.to_string()) Value::String(arg.to_string())
} else { } else {
arg.parse().map_err(field_error)? arg.parse().map_err(field_error)?
}; };
@ -1762,8 +1812,8 @@ fn toggle_option(
return Ok(()); return Ok(());
} }
if args.len() != 1 { if args.is_empty() {
anyhow::bail!("Bad arguments. Usage: `:toggle key`"); anyhow::bail!("Bad arguments. Usage: `:toggle key [values]?`");
} }
let key = &args[0].to_lowercase(); let key = &args[0].to_lowercase();
@ -1773,22 +1823,43 @@ fn toggle_option(
let pointer = format!("/{}", key.replace('.', "/")); let pointer = format!("/{}", key.replace('.', "/"));
let value = config.pointer_mut(&pointer).ok_or_else(key_error)?; let value = config.pointer_mut(&pointer).ok_or_else(key_error)?;
let Value::Bool(old_value) = *value else { *value = match value {
anyhow::bail!("Key `{}` is not toggle-able", key) Value::Bool(ref value) => {
ensure!(
args.len() == 1,
"Bad arguments. For boolean configurations use: `:toggle key`"
);
Value::Bool(!value)
}
Value::String(ref value) => {
ensure!(
args.len() > 2,
"Bad arguments. For string configurations use: `:toggle key val1 val2 ...`",
);
Value::String(
args[1..]
.iter()
.skip_while(|e| *e != value)
.nth(1)
.unwrap_or_else(|| &args[1])
.to_string(),
)
}
Value::Null | Value::Object(_) | Value::Array(_) | Value::Number(_) => {
anyhow::bail!("Configuration {key} does not support toggle yet")
}
}; };
let new_value = !old_value; let status = format!("'{key}' is now set to {value}");
*value = Value::Bool(new_value); let config = serde_json::from_value(config)
// This unwrap should never fail because we only replace one boolean value .map_err(|_| anyhow::anyhow!("Could not parse field: `{:?}`", &args))?;
// with another, maintaining a valid json config
let config = serde_json::from_value(config).unwrap();
cx.editor cx.editor
.config_events .config_events
.0 .0
.send(ConfigEvent::Update(config))?; .send(ConfigEvent::Update(config))?;
cx.editor cx.editor.set_status(status);
.set_status(format!("Option `{}` is now set to `{}`", key, new_value));
Ok(()) Ok(())
} }
@ -1823,7 +1894,7 @@ fn language(
doc.detect_indent_and_line_ending(); doc.detect_indent_and_line_ending();
let id = doc.id(); let id = doc.id();
cx.editor.refresh_language_server(id); cx.editor.refresh_language_servers(id);
Ok(()) Ok(())
} }
@ -2327,10 +2398,24 @@ pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[
TypableCommand { TypableCommand {
name: "write!", name: "write!",
aliases: &["w!"], aliases: &["w!"],
doc: "Force write changes to disk creating necessary subdirectories. Accepts an optional path (:write some/path.txt)", doc: "Force write changes to disk creating necessary subdirectories. Accepts an optional path (:write! some/path.txt)",
fun: force_write, fun: force_write,
signature: CommandSignature::positional(&[completers::filename]), signature: CommandSignature::positional(&[completers::filename]),
}, },
TypableCommand {
name: "write-buffer-close",
aliases: &["wbc"],
doc: "Write changes to disk and closes the buffer. Accepts an optional path (:write-buffer-close some/path.txt)",
fun: write_buffer_close,
signature: CommandSignature::positional(&[completers::filename]),
},
TypableCommand {
name: "write-buffer-close!",
aliases: &["wbc!"],
doc: "Force write changes to disk creating necessary subdirectories and closes the buffer. Accepts an optional path (:write-buffer-close! some/path.txt)",
fun: force_write_buffer_close,
signature: CommandSignature::positional(&[completers::filename]),
},
TypableCommand { TypableCommand {
name: "new", name: "new",
aliases: &["n"], aliases: &["n"],
@ -2448,6 +2533,13 @@ pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[
fun: theme, fun: theme,
signature: CommandSignature::positional(&[completers::theme]), signature: CommandSignature::positional(&[completers::theme]),
}, },
TypableCommand {
name: "yank-join",
aliases: &[],
doc: "Yank joined selections. A separator can be provided as first argument. Default value is newline.",
fun: yank_joined,
signature: CommandSignature::none(),
},
TypableCommand { TypableCommand {
name: "clipboard-yank", name: "clipboard-yank",
aliases: &[], aliases: &[],
@ -2555,14 +2647,14 @@ pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[
}, },
TypableCommand { TypableCommand {
name: "reload", name: "reload",
aliases: &[], aliases: &["rl"],
doc: "Discard changes and reload from the source file.", doc: "Discard changes and reload from the source file.",
fun: reload, fun: reload,
signature: CommandSignature::none(), signature: CommandSignature::none(),
}, },
TypableCommand { TypableCommand {
name: "reload-all", name: "reload-all",
aliases: &[], aliases: &["rla"],
doc: "Discard changes and reload all documents from the source files.", doc: "Discard changes and reload all documents from the source files.",
fun: reload_all, fun: reload_all,
signature: CommandSignature::none(), signature: CommandSignature::none(),
@ -2584,14 +2676,14 @@ pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[
TypableCommand { TypableCommand {
name: "lsp-restart", name: "lsp-restart",
aliases: &[], aliases: &[],
doc: "Restarts the Language Server that is in use by the current doc", doc: "Restarts the language servers used by the current doc",
fun: lsp_restart, fun: lsp_restart,
signature: CommandSignature::none(), signature: CommandSignature::none(),
}, },
TypableCommand { TypableCommand {
name: "lsp-stop", name: "lsp-stop",
aliases: &[], aliases: &[],
doc: "Stops the Language Server that is in use by the current doc", doc: "Stops the language servers that are used by the current doc",
fun: lsp_stop, fun: lsp_stop,
signature: CommandSignature::none(), signature: CommandSignature::none(),
}, },
@ -2859,13 +2951,10 @@ pub(super) fn command_mode(cx: &mut Context) {
} else { } else {
// Otherwise, use the command's completer and the last shellword // Otherwise, use the command's completer and the last shellword
// as completion input. // as completion input.
let (part, part_len) = if words.len() == 1 || shellwords.ends_with_whitespace() { let (word, word_len) = if words.len() == 1 || shellwords.ends_with_whitespace() {
(&Cow::Borrowed(""), 0) (&Cow::Borrowed(""), 0)
} else { } else {
( (words.last().unwrap(), words.last().unwrap().len())
words.last().unwrap(),
shellwords.parts().last().unwrap().len(),
)
}; };
let argument_number = argument_number_of(&shellwords); let argument_number = argument_number_of(&shellwords);
@ -2874,13 +2963,13 @@ pub(super) fn command_mode(cx: &mut Context) {
.get(&words[0] as &str) .get(&words[0] as &str)
.map(|tc| tc.completer_for_argument_number(argument_number)) .map(|tc| tc.completer_for_argument_number(argument_number))
{ {
completer(editor, part) completer(editor, word)
.into_iter() .into_iter()
.map(|(range, file)| { .map(|(range, file)| {
let file = shellwords::escape(file); let file = shellwords::escape(file);
// offset ranges to input // offset ranges to input
let offset = input.len() - part_len; let offset = input.len() - word_len;
let range = (range.start + offset)..; let range = (range.start + offset)..;
(range, file) (range, file)
}) })

@ -1,5 +1,5 @@
use crate::keymap; use crate::keymap;
use crate::keymap::{merge_keys, Keymap}; use crate::keymap::{merge_keys, KeyTrie, Keymaps};
use helix_loader::merge_toml_values; use helix_loader::merge_toml_values;
use helix_view::document::Mode; use helix_view::document::Mode;
use serde::Deserialize; use serde::Deserialize;
@ -12,7 +12,7 @@ use toml::de::Error as TomlError;
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq)]
pub struct Config { pub struct Config {
pub theme: Option<String>, pub theme: Option<String>,
pub keys: HashMap<Mode, Keymap>, pub keys: HashMap<Mode, KeyTrie>,
pub editor: helix_view::editor::Config, pub editor: helix_view::editor::Config,
} }
@ -20,7 +20,7 @@ pub struct Config {
#[serde(deny_unknown_fields)] #[serde(deny_unknown_fields)]
pub struct ConfigRaw { pub struct ConfigRaw {
pub theme: Option<String>, pub theme: Option<String>,
pub keys: Option<HashMap<Mode, Keymap>>, pub keys: Option<HashMap<Mode, KeyTrie>>,
pub editor: Option<toml::Value>, pub editor: Option<toml::Value>,
} }
@ -59,7 +59,7 @@ impl Config {
pub fn load( pub fn load(
global: Result<String, ConfigLoadError>, global: Result<String, ConfigLoadError>,
local: Result<String, ConfigLoadError>, local: Result<String, ConfigLoadError>,
engine_overlay: Option<HashMap<Mode, Keymap>>, engine_overlay: Option<HashMap<Mode, KeyTrie>>,
) -> Result<Config, ConfigLoadError> { ) -> Result<Config, ConfigLoadError> {
let global_config: Result<ConfigRaw, ConfigLoadError> = let global_config: Result<ConfigRaw, ConfigLoadError> =
global.and_then(|file| toml::from_str(&file).map_err(ConfigLoadError::BadConfig)); global.and_then(|file| toml::from_str(&file).map_err(ConfigLoadError::BadConfig));
@ -152,7 +152,6 @@ mod tests {
#[test] #[test]
fn parsing_keymaps_config_file() { fn parsing_keymaps_config_file() {
use crate::keymap; use crate::keymap;
use crate::keymap::Keymap;
use helix_core::hashmap; use helix_core::hashmap;
use helix_view::document::Mode; use helix_view::document::Mode;
@ -169,13 +168,13 @@ mod tests {
merge_keys( merge_keys(
&mut keys, &mut keys,
hashmap! { hashmap! {
Mode::Insert => Keymap::new(keymap!({ "Insert mode" Mode::Insert => keymap!({ "Insert mode"
"y" => move_line_down, "y" => move_line_down,
"S-C-a" => delete_selection, "S-C-a" => delete_selection,
})), }),
Mode::Normal => Keymap::new(keymap!({ "Normal mode" Mode::Normal => keymap!({ "Normal mode"
"A-F12" => move_next_word_end, "A-F12" => move_next_word_end,
})), }),
}, },
); );

@ -192,10 +192,14 @@ pub fn languages_all() -> std::io::Result<()> {
for lang in &syn_loader_conf.language { for lang in &syn_loader_conf.language {
column(&lang.language_id, Color::Reset); column(&lang.language_id, Color::Reset);
let lsp = lang // TODO multiple language servers (check binary for each supported language server, not just the first)
let lsp = lang.language_servers.first().and_then(|ls| {
syn_loader_conf
.language_server .language_server
.as_ref() .get(&ls.name)
.map(|lsp| lsp.command.to_string()); .map(|config| config.command.clone())
});
check_binary(lsp); check_binary(lsp);
let dap = lang.debugger.as_ref().map(|dap| dap.command.to_string()); let dap = lang.debugger.as_ref().map(|dap| dap.command.to_string());
@ -264,11 +268,15 @@ pub fn language(lang_str: String) -> std::io::Result<()> {
} }
}; };
// TODO multiple language servers
probe_protocol( probe_protocol(
"language server", "language server",
lang.language_server lang.language_servers.first().and_then(|ls| {
.as_ref() syn_loader_conf
.map(|lsp| lsp.command.to_string()), .language_server
.get(&ls.name)
.map(|config| config.command.clone())
}),
)?; )?;
probe_protocol( probe_protocol(

@ -18,7 +18,7 @@ use std::{
pub use default::default; pub use default::default;
use macros::key; use macros::key;
#[derive(Debug, Clone)] #[derive(Debug, Clone, Default)]
pub struct KeyTrieNode { pub struct KeyTrieNode {
/// A label for keys coming under this node, like "Goto mode" /// A label for keys coming under this node, like "Goto mode"
name: String, name: String,
@ -52,10 +52,6 @@ impl KeyTrieNode {
} }
} }
pub fn name(&self) -> &str {
&self.name
}
/// Merge another Node in. Leaves and subnodes from the other node replace /// Merge another Node in. Leaves and subnodes from the other node replace
/// corresponding keyevent in self, except when both other and self have /// corresponding keyevent in self, except when both other and self have
/// subnodes for same key. In that case the merge is recursive. /// subnodes for same key. In that case the merge is recursive.
@ -77,49 +73,40 @@ impl KeyTrieNode {
} }
pub fn infobox(&self) -> Info { pub fn infobox(&self) -> Info {
let mut body: Vec<(&str, BTreeSet<KeyEvent>)> = Vec::with_capacity(self.len()); let mut body: Vec<(BTreeSet<KeyEvent>, &str)> = Vec::with_capacity(self.len());
for (&key, trie) in self.iter() { for (&key, trie) in self.iter() {
let desc = match trie { let desc = match trie {
KeyTrie::Leaf(cmd) => { KeyTrie::MappableCommand(cmd) => {
if cmd.name() == "no_op" { if cmd.name() == "no_op" {
continue; continue;
} }
cmd.doc() cmd.doc()
} }
KeyTrie::Node(n) => n.name(), KeyTrie::Node(n) => &n.name,
KeyTrie::Sequence(_) => "[Multiple commands]", KeyTrie::Sequence(_) => "[Multiple commands]",
}; };
match body.iter().position(|(d, _)| d == &desc) { match body.iter().position(|(_, d)| d == &desc) {
Some(pos) => { Some(pos) => {
body[pos].1.insert(key); body[pos].0.insert(key);
} }
None => body.push((desc, BTreeSet::from([key]))), None => body.push((BTreeSet::from([key]), desc)),
} }
} }
body.sort_unstable_by_key(|(_, keys)| { body.sort_unstable_by_key(|(keys, _)| {
self.order self.order
.iter() .iter()
.position(|&k| k == *keys.iter().next().unwrap()) .position(|&k| k == *keys.iter().next().unwrap())
.unwrap() .unwrap()
}); });
let prefix = format!("{} ", self.name());
if body.iter().all(|(desc, _)| desc.starts_with(&prefix)) { let body: Vec<_> = body
body = body
.into_iter() .into_iter()
.map(|(desc, keys)| (desc.strip_prefix(&prefix).unwrap(), keys)) .map(|(events, desc)| {
let events = events.iter().map(ToString::to_string).collect::<Vec<_>>();
(events.join(", "), desc)
})
.collect(); .collect();
} Info::new(&self.name, &body)
Info::from_keymap(self.name(), body)
}
/// Get a reference to the key trie node's order.
pub fn order(&self) -> &[KeyEvent] {
self.order.as_slice()
}
}
impl Default for KeyTrieNode {
fn default() -> Self {
Self::new("", HashMap::new(), Vec::new())
} }
} }
@ -145,7 +132,7 @@ impl DerefMut for KeyTrieNode {
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq)]
pub enum KeyTrie { pub enum KeyTrie {
Leaf(MappableCommand), MappableCommand(MappableCommand),
Sequence(Vec<MappableCommand>), Sequence(Vec<MappableCommand>),
Node(KeyTrieNode), Node(KeyTrieNode),
} }
@ -174,7 +161,7 @@ impl<'de> serde::de::Visitor<'de> for KeyTrieVisitor {
{ {
command command
.parse::<MappableCommand>() .parse::<MappableCommand>()
.map(KeyTrie::Leaf) .map(KeyTrie::MappableCommand)
.map_err(E::custom) .map_err(E::custom)
} }
@ -208,17 +195,43 @@ impl<'de> serde::de::Visitor<'de> for KeyTrieVisitor {
} }
impl KeyTrie { impl KeyTrie {
pub fn reverse_map(&self) -> ReverseKeymap {
// recursively visit all nodes in keymap
fn map_node(cmd_map: &mut ReverseKeymap, node: &KeyTrie, keys: &mut Vec<KeyEvent>) {
match node {
KeyTrie::MappableCommand(cmd) => {
let name = cmd.name();
if name != "no_op" {
cmd_map.entry(name.into()).or_default().push(keys.clone())
}
}
KeyTrie::Node(next) => {
for (key, trie) in &next.map {
keys.push(*key);
map_node(cmd_map, trie, keys);
keys.pop();
}
}
KeyTrie::Sequence(_) => {}
};
}
let mut res = HashMap::new();
map_node(&mut res, self, &mut Vec::new());
res
}
pub fn node(&self) -> Option<&KeyTrieNode> { pub fn node(&self) -> Option<&KeyTrieNode> {
match *self { match *self {
KeyTrie::Node(ref node) => Some(node), KeyTrie::Node(ref node) => Some(node),
KeyTrie::Leaf(_) | KeyTrie::Sequence(_) => None, KeyTrie::MappableCommand(_) | KeyTrie::Sequence(_) => None,
} }
} }
pub fn node_mut(&mut self) -> Option<&mut KeyTrieNode> { pub fn node_mut(&mut self) -> Option<&mut KeyTrieNode> {
match *self { match *self {
KeyTrie::Node(ref mut node) => Some(node), KeyTrie::Node(ref mut node) => Some(node),
KeyTrie::Leaf(_) | KeyTrie::Sequence(_) => None, KeyTrie::MappableCommand(_) | KeyTrie::Sequence(_) => None,
} }
} }
@ -235,7 +248,7 @@ impl KeyTrie {
trie = match trie { trie = match trie {
KeyTrie::Node(map) => map.get(key), KeyTrie::Node(map) => map.get(key),
// leaf encountered while keys left to process // leaf encountered while keys left to process
KeyTrie::Leaf(_) | KeyTrie::Sequence(_) => None, KeyTrie::MappableCommand(_) | KeyTrie::Sequence(_) => None,
}? }?
} }
Some(trie) Some(trie)
@ -256,75 +269,11 @@ pub enum KeymapResult {
Cancelled(Vec<KeyEvent>), Cancelled(Vec<KeyEvent>),
} }
#[derive(Debug, Clone, PartialEq, Deserialize)]
#[serde(transparent)]
pub struct Keymap {
/// Always a Node
root: KeyTrie,
}
/// A map of command names to keybinds that will execute the command. /// A map of command names to keybinds that will execute the command.
pub type ReverseKeymap = HashMap<String, Vec<Vec<KeyEvent>>>; pub type ReverseKeymap = HashMap<String, Vec<Vec<KeyEvent>>>;
impl Keymap {
pub fn new(root: KeyTrie) -> Self {
Keymap { root }
}
pub fn reverse_map(&self) -> ReverseKeymap {
// recursively visit all nodes in keymap
fn map_node(cmd_map: &mut ReverseKeymap, node: &KeyTrie, keys: &mut Vec<KeyEvent>) {
match node {
KeyTrie::Leaf(cmd) => match cmd {
MappableCommand::Typable { name, .. } => {
cmd_map.entry(name.into()).or_default().push(keys.clone())
}
MappableCommand::Static { name, .. } => cmd_map
.entry(name.to_string())
.or_default()
.push(keys.clone()),
},
KeyTrie::Node(next) => {
for (key, trie) in &next.map {
keys.push(*key);
map_node(cmd_map, trie, keys);
keys.pop();
}
}
KeyTrie::Sequence(_) => {}
};
}
let mut res = HashMap::new();
map_node(&mut res, &self.root, &mut Vec::new());
res
}
pub fn root(&self) -> &KeyTrie {
&self.root
}
pub fn merge(&mut self, other: Self) {
self.root.merge_nodes(other.root);
}
}
impl Deref for Keymap {
type Target = KeyTrieNode;
fn deref(&self) -> &Self::Target {
self.root.node().unwrap()
}
}
impl Default for Keymap {
fn default() -> Self {
Self::new(KeyTrie::Node(KeyTrieNode::default()))
}
}
pub struct Keymaps { pub struct Keymaps {
pub map: Box<dyn DynAccess<HashMap<Mode, Keymap>>>, pub map: Box<dyn DynAccess<HashMap<Mode, KeyTrie>>>,
/// Stores pending keys waiting for the next key. This is relative to a /// Stores pending keys waiting for the next key. This is relative to a
/// sticky node if one is in use. /// sticky node if one is in use.
state: Vec<KeyEvent>, state: Vec<KeyEvent>,
@ -333,7 +282,7 @@ pub struct Keymaps {
} }
impl Keymaps { impl Keymaps {
pub fn new(map: Box<dyn DynAccess<HashMap<Mode, Keymap>>>) -> Self { pub fn new(map: Box<dyn DynAccess<HashMap<Mode, KeyTrie>>>) -> Self {
Self { Self {
map, map,
state: Vec::new(), state: Vec::new(),
@ -341,7 +290,7 @@ impl Keymaps {
} }
} }
pub fn map(&self) -> DynGuard<HashMap<Mode, Keymap>> { pub fn map(&self) -> DynGuard<HashMap<Mode, KeyTrie>> {
self.map.load() self.map.load()
} }
@ -373,11 +322,11 @@ impl Keymaps {
let first = self.state.get(0).unwrap_or(&key); let first = self.state.get(0).unwrap_or(&key);
let trie_node = match self.sticky { let trie_node = match self.sticky {
Some(ref trie) => Cow::Owned(KeyTrie::Node(trie.clone())), Some(ref trie) => Cow::Owned(KeyTrie::Node(trie.clone())),
None => Cow::Borrowed(&keymap.root), None => Cow::Borrowed(keymap),
}; };
let trie = match trie_node.search(&[*first]) { let trie = match trie_node.search(&[*first]) {
Some(KeyTrie::Leaf(ref cmd)) => { Some(KeyTrie::MappableCommand(ref cmd)) => {
return KeymapResult::Matched(cmd.clone()); return KeymapResult::Matched(cmd.clone());
} }
Some(KeyTrie::Sequence(ref cmds)) => { Some(KeyTrie::Sequence(ref cmds)) => {
@ -396,7 +345,7 @@ impl Keymaps {
} }
KeymapResult::Pending(map.clone()) KeymapResult::Pending(map.clone())
} }
Some(KeyTrie::Leaf(cmd)) => { Some(KeyTrie::MappableCommand(cmd)) => {
self.state.clear(); self.state.clear();
KeymapResult::Matched(cmd.clone()) KeymapResult::Matched(cmd.clone())
} }
@ -416,9 +365,13 @@ impl Default for Keymaps {
} }
/// Merge default config keys with user overwritten keys for custom user config. /// Merge default config keys with user overwritten keys for custom user config.
pub fn merge_keys(dst: &mut HashMap<Mode, Keymap>, mut delta: HashMap<Mode, Keymap>) { pub fn merge_keys(dst: &mut HashMap<Mode, KeyTrie>, mut delta: HashMap<Mode, KeyTrie>) {
for (mode, keys) in dst { for (mode, keys) in dst {
keys.merge(delta.remove(mode).unwrap_or_default()) keys.merge_nodes(
delta
.remove(mode)
.unwrap_or_else(|| KeyTrie::Node(KeyTrieNode::default())),
)
} }
} }
@ -447,8 +400,7 @@ mod tests {
#[test] #[test]
fn merge_partial_keys() { fn merge_partial_keys() {
let keymap = hashmap! { let keymap = hashmap! {
Mode::Normal => Keymap::new( Mode::Normal => keymap!({ "Normal mode"
keymap!({ "Normal mode"
"i" => normal_mode, "i" => normal_mode,
"无" => insert_mode, "无" => insert_mode,
"z" => jump_backward, "z" => jump_backward,
@ -457,7 +409,6 @@ mod tests {
"g" => delete_char_forward, "g" => delete_char_forward,
}, },
}) })
)
}; };
let mut merged_keyamp = default(); let mut merged_keyamp = default();
merge_keys(&mut merged_keyamp, keymap.clone()); merge_keys(&mut merged_keyamp, keymap.clone());
@ -484,32 +435,45 @@ mod tests {
let keymap = merged_keyamp.get_mut(&Mode::Normal).unwrap(); let keymap = merged_keyamp.get_mut(&Mode::Normal).unwrap();
// Assumes that `g` is a node in default keymap // Assumes that `g` is a node in default keymap
assert_eq!( assert_eq!(
keymap.root().search(&[key!('g'), key!('$')]).unwrap(), keymap.search(&[key!('g'), key!('$')]).unwrap(),
&KeyTrie::Leaf(MappableCommand::goto_line_end), &KeyTrie::MappableCommand(MappableCommand::goto_line_end),
"Leaf should be present in merged subnode" "Leaf should be present in merged subnode"
); );
// Assumes that `gg` is in default keymap // Assumes that `gg` is in default keymap
assert_eq!( assert_eq!(
keymap.root().search(&[key!('g'), key!('g')]).unwrap(), keymap.search(&[key!('g'), key!('g')]).unwrap(),
&KeyTrie::Leaf(MappableCommand::delete_char_forward), &KeyTrie::MappableCommand(MappableCommand::delete_char_forward),
"Leaf should replace old leaf in merged subnode" "Leaf should replace old leaf in merged subnode"
); );
// Assumes that `ge` is in default keymap // Assumes that `ge` is in default keymap
assert_eq!( assert_eq!(
keymap.root().search(&[key!('g'), key!('e')]).unwrap(), keymap.search(&[key!('g'), key!('e')]).unwrap(),
&KeyTrie::Leaf(MappableCommand::goto_last_line), &KeyTrie::MappableCommand(MappableCommand::goto_last_line),
"Old leaves in subnode should be present in merged node" "Old leaves in subnode should be present in merged node"
); );
assert!(merged_keyamp.get(&Mode::Normal).unwrap().len() > 1); assert!(
assert!(merged_keyamp.get(&Mode::Insert).unwrap().len() > 0); merged_keyamp
.get(&Mode::Normal)
.and_then(|key_trie| key_trie.node())
.unwrap()
.len()
> 1
);
assert!(
merged_keyamp
.get(&Mode::Insert)
.and_then(|key_trie| key_trie.node())
.unwrap()
.len()
> 0
);
} }
#[test] #[test]
fn order_should_be_set() { fn order_should_be_set() {
let keymap = hashmap! { let keymap = hashmap! {
Mode::Normal => Keymap::new( Mode::Normal => keymap!({ "Normal mode"
keymap!({ "Normal mode"
"space" => { "" "space" => { ""
"s" => { "" "s" => { ""
"v" => vsplit, "v" => vsplit,
@ -517,7 +481,6 @@ mod tests {
}, },
}, },
}) })
)
}; };
let mut merged_keyamp = default(); let mut merged_keyamp = default();
merge_keys(&mut merged_keyamp, keymap.clone()); merge_keys(&mut merged_keyamp, keymap.clone());
@ -525,22 +488,19 @@ mod tests {
let keymap = merged_keyamp.get_mut(&Mode::Normal).unwrap(); let keymap = merged_keyamp.get_mut(&Mode::Normal).unwrap();
// Make sure mapping works // Make sure mapping works
assert_eq!( assert_eq!(
keymap keymap.search(&[key!(' '), key!('s'), key!('v')]).unwrap(),
.root() &KeyTrie::MappableCommand(MappableCommand::vsplit),
.search(&[key!(' '), key!('s'), key!('v')])
.unwrap(),
&KeyTrie::Leaf(MappableCommand::vsplit),
"Leaf should be present in merged subnode" "Leaf should be present in merged subnode"
); );
// Make sure an order was set during merge // Make sure an order was set during merge
let node = keymap.root().search(&[crate::key!(' ')]).unwrap(); let node = keymap.search(&[crate::key!(' ')]).unwrap();
assert!(!node.node().unwrap().order().is_empty()) assert!(!node.node().unwrap().order.as_slice().is_empty())
} }
#[test] #[test]
fn aliased_modes_are_same_in_default_keymap() { fn aliased_modes_are_same_in_default_keymap() {
let keymaps = Keymaps::default().map(); let keymaps = Keymaps::default().map();
let root = keymaps.get(&Mode::Normal).unwrap().root(); let root = keymaps.get(&Mode::Normal).unwrap();
assert_eq!( assert_eq!(
root.search(&[key!(' '), key!('w')]).unwrap(), root.search(&[key!(' '), key!('w')]).unwrap(),
root.search(&["C-w".parse::<KeyEvent>().unwrap()]).unwrap(), root.search(&["C-w".parse::<KeyEvent>().unwrap()]).unwrap(),
@ -563,7 +523,7 @@ mod tests {
}, },
"j" | "k" => move_line_down, "j" | "k" => move_line_down,
}); });
let keymap = Keymap::new(normal_mode); let keymap = normal_mode;
let mut reverse_map = keymap.reverse_map(); let mut reverse_map = keymap.reverse_map();
// sort keybindings in order to have consistent tests // sort keybindings in order to have consistent tests
@ -611,7 +571,7 @@ mod tests {
modifiers: KeyModifiers::NONE, modifiers: KeyModifiers::NONE,
}; };
let expectation = Keymap::new(KeyTrie::Node(KeyTrieNode::new( let expectation = KeyTrie::Node(KeyTrieNode::new(
"", "",
hashmap! { hashmap! {
key => KeyTrie::Sequence(vec!{ key => KeyTrie::Sequence(vec!{
@ -628,7 +588,7 @@ mod tests {
}) })
}, },
vec![key], vec![key],
))); ));
assert_eq!(toml::from_str(keys), Ok(expectation)); assert_eq!(toml::from_str(keys), Ok(expectation));
} }

@ -1,10 +1,10 @@
use std::collections::HashMap; use std::collections::HashMap;
use super::macros::keymap; use super::macros::keymap;
use super::{Keymap, Mode}; use super::{KeyTrie, Mode};
use helix_core::hashmap; use helix_core::hashmap;
pub fn default() -> HashMap<Mode, Keymap> { pub fn default() -> HashMap<Mode, KeyTrie> {
let normal = keymap!({ "Normal mode" let normal = keymap!({ "Normal mode"
"h" | "left" => move_char_left, "h" | "left" => move_char_left,
"j" | "down" => move_visual_line_down, "j" | "down" => move_visual_line_down,
@ -79,6 +79,7 @@ pub fn default() -> HashMap<Mode, Keymap> {
"s" => select_regex, "s" => select_regex,
"A-s" => split_selection_on_newline, "A-s" => split_selection_on_newline,
"A-minus" => merge_selections,
"A-_" => merge_consecutive_selections, "A-_" => merge_consecutive_selections,
"S" => split_selection, "S" => split_selection,
";" => collapse_selection, ";" => collapse_selection,
@ -379,8 +380,8 @@ pub fn default() -> HashMap<Mode, Keymap> {
"end" => goto_line_end_newline, "end" => goto_line_end_newline,
}); });
hashmap!( hashmap!(
Mode::Normal => Keymap::new(normal), Mode::Normal => normal,
Mode::Select => Keymap::new(select), Mode::Select => select,
Mode::Insert => Keymap::new(insert), Mode::Insert => insert,
) )
} }

@ -62,12 +62,11 @@ macro_rules! alt {
}; };
} }
/// Macro for defining the root of a `Keymap` object. Example: /// Macro for defining a `KeyTrie`. Example:
/// ///
/// ``` /// ```
/// # use helix_core::hashmap; /// # use helix_core::hashmap;
/// # use helix_term::keymap; /// # use helix_term::keymap;
/// # use helix_term::keymap::Keymap;
/// let normal_mode = keymap!({ "Normal mode" /// let normal_mode = keymap!({ "Normal mode"
/// "i" => insert_mode, /// "i" => insert_mode,
/// "g" => { "Goto" /// "g" => { "Goto"
@ -76,12 +75,12 @@ macro_rules! alt {
/// }, /// },
/// "j" | "down" => move_line_down, /// "j" | "down" => move_line_down,
/// }); /// });
/// let keymap = Keymap::new(normal_mode); /// let keymap = normal_mode;
/// ``` /// ```
#[macro_export] #[macro_export]
macro_rules! keymap { macro_rules! keymap {
(@trie $cmd:ident) => { (@trie $cmd:ident) => {
$crate::keymap::KeyTrie::Leaf($crate::commands::MappableCommand::$cmd) $crate::keymap::KeyTrie::MappableCommand($crate::commands::MappableCommand::$cmd)
}; };
(@trie (@trie

@ -68,7 +68,7 @@ FLAGS:
-g, --grammar {{fetch|build}} Fetches or builds tree-sitter grammars listed in languages.toml -g, --grammar {{fetch|build}} Fetches or builds tree-sitter grammars listed in languages.toml
-c, --config <file> Specifies a file to use for configuration -c, --config <file> Specifies a file to use for configuration
-v Increases logging verbosity each use for up to 3 times -v Increases logging verbosity each use for up to 3 times
--log Specifies a file to use for logging --log <file> Specifies a file to use for logging
(default file: {}) (default file: {})
-V, --version Prints version information -V, --version Prints version information
--vsplit Splits all given files vertically into different windows --vsplit Splits all given files vertically into different windows

@ -15,8 +15,7 @@ use helix_view::{graphics::Rect, Document, Editor};
use crate::commands; use crate::commands;
use crate::ui::{menu, Markdown, Menu, Popup, PromptEvent}; use crate::ui::{menu, Markdown, Menu, Popup, PromptEvent};
use helix_lsp::{lsp, util}; use helix_lsp::{lsp, util, OffsetEncoding};
use lsp::CompletionItem;
impl menu::Item for CompletionItem { impl menu::Item for CompletionItem {
type Data = (); type Data = ();
@ -26,28 +25,30 @@ impl menu::Item for CompletionItem {
#[inline] #[inline]
fn filter_text(&self, _data: &Self::Data) -> Cow<str> { fn filter_text(&self, _data: &Self::Data) -> Cow<str> {
self.filter_text self.item
.filter_text
.as_ref() .as_ref()
.unwrap_or(&self.label) .unwrap_or(&self.item.label)
.as_str() .as_str()
.into() .into()
} }
fn format(&self, _data: &Self::Data) -> menu::Row { fn format(&self, _data: &Self::Data) -> menu::Row {
let deprecated = self.deprecated.unwrap_or_default() let deprecated = self.item.deprecated.unwrap_or_default()
|| self.tags.as_ref().map_or(false, |tags| { || self.item.tags.as_ref().map_or(false, |tags| {
tags.contains(&lsp::CompletionItemTag::DEPRECATED) tags.contains(&lsp::CompletionItemTag::DEPRECATED)
}); });
menu::Row::new(vec![ menu::Row::new(vec![
menu::Cell::from(Span::styled( menu::Cell::from(Span::styled(
self.label.as_str(), self.item.label.as_str(),
if deprecated { if deprecated {
Style::default().add_modifier(Modifier::CROSSED_OUT) Style::default().add_modifier(Modifier::CROSSED_OUT)
} else { } else {
Style::default() Style::default()
}, },
)), )),
menu::Cell::from(match self.kind { menu::Cell::from(match self.item.kind {
Some(lsp::CompletionItemKind::TEXT) => "text", Some(lsp::CompletionItemKind::TEXT) => "text",
Some(lsp::CompletionItemKind::METHOD) => "method", Some(lsp::CompletionItemKind::METHOD) => "method",
Some(lsp::CompletionItemKind::FUNCTION) => "function", Some(lsp::CompletionItemKind::FUNCTION) => "function",
@ -79,15 +80,17 @@ impl menu::Item for CompletionItem {
} }
None => "", None => "",
}), }),
// self.detail.as_deref().unwrap_or("")
// self.label_details
// .as_ref()
// .or(self.detail())
// .as_str(),
]) ])
} }
} }
#[derive(Debug, PartialEq, Default, Clone)]
pub struct CompletionItem {
pub item: lsp::CompletionItem,
pub language_server_id: usize,
pub resolved: bool,
}
/// Wraps a Menu. /// Wraps a Menu.
pub struct Completion { pub struct Completion {
popup: Popup<Menu<CompletionItem>>, popup: Popup<Menu<CompletionItem>>,
@ -104,21 +107,21 @@ impl Completion {
editor: &Editor, editor: &Editor,
savepoint: Arc<SavePoint>, savepoint: Arc<SavePoint>,
mut items: Vec<CompletionItem>, mut items: Vec<CompletionItem>,
offset_encoding: helix_lsp::OffsetEncoding,
start_offset: usize, start_offset: usize,
trigger_offset: usize, trigger_offset: usize,
) -> Self { ) -> Self {
let preview_completion_insert = editor.config().preview_completion_insert;
let replace_mode = editor.config().completion_replace; let replace_mode = editor.config().completion_replace;
// Sort completion items according to their preselect status (given by the LSP server) // Sort completion items according to their preselect status (given by the LSP server)
items.sort_by_key(|item| !item.preselect.unwrap_or(false)); items.sort_by_key(|item| !item.item.preselect.unwrap_or(false));
// Then create the menu // Then create the menu
let menu = Menu::new(items, (), move |editor: &mut Editor, item, event| { let menu = Menu::new(items, (), move |editor: &mut Editor, item, event| {
fn item_to_transaction( fn item_to_transaction(
doc: &Document, doc: &Document,
view_id: ViewId, view_id: ViewId,
item: &CompletionItem, item: &lsp::CompletionItem,
offset_encoding: helix_lsp::OffsetEncoding, offset_encoding: OffsetEncoding,
trigger_offset: usize, trigger_offset: usize,
include_placeholder: bool, include_placeholder: bool,
replace_mode: bool, replace_mode: bool,
@ -209,77 +212,108 @@ impl Completion {
let (view, doc) = current!(editor); let (view, doc) = current!(editor);
// if more text was entered, remove it macro_rules! language_server {
doc.restore(view, &savepoint); ($item:expr) => {
match editor
.language_servers
.get_by_id($item.language_server_id)
{
Some(ls) => ls,
None => {
editor.set_error("completions are outdated");
// TODO close the completion menu somehow,
// currently there is no trivial way to access the EditorView to close the completion menu
return;
}
}
};
}
match event { match event {
PromptEvent::Abort => { PromptEvent::Abort => {}
editor.last_completion = None; PromptEvent::Update if preview_completion_insert => {
// Update creates "ghost" transactions which are not sent to the
// lsp server to avoid messing up re-requesting completions. Once a
// completion has been selected (with tab, c-n or c-p) it's always accepted whenever anything
// is typed. The only way to avoid that is to explicitly abort the completion
// with c-c. This will remove the "ghost" transaction.
//
// The ghost transaction is modeled with a transaction that is not sent to the LS.
// (apply_temporary) and a savepoint. It's extremely important this savepoint is restored
// (also without sending the transaction to the LS) *before any further transaction is applied*.
// Otherwise incremental sync breaks (since the state of the LS doesn't match the state the transaction
// is applied to).
if editor.last_completion.is_none() {
editor.last_completion = Some(CompleteAction::Selected {
savepoint: doc.savepoint(view),
})
} }
PromptEvent::Update => { // if more text was entered, remove it
doc.restore(view, &savepoint, false);
// always present here // always present here
let item = item.unwrap(); let item = item.unwrap();
let transaction = item_to_transaction( let transaction = item_to_transaction(
doc, doc,
view.id, view.id,
item, &item.item,
offset_encoding, language_server!(item).offset_encoding(),
trigger_offset, trigger_offset,
true, true,
replace_mode, replace_mode,
); );
doc.apply_temporary(&transaction, view.id);
// initialize a savepoint
doc.apply(&transaction, view.id);
editor.last_completion = Some(CompleteAction {
trigger_offset,
changes: completion_changes(&transaction, trigger_offset),
});
} }
PromptEvent::Update => {}
PromptEvent::Validate => { PromptEvent::Validate => {
if let Some(CompleteAction::Selected { savepoint }) =
editor.last_completion.take()
{
doc.restore(view, &savepoint, false);
}
// always present here // always present here
let item = item.unwrap(); let mut item = item.unwrap().clone();
let language_server = language_server!(item);
let offset_encoding = language_server.offset_encoding();
let language_server = editor
.language_servers
.get_by_id(item.language_server_id)
.unwrap();
// resolve item if not yet resolved
if !item.resolved {
if let Some(resolved) =
Self::resolve_completion_item(language_server, item.item.clone())
{
item.item = resolved;
}
};
// if more text was entered, remove it
doc.restore(view, &savepoint, true);
let transaction = item_to_transaction( let transaction = item_to_transaction(
doc, doc,
view.id, view.id,
item, &item.item,
offset_encoding, offset_encoding,
trigger_offset, trigger_offset,
false, false,
replace_mode, replace_mode,
); );
doc.apply(&transaction, view.id); doc.apply(&transaction, view.id);
editor.last_completion = Some(CompleteAction { editor.last_completion = Some(CompleteAction::Applied {
trigger_offset, trigger_offset,
changes: completion_changes(&transaction, trigger_offset), changes: completion_changes(&transaction, trigger_offset),
}); });
// apply additional edits, mostly used to auto import unqualified types // TODO: add additional _edits to completion_changes?
let resolved_item = if item if let Some(additional_edits) = item.item.additional_text_edits {
.additional_text_edits
.as_ref()
.map(|edits| !edits.is_empty())
.unwrap_or(false)
{
None
} else {
Self::resolve_completion_item(doc, item.clone())
};
if let Some(additional_edits) = resolved_item
.as_ref()
.and_then(|item| item.additional_text_edits.as_ref())
.or(item.additional_text_edits.as_ref())
{
if !additional_edits.is_empty() { if !additional_edits.is_empty() {
let transaction = util::generate_transaction_from_edits( let transaction = util::generate_transaction_from_edits(
doc.text(), doc.text(),
additional_edits.clone(), additional_edits,
offset_encoding, // TODO: should probably transcode in Client offset_encoding, // TODO: should probably transcode in Client
); );
doc.apply(&transaction, view.id); doc.apply(&transaction, view.id);
@ -304,11 +338,9 @@ impl Completion {
} }
fn resolve_completion_item( fn resolve_completion_item(
doc: &Document, language_server: &helix_lsp::Client,
completion_item: lsp::CompletionItem, completion_item: lsp::CompletionItem,
) -> Option<CompletionItem> { ) -> Option<lsp::CompletionItem> {
let language_server = doc.language_server()?;
let future = language_server.resolve_completion_item(completion_item)?; let future = language_server.resolve_completion_item(completion_item)?;
let response = helix_lsp::block_on(future); let response = helix_lsp::block_on(future);
match response { match response {
@ -359,7 +391,7 @@ impl Completion {
self.popup.contents().is_empty() self.popup.contents().is_empty()
} }
fn replace_item(&mut self, old_item: lsp::CompletionItem, new_item: lsp::CompletionItem) { fn replace_item(&mut self, old_item: CompletionItem, new_item: CompletionItem) {
self.popup.contents_mut().replace_option(old_item, new_item); self.popup.contents_mut().replace_option(old_item, new_item);
} }
@ -375,20 +407,14 @@ impl Completion {
// > The returned completion item should have the documentation property filled in. // > The returned completion item should have the documentation property filled in.
// https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_completion // https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_completion
let current_item = match self.popup.contents().selection() { let current_item = match self.popup.contents().selection() {
Some(item) if item.documentation.is_none() => item.clone(), Some(item) if !item.resolved => item.clone(),
_ => return false, _ => return false,
}; };
let language_server = match doc!(cx.editor).language_server() { let Some(language_server) = cx.editor.language_server_by_id(current_item.language_server_id) else { return false; };
Some(language_server) => language_server,
None => return false,
};
// This method should not block the compositor so we handle the response asynchronously. // This method should not block the compositor so we handle the response asynchronously.
let future = match language_server.resolve_completion_item(current_item.clone()) { let Some(future) = language_server.resolve_completion_item(current_item.item.clone()) else { return false; };
Some(future) => future,
None => return false,
};
cx.callback( cx.callback(
future, future,
@ -403,6 +429,12 @@ impl Completion {
.unwrap() .unwrap()
.completion .completion
{ {
let resolved_item = CompletionItem {
item: resolved_item,
language_server_id: current_item.language_server_id,
resolved: true,
};
completion.replace_item(current_item, resolved_item); completion.replace_item(current_item, resolved_item);
} }
}, },
@ -457,25 +489,25 @@ impl Component for Completion {
Markdown::new(md, cx.editor.syn_loader.clone()) Markdown::new(md, cx.editor.syn_loader.clone())
}; };
let mut markdown_doc = match &option.documentation { let mut markdown_doc = match &option.item.documentation {
Some(lsp::Documentation::String(contents)) Some(lsp::Documentation::String(contents))
| Some(lsp::Documentation::MarkupContent(lsp::MarkupContent { | Some(lsp::Documentation::MarkupContent(lsp::MarkupContent {
kind: lsp::MarkupKind::PlainText, kind: lsp::MarkupKind::PlainText,
value: contents, value: contents,
})) => { })) => {
// TODO: convert to wrapped text // TODO: convert to wrapped text
markdowned(language, option.detail.as_deref(), Some(contents)) markdowned(language, option.item.detail.as_deref(), Some(contents))
} }
Some(lsp::Documentation::MarkupContent(lsp::MarkupContent { Some(lsp::Documentation::MarkupContent(lsp::MarkupContent {
kind: lsp::MarkupKind::Markdown, kind: lsp::MarkupKind::Markdown,
value: contents, value: contents,
})) => { })) => {
// TODO: set language based on doc scope // TODO: set language based on doc scope
markdowned(language, option.detail.as_deref(), Some(contents)) markdowned(language, option.item.detail.as_deref(), Some(contents))
} }
None if option.detail.is_some() => { None if option.item.detail.is_some() => {
// TODO: set language based on doc scope // TODO: set language based on doc scope
markdowned(language, option.detail.as_deref(), None) markdowned(language, option.item.detail.as_deref(), None)
} }
None => return, None => return,
}; };

@ -19,7 +19,7 @@ use helix_core::{
syntax::{self, HighlightEvent}, syntax::{self, HighlightEvent},
text_annotations::TextAnnotations, text_annotations::TextAnnotations,
unicode::width::UnicodeWidthStr, unicode::width::UnicodeWidthStr,
visual_offset_from_block, Position, Range, Selection, Transaction, visual_offset_from_block, Change, Position, Range, Selection, Transaction,
}; };
use helix_view::{ use helix_view::{
document::{Mode, SavePoint, SCRATCH_BUFFER_NAME}, document::{Mode, SavePoint, SCRATCH_BUFFER_NAME},
@ -33,7 +33,7 @@ use std::{mem::take, num::NonZeroUsize, path::PathBuf, rc::Rc, sync::Arc};
use tui::{buffer::Buffer as Surface, text::Span}; use tui::{buffer::Buffer as Surface, text::Span};
use super::statusline; use super::{completion::CompletionItem, statusline};
use super::{document::LineDecoration, lsp::SignatureHelp}; use super::{document::LineDecoration, lsp::SignatureHelp};
pub struct EditorView { pub struct EditorView {
@ -48,7 +48,10 @@ pub struct EditorView {
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub enum InsertEvent { pub enum InsertEvent {
Key(KeyEvent), Key(KeyEvent),
CompletionApply(CompleteAction), CompletionApply {
trigger_offset: usize,
changes: Vec<Change>,
},
TriggerCompletion, TriggerCompletion,
RequestCompletion, RequestCompletion,
} }
@ -103,7 +106,7 @@ impl EditorView {
// Set DAP highlights, if needed. // Set DAP highlights, if needed.
if let Some(frame) = editor.current_stack_frame() { if let Some(frame) = editor.current_stack_frame() {
let dap_line = frame.line.saturating_sub(1) as usize; let dap_line = frame.line.saturating_sub(1);
let style = theme.get("ui.highlight.frameline"); let style = theme.get("ui.highlight.frameline");
let line_decoration = move |renderer: &mut TextRenderer, pos: LinePos| { let line_decoration = move |renderer: &mut TextRenderer, pos: LinePos| {
if pos.doc_line != dap_line { if pos.doc_line != dap_line {
@ -498,7 +501,9 @@ impl EditorView {
use helix_core::match_brackets; use helix_core::match_brackets;
let pos = doc.selection(view.id).primary().cursor(text); let pos = doc.selection(view.id).primary().cursor(text);
if let Some(pos) = match_brackets::find_matching_bracket(syntax, doc.text(), pos) { if let Some(pos) =
match_brackets::find_matching_bracket(syntax, doc.text().slice(..), pos)
{
// ensure col is on screen // ensure col is on screen
if let Some(highlight) = theme.find_scope_index_exact("ui.cursor.match") { if let Some(highlight) = theme.find_scope_index_exact("ui.cursor.match") {
return vec![(highlight, pos..pos + 1)]; return vec![(highlight, pos..pos + 1)];
@ -647,7 +652,7 @@ impl EditorView {
.primary() .primary()
.cursor(doc.text().slice(..)); .cursor(doc.text().slice(..));
let diagnostics = doc.diagnostics().iter().filter(|diagnostic| { let diagnostics = doc.shown_diagnostics().filter(|diagnostic| {
diagnostic.range.start <= cursor && diagnostic.range.end >= cursor diagnostic.range.start <= cursor && diagnostic.range.end >= cursor
}); });
@ -825,7 +830,7 @@ impl EditorView {
} }
(Mode::Insert, Mode::Normal) => { (Mode::Insert, Mode::Normal) => {
// if exiting insert mode, remove completion // if exiting insert mode, remove completion
self.completion = None; self.clear_completion(cxt.editor);
cxt.editor.completion_request_handle = None; cxt.editor.completion_request_handle = None;
// TODO: Use an on_mode_change hook to remove signature help // TODO: Use an on_mode_change hook to remove signature help
@ -917,22 +922,26 @@ impl EditorView {
for key in self.last_insert.1.clone() { for key in self.last_insert.1.clone() {
match key { match key {
InsertEvent::Key(key) => self.insert_mode(cxt, key), InsertEvent::Key(key) => self.insert_mode(cxt, key),
InsertEvent::CompletionApply(compl) => { InsertEvent::CompletionApply {
trigger_offset,
changes,
} => {
let (view, doc) = current!(cxt.editor); let (view, doc) = current!(cxt.editor);
if let Some(last_savepoint) = last_savepoint.as_deref() { if let Some(last_savepoint) = last_savepoint.as_deref() {
doc.restore(view, last_savepoint); doc.restore(view, last_savepoint, true);
} }
let text = doc.text().slice(..); let text = doc.text().slice(..);
let cursor = doc.selection(view.id).primary().cursor(text); let cursor = doc.selection(view.id).primary().cursor(text);
let shift_position = let shift_position = |pos: usize| -> usize {
|pos: usize| -> usize { pos + cursor - compl.trigger_offset }; (pos + cursor).saturating_sub(trigger_offset)
};
let tx = Transaction::change( let tx = Transaction::change(
doc.text(), doc.text(),
compl.changes.iter().cloned().map(|(start, end, t)| { changes.iter().cloned().map(|(start, end, t)| {
(shift_position(start), shift_position(end), t) (shift_position(start), shift_position(end), t)
}), }),
); );
@ -963,6 +972,8 @@ impl EditorView {
self.handle_keymap_event(mode, cxt, event); self.handle_keymap_event(mode, cxt, event);
if self.keymaps.pending().is_empty() { if self.keymaps.pending().is_empty() {
cxt.editor.count = None cxt.editor.count = None
} else {
cxt.editor.selected_register = cxt.register.take();
} }
} }
} }
@ -973,20 +984,13 @@ impl EditorView {
&mut self, &mut self,
editor: &mut Editor, editor: &mut Editor,
savepoint: Arc<SavePoint>, savepoint: Arc<SavePoint>,
items: Vec<helix_lsp::lsp::CompletionItem>, items: Vec<CompletionItem>,
offset_encoding: helix_lsp::OffsetEncoding,
start_offset: usize, start_offset: usize,
trigger_offset: usize, trigger_offset: usize,
size: Rect, size: Rect,
) -> Option<Rect> { ) -> Option<Rect> {
let mut completion = Completion::new( let mut completion =
editor, Completion::new(editor, savepoint, items, start_offset, trigger_offset);
savepoint,
items,
offset_encoding,
start_offset,
trigger_offset,
);
if completion.is_empty() { if completion.is_empty() {
// skip if we got no completion results // skip if we got no completion results
@ -1005,6 +1009,21 @@ impl EditorView {
pub fn clear_completion(&mut self, editor: &mut Editor) { pub fn clear_completion(&mut self, editor: &mut Editor) {
self.completion = None; self.completion = None;
if let Some(last_completion) = editor.last_completion.take() {
match last_completion {
CompleteAction::Applied {
trigger_offset,
changes,
} => self.last_insert.1.push(InsertEvent::CompletionApply {
trigger_offset,
changes,
}),
CompleteAction::Selected { savepoint } => {
let (view, doc) = current!(editor);
doc.restore(view, &savepoint, false);
}
}
}
// Clear any savepoints // Clear any savepoints
editor.clear_idle_timer(); // don't retrigger editor.clear_idle_timer(); // don't retrigger
@ -1291,12 +1310,22 @@ impl Component for EditorView {
jobs: cx.jobs, jobs: cx.jobs,
scroll: None, scroll: None,
}; };
completion.handle_event(event, &mut cx)
};
if let EventResult::Consumed(callback) = res { if let EventResult::Consumed(callback) =
completion.handle_event(event, &mut cx)
{
consumed = true; consumed = true;
Some(callback)
} else if let EventResult::Consumed(callback) =
completion.handle_event(&Event::Key(key!(Enter)), &mut cx)
{
Some(callback)
} else {
None
}
};
if let Some(callback) = res {
if callback.is_some() { if callback.is_some() {
// assume close_fn // assume close_fn
self.clear_completion(cx.editor); self.clear_completion(cx.editor);
@ -1312,10 +1341,6 @@ impl Component for EditorView {
// if completion didn't take the event, we pass it onto commands // if completion didn't take the event, we pass it onto commands
if !consumed { if !consumed {
if let Some(compl) = cx.editor.last_completion.take() {
self.last_insert.1.push(InsertEvent::CompletionApply(compl));
}
self.insert_mode(&mut cx, key); self.insert_mode(&mut cx, key);
// record last_insert key // record last_insert key

@ -51,7 +51,7 @@ pub fn highlighted_code_block<'a>(
language.into(), language.into(),
)) ))
.and_then(|config| config.highlight_config(theme.scopes())) .and_then(|config| config.highlight_config(theme.scopes()))
.map(|config| Syntax::new(&rope, config, Arc::clone(&config_loader))); .and_then(|config| Syntax::new(&rope, config, Arc::clone(&config_loader)));
let syntax = match syntax { let syntax = match syntax {
Some(s) => s, Some(s) => s,

@ -17,11 +17,11 @@ mod text;
use crate::compositor::{Component, Compositor}; use crate::compositor::{Component, Compositor};
use crate::filter_picker_entry; use crate::filter_picker_entry;
use crate::job::{self, Callback}; use crate::job::{self, Callback};
pub use completion::Completion; pub use completion::{Completion, CompletionItem};
pub use editor::EditorView; pub use editor::EditorView;
pub use markdown::Markdown; pub use markdown::Markdown;
pub use menu::Menu; pub use menu::Menu;
pub use picker::{DynamicPicker, FileLocation, FilePicker, Picker}; pub use picker::{DynamicPicker, FileLocation, Picker};
pub use popup::Popup; pub use popup::Popup;
pub use prompt::{Prompt, PromptEvent}; pub use prompt::{Prompt, PromptEvent};
pub use spinner::{ProgressSpinners, Spinner}; pub use spinner::{ProgressSpinners, Spinner};
@ -158,7 +158,7 @@ pub fn regex_prompt(
cx.push_layer(Box::new(prompt)); cx.push_layer(Box::new(prompt));
} }
pub fn file_picker(root: PathBuf, config: &helix_view::editor::Config) -> FilePicker<PathBuf> { pub fn file_picker(root: PathBuf, config: &helix_view::editor::Config) -> Picker<PathBuf> {
use ignore::{types::TypesBuilder, WalkBuilder}; use ignore::{types::TypesBuilder, WalkBuilder};
use std::time::Instant; use std::time::Instant;
@ -217,10 +217,7 @@ pub fn file_picker(root: PathBuf, config: &helix_view::editor::Config) -> FilePi
log::debug!("file_picker init {:?}", Instant::now().duration_since(now)); log::debug!("file_picker init {:?}", Instant::now().duration_since(now));
FilePicker::new( Picker::new(files, root, move |cx, path: &PathBuf, action| {
files,
root,
move |cx, path: &PathBuf, action| {
if let Err(e) = cx.editor.open(path, action) { if let Err(e) = cx.editor.open(path, action) {
let err = if let Some(err) = e.source() { let err = if let Some(err) = e.source() {
format!("{}", err) format!("{}", err)
@ -229,15 +226,15 @@ pub fn file_picker(root: PathBuf, config: &helix_view::editor::Config) -> FilePi
}; };
cx.editor.set_error(err); cx.editor.set_error(err);
} }
}, })
|_editor, path| Some((path.clone().into(), None)), .with_preview(|_editor, path| Some((path.clone().into(), None)))
)
} }
pub mod completers { pub mod completers {
use crate::ui::prompt::Completion; use crate::ui::prompt::Completion;
use fuzzy_matcher::skim::SkimMatcherV2 as Matcher; use fuzzy_matcher::skim::SkimMatcherV2 as Matcher;
use fuzzy_matcher::FuzzyMatcher; use fuzzy_matcher::FuzzyMatcher;
use helix_core::syntax::LanguageServerFeature;
use helix_view::document::SCRATCH_BUFFER_NAME; use helix_view::document::SCRATCH_BUFFER_NAME;
use helix_view::theme; use helix_view::theme;
use helix_view::{editor::Config, Editor}; use helix_view::{editor::Config, Editor};
@ -393,20 +390,11 @@ pub mod completers {
pub fn lsp_workspace_command(editor: &Editor, input: &str) -> Vec<Completion> { pub fn lsp_workspace_command(editor: &Editor, input: &str) -> Vec<Completion> {
let matcher = Matcher::default(); let matcher = Matcher::default();
let (_, doc) = current_ref!(editor); let Some(options) = doc!(editor)
.language_servers_with_feature(LanguageServerFeature::WorkspaceCommand)
let language_server = match doc.language_server() { .find_map(|ls| ls.capabilities().execute_command_provider.as_ref())
Some(language_server) => language_server, else {
None => {
return vec![]; return vec![];
}
};
let options = match &language_server.capabilities().execute_command_provider {
Some(options) => options,
None => {
return vec![];
}
}; };
let mut matches: Vec<_> = options let mut matches: Vec<_> = options

@ -1,7 +1,9 @@
use crate::{ use crate::{
alt, alt,
compositor::{Component, Compositor, Context, Event, EventResult}, compositor::{self, Component, Compositor, Context, Event, EventResult},
ctrl, key, shift, ctrl,
job::Callback,
key, shift,
ui::{ ui::{
self, self,
document::{render_document, LineDecoration, LinePos, TextRenderer}, document::{render_document, LineDecoration, LinePos, TextRenderer},
@ -9,7 +11,7 @@ use crate::{
EditorView, EditorView,
}, },
}; };
use futures_util::future::BoxFuture; use futures_util::{future::BoxFuture, FutureExt};
use tui::{ use tui::{
buffer::Buffer as Surface, buffer::Buffer as Surface,
layout::Constraint, layout::Constraint,
@ -26,7 +28,7 @@ use std::{collections::HashMap, io::Read, path::PathBuf};
use crate::ui::{Prompt, PromptEvent}; use crate::ui::{Prompt, PromptEvent};
use helix_core::{ use helix_core::{
movement::Direction, text_annotations::TextAnnotations, movement::Direction, text_annotations::TextAnnotations,
unicode::segmentation::UnicodeSegmentation, Position, unicode::segmentation::UnicodeSegmentation, Position, Syntax,
}; };
use helix_view::{ use helix_view::{
editor::Action, editor::Action,
@ -75,16 +77,6 @@ type FileCallback<T> = Box<dyn Fn(&Editor, &T) -> Option<FileLocation>>;
/// File path and range of lines (used to align and highlight lines) /// File path and range of lines (used to align and highlight lines)
pub type FileLocation = (PathOrId, Option<(usize, usize)>); pub type FileLocation = (PathOrId, Option<(usize, usize)>);
pub struct FilePicker<T: Item> {
picker: Picker<T>,
pub truncate_start: bool,
/// Caches paths to documents
preview_cache: HashMap<PathBuf, CachedPreview>,
read_buffer: Vec<u8>,
/// Given an item in the picker, return the file path and line number to display.
file_fn: FileCallback<T>,
}
pub enum CachedPreview { pub enum CachedPreview {
Document(Box<Document>), Document(Box<Document>),
Binary, Binary,
@ -122,286 +114,6 @@ impl Preview<'_, '_> {
} }
} }
impl<T: Item> FilePicker<T> {
pub fn new(
options: Vec<T>,
editor_data: T::Data,
callback_fn: impl Fn(&mut Context, &T, Action) + 'static,
preview_fn: impl Fn(&Editor, &T) -> Option<FileLocation> + 'static,
) -> Self {
let truncate_start = true;
let mut picker = Picker::new(options, editor_data, callback_fn);
picker.truncate_start = truncate_start;
Self {
picker,
truncate_start,
preview_cache: HashMap::new(),
read_buffer: Vec::with_capacity(1024),
file_fn: Box::new(preview_fn),
}
}
pub fn truncate_start(mut self, truncate_start: bool) -> Self {
self.truncate_start = truncate_start;
self.picker.truncate_start = truncate_start;
self
}
fn current_file(&self, editor: &Editor) -> Option<FileLocation> {
self.picker
.selection()
.and_then(|current| (self.file_fn)(editor, current))
.and_then(|(path_or_id, line)| path_or_id.get_canonicalized().ok().zip(Some(line)))
}
/// Get (cached) preview for a given path. If a document corresponding
/// to the path is already open in the editor, it is used instead.
fn get_preview<'picker, 'editor>(
&'picker mut self,
path_or_id: PathOrId,
editor: &'editor Editor,
) -> Preview<'picker, 'editor> {
match path_or_id {
PathOrId::Path(path) => {
let path = &path;
if let Some(doc) = editor.document_by_path(path) {
return Preview::EditorDocument(doc);
}
if self.preview_cache.contains_key(path) {
return Preview::Cached(&self.preview_cache[path]);
}
let data = std::fs::File::open(path).and_then(|file| {
let metadata = file.metadata()?;
// Read up to 1kb to detect the content type
let n = file.take(1024).read_to_end(&mut self.read_buffer)?;
let content_type = content_inspector::inspect(&self.read_buffer[..n]);
self.read_buffer.clear();
Ok((metadata, content_type))
});
let preview = data
.map(
|(metadata, content_type)| match (metadata.len(), content_type) {
(_, content_inspector::ContentType::BINARY) => CachedPreview::Binary,
(size, _) if size > MAX_FILE_SIZE_FOR_PREVIEW => {
CachedPreview::LargeFile
}
_ => {
// TODO: enable syntax highlighting; blocked by async rendering
Document::open(path, None, None, editor.config.clone())
.map(|doc| CachedPreview::Document(Box::new(doc)))
.unwrap_or(CachedPreview::NotFound)
}
},
)
.unwrap_or(CachedPreview::NotFound);
self.preview_cache.insert(path.to_owned(), preview);
Preview::Cached(&self.preview_cache[path])
}
PathOrId::Id(id) => {
let doc = editor.documents.get(&id).unwrap();
Preview::EditorDocument(doc)
}
}
}
fn handle_idle_timeout(&mut self, cx: &mut Context) -> EventResult {
// Try to find a document in the cache
let doc = self
.current_file(cx.editor)
.and_then(|(path, _range)| match path {
PathOrId::Id(doc_id) => Some(doc_mut!(cx.editor, &doc_id)),
PathOrId::Path(path) => match self.preview_cache.get_mut(&path) {
Some(CachedPreview::Document(doc)) => Some(doc),
_ => None,
},
});
// Then attempt to highlight it if it has no language set
if let Some(doc) = doc {
if doc.language_config().is_none() {
let loader = cx.editor.syn_loader.clone();
doc.detect_language(loader);
}
// QUESTION: do we want to compute inlay hints in pickers too ? Probably not for now
// but it could be interesting in the future
}
EventResult::Consumed(None)
}
}
impl<T: Item + 'static> Component for FilePicker<T> {
fn render(&mut self, area: Rect, surface: &mut Surface, cx: &mut Context) {
// +---------+ +---------+
// |prompt | |preview |
// +---------+ | |
// |picker | | |
// | | | |
// +---------+ +---------+
let render_preview = self.picker.show_preview && area.width > MIN_AREA_WIDTH_FOR_PREVIEW;
// -- Render the frame:
// clear area
let background = cx.editor.theme.get("ui.background");
let text = cx.editor.theme.get("ui.text");
surface.clear_with(area, background);
let picker_width = if render_preview {
area.width / 2
} else {
area.width
};
let picker_area = area.with_width(picker_width);
self.picker.render(picker_area, surface, cx);
if !render_preview {
return;
}
let preview_area = area.clip_left(picker_width);
// don't like this but the lifetime sucks
let block = Block::default().borders(Borders::ALL);
// calculate the inner area inside the box
let inner = block.inner(preview_area);
// 1 column gap on either side
let margin = Margin::horizontal(1);
let inner = inner.inner(&margin);
block.render(preview_area, surface);
if let Some((path, range)) = self.current_file(cx.editor) {
let preview = self.get_preview(path, cx.editor);
let doc = match preview.document() {
Some(doc) => doc,
None => {
let alt_text = preview.placeholder();
let x = inner.x + inner.width.saturating_sub(alt_text.len() as u16) / 2;
let y = inner.y + inner.height / 2;
surface.set_stringn(x, y, alt_text, inner.width as usize, text);
return;
}
};
// align to middle
let first_line = range
.map(|(start, end)| {
let height = end.saturating_sub(start) + 1;
let middle = start + (height.saturating_sub(1) / 2);
middle.saturating_sub(inner.height as usize / 2).min(start)
})
.unwrap_or(0);
let offset = ViewPosition {
anchor: doc.text().line_to_char(first_line),
horizontal_offset: 0,
vertical_offset: 0,
};
let mut highlights = EditorView::doc_syntax_highlights(
doc,
offset.anchor,
area.height,
&cx.editor.theme,
);
for spans in EditorView::doc_diagnostics_highlights(doc, &cx.editor.theme) {
if spans.is_empty() {
continue;
}
highlights = Box::new(helix_core::syntax::merge(highlights, spans));
}
let mut decorations: Vec<Box<dyn LineDecoration>> = Vec::new();
if let Some((start, end)) = range {
let style = cx
.editor
.theme
.try_get("ui.highlight")
.unwrap_or_else(|| cx.editor.theme.get("ui.selection"));
let draw_highlight = move |renderer: &mut TextRenderer, pos: LinePos| {
if (start..=end).contains(&pos.doc_line) {
let area = Rect::new(
renderer.viewport.x,
renderer.viewport.y + pos.visual_line,
renderer.viewport.width,
1,
);
renderer.surface.set_style(area, style)
}
};
decorations.push(Box::new(draw_highlight))
}
render_document(
surface,
inner,
doc,
offset,
// TODO: compute text annotations asynchronously here (like inlay hints)
&TextAnnotations::default(),
highlights,
&cx.editor.theme,
&mut decorations,
&mut [],
);
}
}
fn handle_event(&mut self, event: &Event, ctx: &mut Context) -> EventResult {
if let Event::IdleTimeout = event {
return self.handle_idle_timeout(ctx);
}
// TODO: keybinds for scrolling preview
self.picker.handle_event(event, ctx)
}
fn cursor(&self, area: Rect, ctx: &Editor) -> (Option<Position>, CursorKind) {
self.picker.cursor(area, ctx)
}
fn required_size(&mut self, (width, height): (u16, u16)) -> Option<(u16, u16)> {
let picker_width = if width > MIN_AREA_WIDTH_FOR_PREVIEW {
width / 2
} else {
width
};
self.picker.required_size((picker_width, height))?;
Some((width, height))
}
}
#[derive(PartialEq, Eq, Debug)]
struct PickerMatch {
score: i64,
index: usize,
len: usize,
}
impl PickerMatch {
fn key(&self) -> impl Ord {
(cmp::Reverse(self.score), self.len, self.index)
}
}
impl PartialOrd for PickerMatch {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Ord for PickerMatch {
fn cmp(&self, other: &Self) -> Ordering {
self.key().cmp(&other.key())
}
}
type PickerCallback<T> = Box<dyn Fn(&mut Context, &T, Action)>;
pub struct Picker<T: Item> { pub struct Picker<T: Item> {
options: Vec<T>, options: Vec<T>,
editor_data: T::Data, editor_data: T::Data,
@ -416,17 +128,22 @@ pub struct Picker<T: Item> {
// pattern: String, // pattern: String,
prompt: Prompt, prompt: Prompt,
previous_pattern: (String, FuzzyQuery), previous_pattern: (String, FuzzyQuery),
/// Whether to truncate the start (default true)
pub truncate_start: bool,
/// Whether to show the preview panel (default true) /// Whether to show the preview panel (default true)
show_preview: bool, show_preview: bool,
/// Constraints for tabular formatting /// Constraints for tabular formatting
widths: Vec<Constraint>, widths: Vec<Constraint>,
callback_fn: PickerCallback<T>, callback_fn: PickerCallback<T>,
pub truncate_start: bool,
/// Caches paths to documents
preview_cache: HashMap<PathBuf, CachedPreview>,
read_buffer: Vec<u8>,
/// Given an item in the picker, return the file path and line number to display.
file_fn: Option<FileCallback<T>>,
} }
impl<T: Item> Picker<T> { impl<T: Item + 'static> Picker<T> {
pub fn new( pub fn new(
options: Vec<T>, options: Vec<T>,
editor_data: T::Data, editor_data: T::Data,
@ -452,6 +169,9 @@ impl<T: Item> Picker<T> {
callback_fn: Box::new(callback_fn), callback_fn: Box::new(callback_fn),
completion_height: 0, completion_height: 0,
widths: Vec::new(), widths: Vec::new(),
preview_cache: HashMap::new(),
read_buffer: Vec::with_capacity(1024),
file_fn: None,
}; };
picker.calculate_column_widths(); picker.calculate_column_widths();
@ -472,6 +192,19 @@ impl<T: Item> Picker<T> {
picker picker
} }
pub fn truncate_start(mut self, truncate_start: bool) -> Self {
self.truncate_start = truncate_start;
self
}
pub fn with_preview(
mut self,
preview_fn: impl Fn(&Editor, &T) -> Option<FileLocation> + 'static,
) -> Self {
self.file_fn = Some(Box::new(preview_fn));
self
}
pub fn set_options(&mut self, new_options: Vec<T>) { pub fn set_options(&mut self, new_options: Vec<T>) {
self.options = new_options; self.options = new_options;
self.cursor = 0; self.cursor = 0;
@ -638,92 +371,127 @@ impl<T: Item> Picker<T> {
} }
EventResult::Consumed(None) EventResult::Consumed(None)
} }
}
// process: fn current_file(&self, editor: &Editor) -> Option<FileLocation> {
// - read all the files into a list, maxed out at a large value self.selection()
// - on input change: .and_then(|current| (self.file_fn.as_ref()?)(editor, current))
// - score all the names in relation to input .and_then(|(path_or_id, line)| path_or_id.get_canonicalized().ok().zip(Some(line)))
impl<T: Item + 'static> Component for Picker<T> {
fn required_size(&mut self, viewport: (u16, u16)) -> Option<(u16, u16)> {
self.completion_height = viewport.1.saturating_sub(4);
Some(viewport)
} }
fn handle_event(&mut self, event: &Event, cx: &mut Context) -> EventResult { /// Get (cached) preview for a given path. If a document corresponding
let key_event = match event { /// to the path is already open in the editor, it is used instead.
Event::Key(event) => *event, fn get_preview<'picker, 'editor>(
Event::Paste(..) => return self.prompt_handle_event(event, cx), &'picker mut self,
Event::Resize(..) => return EventResult::Consumed(None), path_or_id: PathOrId,
_ => return EventResult::Ignored(None), editor: &'editor Editor,
}; ) -> Preview<'picker, 'editor> {
match path_or_id {
let close_fn = EventResult::Consumed(Some(Box::new(|compositor: &mut Compositor, _cx| { PathOrId::Path(path) => {
// remove the layer let path = &path;
compositor.last_picker = compositor.pop(); if let Some(doc) = editor.document_by_path(path) {
}))); return Preview::EditorDocument(doc);
// So that idle timeout retriggers
cx.editor.reset_idle_timer();
match key_event {
shift!(Tab) | key!(Up) | ctrl!('p') => {
self.move_by(1, Direction::Backward);
}
key!(Tab) | key!(Down) | ctrl!('n') => {
self.move_by(1, Direction::Forward);
}
key!(PageDown) | ctrl!('d') => {
self.page_down();
}
key!(PageUp) | ctrl!('u') => {
self.page_up();
}
key!(Home) => {
self.to_start();
}
key!(End) => {
self.to_end();
}
key!(Esc) | ctrl!('c') => {
return close_fn;
}
alt!(Enter) => {
if let Some(option) = self.selection() {
(self.callback_fn)(cx, option, Action::Load);
}
} }
key!(Enter) => {
if let Some(option) = self.selection() { if self.preview_cache.contains_key(path) {
(self.callback_fn)(cx, option, Action::Replace); return Preview::Cached(&self.preview_cache[path]);
} }
return close_fn;
let data = std::fs::File::open(path).and_then(|file| {
let metadata = file.metadata()?;
// Read up to 1kb to detect the content type
let n = file.take(1024).read_to_end(&mut self.read_buffer)?;
let content_type = content_inspector::inspect(&self.read_buffer[..n]);
self.read_buffer.clear();
Ok((metadata, content_type))
});
let preview = data
.map(
|(metadata, content_type)| match (metadata.len(), content_type) {
(_, content_inspector::ContentType::BINARY) => CachedPreview::Binary,
(size, _) if size > MAX_FILE_SIZE_FOR_PREVIEW => {
CachedPreview::LargeFile
} }
ctrl!('s') => { _ => {
if let Some(option) = self.selection() { // TODO: enable syntax highlighting; blocked by async rendering
(self.callback_fn)(cx, option, Action::HorizontalSplit); Document::open(path, None, None, editor.config.clone())
.map(|doc| CachedPreview::Document(Box::new(doc)))
.unwrap_or(CachedPreview::NotFound)
} }
return close_fn; },
)
.unwrap_or(CachedPreview::NotFound);
self.preview_cache.insert(path.to_owned(), preview);
Preview::Cached(&self.preview_cache[path])
} }
ctrl!('v') => { PathOrId::Id(id) => {
if let Some(option) = self.selection() { let doc = editor.documents.get(&id).unwrap();
(self.callback_fn)(cx, option, Action::VerticalSplit); Preview::EditorDocument(doc)
} }
return close_fn;
} }
ctrl!('t') => {
self.toggle_preview();
} }
_ => {
self.prompt_handle_event(event, cx); fn handle_idle_timeout(&mut self, cx: &mut Context) -> EventResult {
let Some((current_file, _)) = self.current_file(cx.editor) else {
return EventResult::Consumed(None)
};
// Try to find a document in the cache
let doc = match &current_file {
PathOrId::Id(doc_id) => doc_mut!(cx.editor, doc_id),
PathOrId::Path(path) => match self.preview_cache.get_mut(path) {
Some(CachedPreview::Document(ref mut doc)) => doc,
_ => return EventResult::Consumed(None),
},
};
let mut callback: Option<compositor::Callback> = None;
// Then attempt to highlight it if it has no language set
if doc.language_config().is_none() {
if let Some(language_config) = doc.detect_language_config(&cx.editor.syn_loader) {
doc.language = Some(language_config.clone());
let text = doc.text().clone();
let loader = cx.editor.syn_loader.clone();
let job = tokio::task::spawn_blocking(move || {
let syntax = language_config
.highlight_config(&loader.scopes())
.and_then(|highlight_config| Syntax::new(&text, highlight_config, loader));
let callback = move |editor: &mut Editor, compositor: &mut Compositor| {
let Some(syntax) = syntax else {
log::info!("highlighting picker item failed");
return
};
let Some(Overlay { content: picker, .. }) = compositor.find::<Overlay<Self>>() else {
log::info!("picker closed before syntax highlighting finished");
return
};
// Try to find a document in the cache
let doc = match current_file {
PathOrId::Id(doc_id) => doc_mut!(editor, &doc_id),
PathOrId::Path(path) => match picker.preview_cache.get_mut(&path) {
Some(CachedPreview::Document(ref mut doc)) => doc,
_ => return,
},
};
doc.syntax = Some(syntax);
};
Callback::EditorCompositor(Box::new(callback))
});
let tmp: compositor::Callback = Box::new(move |_, ctx| {
ctx.jobs
.callback(job.map(|res| res.map_err(anyhow::Error::from)))
});
callback = Some(Box::new(tmp))
} }
} }
EventResult::Consumed(None) // QUESTION: do we want to compute inlay hints in pickers too ? Probably not for now
// but it could be interesting in the future
EventResult::Consumed(callback)
} }
fn render(&mut self, area: Rect, surface: &mut Surface, cx: &mut Context) { fn render_picker(&mut self, area: Rect, surface: &mut Surface, cx: &mut Context) {
let text_style = cx.editor.theme.get("ui.text"); let text_style = cx.editor.theme.get("ui.text");
let selected = cx.editor.theme.get("ui.text.focus"); let selected = cx.editor.theme.get("ui.text.focus");
let highlight_style = cx.editor.theme.get("special").add_modifier(Modifier::BOLD); let highlight_style = cx.editor.theme.get("special").add_modifier(Modifier::BOLD);
@ -889,6 +657,205 @@ impl<T: Item + 'static> Component for Picker<T> {
); );
} }
fn render_preview(&mut self, area: Rect, surface: &mut Surface, cx: &mut Context) {
// -- Render the frame:
// clear area
let background = cx.editor.theme.get("ui.background");
let text = cx.editor.theme.get("ui.text");
surface.clear_with(area, background);
// don't like this but the lifetime sucks
let block = Block::default().borders(Borders::ALL);
// calculate the inner area inside the box
let inner = block.inner(area);
// 1 column gap on either side
let margin = Margin::horizontal(1);
let inner = inner.inner(&margin);
block.render(area, surface);
if let Some((path, range)) = self.current_file(cx.editor) {
let preview = self.get_preview(path, cx.editor);
let doc = match preview.document() {
Some(doc) => doc,
None => {
let alt_text = preview.placeholder();
let x = inner.x + inner.width.saturating_sub(alt_text.len() as u16) / 2;
let y = inner.y + inner.height / 2;
surface.set_stringn(x, y, alt_text, inner.width as usize, text);
return;
}
};
// align to middle
let first_line = range
.map(|(start, end)| {
let height = end.saturating_sub(start) + 1;
let middle = start + (height.saturating_sub(1) / 2);
middle.saturating_sub(inner.height as usize / 2).min(start)
})
.unwrap_or(0);
let offset = ViewPosition {
anchor: doc.text().line_to_char(first_line),
horizontal_offset: 0,
vertical_offset: 0,
};
let mut highlights = EditorView::doc_syntax_highlights(
doc,
offset.anchor,
area.height,
&cx.editor.theme,
);
for spans in EditorView::doc_diagnostics_highlights(doc, &cx.editor.theme) {
if spans.is_empty() {
continue;
}
highlights = Box::new(helix_core::syntax::merge(highlights, spans));
}
let mut decorations: Vec<Box<dyn LineDecoration>> = Vec::new();
if let Some((start, end)) = range {
let style = cx
.editor
.theme
.try_get("ui.highlight")
.unwrap_or_else(|| cx.editor.theme.get("ui.selection"));
let draw_highlight = move |renderer: &mut TextRenderer, pos: LinePos| {
if (start..=end).contains(&pos.doc_line) {
let area = Rect::new(
renderer.viewport.x,
renderer.viewport.y + pos.visual_line,
renderer.viewport.width,
1,
);
renderer.surface.set_style(area, style)
}
};
decorations.push(Box::new(draw_highlight))
}
render_document(
surface,
inner,
doc,
offset,
// TODO: compute text annotations asynchronously here (like inlay hints)
&TextAnnotations::default(),
highlights,
&cx.editor.theme,
&mut decorations,
&mut [],
);
}
}
}
impl<T: Item + 'static> Component for Picker<T> {
fn render(&mut self, area: Rect, surface: &mut Surface, cx: &mut Context) {
// +---------+ +---------+
// |prompt | |preview |
// +---------+ | |
// |picker | | |
// | | | |
// +---------+ +---------+
let render_preview = self.show_preview && area.width > MIN_AREA_WIDTH_FOR_PREVIEW;
let picker_width = if render_preview {
area.width / 2
} else {
area.width
};
let picker_area = area.with_width(picker_width);
self.render_picker(picker_area, surface, cx);
if render_preview {
let preview_area = area.clip_left(picker_width);
self.render_preview(preview_area, surface, cx);
}
}
fn handle_event(&mut self, event: &Event, ctx: &mut Context) -> EventResult {
if let Event::IdleTimeout = event {
return self.handle_idle_timeout(ctx);
}
// TODO: keybinds for scrolling preview
let key_event = match event {
Event::Key(event) => *event,
Event::Paste(..) => return self.prompt_handle_event(event, ctx),
Event::Resize(..) => return EventResult::Consumed(None),
_ => return EventResult::Ignored(None),
};
let close_fn =
EventResult::Consumed(Some(Box::new(|compositor: &mut Compositor, _ctx| {
// remove the layer
compositor.last_picker = compositor.pop();
})));
// So that idle timeout retriggers
ctx.editor.reset_idle_timer();
match key_event {
shift!(Tab) | key!(Up) | ctrl!('p') => {
self.move_by(1, Direction::Backward);
}
key!(Tab) | key!(Down) | ctrl!('n') => {
self.move_by(1, Direction::Forward);
}
key!(PageDown) | ctrl!('d') => {
self.page_down();
}
key!(PageUp) | ctrl!('u') => {
self.page_up();
}
key!(Home) => {
self.to_start();
}
key!(End) => {
self.to_end();
}
key!(Esc) | ctrl!('c') => {
return close_fn;
}
alt!(Enter) => {
if let Some(option) = self.selection() {
(self.callback_fn)(ctx, option, Action::Load);
}
}
key!(Enter) => {
if let Some(option) = self.selection() {
(self.callback_fn)(ctx, option, Action::Replace);
}
return close_fn;
}
ctrl!('s') => {
if let Some(option) = self.selection() {
(self.callback_fn)(ctx, option, Action::HorizontalSplit);
}
return close_fn;
}
ctrl!('v') => {
if let Some(option) = self.selection() {
(self.callback_fn)(ctx, option, Action::VerticalSplit);
}
return close_fn;
}
ctrl!('t') => {
self.toggle_preview();
}
_ => {
self.prompt_handle_event(event, ctx);
}
}
EventResult::Consumed(None)
}
fn cursor(&self, area: Rect, editor: &Editor) -> (Option<Position>, CursorKind) { fn cursor(&self, area: Rect, editor: &Editor) -> (Option<Position>, CursorKind) {
let block = Block::default().borders(Borders::ALL); let block = Block::default().borders(Borders::ALL);
// calculate the inner area inside the box // calculate the inner area inside the box
@ -899,8 +866,40 @@ impl<T: Item + 'static> Component for Picker<T> {
self.prompt.cursor(area, editor) self.prompt.cursor(area, editor)
} }
fn required_size(&mut self, (width, height): (u16, u16)) -> Option<(u16, u16)> {
self.completion_height = height.saturating_sub(4);
Some((width, height))
}
}
#[derive(PartialEq, Eq, Debug)]
struct PickerMatch {
score: i64,
index: usize,
len: usize,
}
impl PickerMatch {
fn key(&self) -> impl Ord {
(cmp::Reverse(self.score), self.len, self.index)
}
}
impl PartialOrd for PickerMatch {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
} }
impl Ord for PickerMatch {
fn cmp(&self, other: &Self) -> Ordering {
self.key().cmp(&other.key())
}
}
type PickerCallback<T> = Box<dyn Fn(&mut Context, &T, Action)>;
/// Returns a new list of options to replace the contents of the picker /// Returns a new list of options to replace the contents of the picker
/// when called with the current picker query, /// when called with the current picker query,
pub type DynQueryCallback<T> = pub type DynQueryCallback<T> =
@ -909,7 +908,7 @@ pub type DynQueryCallback<T> =
/// A picker that updates its contents via a callback whenever the /// A picker that updates its contents via a callback whenever the
/// query string changes. Useful for live grep, workspace symbols, etc. /// query string changes. Useful for live grep, workspace symbols, etc.
pub struct DynamicPicker<T: ui::menu::Item + Send> { pub struct DynamicPicker<T: ui::menu::Item + Send> {
file_picker: FilePicker<T>, file_picker: Picker<T>,
query_callback: DynQueryCallback<T>, query_callback: DynQueryCallback<T>,
query: String, query: String,
} }
@ -917,7 +916,7 @@ pub struct DynamicPicker<T: ui::menu::Item + Send> {
impl<T: ui::menu::Item + Send> DynamicPicker<T> { impl<T: ui::menu::Item + Send> DynamicPicker<T> {
pub const ID: &'static str = "dynamic-picker"; pub const ID: &'static str = "dynamic-picker";
pub fn new(file_picker: FilePicker<T>, query_callback: DynQueryCallback<T>) -> Self { pub fn new(file_picker: Picker<T>, query_callback: DynQueryCallback<T>) -> Self {
Self { Self {
file_picker, file_picker,
query_callback, query_callback,
@ -933,7 +932,7 @@ impl<T: Item + Send + 'static> Component for DynamicPicker<T> {
fn handle_event(&mut self, event: &Event, cx: &mut Context) -> EventResult { fn handle_event(&mut self, event: &Event, cx: &mut Context) -> EventResult {
let event_result = self.file_picker.handle_event(event, cx); let event_result = self.file_picker.handle_event(event, cx);
let current_query = self.file_picker.picker.prompt.line(); let current_query = self.file_picker.prompt.line();
if !matches!(event, Event::IdleTimeout) || self.query == *current_query { if !matches!(event, Event::IdleTimeout) || self.query == *current_query {
return event_result; return event_result;
@ -945,12 +944,11 @@ impl<T: Item + Send + 'static> Component for DynamicPicker<T> {
cx.jobs.callback(async move { cx.jobs.callback(async move {
let new_options = new_options.await?; let new_options = new_options.await?;
let callback = let callback = Callback::EditorCompositor(Box::new(move |editor, compositor| {
crate::job::Callback::EditorCompositor(Box::new(move |editor, compositor| {
// Wrapping of pickers in overlay is done outside the picker code, // Wrapping of pickers in overlay is done outside the picker code,
// so this is fragile and will break if wrapped in some other widget. // so this is fragile and will break if wrapped in some other widget.
let picker = match compositor.find_id::<Overlay<DynamicPicker<T>>>(Self::ID) { let picker = match compositor.find_id::<Overlay<DynamicPicker<T>>>(Self::ID) {
Some(overlay) => &mut overlay.content.file_picker.picker, Some(overlay) => &mut overlay.content.file_picker,
None => return, None => return,
}; };
picker.set_options(new_options); picker.set_options(new_options);

@ -164,6 +164,7 @@ where
helix_view::editor::StatusLineElement::Spacer => render_spacer, helix_view::editor::StatusLineElement::Spacer => render_spacer,
helix_view::editor::StatusLineElement::VersionControl => render_version_control, helix_view::editor::StatusLineElement::VersionControl => render_version_control,
helix_view::editor::StatusLineElement::Custom => render_custom_text, helix_view::editor::StatusLineElement::Custom => render_custom_text,
helix_view::editor::StatusLineElement::Register => render_register,
} }
} }
@ -201,15 +202,15 @@ where
); );
} }
// TODO think about handling multiple language servers
fn render_lsp_spinner<F>(context: &mut RenderContext, write: F) fn render_lsp_spinner<F>(context: &mut RenderContext, write: F)
where where
F: Fn(&mut RenderContext, String, Option<Style>) + Copy, F: Fn(&mut RenderContext, String, Option<Style>) + Copy,
{ {
let language_server = context.doc.language_servers().next();
write( write(
context, context,
context language_server
.doc
.language_server()
.and_then(|srv| { .and_then(|srv| {
context context
.spinners .spinners
@ -229,8 +230,7 @@ where
{ {
let (warnings, errors) = context let (warnings, errors) = context
.doc .doc
.diagnostics() .shown_diagnostics()
.iter()
.fold((0, 0), |mut counts, diag| { .fold((0, 0), |mut counts, diag| {
use helix_core::diagnostic::Severity; use helix_core::diagnostic::Severity;
match diag.severity { match diag.severity {
@ -270,7 +270,7 @@ where
.diagnostics .diagnostics
.values() .values()
.flatten() .flatten()
.fold((0, 0), |mut counts, diag| { .fold((0, 0), |mut counts, (diag, _)| {
match diag.severity { match diag.severity {
Some(DiagnosticSeverity::WARNING) => counts.0 += 1, Some(DiagnosticSeverity::WARNING) => counts.0 += 1,
Some(DiagnosticSeverity::ERROR) | None => counts.1 += 1, Some(DiagnosticSeverity::ERROR) | None => counts.1 += 1,
@ -280,7 +280,7 @@ where
}); });
if warnings > 0 || errors > 0 { if warnings > 0 || errors > 0 {
write(context, format!(" {} ", "W"), None); write(context, " W ".into(), None);
} }
if warnings > 0 { if warnings > 0 {
@ -503,3 +503,12 @@ where
write(context, message, None); write(context, message, None);
} }
} }
fn render_register<F>(context: &mut RenderContext, write: F)
where
F: Fn(&mut RenderContext, String, Option<Style>) + Copy,
{
if let Some(reg) = context.editor.selected_register {
write(context, format!(" reg={} ", reg), None)
}
}

@ -2,7 +2,7 @@ use helix_core::{auto_pairs::DEFAULT_PAIRS, hashmap};
use super::*; use super::*;
const LINE_END: &str = helix_core::DEFAULT_LINE_ENDING.as_str(); const LINE_END: &str = helix_core::NATIVE_LINE_ENDING.as_str();
fn differing_pairs() -> impl Iterator<Item = &'static (char, char)> { fn differing_pairs() -> impl Iterator<Item = &'static (char, char)> {
DEFAULT_PAIRS.iter().filter(|(open, close)| open != close) DEFAULT_PAIRS.iter().filter(|(open, close)| open != close)

@ -12,15 +12,13 @@ async fn test_selection_duplication() -> anyhow::Result<()> {
#[lo|]#rem #[lo|]#rem
ipsum ipsum
dolor dolor
"}) "}),
.as_str(),
"CC", "CC",
platform_line(indoc! {"\ platform_line(indoc! {"\
#(lo|)#rem #(lo|)#rem
#(ip|)#sum #(ip|)#sum
#[do|]#lor #[do|]#lor
"}) "}),
.as_str(),
)) ))
.await?; .await?;
@ -30,15 +28,13 @@ async fn test_selection_duplication() -> anyhow::Result<()> {
#[|lo]#rem #[|lo]#rem
ipsum ipsum
dolor dolor
"}) "}),
.as_str(),
"CC", "CC",
platform_line(indoc! {"\ platform_line(indoc! {"\
#(|lo)#rem #(|lo)#rem
#(|ip)#sum #(|ip)#sum
#[|do]#lor #[|do]#lor
"}) "}),
.as_str(),
)) ))
.await?; .await?;
@ -47,14 +43,12 @@ async fn test_selection_duplication() -> anyhow::Result<()> {
platform_line(indoc! {"\ platform_line(indoc! {"\
test test
#[testitem|]# #[testitem|]#
"}) "}),
.as_str(),
"<A-C>", "<A-C>",
platform_line(indoc! {"\ platform_line(indoc! {"\
test test
#[testitem|]# #[testitem|]#
"}) "}),
.as_str(),
)) ))
.await?; .await?;
@ -63,14 +57,12 @@ async fn test_selection_duplication() -> anyhow::Result<()> {
platform_line(indoc! {"\ platform_line(indoc! {"\
test test
#[test|]# #[test|]#
"}) "}),
.as_str(),
"<A-C>", "<A-C>",
platform_line(indoc! {"\ platform_line(indoc! {"\
#[test|]# #[test|]#
#(test|)# #(test|)#
"}) "}),
.as_str(),
)) ))
.await?; .await?;
@ -79,14 +71,12 @@ async fn test_selection_duplication() -> anyhow::Result<()> {
platform_line(indoc! {"\ platform_line(indoc! {"\
#[testitem|]# #[testitem|]#
test test
"}) "}),
.as_str(),
"C", "C",
platform_line(indoc! {"\ platform_line(indoc! {"\
#[testitem|]# #[testitem|]#
test test
"}) "}),
.as_str(),
)) ))
.await?; .await?;
@ -95,14 +85,12 @@ async fn test_selection_duplication() -> anyhow::Result<()> {
platform_line(indoc! {"\ platform_line(indoc! {"\
#[test|]# #[test|]#
test test
"}) "}),
.as_str(),
"C", "C",
platform_line(indoc! {"\ platform_line(indoc! {"\
#(test|)# #(test|)#
#[test|]# #[test|]#
"}) "}),
.as_str(),
)) ))
.await?; .await?;
Ok(()) Ok(())
@ -174,15 +162,13 @@ async fn test_multi_selection_paste() -> anyhow::Result<()> {
#[|lorem]# #[|lorem]#
#(|ipsum)# #(|ipsum)#
#(|dolor)# #(|dolor)#
"}) "}),
.as_str(),
"yp", "yp",
platform_line(indoc! {"\ platform_line(indoc! {"\
lorem#[|lorem]# lorem#[|lorem]#
ipsum#(|ipsum)# ipsum#(|ipsum)#
dolor#(|dolor)# dolor#(|dolor)#
"}) "}),
.as_str(),
)) ))
.await?; .await?;
@ -197,8 +183,7 @@ async fn test_multi_selection_shell_commands() -> anyhow::Result<()> {
#[|lorem]# #[|lorem]#
#(|ipsum)# #(|ipsum)#
#(|dolor)# #(|dolor)#
"}) "}),
.as_str(),
"|echo foo<ret>", "|echo foo<ret>",
platform_line(indoc! {"\ platform_line(indoc! {"\
#[|foo\n]# #[|foo\n]#
@ -207,8 +192,7 @@ async fn test_multi_selection_shell_commands() -> anyhow::Result<()> {
#(|foo\n)# #(|foo\n)#
"}) "}),
.as_str(),
)) ))
.await?; .await?;
@ -218,8 +202,7 @@ async fn test_multi_selection_shell_commands() -> anyhow::Result<()> {
#[|lorem]# #[|lorem]#
#(|ipsum)# #(|ipsum)#
#(|dolor)# #(|dolor)#
"}) "}),
.as_str(),
"!echo foo<ret>", "!echo foo<ret>",
platform_line(indoc! {"\ platform_line(indoc! {"\
#[|foo\n]# #[|foo\n]#
@ -228,8 +211,7 @@ async fn test_multi_selection_shell_commands() -> anyhow::Result<()> {
ipsum ipsum
#(|foo\n)# #(|foo\n)#
dolor dolor
"}) "}),
.as_str(),
)) ))
.await?; .await?;
@ -239,8 +221,7 @@ async fn test_multi_selection_shell_commands() -> anyhow::Result<()> {
#[|lorem]# #[|lorem]#
#(|ipsum)# #(|ipsum)#
#(|dolor)# #(|dolor)#
"}) "}),
.as_str(),
"<A-!>echo foo<ret>", "<A-!>echo foo<ret>",
platform_line(indoc! {"\ platform_line(indoc! {"\
lorem#[|foo\n]# lorem#[|foo\n]#
@ -249,8 +230,7 @@ async fn test_multi_selection_shell_commands() -> anyhow::Result<()> {
dolor#(|foo\n)# dolor#(|foo\n)#
"}) "}),
.as_str(),
)) ))
.await?; .await?;
@ -294,16 +274,14 @@ async fn test_extend_line() -> anyhow::Result<()> {
ipsum ipsum
dolor dolor
"}) "}),
.as_str(),
"x2x", "x2x",
platform_line(indoc! {"\ platform_line(indoc! {"\
#[lorem #[lorem
ipsum ipsum
dolor\n|]# dolor\n|]#
"}) "}),
.as_str(),
)) ))
.await?; .await?;
@ -313,15 +291,13 @@ async fn test_extend_line() -> anyhow::Result<()> {
#[l|]#orem #[l|]#orem
ipsum ipsum
"}) "}),
.as_str(),
"2x", "2x",
platform_line(indoc! {"\ platform_line(indoc! {"\
#[lorem #[lorem
ipsum\n|]# ipsum\n|]#
"}) "}),
.as_str(),
)) ))
.await?; .await?;
@ -385,3 +361,121 @@ async fn test_character_info() -> anyhow::Result<()> {
Ok(()) Ok(())
} }
#[tokio::test(flavor = "multi_thread")]
async fn test_delete_char_backward() -> anyhow::Result<()> {
// don't panic when deleting overlapping ranges
test((
platform_line("#(x|)# #[x|]#"),
"c<space><backspace><esc>",
platform_line("#[\n|]#"),
))
.await?;
test((
platform_line("#( |)##( |)#a#( |)#axx#[x|]#a"),
"li<backspace><esc>",
platform_line("#(a|)##(|a)#xx#[|a]#"),
))
.await?;
Ok(())
}
#[tokio::test(flavor = "multi_thread")]
async fn test_delete_word_backward() -> anyhow::Result<()> {
// don't panic when deleting overlapping ranges
test((
platform_line("fo#[o|]#ba#(r|)#"),
"a<C-w><esc>",
platform_line("#[\n|]#"),
))
.await?;
Ok(())
}
#[tokio::test(flavor = "multi_thread")]
async fn test_delete_word_forward() -> anyhow::Result<()> {
// don't panic when deleting overlapping ranges
test((
platform_line("fo#[o|]#b#(|ar)#"),
"i<A-d><esc>",
platform_line("fo#[\n|]#"),
))
.await?;
Ok(())
}
#[tokio::test(flavor = "multi_thread")]
async fn test_delete_char_forward() -> anyhow::Result<()> {
test((
platform_line(indoc! {"\
#[abc|]#def
#(abc|)#ef
#(abc|)#f
#(abc|)#
"}),
"a<del><esc>",
platform_line(indoc! {"\
#[abc|]#ef
#(abc|)#f
#(abc|)#
#(abc|)#
"}),
))
.await?;
Ok(())
}
#[tokio::test(flavor = "multi_thread")]
async fn test_insert_with_indent() -> anyhow::Result<()> {
const INPUT: &str = "\
#[f|]#n foo() {
if let Some(_) = None {
}
\x20
}
fn bar() {
}";
// insert_at_line_start
test((
INPUT,
":lang rust<ret>%<A-s>I",
"\
#[f|]#n foo() {
#(i|)#f let Some(_) = None {
#(\n|)#\
\x20 #(}|)#
#(\x20|)#
#(}|)#
#(\n|)#\
#(f|)#n bar() {
#(\n|)#\
#(}|)#",
))
.await?;
// insert_at_line_end
test((
INPUT,
":lang rust<ret>%<A-s>A",
"\
fn foo() {#[\n|]#\
\x20 if let Some(_) = None {#(\n|)#\
\x20 #(\n|)#\
\x20 }#(\n|)#\
\x20#(\n|)#\
}#(\n|)#\
#(\n|)#\
fn bar() {#(\n|)#\
\x20 #(\n|)#\
}#(|)#",
))
.await?;
Ok(())
}

@ -407,3 +407,41 @@ async fn test_write_fail_new_path() -> anyhow::Result<()> {
Ok(()) Ok(())
} }
#[tokio::test(flavor = "multi_thread")]
async fn test_write_utf_bom_file() -> anyhow::Result<()> {
// "ABC" with utf8 bom
const UTF8_FILE: [u8; 6] = [0xef, 0xbb, 0xbf, b'A', b'B', b'C'];
// "ABC" in UTF16 with bom
const UTF16LE_FILE: [u8; 8] = [0xff, 0xfe, b'A', 0x00, b'B', 0x00, b'C', 0x00];
const UTF16BE_FILE: [u8; 8] = [0xfe, 0xff, 0x00, b'A', 0x00, b'B', 0x00, b'C'];
edit_file_with_content(&UTF8_FILE).await?;
edit_file_with_content(&UTF16LE_FILE).await?;
edit_file_with_content(&UTF16BE_FILE).await?;
Ok(())
}
async fn edit_file_with_content(file_content: &[u8]) -> anyhow::Result<()> {
let mut file = tempfile::NamedTempFile::new()?;
file.as_file_mut().write_all(&file_content)?;
helpers::test_key_sequence(
&mut helpers::AppBuilder::new().build()?,
Some(&format!(":o {}<ret>:x<ret>", file.path().to_string_lossy())),
None,
true,
)
.await?;
file.rewind()?;
let mut new_file_content: Vec<u8> = Vec::new();
file.read_to_end(&mut new_file_content)?;
assert_eq!(file_content, new_file_content);
Ok(())
}

@ -244,7 +244,7 @@ pub fn test_editor_config() -> helix_view::editor::Config {
/// character, and if one doesn't exist already, appends the system's /// character, and if one doesn't exist already, appends the system's
/// appropriate line ending to the end of a string. /// appropriate line ending to the end of a string.
pub fn platform_line(input: &str) -> String { pub fn platform_line(input: &str) -> String {
let line_end = helix_core::DEFAULT_LINE_ENDING.as_str(); let line_end = helix_core::NATIVE_LINE_ENDING.as_str();
// we can assume that the source files in this code base will always // we can assume that the source files in this code base will always
// be LF, so indoc strings will always insert LF // be LF, so indoc strings will always insert LF

@ -16,13 +16,13 @@ include = ["src/**/*", "README.md"]
default = ["crossterm"] default = ["crossterm"]
[dependencies] [dependencies]
bitflags = "2.2" bitflags = "2.3"
cassowary = "0.3" cassowary = "0.3"
unicode-segmentation = "1.10" unicode-segmentation = "1.10"
crossterm = { version = "0.26", optional = true } crossterm = { version = "0.26", optional = true }
termini = "0.1" termini = "1.0"
serde = { version = "1", "optional" = true, features = ["derive"]} serde = { version = "1", "optional" = true, features = ["derive"]}
once_cell = "1.17" once_cell = "1.18"
log = "~0.4" log = "~0.4"
helix-view = { version = "0.6", path = "../helix-view", features = ["term"] } helix-view = { version = "0.6", path = "../helix-view", features = ["term"] }
helix-core = { version = "0.6", path = "../helix-core" } helix-core = { version = "0.6", path = "../helix-core" }

@ -442,7 +442,7 @@ impl Buffer {
let mut x_offset = x as usize; let mut x_offset = x as usize;
let max_offset = min(self.area.right(), width.saturating_add(x)); let max_offset = min(self.area.right(), width.saturating_add(x));
let mut start_index = self.index_of(x, y); let mut start_index = self.index_of(x, y);
let mut index = self.index_of(max_offset as u16, y); let mut index = self.index_of(max_offset, y);
let content_width = spans.width(); let content_width = spans.width();
let truncated = content_width > width as usize; let truncated = content_width > width as usize;

@ -450,11 +450,11 @@ impl<'a> Table<'a> {
} else { } else {
col col
}; };
let mut col = table_row_start_col;
for (width, cell) in columns_widths.iter().zip(table_row.cells.iter()) {
if is_selected { if is_selected {
buf.set_style(table_row_area, self.highlight_style); buf.set_style(table_row_area, self.highlight_style);
} }
let mut col = table_row_start_col;
for (width, cell) in columns_widths.iter().zip(table_row.cells.iter()) {
render_cell( render_cell(
buf, buf,
cell, cell,

@ -17,7 +17,7 @@ tokio = { version = "1", features = ["rt", "rt-multi-thread", "time", "sync", "p
parking_lot = "0.12" parking_lot = "0.12"
arc-swap = { version = "1.6.0" } arc-swap = { version = "1.6.0" }
gix = { version = "0.44.0", default-features = false , optional = true } gix = { version = "0.47.0", default-features = false , optional = true }
imara-diff = "0.1.5" imara-diff = "0.1.5"
anyhow = "1" anyhow = "1"
@ -27,4 +27,4 @@ log = "0.4"
git = ["gix"] git = ["gix"]
[dev-dependencies] [dev-dependencies]
tempfile = "3.4" tempfile = "3.6"

@ -20,8 +20,8 @@ use super::{MAX_DIFF_BYTES, MAX_DIFF_LINES};
/// A cache that stores the `lines` of a rope as a vector. /// A cache that stores the `lines` of a rope as a vector.
/// It allows safely reusing the allocation of the vec when updating the rope /// It allows safely reusing the allocation of the vec when updating the rope
pub(crate) struct InternedRopeLines { pub(crate) struct InternedRopeLines {
diff_base: Rope, diff_base: Box<Rope>,
doc: Rope, doc: Box<Rope>,
num_tokens_diff_base: u32, num_tokens_diff_base: u32,
interned: InternedInput<RopeSlice<'static>>, interned: InternedInput<RopeSlice<'static>>,
} }
@ -34,8 +34,8 @@ impl InternedRopeLines {
after: Vec::with_capacity(doc.len_lines()), after: Vec::with_capacity(doc.len_lines()),
interner: Interner::new(diff_base.len_lines() + doc.len_lines()), interner: Interner::new(diff_base.len_lines() + doc.len_lines()),
}, },
diff_base, diff_base: Box::new(diff_base),
doc, doc: Box::new(doc),
// will be populated by update_diff_base_impl // will be populated by update_diff_base_impl
num_tokens_diff_base: 0, num_tokens_diff_base: 0,
}; };
@ -44,19 +44,19 @@ impl InternedRopeLines {
} }
pub fn doc(&self) -> Rope { pub fn doc(&self) -> Rope {
self.doc.clone() Rope::clone(&*self.doc)
} }
pub fn diff_base(&self) -> Rope { pub fn diff_base(&self) -> Rope {
self.diff_base.clone() Rope::clone(&*self.diff_base)
} }
/// Updates the `diff_base` and optionally the document if `doc` is not None /// Updates the `diff_base` and optionally the document if `doc` is not None
pub fn update_diff_base(&mut self, diff_base: Rope, doc: Option<Rope>) { pub fn update_diff_base(&mut self, diff_base: Rope, doc: Option<Rope>) {
self.interned.clear(); self.interned.clear();
self.diff_base = diff_base; self.diff_base = Box::new(diff_base);
if let Some(doc) = doc { if let Some(doc) = doc {
self.doc = doc self.doc = Box::new(doc)
} }
if !self.is_too_large() { if !self.is_too_large() {
self.update_diff_base_impl(); self.update_diff_base_impl();
@ -74,7 +74,7 @@ impl InternedRopeLines {
.interner .interner
.erase_tokens_after(self.num_tokens_diff_base.into()); .erase_tokens_after(self.num_tokens_diff_base.into());
self.doc = doc; self.doc = Box::new(doc);
if self.is_too_large() { if self.is_too_large() {
self.interned.after.clear(); self.interned.after.clear();
} else { } else {

@ -46,8 +46,8 @@ impl DiffProviderRegistry {
.find_map(|provider| match provider.get_diff_base(file) { .find_map(|provider| match provider.get_diff_base(file) {
Ok(res) => Some(res), Ok(res) => Some(res),
Err(err) => { Err(err) => {
log::error!("{err:#?}"); log::info!("{err:#?}");
log::error!("failed to open diff base for {}", file.display()); log::info!("failed to open diff base for {}", file.display());
None None
} }
}) })
@ -59,8 +59,8 @@ impl DiffProviderRegistry {
.find_map(|provider| match provider.get_current_head_name(file) { .find_map(|provider| match provider.get_current_head_name(file) {
Ok(res) => Some(res), Ok(res) => Some(res),
Err(err) => { Err(err) => {
log::error!("{err:#?}"); log::info!("{err:#?}");
log::error!("failed to obtain current head name for {}", file.display()); log::info!("failed to obtain current head name for {}", file.display());
None None
} }
}) })

@ -14,7 +14,7 @@ default = []
term = ["crossterm"] term = ["crossterm"]
[dependencies] [dependencies]
bitflags = "2.2" bitflags = "2.3"
anyhow = "1" anyhow = "1"
helix-core = { version = "0.6", path = "../helix-core" } helix-core = { version = "0.6", path = "../helix-core" }
helix-loader = { version = "0.6", path = "../helix-loader" } helix-loader = { version = "0.6", path = "../helix-loader" }
@ -24,7 +24,7 @@ crossterm = { version = "0.26", optional = true }
helix-vcs = { version = "0.6", path = "../helix-vcs" } helix-vcs = { version = "0.6", path = "../helix-vcs" }
# Conversion traits # Conversion traits
once_cell = "1.17" once_cell = "1.18"
url = "2" url = "2"
arc-swap = { version = "1.6.0" } arc-swap = { version = "1.6.0" }

@ -68,7 +68,7 @@ macro_rules! command_provider {
#[cfg(windows)] #[cfg(windows)]
pub fn get_clipboard_provider() -> Box<dyn ClipboardProvider> { pub fn get_clipboard_provider() -> Box<dyn ClipboardProvider> {
Box::new(provider::WindowsProvider::default()) Box::<provider::WindowsProvider>::default()
} }
#[cfg(target_os = "macos")] #[cfg(target_os = "macos")]

@ -5,9 +5,9 @@ use futures_util::future::BoxFuture;
use futures_util::FutureExt; use futures_util::FutureExt;
use helix_core::auto_pairs::AutoPairs; use helix_core::auto_pairs::AutoPairs;
use helix_core::doc_formatter::TextFormat; use helix_core::doc_formatter::TextFormat;
use helix_core::syntax::Highlight; use helix_core::encoding::Encoding;
use helix_core::syntax::{Highlight, LanguageServerFeature};
use helix_core::text_annotations::{InlineAnnotation, TextAnnotations}; use helix_core::text_annotations::{InlineAnnotation, TextAnnotations};
use helix_core::Range;
use helix_vcs::{DiffHandle, DiffProviderRegistry}; use helix_vcs::{DiffHandle, DiffProviderRegistry};
use ::parking_lot::Mutex; use ::parking_lot::Mutex;
@ -30,8 +30,7 @@ use helix_core::{
indent::{auto_detect_indent_style, IndentStyle}, indent::{auto_detect_indent_style, IndentStyle},
line_ending::auto_detect_line_ending, line_ending::auto_detect_line_ending,
syntax::{self, LanguageConfiguration}, syntax::{self, LanguageConfiguration},
ChangeSet, Diagnostic, LineEnding, Rope, RopeBuilder, Selection, Syntax, Transaction, ChangeSet, Diagnostic, LineEnding, Range, Rope, RopeBuilder, Selection, Syntax, Transaction,
DEFAULT_LINE_ENDING,
}; };
use crate::editor::{Config, RedrawHandle}; use crate::editor::{Config, RedrawHandle};
@ -113,6 +112,19 @@ pub struct SavePoint {
/// The view this savepoint is associated with /// The view this savepoint is associated with
pub view: ViewId, pub view: ViewId,
revert: Mutex<Transaction>, revert: Mutex<Transaction>,
pub text: Rope,
}
impl SavePoint {
pub fn cursor(&self) -> usize {
// we always create transactions with selections
self.revert
.lock()
.selection()
.unwrap()
.primary()
.cursor(self.text.slice(..))
}
} }
pub struct Document { pub struct Document {
@ -130,6 +142,7 @@ pub struct Document {
path: Option<PathBuf>, path: Option<PathBuf>,
encoding: &'static encoding::Encoding, encoding: &'static encoding::Encoding,
has_bom: bool,
pub restore_cursor: bool, pub restore_cursor: bool,
@ -139,9 +152,9 @@ pub struct Document {
/// The document's default line ending. /// The document's default line ending.
pub line_ending: LineEnding, pub line_ending: LineEnding,
syntax: Option<Syntax>, pub syntax: Option<Syntax>,
/// Corresponding language scope name. Usually `source.<lang>`. /// Corresponding language scope name. Usually `source.<lang>`.
pub(crate) language: Option<Arc<LanguageConfiguration>>, pub language: Option<Arc<LanguageConfiguration>>,
/// Pending changes since last history commit. /// Pending changes since last history commit.
changes: ChangeSet, changes: ChangeSet,
@ -164,8 +177,8 @@ pub struct Document {
version: i32, // should be usize? version: i32, // should be usize?
pub(crate) modified_since_accessed: bool, pub(crate) modified_since_accessed: bool,
diagnostics: Vec<Diagnostic>, pub(crate) diagnostics: Vec<Diagnostic>,
language_server: Option<Arc<helix_lsp::Client>>, pub(crate) language_servers: HashMap<LanguageServerName, Arc<Client>>,
diff_handle: Option<DiffHandle>, diff_handle: Option<DiffHandle>,
version_control_head: Option<Arc<ArcSwap<Box<str>>>>, version_control_head: Option<Arc<ArcSwap<Box<str>>>>,
@ -277,16 +290,104 @@ impl fmt::Debug for DocumentInlayHintsId {
} }
} }
enum Encoder {
Utf16Be,
Utf16Le,
EncodingRs(encoding::Encoder),
}
impl Encoder {
fn from_encoding(encoding: &'static encoding::Encoding) -> Self {
if encoding == encoding::UTF_16BE {
Self::Utf16Be
} else if encoding == encoding::UTF_16LE {
Self::Utf16Le
} else {
Self::EncodingRs(encoding.new_encoder())
}
}
fn encode_from_utf8(
&mut self,
src: &str,
dst: &mut [u8],
is_empty: bool,
) -> (encoding::CoderResult, usize, usize) {
if src.is_empty() {
return (encoding::CoderResult::InputEmpty, 0, 0);
}
let mut write_to_buf = |convert: fn(u16) -> [u8; 2]| {
let to_write = src.char_indices().map(|(indice, char)| {
let mut encoded: [u16; 2] = [0, 0];
(
indice,
char.encode_utf16(&mut encoded)
.iter_mut()
.flat_map(|char| convert(*char))
.collect::<Vec<u8>>(),
)
});
let mut total_written = 0usize;
for (indice, utf16_bytes) in to_write {
let character_size = utf16_bytes.len();
if dst.len() <= (total_written + character_size) {
return (encoding::CoderResult::OutputFull, indice, total_written);
}
for character in utf16_bytes {
dst[total_written] = character;
total_written += 1;
}
}
(encoding::CoderResult::InputEmpty, src.len(), total_written)
};
match self {
Self::Utf16Be => write_to_buf(u16::to_be_bytes),
Self::Utf16Le => write_to_buf(u16::to_le_bytes),
Self::EncodingRs(encoder) => {
let (code_result, read, written, ..) = encoder.encode_from_utf8(src, dst, is_empty);
(code_result, read, written)
}
}
}
}
// Apply BOM if encoding permit it, return the number of bytes written at the start of buf
fn apply_bom(encoding: &'static encoding::Encoding, buf: &mut [u8; BUF_SIZE]) -> usize {
if encoding == encoding::UTF_8 {
buf[0] = 0xef;
buf[1] = 0xbb;
buf[2] = 0xbf;
3
} else if encoding == encoding::UTF_16BE {
buf[0] = 0xfe;
buf[1] = 0xff;
2
} else if encoding == encoding::UTF_16LE {
buf[0] = 0xff;
buf[1] = 0xfe;
2
} else {
0
}
}
// The documentation and implementation of this function should be up-to-date with // The documentation and implementation of this function should be up-to-date with
// its sibling function, `to_writer()`. // its sibling function, `to_writer()`.
// //
/// Decodes a stream of bytes into UTF-8, returning a `Rope` and the /// Decodes a stream of bytes into UTF-8, returning a `Rope` and the
/// encoding it was decoded as. The optional `encoding` parameter can /// encoding it was decoded as with BOM information. The optional `encoding`
/// be used to override encoding auto-detection. /// parameter can be used to override encoding auto-detection.
pub fn from_reader<R: std::io::Read + ?Sized>( pub fn from_reader<R: std::io::Read + ?Sized>(
reader: &mut R, reader: &mut R,
encoding: Option<&'static encoding::Encoding>, encoding: Option<&'static Encoding>,
) -> Result<(Rope, &'static encoding::Encoding), Error> { ) -> Result<(Rope, &'static Encoding, bool), Error> {
// These two buffers are 8192 bytes in size each and are used as // These two buffers are 8192 bytes in size each and are used as
// intermediaries during the decoding process. Text read into `buf` // intermediaries during the decoding process. Text read into `buf`
// from `reader` is decoded into `buf_out` as UTF-8. Once either // from `reader` is decoded into `buf_out` as UTF-8. Once either
@ -296,26 +397,11 @@ pub fn from_reader<R: std::io::Read + ?Sized>(
let mut buf_out = [0u8; BUF_SIZE]; let mut buf_out = [0u8; BUF_SIZE];
let mut builder = RopeBuilder::new(); let mut builder = RopeBuilder::new();
// By default, the encoding of the text is auto-detected via the let (encoding, has_bom, mut decoder, read) =
// `chardetng` crate which requires sample data from the reader. read_and_detect_encoding(reader, encoding, &mut buf)?;
// As a manual override to this auto-detection is possible, the
// same data is read into `buf` to ensure symmetry in the upcoming
// loop.
let (encoding, mut decoder, mut slice, mut is_empty) = {
let read = reader.read(&mut buf)?;
let is_empty = read == 0;
let encoding = encoding.unwrap_or_else(|| {
let mut encoding_detector = chardetng::EncodingDetector::new();
encoding_detector.feed(&buf, is_empty);
encoding_detector.guess(None, true)
});
let decoder = encoding.new_decoder();
// If the amount of bytes read from the reader is less than let mut slice = &buf[..read];
// `buf.len()`, it is undesirable to read the bytes afterwards. let mut is_empty = read == 0;
let slice = &buf[..read];
(encoding, decoder, slice, is_empty)
};
// `RopeBuilder::append()` expects a `&str`, so this is the "real" // `RopeBuilder::append()` expects a `&str`, so this is the "real"
// output buffer. When decoding, the number of bytes in the output // output buffer. When decoding, the number of bytes in the output
@ -382,7 +468,82 @@ pub fn from_reader<R: std::io::Read + ?Sized>(
is_empty = read == 0; is_empty = read == 0;
} }
let rope = builder.finish(); let rope = builder.finish();
Ok((rope, encoding)) Ok((rope, encoding, has_bom))
}
pub fn read_to_string<R: std::io::Read + ?Sized>(
reader: &mut R,
encoding: Option<&'static Encoding>,
) -> Result<(String, &'static Encoding, bool), Error> {
let mut buf = [0u8; BUF_SIZE];
let (encoding, has_bom, mut decoder, read) =
read_and_detect_encoding(reader, encoding, &mut buf)?;
let mut slice = &buf[..read];
let mut is_empty = read == 0;
let mut buf_string = String::with_capacity(buf.len());
loop {
let mut total_read = 0usize;
loop {
let (result, read, ..) =
decoder.decode_to_string(&slice[total_read..], &mut buf_string, is_empty);
total_read += read;
match result {
encoding::CoderResult::InputEmpty => {
debug_assert_eq!(slice.len(), total_read);
break;
}
encoding::CoderResult::OutputFull => {
debug_assert!(slice.len() > total_read);
buf_string.reserve(buf.len())
}
}
}
if is_empty {
debug_assert_eq!(reader.read(&mut buf)?, 0);
break;
}
let read = reader.read(&mut buf)?;
slice = &buf[..read];
is_empty = read == 0;
}
Ok((buf_string, encoding, has_bom))
}
/// Reads the first chunk from a Reader into the given buffer
/// and detects the encoding.
///
/// By default, the encoding of the text is auto-detected by
/// `encoding_rs` for_bom, and if it fails, from `chardetng`
/// crate which requires sample data from the reader.
/// As a manual override to this auto-detection is possible, the
/// same data is read into `buf` to ensure symmetry in the upcoming
/// loop.
fn read_and_detect_encoding<R: std::io::Read + ?Sized>(
reader: &mut R,
encoding: Option<&'static Encoding>,
buf: &mut [u8],
) -> Result<(&'static Encoding, bool, encoding::Decoder, usize), Error> {
let read = reader.read(buf)?;
let is_empty = read == 0;
let (encoding, has_bom) = encoding
.map(|encoding| (encoding, false))
.or_else(|| encoding::Encoding::for_bom(buf).map(|(encoding, _bom_size)| (encoding, true)))
.unwrap_or_else(|| {
let mut encoding_detector = chardetng::EncodingDetector::new();
encoding_detector.feed(buf, is_empty);
(encoding_detector.guess(None, true), false)
});
let decoder = encoding.new_decoder();
Ok((encoding, has_bom, decoder, read))
} }
// The documentation and implementation of this function should be up-to-date with // The documentation and implementation of this function should be up-to-date with
@ -393,7 +554,7 @@ pub fn from_reader<R: std::io::Read + ?Sized>(
/// replacement characters may appear in the encoded text. /// replacement characters may appear in the encoded text.
pub async fn to_writer<'a, W: tokio::io::AsyncWriteExt + Unpin + ?Sized>( pub async fn to_writer<'a, W: tokio::io::AsyncWriteExt + Unpin + ?Sized>(
writer: &'a mut W, writer: &'a mut W,
encoding: &'static encoding::Encoding, encoding_with_bom_info: (&'static Encoding, bool),
rope: &'a Rope, rope: &'a Rope,
) -> Result<(), Error> { ) -> Result<(), Error> {
// Text inside a `Rope` is stored as non-contiguous blocks of data called // Text inside a `Rope` is stored as non-contiguous blocks of data called
@ -402,13 +563,22 @@ pub async fn to_writer<'a, W: tokio::io::AsyncWriteExt + Unpin + ?Sized>(
// determined by filtering the iterator to remove all empty chunks and then // determined by filtering the iterator to remove all empty chunks and then
// appending an empty chunk to it. This is valuable for detecting when all // appending an empty chunk to it. This is valuable for detecting when all
// chunks in the `Rope` have been iterated over in the subsequent loop. // chunks in the `Rope` have been iterated over in the subsequent loop.
let (encoding, has_bom) = encoding_with_bom_info;
let iter = rope let iter = rope
.chunks() .chunks()
.filter(|c| !c.is_empty()) .filter(|c| !c.is_empty())
.chain(std::iter::once("")); .chain(std::iter::once(""));
let mut buf = [0u8; BUF_SIZE]; let mut buf = [0u8; BUF_SIZE];
let mut encoder = encoding.new_encoder();
let mut total_written = 0usize; let mut total_written = if has_bom {
apply_bom(encoding, &mut buf)
} else {
0
};
let mut encoder = Encoder::from_encoding(encoding);
for chunk in iter { for chunk in iter {
let is_empty = chunk.is_empty(); let is_empty = chunk.is_empty();
let mut total_read = 0usize; let mut total_read = 0usize;
@ -449,6 +619,7 @@ pub async fn to_writer<'a, W: tokio::io::AsyncWriteExt + Unpin + ?Sized>(
break; break;
} }
} }
Ok(()) Ok(())
} }
@ -460,16 +631,17 @@ where
*mut_ref = f(mem::take(mut_ref)); *mut_ref = f(mem::take(mut_ref));
} }
use helix_lsp::lsp; use helix_lsp::{lsp, Client, LanguageServerName};
use url::Url; use url::Url;
impl Document { impl Document {
pub fn from( pub fn from(
text: Rope, text: Rope,
encoding: Option<&'static encoding::Encoding>, encoding_with_bom_info: Option<(&'static Encoding, bool)>,
config: Arc<dyn DynAccess<Config>>, config: Arc<dyn DynAccess<Config>>,
) -> Self { ) -> Self {
let encoding = encoding.unwrap_or(encoding::UTF_8); let (encoding, has_bom) = encoding_with_bom_info.unwrap_or((encoding::UTF_8, false));
let line_ending = config.load().default_line_ending.into();
let changes = ChangeSet::new(&text); let changes = ChangeSet::new(&text);
let old_state = None; let old_state = None;
@ -477,12 +649,13 @@ impl Document {
id: DocumentId::default(), id: DocumentId::default(),
path: None, path: None,
encoding, encoding,
has_bom,
text, text,
selections: HashMap::default(), selections: HashMap::default(),
inlay_hints: HashMap::default(), inlay_hints: HashMap::default(),
inlay_hints_oudated: false, inlay_hints_oudated: false,
indent_style: DEFAULT_INDENT, indent_style: DEFAULT_INDENT,
line_ending: DEFAULT_LINE_ENDING, line_ending,
restore_cursor: false, restore_cursor: false,
syntax: None, syntax: None,
language: None, language: None,
@ -495,37 +668,41 @@ impl Document {
last_saved_time: SystemTime::now(), last_saved_time: SystemTime::now(),
last_saved_revision: 0, last_saved_revision: 0,
modified_since_accessed: false, modified_since_accessed: false,
language_server: None, language_servers: HashMap::new(),
diff_handle: None, diff_handle: None,
config, config,
version_control_head: None, version_control_head: None,
focused_at: std::time::Instant::now(), focused_at: std::time::Instant::now(),
} }
} }
pub fn default(config: Arc<dyn DynAccess<Config>>) -> Self { pub fn default(config: Arc<dyn DynAccess<Config>>) -> Self {
let text = Rope::from(DEFAULT_LINE_ENDING.as_str()); let line_ending: LineEnding = config.load().default_line_ending.into();
let text = Rope::from(line_ending.as_str());
Self::from(text, None, config) Self::from(text, None, config)
} }
// TODO: async fn? // TODO: async fn?
/// Create a new document from `path`. Encoding is auto-detected, but it can be manually /// Create a new document from `path`. Encoding is auto-detected, but it can be manually
/// overwritten with the `encoding` parameter. /// overwritten with the `encoding` parameter.
pub fn open( pub fn open(
path: &Path, path: &Path,
encoding: Option<&'static encoding::Encoding>, encoding: Option<&'static Encoding>,
config_loader: Option<Arc<syntax::Loader>>, config_loader: Option<Arc<syntax::Loader>>,
config: Arc<dyn DynAccess<Config>>, config: Arc<dyn DynAccess<Config>>,
) -> Result<Self, Error> { ) -> Result<Self, Error> {
// Open the file if it exists, otherwise assume it is a new file (and thus empty). // Open the file if it exists, otherwise assume it is a new file (and thus empty).
let (rope, encoding) = if path.exists() { let (rope, encoding, has_bom) = if path.exists() {
let mut file = let mut file =
std::fs::File::open(path).context(format!("unable to open {:?}", path))?; std::fs::File::open(path).context(format!("unable to open {:?}", path))?;
from_reader(&mut file, encoding)? from_reader(&mut file, encoding)?
} else { } else {
let line_ending: LineEnding = config.load().default_line_ending.into();
let encoding = encoding.unwrap_or(encoding::UTF_8); let encoding = encoding.unwrap_or(encoding::UTF_8);
(Rope::from(DEFAULT_LINE_ENDING.as_str()), encoding) (Rope::from(line_ending.as_str()), encoding, false)
}; };
let mut doc = Self::from(rope, Some(encoding), config); let mut doc = Self::from(rope, Some((encoding, has_bom)), config);
// set the path and try detecting the language // set the path and try detecting the language
doc.set_path(Some(path))?; doc.set_path(Some(path))?;
@ -576,7 +753,7 @@ impl Document {
})?; })?;
{ {
let mut stdin = process.stdin.take().ok_or(FormatterError::BrokenStdin)?; let mut stdin = process.stdin.take().ok_or(FormatterError::BrokenStdin)?;
to_writer(&mut stdin, encoding::UTF_8, &text) to_writer(&mut stdin, (encoding::UTF_8, false), &text)
.await .await
.map_err(|_| FormatterError::BrokenStdin)?; .map_err(|_| FormatterError::BrokenStdin)?;
} }
@ -609,10 +786,12 @@ impl Document {
return Some(formatting_future.boxed()); return Some(formatting_future.boxed());
}; };
let language_server = self.language_server()?;
let text = self.text.clone(); let text = self.text.clone();
// finds first language server that supports formatting and then formats
let language_server = self
.language_servers_with_feature(LanguageServerFeature::Format)
.next()?;
let offset_encoding = language_server.offset_encoding(); let offset_encoding = language_server.offset_encoding();
let request = language_server.text_document_formatting( let request = language_server.text_document_formatting(
self.identifier(), self.identifier(),
lsp::FormattingOptions { lsp::FormattingOptions {
@ -676,20 +855,18 @@ impl Document {
if self.path.is_none() { if self.path.is_none() {
bail!("Can't save with no path set!"); bail!("Can't save with no path set!");
} }
self.path.as_ref().unwrap().clone() self.path.as_ref().unwrap().clone()
} }
}; };
let identifier = self.path().map(|_| self.identifier()); let identifier = self.path().map(|_| self.identifier());
let language_server = self.language_server.clone(); let language_servers = self.language_servers.clone();
// mark changes up to now as saved // mark changes up to now as saved
let current_rev = self.get_current_revision(); let current_rev = self.get_current_revision();
let doc_id = self.id(); let doc_id = self.id();
let encoding = self.encoding; let encoding_with_bom_info = (self.encoding, self.has_bom);
let last_saved_time = self.last_saved_time; let last_saved_time = self.last_saved_time;
// We encode the file according to the `Document`'s encoding. // We encode the file according to the `Document`'s encoding.
@ -701,7 +878,7 @@ impl Document {
if force { if force {
std::fs::DirBuilder::new().recursive(true).create(parent)?; std::fs::DirBuilder::new().recursive(true).create(parent)?;
} else { } else {
bail!("can't save file, parent directory does not exist"); bail!("can't save file, parent directory does not exist (use :w! to create it)");
} }
} }
} }
@ -718,7 +895,7 @@ impl Document {
} }
let mut file = File::create(&path).await?; let mut file = File::create(&path).await?;
to_writer(&mut file, encoding, &text).await?; to_writer(&mut file, encoding_with_bom_info, &text).await?;
let event = DocumentSavedEvent { let event = DocumentSavedEvent {
revision: current_rev, revision: current_rev,
@ -727,14 +904,13 @@ impl Document {
text: text.clone(), text: text.clone(),
}; };
if let Some(language_server) = language_server { for (_, language_server) in language_servers {
if !language_server.is_initialized() { if !language_server.is_initialized() {
return Ok(event); return Ok(event);
} }
if let Some(identifier) = &identifier {
if let Some(identifier) = identifier {
if let Some(notification) = if let Some(notification) =
language_server.text_document_did_save(identifier, &text) language_server.text_document_did_save(identifier.clone(), &text)
{ {
notification.await?; notification.await?;
} }
@ -749,24 +925,34 @@ impl Document {
/// Detect the programming language based on the file type. /// Detect the programming language based on the file type.
pub fn detect_language(&mut self, config_loader: Arc<syntax::Loader>) { pub fn detect_language(&mut self, config_loader: Arc<syntax::Loader>) {
if let Some(path) = &self.path { self.set_language(
let language_config = config_loader self.detect_language_config(&config_loader),
.language_config_for_file_name(path) Some(config_loader),
.or_else(|| config_loader.language_config_for_shebang(self.text())); );
self.set_language(language_config, Some(config_loader));
} }
/// Detect the programming language based on the file type.
pub fn detect_language_config(
&self,
config_loader: &syntax::Loader,
) -> Option<Arc<helix_core::syntax::LanguageConfiguration>> {
config_loader
.language_config_for_file_name(self.path.as_ref()?)
.or_else(|| config_loader.language_config_for_shebang(self.text()))
} }
/// Detect the indentation used in the file, or otherwise defaults to the language indentation /// Detect the indentation used in the file, or otherwise defaults to the language indentation
/// configured in `languages.toml`, with a fallback to tabs if it isn't specified. Line ending /// configured in `languages.toml`, with a fallback to tabs if it isn't specified. Line ending
/// is likewise auto-detected, and will fallback to the default OS line ending. /// is likewise auto-detected, and will remain unchanged if no line endings were detected.
pub fn detect_indent_and_line_ending(&mut self) { pub fn detect_indent_and_line_ending(&mut self) {
self.indent_style = auto_detect_indent_style(&self.text).unwrap_or_else(|| { self.indent_style = auto_detect_indent_style(&self.text).unwrap_or_else(|| {
self.language_config() self.language_config()
.and_then(|config| config.indent.as_ref()) .and_then(|config| config.indent.as_ref())
.map_or(DEFAULT_INDENT, |config| IndentStyle::from_str(&config.unit)) .map_or(DEFAULT_INDENT, |config| IndentStyle::from_str(&config.unit))
}); });
self.line_ending = auto_detect_line_ending(&self.text).unwrap_or(DEFAULT_LINE_ENDING); if let Some(line_ending) = auto_detect_line_ending(&self.text) {
self.line_ending = line_ending;
}
} }
/// Reload the document from its path. /// Reload the document from its path.
@ -776,7 +962,7 @@ impl Document {
provider_registry: &DiffProviderRegistry, provider_registry: &DiffProviderRegistry,
redraw_handle: RedrawHandle, redraw_handle: RedrawHandle,
) -> Result<(), Error> { ) -> Result<(), Error> {
let encoding = &self.encoding; let encoding = self.encoding;
let path = self let path = self
.path() .path()
.filter(|path| path.exists()) .filter(|path| path.exists())
@ -810,13 +996,16 @@ impl Document {
/// Sets the [`Document`]'s encoding with the encoding correspondent to `label`. /// Sets the [`Document`]'s encoding with the encoding correspondent to `label`.
pub fn set_encoding(&mut self, label: &str) -> Result<(), Error> { pub fn set_encoding(&mut self, label: &str) -> Result<(), Error> {
self.encoding = encoding::Encoding::for_label(label.as_bytes()) let encoding =
.ok_or_else(|| anyhow!("unknown encoding"))?; Encoding::for_label(label.as_bytes()).ok_or_else(|| anyhow!("unknown encoding"))?;
self.encoding = encoding;
Ok(()) Ok(())
} }
/// Returns the [`Document`]'s current encoding. /// Returns the [`Document`]'s current encoding.
pub fn encoding(&self) -> &'static encoding::Encoding { pub fn encoding(&self) -> &'static Encoding {
self.encoding self.encoding
} }
@ -841,8 +1030,7 @@ impl Document {
) { ) {
if let (Some(language_config), Some(loader)) = (language_config, loader) { if let (Some(language_config), Some(loader)) = (language_config, loader) {
if let Some(highlight_config) = language_config.highlight_config(&loader.scopes()) { if let Some(highlight_config) = language_config.highlight_config(&loader.scopes()) {
let syntax = Syntax::new(&self.text, highlight_config, loader); self.syntax = Syntax::new(&self.text, highlight_config, loader);
self.syntax = Some(syntax);
} }
self.language = Some(language_config); self.language = Some(language_config);
@ -874,11 +1062,6 @@ impl Document {
Ok(()) Ok(())
} }
/// Set the LSP.
pub fn set_language_server(&mut self, language_server: Option<Arc<helix_lsp::Client>>) {
self.language_server = language_server;
}
/// Select text within the [`Document`]. /// Select text within the [`Document`].
pub fn set_selection(&mut self, view_id: ViewId, selection: Selection) { pub fn set_selection(&mut self, view_id: ViewId, selection: Selection) {
// TODO: use a transaction? // TODO: use a transaction?
@ -924,7 +1107,12 @@ impl Document {
} }
/// Apply a [`Transaction`] to the [`Document`] to change its text. /// Apply a [`Transaction`] to the [`Document`] to change its text.
fn apply_impl(&mut self, transaction: &Transaction, view_id: ViewId) -> bool { fn apply_impl(
&mut self,
transaction: &Transaction,
view_id: ViewId,
emit_lsp_notification: bool,
) -> bool {
use helix_core::Assoc; use helix_core::Assoc;
let old_doc = self.text().clone(); let old_doc = self.text().clone();
@ -977,28 +1165,40 @@ impl Document {
// update tree-sitter syntax tree // update tree-sitter syntax tree
if let Some(syntax) = &mut self.syntax { if let Some(syntax) = &mut self.syntax {
// TODO: no unwrap // TODO: no unwrap
syntax let res = syntax.update(&old_doc, &self.text, transaction.changes());
.update(&old_doc, &self.text, transaction.changes()) if res.is_err() {
.unwrap(); log::error!("TS parser failed, disabeling TS for the current buffer: {res:?}");
self.syntax = None;
}
} }
let changes = transaction.changes(); let changes = transaction.changes();
changes.update_positions(
self.diagnostics
.iter_mut()
.map(|diagnostic| (&mut diagnostic.range.start, Assoc::After)),
);
changes.update_positions(
self.diagnostics
.iter_mut()
.map(|diagnostic| (&mut diagnostic.range.end, Assoc::After)),
);
// map state.diagnostics over changes::map_pos too // map state.diagnostics over changes::map_pos too
for diagnostic in &mut self.diagnostics { for diagnostic in &mut self.diagnostics {
diagnostic.range.start = changes.map_pos(diagnostic.range.start, Assoc::After);
diagnostic.range.end = changes.map_pos(diagnostic.range.end, Assoc::After);
diagnostic.line = self.text.char_to_line(diagnostic.range.start); diagnostic.line = self.text.char_to_line(diagnostic.range.start);
} }
self.diagnostics self.diagnostics
.sort_unstable_by_key(|diagnostic| diagnostic.range); .sort_unstable_by_key(|diagnostic| diagnostic.range);
// Update the inlay hint annotations' positions, helping ensure they are displayed in the proper place // Update the inlay hint annotations' positions, helping ensure they are displayed in the proper place
let apply_inlay_hint_changes = |annotations: &mut Rc<[InlineAnnotation]>| { let apply_inlay_hint_changes = |annotations: &mut Rc<[InlineAnnotation]>| {
if let Some(data) = Rc::get_mut(annotations) { if let Some(data) = Rc::get_mut(annotations) {
for inline in data.iter_mut() { changes.update_positions(
inline.char_idx = changes.map_pos(inline.char_idx, Assoc::After); data.iter_mut()
} .map(|annotation| (&mut annotation.char_idx, Assoc::After)),
);
} }
}; };
@ -1020,8 +1220,9 @@ impl Document {
apply_inlay_hint_changes(padding_after_inlay_hints); apply_inlay_hint_changes(padding_after_inlay_hints);
} }
if emit_lsp_notification {
// emit lsp notification // emit lsp notification
if let Some(language_server) = self.language_server() { for language_server in self.language_servers() {
let notify = language_server.text_document_did_change( let notify = language_server.text_document_did_change(
self.versioned_identifier(), self.versioned_identifier(),
&old_doc, &old_doc,
@ -1034,11 +1235,16 @@ impl Document {
} }
} }
} }
}
success success
} }
/// Apply a [`Transaction`] to the [`Document`] to change its text. fn apply_inner(
pub fn apply(&mut self, transaction: &Transaction, view_id: ViewId) -> bool { &mut self,
transaction: &Transaction,
view_id: ViewId,
emit_lsp_notification: bool,
) -> bool {
// store the state just before any changes are made. This allows us to undo to the // store the state just before any changes are made. This allows us to undo to the
// state just before a transaction was applied. // state just before a transaction was applied.
if self.changes.is_empty() && !transaction.changes().is_empty() { if self.changes.is_empty() && !transaction.changes().is_empty() {
@ -1048,7 +1254,7 @@ impl Document {
}); });
} }
let success = self.apply_impl(transaction, view_id); let success = self.apply_impl(transaction, view_id, emit_lsp_notification);
if !transaction.changes().is_empty() { if !transaction.changes().is_empty() {
// Compose this transaction with the previous one // Compose this transaction with the previous one
@ -1058,12 +1264,23 @@ impl Document {
} }
success success
} }
/// Apply a [`Transaction`] to the [`Document`] to change its text.
pub fn apply(&mut self, transaction: &Transaction, view_id: ViewId) -> bool {
self.apply_inner(transaction, view_id, true)
}
/// Apply a [`Transaction`] to the [`Document`] to change its text
/// without notifying the language servers. This is useful for temporary transactions
/// that must not influence the server.
pub fn apply_temporary(&mut self, transaction: &Transaction, view_id: ViewId) -> bool {
self.apply_inner(transaction, view_id, false)
}
fn undo_redo_impl(&mut self, view: &mut View, undo: bool) -> bool { fn undo_redo_impl(&mut self, view: &mut View, undo: bool) -> bool {
let mut history = self.history.take(); let mut history = self.history.take();
let txn = if undo { history.undo() } else { history.redo() }; let txn = if undo { history.undo() } else { history.redo() };
let success = if let Some(txn) = txn { let success = if let Some(txn) = txn {
self.apply_impl(txn, view.id) self.apply_impl(txn, view.id, true)
} else { } else {
false false
}; };
@ -1095,15 +1312,32 @@ impl Document {
/// the state it had when this function was called. /// the state it had when this function was called.
pub fn savepoint(&mut self, view: &View) -> Arc<SavePoint> { pub fn savepoint(&mut self, view: &View) -> Arc<SavePoint> {
let revert = Transaction::new(self.text()).with_selection(self.selection(view.id).clone()); let revert = Transaction::new(self.text()).with_selection(self.selection(view.id).clone());
// check if there is already an existing (identical) savepoint around
if let Some(savepoint) = self
.savepoints
.iter()
.rev()
.find_map(|savepoint| savepoint.upgrade())
{
let transaction = savepoint.revert.lock();
if savepoint.view == view.id
&& transaction.changes().is_empty()
&& transaction.selection() == revert.selection()
{
drop(transaction);
return savepoint;
}
}
let savepoint = Arc::new(SavePoint { let savepoint = Arc::new(SavePoint {
view: view.id, view: view.id,
revert: Mutex::new(revert), revert: Mutex::new(revert),
text: self.text.clone(),
}); });
self.savepoints.push(Arc::downgrade(&savepoint)); self.savepoints.push(Arc::downgrade(&savepoint));
savepoint savepoint
} }
pub fn restore(&mut self, view: &mut View, savepoint: &SavePoint) { pub fn restore(&mut self, view: &mut View, savepoint: &SavePoint, emit_lsp_notification: bool) {
assert_eq!( assert_eq!(
savepoint.view, view.id, savepoint.view, view.id,
"Savepoint must not be used with a different view!" "Savepoint must not be used with a different view!"
@ -1118,7 +1352,7 @@ impl Document {
let savepoint_ref = self.savepoints.remove(savepoint_idx); let savepoint_ref = self.savepoints.remove(savepoint_idx);
let mut revert = savepoint.revert.lock(); let mut revert = savepoint.revert.lock();
self.apply(&revert, view.id); self.apply_inner(&revert, view.id, emit_lsp_notification);
*revert = Transaction::new(self.text()).with_selection(self.selection(view.id).clone()); *revert = Transaction::new(self.text()).with_selection(self.selection(view.id).clone());
self.savepoints.push(savepoint_ref) self.savepoints.push(savepoint_ref)
} }
@ -1131,7 +1365,7 @@ impl Document {
}; };
let mut success = false; let mut success = false;
for txn in txns { for txn in txns {
if self.apply_impl(&txn, view.id) { if self.apply_impl(&txn, view.id, true) {
success = true; success = true;
} }
} }
@ -1244,18 +1478,13 @@ impl Document {
.map(|language| language.language_id.as_str()) .map(|language| language.language_id.as_str())
} }
/// Language ID for the document. Either the `language-id` from the /// Language ID for the document. Either the `language-id`,
/// `language-server` configuration, or the document language if no /// or the document language name if no `language-id` has been specified.
/// `language-id` has been specified.
pub fn language_id(&self) -> Option<&str> { pub fn language_id(&self) -> Option<&str> {
let language_config = self.language.as_deref()?; self.language_config()?
.language_server_language_id
language_config
.language_server
.as_ref()?
.language_id
.as_deref() .as_deref()
.or(Some(language_config.language_id.as_str())) .or_else(|| self.language_name())
} }
/// Corresponding [`LanguageConfiguration`]. /// Corresponding [`LanguageConfiguration`].
@ -1268,10 +1497,45 @@ impl Document {
self.version self.version
} }
/// Language server if it has been initialized. /// maintains the order as configured in the language_servers TOML array
pub fn language_server(&self) -> Option<&helix_lsp::Client> { pub fn language_servers(&self) -> impl Iterator<Item = &helix_lsp::Client> {
let server = self.language_server.as_deref()?; self.language_config().into_iter().flat_map(move |config| {
server.is_initialized().then_some(server) config.language_servers.iter().filter_map(move |features| {
let ls = &**self.language_servers.get(&features.name)?;
if ls.is_initialized() {
Some(ls)
} else {
None
}
})
})
}
pub fn remove_language_server_by_name(&mut self, name: &str) -> Option<Arc<Client>> {
self.language_servers.remove(name)
}
pub fn language_servers_with_feature(
&self,
feature: LanguageServerFeature,
) -> impl Iterator<Item = &helix_lsp::Client> {
self.language_config().into_iter().flat_map(move |config| {
config.language_servers.iter().filter_map(move |features| {
let ls = &**self.language_servers.get(&features.name)?;
if ls.is_initialized()
&& ls.supports_feature(feature)
&& features.has_feature(feature)
{
Some(ls)
} else {
None
}
})
})
}
pub fn supports_language_server(&self, id: usize) -> bool {
self.language_servers().any(|l| l.id() == id)
} }
pub fn diff_handle(&self) -> Option<&DiffHandle> { pub fn diff_handle(&self) -> Option<&DiffHandle> {
@ -1280,7 +1544,7 @@ impl Document {
/// Intialize/updates the differ for this document with a new base. /// Intialize/updates the differ for this document with a new base.
pub fn set_diff_base(&mut self, diff_base: Vec<u8>, redraw_handle: RedrawHandle) { pub fn set_diff_base(&mut self, diff_base: Vec<u8>, redraw_handle: RedrawHandle) {
if let Ok((diff_base, _)) = from_reader(&mut diff_base.as_slice(), Some(self.encoding)) { if let Ok((diff_base, ..)) = from_reader(&mut diff_base.as_slice(), Some(self.encoding)) {
if let Some(differ) = &self.diff_handle { if let Some(differ) = &self.diff_handle {
differ.update_diff_base(diff_base); differ.update_diff_base(diff_base);
return; return;
@ -1394,12 +1658,29 @@ impl Document {
&self.diagnostics &self.diagnostics
} }
pub fn set_diagnostics(&mut self, diagnostics: Vec<Diagnostic>) { pub fn shown_diagnostics(&self) -> impl Iterator<Item = &Diagnostic> + DoubleEndedIterator {
self.diagnostics = diagnostics; self.diagnostics.iter().filter(|d| {
self.language_servers_with_feature(LanguageServerFeature::Diagnostics)
.any(|ls| ls.id() == d.language_server_id)
})
}
pub fn replace_diagnostics(
&mut self,
mut diagnostics: Vec<Diagnostic>,
language_server_id: usize,
) {
self.clear_diagnostics(language_server_id);
self.diagnostics.append(&mut diagnostics);
self.diagnostics self.diagnostics
.sort_unstable_by_key(|diagnostic| diagnostic.range); .sort_unstable_by_key(|diagnostic| diagnostic.range);
} }
pub fn clear_diagnostics(&mut self, language_server_id: usize) {
self.diagnostics
.retain(|d| d.language_server_id != language_server_id);
}
/// Get the document's auto pairs. If the document has a recognized /// Get the document's auto pairs. If the document has a recognized
/// language config with auto pairs configured, returns that; /// language config with auto pairs configured, returns that;
/// otherwise, falls back to the global auto pairs config. If the global /// otherwise, falls back to the global auto pairs config. If the global
@ -1708,7 +1989,7 @@ mod test {
Document::default(Arc::new(ArcSwap::new(Arc::new(Config::default())))) Document::default(Arc::new(ArcSwap::new(Arc::new(Config::default()))))
.text() .text()
.to_string(), .to_string(),
DEFAULT_LINE_ENDING.as_str() helix_core::NATIVE_LINE_ENDING.as_str()
); );
} }
@ -1724,7 +2005,7 @@ mod test {
assert!(ref_path.exists()); assert!(ref_path.exists());
let mut file = std::fs::File::open(path).unwrap(); let mut file = std::fs::File::open(path).unwrap();
let text = from_reader(&mut file, Some(encoding)) let text = from_reader(&mut file, Some(encoding.into()))
.unwrap() .unwrap()
.0 .0
.to_string(); .to_string();
@ -1750,7 +2031,7 @@ mod test {
let text = Rope::from_str(&std::fs::read_to_string(path).unwrap()); let text = Rope::from_str(&std::fs::read_to_string(path).unwrap());
let mut buf: Vec<u8> = Vec::new(); let mut buf: Vec<u8> = Vec::new();
helix_lsp::block_on(to_writer(&mut buf, encoding, &text)).unwrap(); helix_lsp::block_on(to_writer(&mut buf, (encoding, false), &text)).unwrap();
let expectation = std::fs::read(ref_path).unwrap(); let expectation = std::fs::read(ref_path).unwrap();
assert_eq!(buf, expectation); assert_eq!(buf, expectation);

@ -1,7 +1,7 @@
use crate::{ use crate::{
align_view, align_view,
clipboard::{get_clipboard_provider, ClipboardProvider}, clipboard::{get_clipboard_provider, ClipboardProvider},
document::{DocumentSavedEventFuture, DocumentSavedEventResult, Mode}, document::{DocumentSavedEventFuture, DocumentSavedEventResult, Mode, SavePoint},
graphics::{CursorKind, Rect}, graphics::{CursorKind, Rect},
info::Info, info::Info,
input::KeyEvent, input::KeyEvent,
@ -44,7 +44,7 @@ pub use helix_core::register::Registers;
use helix_core::{ use helix_core::{
auto_pairs::AutoPairs, auto_pairs::AutoPairs,
syntax::{self, AutoPairConfig, SoftWrap}, syntax::{self, AutoPairConfig, SoftWrap},
Change, Change, LineEnding, NATIVE_LINE_ENDING,
}; };
use helix_core::{Position, Selection}; use helix_core::{Position, Selection};
use helix_dap as dap; use helix_dap as dap;
@ -251,6 +251,8 @@ pub struct Config {
deserialize_with = "deserialize_duration_millis" deserialize_with = "deserialize_duration_millis"
)] )]
pub idle_timeout: Duration, pub idle_timeout: Duration,
/// Whether to insert the completion suggestion on hover. Defaults to true.
pub preview_completion_insert: bool,
pub completion_trigger_len: u8, pub completion_trigger_len: u8,
/// Whether to instruct the LSP to replace the entire word when applying a completion /// Whether to instruct the LSP to replace the entire word when applying a completion
/// or to only insert new text /// or to only insert new text
@ -271,7 +273,7 @@ pub struct Config {
pub search: SearchConfig, pub search: SearchConfig,
pub lsp: LspConfig, pub lsp: LspConfig,
pub terminal: Option<TerminalConfig>, pub terminal: Option<TerminalConfig>,
/// Column numbers at which to draw the rulers. Default to `[]`, meaning no rulers. /// Column numbers at which to draw the rulers. Defaults to `[]`, meaning no rulers.
pub rulers: Vec<u16>, pub rulers: Vec<u16>,
#[serde(default)] #[serde(default)]
pub whitespace: WhitespaceConfig, pub whitespace: WhitespaceConfig,
@ -284,6 +286,8 @@ pub struct Config {
pub soft_wrap: SoftWrap, pub soft_wrap: SoftWrap,
/// Workspace specific lsp ceiling dirs /// Workspace specific lsp ceiling dirs
pub workspace_lsp_roots: Vec<PathBuf>, pub workspace_lsp_roots: Vec<PathBuf>,
/// Which line ending to choose for new documents. Defaults to `native`. i.e. `crlf` on Windows, otherwise `lf`.
pub default_line_ending: LineEndingConfig,
} }
#[derive(Debug, Default, Clone, PartialEq, Eq, Serialize, Deserialize)] #[derive(Debug, Default, Clone, PartialEq, Eq, Serialize, Deserialize)]
@ -403,7 +407,13 @@ impl Default for StatusLineConfig {
E::FileModificationIndicator, E::FileModificationIndicator,
], ],
center: vec![], center: vec![],
right: vec![E::Diagnostics, E::Selections, E::Position, E::FileEncoding], right: vec![
E::Diagnostics,
E::Selections,
E::Register,
E::Position,
E::FileEncoding,
],
separator: String::from("│"), separator: String::from("│"),
mode: ModeConfig::default(), mode: ModeConfig::default(),
} }
@ -487,6 +497,9 @@ pub enum StatusLineElement {
/// Custom /// Custom
Custom, Custom,
/// Indicator for selected register
Register,
} }
// Cursor shape is read and used on every rendered frame and so needs // Cursor shape is read and used on every rendered frame and so needs
@ -719,6 +732,51 @@ impl Default for IndentGuidesConfig {
} }
} }
/// Line ending configuration.
#[derive(Debug, Copy, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum LineEndingConfig {
/// The platform's native line ending.
///
/// `crlf` on Windows, otherwise `lf`.
Native,
/// Line feed.
LF,
/// Carriage return followed by line feed.
Crlf,
/// Form feed.
#[cfg(feature = "unicode-lines")]
FF,
/// Carriage return.
#[cfg(feature = "unicode-lines")]
CR,
/// Next line.
#[cfg(feature = "unicode-lines")]
Nel,
}
impl Default for LineEndingConfig {
fn default() -> Self {
LineEndingConfig::Native
}
}
impl From<LineEndingConfig> for LineEnding {
fn from(line_ending: LineEndingConfig) -> Self {
match line_ending {
LineEndingConfig::Native => NATIVE_LINE_ENDING,
LineEndingConfig::LF => LineEnding::LF,
LineEndingConfig::Crlf => LineEnding::Crlf,
#[cfg(feature = "unicode-lines")]
LineEndingConfig::FF => LineEnding::FF,
#[cfg(feature = "unicode-lines")]
LineEndingConfig::CR => LineEnding::CR,
#[cfg(feature = "unicode-lines")]
LineEndingConfig::Nel => LineEnding::Nel,
}
}
}
impl Default for Config { impl Default for Config {
fn default() -> Self { fn default() -> Self {
Self { Self {
@ -740,6 +798,7 @@ impl Default for Config {
auto_format: true, auto_format: true,
auto_save: false, auto_save: false,
idle_timeout: Duration::from_millis(400), idle_timeout: Duration::from_millis(400),
preview_completion_insert: true,
completion_trigger_len: 2, completion_trigger_len: 2,
auto_info: true, auto_info: true,
file_picker: FilePickerConfig::default(), file_picker: FilePickerConfig::default(),
@ -762,6 +821,7 @@ impl Default for Config {
text_width: 80, text_width: 80,
completion_replace: false, completion_replace: false,
workspace_lsp_roots: Vec::new(), workspace_lsp_roots: Vec::new(),
default_line_ending: LineEndingConfig::default(),
} }
} }
} }
@ -827,7 +887,7 @@ pub struct Editor {
pub macro_recording: Option<(char, Vec<KeyEvent>)>, pub macro_recording: Option<(char, Vec<KeyEvent>)>,
pub macro_replaying: Vec<char>, pub macro_replaying: Vec<char>,
pub language_servers: helix_lsp::Registry, pub language_servers: helix_lsp::Registry,
pub diagnostics: BTreeMap<lsp::Url, Vec<lsp::Diagnostic>>, pub diagnostics: BTreeMap<lsp::Url, Vec<(lsp::Diagnostic, usize)>>,
pub diff_providers: DiffProviderRegistry, pub diff_providers: DiffProviderRegistry,
pub debugger: Option<dap::Client>, pub debugger: Option<dap::Client>,
@ -883,7 +943,7 @@ pub struct Editor {
/// times during rendering and should not be set by other functions. /// times during rendering and should not be set by other functions.
pub cursor_cache: Cell<Option<Option<Position>>>, pub cursor_cache: Cell<Option<Option<Position>>>,
/// When a new completion request is sent to the server old /// When a new completion request is sent to the server old
/// unifinished request must be dropped. Each completion /// unfinished request must be dropped. Each completion
/// request is associated with a channel that cancels /// request is associated with a channel that cancels
/// when the channel is dropped. That channel is stored /// when the channel is dropped. That channel is stored
/// here. When a new completion request is sent this /// here. When a new completion request is sent this
@ -915,9 +975,14 @@ enum ThemeAction {
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct CompleteAction { pub enum CompleteAction {
pub trigger_offset: usize, Applied {
pub changes: Vec<Change>, trigger_offset: usize,
changes: Vec<Change>,
},
/// A savepoint of the currently selected completion. The savepoint
/// MUST be restored before sending any event to the LSP
Selected { savepoint: Arc<SavePoint> },
} }
#[derive(Debug, Copy, Clone)] #[derive(Debug, Copy, Clone)]
@ -945,6 +1010,7 @@ impl Editor {
syn_loader: Arc<syntax::Loader>, syn_loader: Arc<syntax::Loader>,
config: Arc<dyn DynAccess<Config>>, config: Arc<dyn DynAccess<Config>>,
) -> Self { ) -> Self {
let language_servers = helix_lsp::Registry::new(syn_loader.clone());
let conf = config.load(); let conf = config.load();
let auto_pairs = (&conf.auto_pairs).into(); let auto_pairs = (&conf.auto_pairs).into();
@ -964,7 +1030,7 @@ impl Editor {
macro_recording: None, macro_recording: None,
macro_replaying: Vec::new(), macro_replaying: Vec::new(),
theme: theme_loader.default(), theme: theme_loader.default(),
language_servers: helix_lsp::Registry::new(), language_servers,
diagnostics: BTreeMap::new(), diagnostics: BTreeMap::new(),
diff_providers: DiffProviderRegistry::default(), diff_providers: DiffProviderRegistry::default(),
debugger: None, debugger: None,
@ -1096,60 +1162,75 @@ impl Editor {
self._refresh(); self._refresh();
} }
#[inline]
pub fn language_server_by_id(&self, language_server_id: usize) -> Option<&helix_lsp::Client> {
self.language_servers.get_by_id(language_server_id)
}
/// Refreshes the language server for a given document /// Refreshes the language server for a given document
pub fn refresh_language_server(&mut self, doc_id: DocumentId) -> Option<()> { pub fn refresh_language_servers(&mut self, doc_id: DocumentId) -> Option<()> {
self.launch_language_server(doc_id) self.launch_language_servers(doc_id)
} }
/// Launch a language server for a given document /// Launch a language server for a given document
fn launch_language_server(&mut self, doc_id: DocumentId) -> Option<()> { fn launch_language_servers(&mut self, doc_id: DocumentId) -> Option<()> {
if !self.config().lsp.enable { if !self.config().lsp.enable {
return None; return None;
} }
// if doc doesn't have a URL it's a scratch buffer, ignore it // if doc doesn't have a URL it's a scratch buffer, ignore it
let doc = self.document(doc_id)?; let doc = self.documents.get_mut(&doc_id)?;
let doc_url = doc.url()?;
let (lang, path) = (doc.language.clone(), doc.path().cloned()); let (lang, path) = (doc.language.clone(), doc.path().cloned());
let config = doc.config.load(); let config = doc.config.load();
let root_dirs = &config.workspace_lsp_roots; let root_dirs = &config.workspace_lsp_roots;
// try to find a language server based on the language name // try to find language servers based on the language name
let language_server = lang.as_ref().and_then(|language| { let language_servers = lang.as_ref().and_then(|language| {
self.language_servers self.language_servers
.get(language, path.as_ref(), root_dirs, config.lsp.snippets) .get(language, path.as_ref(), root_dirs, config.lsp.snippets)
.map_err(|e| { .map_err(|e| {
log::error!( log::error!(
"Failed to initialize the LSP for `{}` {{ {} }}", "Failed to initialize the language servers for `{}` {{ {} }}",
language.scope(), language.scope(),
e e
) )
}) })
.ok() .ok()
.flatten()
}); });
let doc = self.document_mut(doc_id)?; if let Some(language_servers) = language_servers {
let doc_url = doc.url()?; let language_id = doc.language_id().map(ToOwned::to_owned).unwrap_or_default();
if let Some(language_server) = language_server { // only spawn new language servers if the servers aren't the same
// only spawn a new lang server if the servers aren't the same
if Some(language_server.id()) != doc.language_server().map(|server| server.id()) { let doc_language_servers_not_in_registry =
if let Some(language_server) = doc.language_server() { doc.language_servers.iter().filter(|(name, doc_ls)| {
language_servers
.get(*name)
.map_or(true, |ls| ls.id() != doc_ls.id())
});
for (_, language_server) in doc_language_servers_not_in_registry {
tokio::spawn(language_server.text_document_did_close(doc.identifier())); tokio::spawn(language_server.text_document_did_close(doc.identifier()));
} }
let language_id = doc.language_id().map(ToOwned::to_owned).unwrap_or_default(); let language_servers_not_in_doc = language_servers.iter().filter(|(name, ls)| {
doc.language_servers
.get(*name)
.map_or(true, |doc_ls| ls.id() != doc_ls.id())
});
for (_, language_server) in language_servers_not_in_doc {
// TODO: this now races with on_init code if the init happens too quickly // TODO: this now races with on_init code if the init happens too quickly
tokio::spawn(language_server.text_document_did_open( tokio::spawn(language_server.text_document_did_open(
doc_url, doc_url.clone(),
doc.version(), doc.version(),
doc.text(), doc.text(),
language_id, language_id.clone(),
)); ));
doc.set_language_server(Some(language_server));
} }
doc.language_servers = language_servers;
} }
Some(()) Some(())
} }
@ -1316,11 +1397,22 @@ impl Editor {
} }
pub fn new_file_from_stdin(&mut self, action: Action) -> Result<DocumentId, Error> { pub fn new_file_from_stdin(&mut self, action: Action) -> Result<DocumentId, Error> {
let (rope, encoding) = crate::document::from_reader(&mut stdin(), None)?; let (stdin, encoding, has_bom) = crate::document::read_to_string(&mut stdin(), None)?;
Ok(self.new_file_from_document( let doc = Document::from(
action, helix_core::Rope::default(),
Document::from(rope, Some(encoding), self.config.clone()), Some((encoding, has_bom)),
)) self.config.clone(),
);
let doc_id = self.new_file_from_document(action, doc);
let doc = doc_mut!(self, &doc_id);
let view = view_mut!(self);
doc.ensure_view_init(view.id);
let transaction =
helix_core::Transaction::insert(doc.text(), doc.selection(view.id), stdin.into())
.with_selection(Selection::point(0));
doc.apply(&transaction, view.id);
doc.append_changes_to_history(view);
Ok(doc_id)
} }
// ??? possible use for integration tests // ??? possible use for integration tests
@ -1344,7 +1436,7 @@ impl Editor {
doc.set_version_control_head(self.diff_providers.get_current_head_name(&path)); doc.set_version_control_head(self.diff_providers.get_current_head_name(&path));
let id = self.new_document(doc); let id = self.new_document(doc);
let _ = self.launch_language_server(id); let _ = self.launch_language_servers(id);
id id
}; };
@ -1374,7 +1466,7 @@ impl Editor {
// This will also disallow any follow-up writes // This will also disallow any follow-up writes
self.saves.remove(&doc_id); self.saves.remove(&doc_id);
if let Some(language_server) = doc.language_server() { for language_server in doc.language_servers() {
// TODO: track error // TODO: track error
tokio::spawn(language_server.text_document_did_close(doc.identifier())); tokio::spawn(language_server.text_document_did_close(doc.identifier()));
} }

@ -1,5 +1,7 @@
use std::fmt::Write; use std::fmt::Write;
use helix_core::syntax::LanguageServerFeature;
use crate::{ use crate::{
editor::GutterType, editor::GutterType,
graphics::{Style, UnderlineStyle}, graphics::{Style, UnderlineStyle},
@ -55,7 +57,7 @@ pub fn diagnostic<'doc>(
let error = theme.get("error"); let error = theme.get("error");
let info = theme.get("info"); let info = theme.get("info");
let hint = theme.get("hint"); let hint = theme.get("hint");
let diagnostics = doc.diagnostics(); let diagnostics = &doc.diagnostics;
Box::new( Box::new(
move |line: usize, _selected: bool, first_visual_line: bool, out: &mut String| { move |line: usize, _selected: bool, first_visual_line: bool, out: &mut String| {
@ -63,28 +65,24 @@ pub fn diagnostic<'doc>(
return None; return None;
} }
use helix_core::diagnostic::Severity; use helix_core::diagnostic::Severity;
if let Ok(index) = diagnostics.binary_search_by_key(&line, |d| d.line) { let first_diag_idx_maybe_on_line = diagnostics.partition_point(|d| d.line < line);
let after = diagnostics[index..].iter().take_while(|d| d.line == line); let diagnostics_on_line = diagnostics[first_diag_idx_maybe_on_line..]
let before = diagnostics[..index]
.iter() .iter()
.rev() .take_while(|d| {
.take_while(|d| d.line == line); d.line == line
&& doc
let diagnostics_on_line = after.chain(before); .language_servers_with_feature(LanguageServerFeature::Diagnostics)
.any(|ls| ls.id() == d.language_server_id)
// This unwrap is safe because the iterator cannot be empty as it contains at least the item found by the binary search. });
let diagnostic = diagnostics_on_line.max_by_key(|d| d.severity).unwrap(); diagnostics_on_line.max_by_key(|d| d.severity).map(|d| {
write!(out, "●").ok();
write!(out, "●").unwrap(); match d.severity {
return Some(match diagnostic.severity {
Some(Severity::Error) => error, Some(Severity::Error) => error,
Some(Severity::Warning) | None => warning, Some(Severity::Warning) | None => warning,
Some(Severity::Info) => info, Some(Severity::Info) => info,
Some(Severity::Hint) => hint, Some(Severity::Hint) => hint,
});
} }
None })
}, },
) )
} }

@ -1,6 +1,5 @@
use crate::input::KeyEvent;
use helix_core::{register::Registers, unicode::width::UnicodeWidthStr}; use helix_core::{register::Registers, unicode::width::UnicodeWidthStr};
use std::{collections::BTreeSet, fmt::Write}; use std::fmt::Write;
#[derive(Debug)] #[derive(Debug)]
/// Info box used in editor. Rendering logic will be in other crate. /// Info box used in editor. Rendering logic will be in other crate.
@ -55,18 +54,6 @@ impl Info {
} }
} }
pub fn from_keymap(title: &str, body: Vec<(&str, BTreeSet<KeyEvent>)>) -> Self {
let body: Vec<_> = body
.into_iter()
.map(|(desc, events)| {
let events = events.iter().map(ToString::to_string).collect::<Vec<_>>();
(events.join(", "), desc)
})
.collect();
Self::new(title, &body)
}
pub fn from_registers(registers: &Registers) -> Self { pub fn from_registers(registers: &Registers) -> Self {
let body: Vec<_> = registers let body: Vec<_> = registers
.inner() .inner()

@ -390,8 +390,23 @@ impl ThemePalette {
Self { palette: default } Self { palette: default }
} }
pub fn hex_string_to_rgb(s: &str) -> Result<Color, String> { pub fn string_to_rgb(s: &str) -> Result<Color, String> {
if s.starts_with('#') && s.len() >= 7 { if s.starts_with('#') {
Self::hex_string_to_rgb(s)
} else {
Self::ansi_string_to_rgb(s)
}
}
fn ansi_string_to_rgb(s: &str) -> Result<Color, String> {
if let Ok(index) = s.parse::<u8>() {
return Ok(Color::Indexed(index));
}
Err(format!("Theme: malformed ANSI: {}", s))
}
fn hex_string_to_rgb(s: &str) -> Result<Color, String> {
if s.len() >= 7 {
if let (Ok(red), Ok(green), Ok(blue)) = ( if let (Ok(red), Ok(green), Ok(blue)) = (
u8::from_str_radix(&s[1..3], 16), u8::from_str_radix(&s[1..3], 16),
u8::from_str_radix(&s[3..5], 16), u8::from_str_radix(&s[3..5], 16),
@ -417,7 +432,7 @@ impl ThemePalette {
.get(value) .get(value)
.copied() .copied()
.ok_or("") .ok_or("")
.or_else(|_| Self::hex_string_to_rgb(value)) .or_else(|_| Self::string_to_rgb(value))
} }
pub fn parse_modifier(value: &Value) -> Result<Modifier, String> { pub fn parse_modifier(value: &Value) -> Result<Modifier, String> {
@ -493,7 +508,7 @@ impl TryFrom<Value> for ThemePalette {
let mut palette = HashMap::with_capacity(map.len()); let mut palette = HashMap::with_capacity(map.len());
for (name, value) in map { for (name, value) in map {
let value = Self::parse_value_as_str(&value)?; let value = Self::parse_value_as_str(&value)?;
let color = Self::hex_string_to_rgb(value)?; let color = Self::string_to_rgb(value)?;
palette.insert(name, color); palette.insert(name, color);
} }

@ -728,12 +728,11 @@ mod test {
tree.focus = l0; tree.focus = l0;
let view = View::new(DocumentId::default(), GutterConfig::default()); let view = View::new(DocumentId::default(), GutterConfig::default());
tree.split(view, Layout::Vertical); tree.split(view, Layout::Vertical);
let l2 = tree.focus;
// Tree in test // Tree in test
// | L0 | L2 | | // | L0 | L2 | |
// | L1 | R0 | // | L1 | R0 |
tree.focus = l2; let l2 = tree.focus;
assert_eq!(Some(l0), tree.find_split_in_direction(l2, Direction::Left)); assert_eq!(Some(l0), tree.find_split_in_direction(l2, Direction::Left));
assert_eq!(Some(l1), tree.find_split_in_direction(l2, Direction::Down)); assert_eq!(Some(l1), tree.find_split_in_direction(l2, Direction::Down));
assert_eq!(Some(r0), tree.find_split_in_direction(l2, Direction::Right)); assert_eq!(Some(r0), tree.find_split_in_direction(l2, Direction::Right));

File diff suppressed because it is too large Load Diff

@ -0,0 +1,56 @@
(object_id) @attribute
(string) @string
(escape_sequence) @constant.character.escape
(comment) @comment
(constant) @constant.builtin
(boolean) @constant.builtin.boolean
(template) @keyword
(using) @keyword.control.import
(decorator) @attribute
(property_definition (property_name) @variable.other.member)
(object) @type
(signal_binding (signal_name) @function.builtin)
(signal_binding (function (identifier)) @function)
(signal_binding "swapped" @keyword)
(styles_list "styles" @function.macro)
(layout_definition "layout" @function.macro)
(gettext_string "_" @function.builtin)
(menu_definition "menu" @keyword)
(menu_section "section" @keyword)
(menu_item "item" @function.macro)
(template_definition (template_name_qualifier) @keyword.storage.type)
(import_statement (gobject_library) @namespace)
(import_statement (version_number) @constant.numeric.float)
(float) @constant.numeric.float
(number) @constant.numeric
[
";"
"."
","
] @punctuation.delimiter
[
"("
")"
"["
"]"
"{"
"}"
] @punctuation.bracket

@ -1,15 +0,0 @@
(comment) @comment
[
"cabal-version"
(field_name)
] @type
(section_name) @type
[
(section_type)
"if"
"elseif"
"else"
] @keyword

@ -1,94 +1 @@
(ERROR) @error ; inherits: rust
((identifier) @constant
(#match? @constant "^[A-Z][A-Z\\d_]+$"))
((identifier_def) @constant
(#match? @constant "^[A-Z][A-Z\\d_]+$"))
((identifier) @namespace
(#match? @namespace "^[A-Z]"))
((identifier_def) @namespace
(#match? @namespace "^[A-Z]"))
(identifier "." @punctuation)
(function_call (identifier) @function)
(func (identifier_def) @function)
(string) @string
(atom_short_string) @string
(code_element_directive) @keyword.directive
"return" @keyword
(number) @constant.numeric
(atom_hex_number) @constant.numeric
(comment) @comment
"*" @special
(type) @type
[
"felt"
; "codeoffset"
] @type.builtin
[
"if"
"else"
"assert"
"with"
"with_attr"
] @keyword.control
[
"from"
"import"
"func"
"namespace"
] @keyword ; keyword.declaration
[
"let"
"const"
"local"
"struct"
"alloc_locals"
"tempvar"
] @keyword
(decorator) @attribute
[
"="
"+"
"-"
"*"
"/"
; "%"
; "!"
; ">"
; "<"
; "\\"
; "&"
; "?"
; "^"
; "~"
"=="
"!="
"new"
] @operator
[
"("
")"
"["
"]"
"{"
"}"
] @punctuation.bracket
[
","
":"
] @punctuation.delimiter

@ -1,5 +1,3 @@
((hint) @injection.content ([(line_comment) (block_comment)] @injection.content
(#set! injection.language "python"))
((comment) @injection.content
(#set! injection.language "comment")) (#set! injection.language "comment"))

@ -32,7 +32,7 @@
(using_declaration ("using" "namespace" (identifier) @namespace)) (using_declaration ("using" "namespace" (identifier) @namespace))
(using_declaration ("using" "namespace" (qualified_identifier name: (identifier) @namespace))) (using_declaration ("using" "namespace" (qualified_identifier name: (identifier) @namespace)))
(namespace_definition name: (identifier) @namespace) (namespace_definition name: (namespace_identifier) @namespace)
(namespace_identifier) @namespace (namespace_identifier) @namespace
(qualified_identifier name: (identifier) @type.enum.variant) (qualified_identifier name: (identifier) @type.enum.variant)

@ -48,4 +48,7 @@
((variable) @constant ((variable) @constant
(#match? @constant "^[A-Z][A-Z_0-9]*$")) (#match? @constant "^[A-Z][A-Z_0-9]*$"))
[
(param)
(mount_param)
] @constant

@ -0,0 +1,7 @@
([(start_definition)(end_definition)] @keyword)
((number) @constant)
((string) @string)
((word) @function)
((comment) @comment)
([(core)] @type)
([(operator)] @operator)

@ -0,0 +1,2 @@
((comment) @injection.content
(#set! injection.language "comment"))

@ -13,8 +13,6 @@
(marker_annotation (marker_annotation
name: (identifier) @attribute) name: (identifier) @attribute)
"@" @operator
; Types ; Types
(interface_declaration (interface_declaration
@ -48,6 +46,9 @@
(void_type) (void_type)
] @type.builtin ] @type.builtin
(type_arguments
(wildcard "?" @type.builtin))
; Variables ; Variables
((identifier) @constant ((identifier) @constant
@ -87,6 +88,84 @@
(line_comment) @comment (line_comment) @comment
(block_comment) @comment (block_comment) @comment
; Punctuation
[
"::"
"."
";"
","
] @punctuation.delimiter
[
"@"
"..."
] @punctuation.special
[
"("
")"
"["
"]"
"{"
"}"
] @punctuation.bracket
(type_arguments
[
"<"
">"
] @punctuation.bracket)
(type_parameters
[
"<"
">"
] @punctuation.bracket)
; Operators
[
"="
">"
"<"
"!"
"~"
"?"
":"
"->"
"=="
">="
"<="
"!="
"&&"
"||"
"++"
"--"
"+"
"-"
"*"
"/"
"&"
"|"
"^"
"%"
"<<"
">>"
">>>"
"+="
"-="
"*="
"/="
"&="
"|="
"^="
"%="
"<<="
">>="
">>>="
] @operator
; Keywords ; Keywords
[ [

@ -0,0 +1,41 @@
(function_definition
(block (_) @context.end)
) @context
(while_statement
(block (_) @context.end)
) @context
(for_statement
(block (_) @context.end)
) @context
(if_statement
(block (_) @context.end)
) @context
(elseif_clause
(block (_) @context.end)
) @context
(else_clause
(block (_) @context.end)
) @context
(switch_statement) @context
(case_clause
(block (_) @context.end)
) @context
(otherwise_clause
(block (_) @context.end)
) @context
(try_statement
"try"
(block (_) @context.end) @context
"end")
(catch_clause
"catch"
(block (_) @context.end) @context)

@ -0,0 +1,11 @@
[(if_statement)
(for_statement)
(while_statement)
(switch_statement)
(try_statement)
(function_definition)
(class_definition)
(enumeration)
(events)
(methods)
(properties)] @fold

@ -1,97 +1,128 @@
; highlights.scm ; Constants
function_keyword: (function_keyword) @keyword.function (events (identifier) @constant)
(attribute (identifier) @constant)
"~" @constant.builtin
; Fields/Properties
(superclass "." (identifier) @variable.other.member)
(property_name "." (identifier) @variable.other.member)
(property name: (identifier) @variable.other.member)
; Types
(class_definition name: (identifier) @keyword.storage.type)
(attributes (identifier) @constant)
(enum . (identifier) @type.enum.variant)
; Functions
(function_definition (function_definition
function_name: (identifier) @function "function" @keyword.function
(end) @function) name: (identifier) @function
[ "end" "endfunction" ]? @keyword.function)
(parameter_list (identifier) @variable.parameter) (function_signature name: (identifier) @function)
(function_call name: (identifier) @function)
(handle_operator (identifier) @function)
(validation_functions (identifier) @function)
(command (command_name) @function.macro)
(command_argument) @string
(return_statement) @keyword.control.return
[ ; Assignments
"if"
"elseif" (assignment left: (_) @variable)
"else" (multioutput_variable (_) @variable)
"switch"
"case"
"otherwise"
] @keyword.control.conditional
(if_statement (end) @keyword.control.conditional) ; Parameters
(switch_statement (end) @keyword.control.conditional)
["for" "while"] @keyword.control.repeat (function_arguments (identifier) @variable.parameter)
(for_statement (end) @keyword.control.repeat)
(while_statement (end) @keyword.control.repeat)
["try" "catch"] @keyword.control.exception ; Conditionals
(try_statement (end) @keyword.control.exception)
(function_definition end: (end) @keyword) (if_statement [ "if" "end" ] @keyword.control.conditional)
(elseif_clause "elseif" @keyword.control.conditional)
(else_clause "else" @keyword.control.conditional)
(switch_statement [ "switch" "end" ] @keyword.control.conditional)
(case_clause "case" @keyword.control.conditional)
(otherwise_clause "otherwise" @keyword.control.conditional)
(break_statement) @keyword.control.conditional
["return" "break" "continue"] @keyword.return ; Repeats
( (for_statement [ "for" "parfor" "end" ] @keyword.control.repeat)
(identifier) @constant.builtin (while_statement [ "while" "end" ] @keyword.control.repeat)
(#any-of? @constant.builtin "true" "false") (continue_statement) @keyword.control.repeat
)
( ; Exceptions
(identifier) @constant.builtin
(#eq? @constant.builtin "end")
)
;; Punctuations (try_statement [ "try" "end" ] @keyword.control.exception)
(catch_clause "catch" @keyword.control.exception)
[";" ","] @punctuation.special ; Punctuation
(argument_list "," @punctuation.delimiter)
(vector_definition ["," ";"] @punctuation.delimiter)
(cell_definition ["," ";"] @punctuation.delimiter)
":" @punctuation.delimiter
(parameter_list "," @punctuation.delimiter)
(return_value "," @punctuation.delimiter)
; ;; Brackets [ ";" "," "." ] @punctuation.delimiter
[ "(" ")" "[" "]" "{" "}" ] @punctuation.bracket
; Literals
(escape_sequence) @constant.character.escape
(formatting_sequence) @constant.character.escape
(string) @string
(number) @constant.numeric.float
(unary_operator ["+" "-"] @constant.numeric.float)
(boolean) @constant.builtin.boolean
; Comments
[ (comment) (line_continuation) ] @comment.line
; Operators
[ [
"(" "+"
")" ".+"
"[" "-"
"]" ".*"
"{"
"}"
] @punctuation.bracket
;; Operators
"=" @operator
(operation [ ">"
"<"
"=="
"<="
">="
"=<"
"=>"
"~="
"*" "*"
".*" ".*"
"/" "/"
"\\"
"./" "./"
"\\"
".\\"
"^" "^"
".^" ".^"
"+"] @operator) "'"
".'"
;; boolean operator "|"
[ "&"
"?"
"@"
"<"
"<="
">"
">="
"=="
"~="
"="
"&&" "&&"
"||" "||"
":"
] @operator ] @operator
;; Number ; Keywords
(number) @constant.numeric
;; String
(string) @string
;; Comment "classdef" @keyword.storage.type
(comment) @comment [
"arguments"
"end"
"enumeration"
"events"
"global"
"methods"
"persistent"
"properties"
] @keyword

@ -0,0 +1,23 @@
[
(if_statement)
(for_statement)
(while_statement)
(switch_statement)
(try_statement)
(function_definition)
(class_definition)
(enumeration)
(events)
(methods)
(properties)
] @indent
[
(elseif_clause)
(else_clause)
(case_clause)
(otherwise_clause)
(catch_clause)
] @indent @extend
[ "end" ] @outdent

@ -0,0 +1,2 @@
((comment) @injection.content
(#set! injection.language "comment"))

@ -0,0 +1,19 @@
(function_definition name: (identifier) @local.definition ?) @local.scope
(function_arguments (identifier)* @local.definition)
(lambda (arguments (identifier) @local.definition)) @local.scope
(assignment left: ((function_call
name: (identifier) @local.definition)))
(assignment left: ((field_expression . [(function_call
name: (identifier) @local.definition)
(identifier) @local.definition])))
(assignment left: (_) @local.definition)
(assignment (multioutput_variable (_) @local.definition))
(iterator . (identifier) @local.definition)
(global_operator (identifier) @local.definition)
(persistent_operator (identifier) @local.definition)
(catch_clause (identifier) @local.definition)
(identifier) @local.reference

@ -0,0 +1,9 @@
(arguments ((_) @parameter.inside . ","? @parameter.around) @parameter.around)
(function_arguments ((_) @parameter.inside . ","? @parameter.around) @parameter.around)
(lambda expression: (_) @function.inside) @function.around
(function_definition (block) @function.inside) @function.around
(class_definition) @class.inside @class.around
(comment) @comment.inside @comment.around

@ -20,7 +20,7 @@
) )
(record_operand (atom (ident) @variable)) (record_operand (atom (ident) @variable))
(let_expr (let_in_block
"let" @keyword "let" @keyword
"rec"? @keyword "rec"? @keyword
pat: (pattern pat: (pattern
@ -53,7 +53,7 @@
(interpolation_end) @punctuation.bracket (interpolation_end) @punctuation.bracket
["forall" "default" "doc"] @keyword ["forall" "default" "doc"] @keyword
["if" "then" "else" "switch"] @keyword.control.conditional ["if" "then" "else" "match"] @keyword.control.conditional
"import" @keyword.control.import "import" @keyword.control.import
(infix_expr (infix_expr

@ -1,7 +1,7 @@
[ [
(fun_expr) (fun_expr)
(let_expr) (let_expr)
(switch_expr) (match_expr)
(ite_expr) (ite_expr)
(uni_record) (uni_record)

@ -0,0 +1,3 @@
(annot_atom doc: (static_string)
@injection.content
(#set! injection.language "markdown"))

@ -0,0 +1,2 @@
((comment) @injection.content
(#set! injection.language "comment"))

@ -0,0 +1,29 @@
(template_body) @local.scope
(lambda_expression) @local.scope
(function_declaration
name: (identifier) @local.definition) @local.scope
(function_definition
name: (identifier) @local.definition)
(parameter
name: (identifier) @local.definition)
(binding
name: (identifier) @local.definition)
(val_definition
pattern: (identifier) @local.definition)
(var_definition
pattern: (identifier) @local.definition)
(val_declaration
name: (identifier) @local.definition)
(var_declaration
name: (identifier) @local.definition)
(identifier) @local.reference

Some files were not shown because too many files have changed in this diff Show More

Loading…
Cancel
Save