rebase from latest

pull/8675/merge^2
mattwparas 7 months ago
commit ce3959aef8

@ -7,6 +7,14 @@ updates:
directory: "/" directory: "/"
schedule: schedule:
interval: "weekly" interval: "weekly"
groups:
tree-sitter:
patterns:
- "tree-sitter*"
rust-dependencies:
update-types:
- "minor"
- "patch"
- package-ecosystem: "github-actions" - package-ecosystem: "github-actions"
directory: "/" directory: "/"

@ -12,6 +12,7 @@ jobs:
check: check:
name: Check (msrv) name: Check (msrv)
runs-on: ubuntu-latest runs-on: ubuntu-latest
if: github.repository == 'helix-editor/helix' || github.event_name != 'schedule'
steps: steps:
- name: Checkout sources - name: Checkout sources
uses: actions/checkout@v4 uses: actions/checkout@v4
@ -31,6 +32,7 @@ jobs:
test: test:
name: Test Suite name: Test Suite
runs-on: ${{ matrix.os }} runs-on: ${{ matrix.os }}
if: github.repository == 'helix-editor/helix' || github.event_name != 'schedule'
env: env:
RUST_BACKTRACE: 1 RUST_BACKTRACE: 1
HELIX_LOG_LEVEL: info HELIX_LOG_LEVEL: info
@ -65,6 +67,7 @@ jobs:
lints: lints:
name: Lints name: Lints
runs-on: ubuntu-latest runs-on: ubuntu-latest
if: github.repository == 'helix-editor/helix' || github.event_name != 'schedule'
steps: steps:
- name: Checkout sources - name: Checkout sources
uses: actions/checkout@v4 uses: actions/checkout@v4
@ -92,6 +95,7 @@ jobs:
docs: docs:
name: Docs name: Docs
runs-on: ubuntu-latest runs-on: ubuntu-latest
if: github.repository == 'helix-editor/helix' || github.event_name != 'schedule'
steps: steps:
- name: Checkout sources - name: Checkout sources
uses: actions/checkout@v4 uses: actions/checkout@v4

@ -14,7 +14,7 @@ jobs:
uses: actions/checkout@v4 uses: actions/checkout@v4
- name: Install nix - name: Install nix
uses: cachix/install-nix-action@v25 uses: cachix/install-nix-action@v26
- name: Authenticate with Cachix - name: Authenticate with Cachix
uses: cachix/cachix-action@v14 uses: cachix/cachix-action@v14

@ -14,7 +14,7 @@ jobs:
- uses: actions/checkout@v4 - uses: actions/checkout@v4
- name: Setup mdBook - name: Setup mdBook
uses: peaceiris/actions-mdbook@v1 uses: peaceiris/actions-mdbook@v2
with: with:
mdbook-version: 'latest' mdbook-version: 'latest'
# mdbook-version: '0.4.8' # mdbook-version: '0.4.8'
@ -27,14 +27,14 @@ jobs:
echo "OUTDIR=$OUTDIR" >> $GITHUB_ENV echo "OUTDIR=$OUTDIR" >> $GITHUB_ENV
- name: Deploy stable - name: Deploy stable
uses: peaceiris/actions-gh-pages@v3 uses: peaceiris/actions-gh-pages@v4
if: startswith(github.ref, 'refs/tags/') if: startswith(github.ref, 'refs/tags/')
with: with:
github_token: ${{ secrets.GITHUB_TOKEN }} github_token: ${{ secrets.GITHUB_TOKEN }}
publish_dir: ./book/book publish_dir: ./book/book
- name: Deploy - name: Deploy
uses: peaceiris/actions-gh-pages@v3 uses: peaceiris/actions-gh-pages@v4
with: with:
github_token: ${{ secrets.GITHUB_TOKEN }} github_token: ${{ secrets.GITHUB_TOKEN }}
publish_dir: ./book/book publish_dir: ./book/book

@ -1,3 +1,256 @@
# 24.03 (2024-03-30)
As always, a big thank you to all of the contributors! This release saw changes from 125 contributors.
Breaking changes:
- `suffix` file-types in the `file-types` key in language configuration have been removed ([#8006](https://github.com/helix-editor/helix/pull/8006))
- The `file-types` key in language configuration no longer matches full filenames without a glob pattern ([#8006](https://github.com/helix-editor/helix/pull/8006))
Features:
- Open URLs with the `goto_file` command ([#5820](https://github.com/helix-editor/helix/pull/5820))
- Support drawing a border around popups and menus ([#4313](https://github.com/helix-editor/helix/pull/4313), [#9508](https://github.com/helix-editor/helix/pull/9508))
- Track long lived diagnostic sources like Clippy or `rustc` ([#6447](https://github.com/helix-editor/helix/pull/6447), [#9280](https://github.com/helix-editor/helix/pull/9280))
- This improves the handling of diagnostics from sources that only update the diagnostic positions on save.
- Add support for LSP `window/showDocument` requests ([#8865](https://github.com/helix-editor/helix/pull/8865))
- Refactor ad-hoc hooks to use a new generic event system ([#8021](https://github.com/helix-editor/helix/pull/8021), [#9668](https://github.com/helix-editor/helix/pull/9668), [#9660](https://github.com/helix-editor/helix/pull/9660))
- This improves the behavior of autocompletions. For example navigating in insert mode no longer automatically triggers completions.
- Allow using globs in the language configuration `file-types` key ([#8006](https://github.com/helix-editor/helix/pull/8006))
- Allow specifying required roots for situational LSP activation ([#8696](https://github.com/helix-editor/helix/pull/8696))
- Extend selections using mouse clicks in select mode ([#5436](https://github.com/helix-editor/helix/pull/5436))
- Toggle block comments ([#4718](https://github.com/helix-editor/helix/pull/4718), [#9894](https://github.com/helix-editor/helix/pull/9894))
- Support LSP diagnostic tags ([#9780](https://github.com/helix-editor/helix/pull/9780))
- Add a `file-absolute-path` statusline element ([#4535](https://github.com/helix-editor/helix/pull/4535))
- Cross injection layers in tree-sitter motions (`A-p`/`A-o`/`A-i`/`A-n`) ([#5176](https://github.com/helix-editor/helix/pull/5176))
- Add a Amp-editor-like jumping command ([#8875](https://github.com/helix-editor/helix/pull/8875))
Commands:
- `:move` - move buffers with LSP support ([#8584](https://github.com/helix-editor/helix/pull/8584))
- Also see [#8949](https://github.com/helix-editor/helix/pull/8949) which made path changes conform to the LSP spec and fixed the behavior of this command.
- `page_cursor_up`, `page_cursor_down`, `page_cursor_half_up`, `page_cursor_half_down` - commands for scrolling the cursor and page together ([#8015](https://github.com/helix-editor/helix/pull/8015))
- `:yank-diagnostic` - yank the diagnostic(s) under the primary cursor ([#9640](https://github.com/helix-editor/helix/pull/9640))
- `select_line_above` / `select_line_below` - extend or shrink a selection based on the direction and anchor ([#9080](https://github.com/helix-editor/helix/pull/9080))
Usability improvements:
- Make `roots` key of `[[language]]` entries in `languages.toml` configuration optional ([#8803](https://github.com/helix-editor/helix/pull/8803))
- Exit select mode in commands that modify the buffer ([#8689](https://github.com/helix-editor/helix/pull/8689))
- Use crossterm cursor when out of focus ([#6858](https://github.com/helix-editor/helix/pull/6858), [#8934](https://github.com/helix-editor/helix/pull/8934))
- Join empty lines with only one space in `join_selections` ([#8989](https://github.com/helix-editor/helix/pull/8989))
- Introduce a hybrid tree-sitter and contextual indentation heuristic ([#8307](https://github.com/helix-editor/helix/pull/8307))
- Allow configuring the indentation heuristic ([#8307](https://github.com/helix-editor/helix/pull/8307))
- Check for LSP rename support before showing rename prompt ([#9277](https://github.com/helix-editor/helix/pull/9277))
- Normalize `S-<lower-ascii>` keymaps to uppercase ascii ([#9213](https://github.com/helix-editor/helix/pull/9213))
- Add formatter status to `--health` output ([#7986](https://github.com/helix-editor/helix/pull/7986))
- Change path normalization strategy to not resolve symlinks ([#9330](https://github.com/helix-editor/helix/pull/9330))
- Select subtree within injections in `:tree-sitter-subtree` ([#9309](https://github.com/helix-editor/helix/pull/9309))
- Use tilde expansion and normalization for `$HELIX_RUNTIME` paths ([1bc7aac](https://github.com/helix-editor/helix/commit/1bc7aac))
- Improve failure message for LSP goto references ([#9382](https://github.com/helix-editor/helix/pull/9382))
- Use injection syntax trees for bracket matching ([5e0b3cc](https://github.com/helix-editor/helix/commit/5e0b3cc))
- Respect injections in `:tree-sitter-highlight-name` ([8b6565c](https://github.com/helix-editor/helix/commit/8b6565c))
- Respect injections in `move_parent_node_end` ([035b8ea](https://github.com/helix-editor/helix/commit/035b8ea))
- Use `gix` pipeline filter instead of manual CRLF implementation ([#9503](https://github.com/helix-editor/helix/pull/9503))
- Follow Neovim's truecolor detection ([#9577](https://github.com/helix-editor/helix/pull/9577))
- Reload language configuration with `:reload`, SIGHUP ([#9415](https://github.com/helix-editor/helix/pull/9415))
- Allow numbers as bindings ([#8471](https://github.com/helix-editor/helix/pull/8471), [#9887](https://github.com/helix-editor/helix/pull/9887))
- Respect undercurl config when terminfo is not available ([#9897](https://github.com/helix-editor/helix/pull/9897))
- Ignore `.pijul`, `.hg`, `.jj` in addition to `.git` in file pickers configured to show hidden files ([#9935](https://github.com/helix-editor/helix/pull/9935))
- Add completion for registers to `:clear-register` and `:yank-diagnostic` ([#9936](https://github.com/helix-editor/helix/pull/9936))
- Repeat last motion for goto next/prev diagnostic ([#9966](https://github.com/helix-editor/helix/pull/9966))
- Allow configuring a character to use when rendering narrow no-breaking space ([#9604](https://github.com/helix-editor/helix/pull/9604))
- Switch to a streaming regex engine (regex-cursor crate) to significantly speed up regex-based commands and features ([#9422](https://github.com/helix-editor/helix/pull/9422), [#9756](https://github.com/helix-editor/helix/pull/9756), [#9891](https://github.com/helix-editor/helix/pull/9891))
Fixes:
- Swap `*` and `+` registers ([#8703](https://github.com/helix-editor/helix/pull/8703), [#8708](https://github.com/helix-editor/helix/pull/8708))
- Use terminfo to reset terminal cursor style ([#8591](https://github.com/helix-editor/helix/pull/8591))
- Fix precedence of `@align` captures in indentat computation ([#8659](https://github.com/helix-editor/helix/pull/8659))
- Only render the preview if a Picker has a preview function ([#8667](https://github.com/helix-editor/helix/pull/8667))
- Fix the precedence of `ui.virtual.whitespace` ([#8750](https://github.com/helix-editor/helix/pull/8750), [#8879](https://github.com/helix-editor/helix/pull/8879))
- Fix crash in `:indent-style` ([#9087](https://github.com/helix-editor/helix/pull/9087))
- Fix `didSave` text inclusion when sync capability is a kind variant ([#9101](https://github.com/helix-editor/helix/pull/9101))
- Update the history of newly focused views ([#9271](https://github.com/helix-editor/helix/pull/9271))
- Initialize diagnostics when opening a document ([#8873](https://github.com/helix-editor/helix/pull/8873))
- Sync views when applying edits to unfocused views ([#9173](https://github.com/helix-editor/helix/pull/9173))
- This fixes crashes that could occur from LSP workspace edits or `:write-all`.
- Treat non-numeric `+arg`s passed in the CLI args as filenames ([#9333](https://github.com/helix-editor/helix/pull/9333))
- Fix crash when using `mm` on an empty plaintext file ([2fb7e50](https://github.com/helix-editor/helix/commit/2fb7e50))
- Ignore empty tree-sitter nodes in match bracket ([445f7a2](https://github.com/helix-editor/helix/commit/445f7a2))
- Exit a language server if it sends a message with invalid JSON ([#9332](https://github.com/helix-editor/helix/pull/9332))
- Handle failures to enable bracketed paste ([#9353](https://github.com/helix-editor/helix/pull/9353))
- Gate all captures in a pattern behind `#is-not? local` predicates ([#9390](https://github.com/helix-editor/helix/pull/9390))
- Make path changes LSP spec conformant ([#8949](https://github.com/helix-editor/helix/pull/8949))
- Use range positions to determine `insert_newline` motion ([#9448](https://github.com/helix-editor/helix/pull/9448))
- Fix division by zero when prompt completion area is too small ([#9524](https://github.com/helix-editor/helix/pull/9524))
- Add changes to history in clipboard replacement typable commands ([#9625](https://github.com/helix-editor/helix/pull/9625))
- Fix a crash in DAP with an unspecified `line` in breakpoints ([#9632](https://github.com/helix-editor/helix/pull/9632))
- Fix space handling for filenames in bash completion ([#9702](https://github.com/helix-editor/helix/pull/9702), [#9708](https://github.com/helix-editor/helix/pull/9708))
- Key diagnostics off of paths instead of LSP URIs ([#7367](https://github.com/helix-editor/helix/pull/7367))
- Fix panic when using `join_selections_space` ([#9783](https://github.com/helix-editor/helix/pull/9783))
- Fix panic when using `surround_replace`, `surround_delete` ([#9796](https://github.com/helix-editor/helix/pull/9796))
- Fix panic in `surround_replace`, `surround_delete` with nested surrounds and multiple cursors ([#9815](https://github.com/helix-editor/helix/pull/9815))
- Fix panic in `select_textobject_around` ([#9832](https://github.com/helix-editor/helix/pull/9832))
- Don't stop reloading documents when reloading fails in `:reload-all` ([#9870](https://github.com/helix-editor/helix/pull/9870))
- Prevent `shell_keep_pipe` from stopping on nonzero exit status codes ([#9817](https://github.com/helix-editor/helix/pull/9817))
Themes:
- Add `gruber-dark` ([#8598](https://github.com/helix-editor/helix/pull/8598))
- Update `everblush` ([#8705](https://github.com/helix-editor/helix/pull/8705))
- Update `papercolor` ([#8718](https://github.com/helix-editor/helix/pull/8718), [#8827](https://github.com/helix-editor/helix/pull/8827))
- Add `polmandres` ([#8759](https://github.com/helix-editor/helix/pull/8759))
- Add `starlight` ([#8787](https://github.com/helix-editor/helix/pull/8787))
- Update `naysayer` ([#8838](https://github.com/helix-editor/helix/pull/8838))
- Add modus operandi themes ([#8728](https://github.com/helix-editor/helix/pull/8728), [#9912](https://github.com/helix-editor/helix/pull/9912))
- Update `rose_pine` ([#8946](https://github.com/helix-editor/helix/pull/8946))
- Update `darcula` ([#8738](https://github.com/helix-editor/helix/pull/8738), [#9002](https://github.com/helix-editor/helix/pull/9002), [#9449](https://github.com/helix-editor/helix/pull/9449), [#9588](https://github.com/helix-editor/helix/pull/9588))
- Add modus vivendi themes ([#8894](https://github.com/helix-editor/helix/pull/8894), [#9912](https://github.com/helix-editor/helix/pull/9912))
- Add `horizon-dark` ([#9008](https://github.com/helix-editor/helix/pull/9008), [#9493](https://github.com/helix-editor/helix/pull/9493))
- Update `noctis` ([#9123](https://github.com/helix-editor/helix/pull/9123))
- Update `nord` ([#9135](https://github.com/helix-editor/helix/pull/9135))
- Update monokai pro themes ([#9148](https://github.com/helix-editor/helix/pull/9148))
- Update tokyonight themes ([#9099](https://github.com/helix-editor/helix/pull/9099), [#9724](https://github.com/helix-editor/helix/pull/9724), [#9789](https://github.com/helix-editor/helix/pull/9789))
- Add `ttox` ([#8524](https://github.com/helix-editor/helix/pull/8524))
- Add `voxed` ([#9164](https://github.com/helix-editor/helix/pull/9164))
- Update `sonokai` ([#9370](https://github.com/helix-editor/helix/pull/9370), [#9376](https://github.com/helix-editor/helix/pull/9376), [#5379](https://github.com/helix-editor/helix/pull/5379))
- Update `onedark`, `onedarker` ([#9397](https://github.com/helix-editor/helix/pull/9397))
- Update `cyan_light` ([#9375](https://github.com/helix-editor/helix/pull/9375), [#9688](https://github.com/helix-editor/helix/pull/9688))
- Add `gruvbox_light_soft`, `gruvbox_light_hard` ([#9266](https://github.com/helix-editor/helix/pull/9266))
- Update GitHub themes ([#9487](https://github.com/helix-editor/helix/pull/9487))
- Add `term16_dark`, `term16_light` ([#9477](https://github.com/helix-editor/helix/pull/9477))
- Update Zed themes ([#9544](https://github.com/helix-editor/helix/pull/9544), [#9549](https://github.com/helix-editor/helix/pull/9549))
- Add `curzon` ([#9553](https://github.com/helix-editor/helix/pull/9553))
- Add `monokai_soda` ([#9651](https://github.com/helix-editor/helix/pull/9651))
- Update catppuccin themes ([#9859](https://github.com/helix-editor/helix/pull/9859))
- Update `rasmus` ([#9939](https://github.com/helix-editor/helix/pull/9939))
- Update `dark_plus` ([#9949](https://github.com/helix-editor/helix/pull/9949), [628dcd5](https://github.com/helix-editor/helix/commit/628dcd5))
- Update gruvbox themes ([#9960](https://github.com/helix-editor/helix/pull/9960))
- Add jump label theming to `dracula` ([#9973](https://github.com/helix-editor/helix/pull/9973))
- Add jump label theming to `horizon-dark` ([#9984](https://github.com/helix-editor/helix/pull/9984))
- Add jump label theming to catppuccin themes ([2178adf](https://github.com/helix-editor/helix/commit/2178adf), [#9983](https://github.com/helix-editor/helix/pull/9983))
- Add jump label theming to `onedark` themes ([da2dec1](https://github.com/helix-editor/helix/commit/da2dec1))
- Add jump label theming to rose-pine themes ([#9981](https://github.com/helix-editor/helix/pull/9981))
- Add jump label theming to Nord themes ([#10008](https://github.com/helix-editor/helix/pull/10008))
- Add jump label theming to Monokai themes ([#10009](https://github.com/helix-editor/helix/pull/10009))
- Add jump label theming to gruvbox themes ([#10012](https://github.com/helix-editor/helix/pull/10012))
- Add jump label theming to `kanagawa` ([#10030](https://github.com/helix-editor/helix/pull/10030))
- Update material themes ([#10043](https://github.com/helix-editor/helix/pull/10043))
- Add `jetbrains_dark` ([#9967](https://github.com/helix-editor/helix/pull/9967))
New languages:
- Typst ([#7474](https://github.com/helix-editor/helix/pull/7474))
- LPF ([#8536](https://github.com/helix-editor/helix/pull/8536))
- GN ([#6969](https://github.com/helix-editor/helix/pull/6969))
- DBML ([#8860](https://github.com/helix-editor/helix/pull/8860))
- log ([#8916](https://github.com/helix-editor/helix/pull/8916))
- Janet ([#9081](https://github.com/helix-editor/helix/pull/9081), [#9247](https://github.com/helix-editor/helix/pull/9247))
- Agda ([#8285](https://github.com/helix-editor/helix/pull/8285))
- Avro ([#9113](https://github.com/helix-editor/helix/pull/9113))
- Smali ([#9089](https://github.com/helix-editor/helix/pull/9089))
- HOCON ([#9203](https://github.com/helix-editor/helix/pull/9203))
- Tact ([#9512](https://github.com/helix-editor/helix/pull/9512))
- PKL ([#9515](https://github.com/helix-editor/helix/pull/9515))
- CEL ([#9296](https://github.com/helix-editor/helix/pull/9296))
- SpiceDB ([#9296](https://github.com/helix-editor/helix/pull/9296))
- Hoon ([#9190](https://github.com/helix-editor/helix/pull/9190))
- DockerCompose ([#9661](https://github.com/helix-editor/helix/pull/9661), [#9916](https://github.com/helix-editor/helix/pull/9916))
- Groovy ([#9350](https://github.com/helix-editor/helix/pull/9350), [#9681](https://github.com/helix-editor/helix/pull/9681), [#9677](https://github.com/helix-editor/helix/pull/9677))
- FIDL ([#9713](https://github.com/helix-editor/helix/pull/9713))
- Powershell ([#9827](https://github.com/helix-editor/helix/pull/9827))
- ld ([#9835](https://github.com/helix-editor/helix/pull/9835))
- Hyperland config ([#9899](https://github.com/helix-editor/helix/pull/9899))
- JSONC ([#9906](https://github.com/helix-editor/helix/pull/9906))
- PHP Blade ([#9513](https://github.com/helix-editor/helix/pull/9513))
- SuperCollider ([#9329](https://github.com/helix-editor/helix/pull/9329))
- Koka ([#8727](https://github.com/helix-editor/helix/pull/8727))
- PKGBUILD ([#9909](https://github.com/helix-editor/helix/pull/9909), [#9943](https://github.com/helix-editor/helix/pull/9943))
- Ada ([#9908](https://github.com/helix-editor/helix/pull/9908))
- Helm charts ([#9900](https://github.com/helix-editor/helix/pull/9900))
- Ember.js templates ([#9902](https://github.com/helix-editor/helix/pull/9902))
- Ohm ([#9991](https://github.com/helix-editor/helix/pull/9991))
Updated languages and queries:
- Add HTML injection queries for Rust ([#8603](https://github.com/helix-editor/helix/pull/8603))
- Switch to tree-sitter-ron for RON ([#8624](https://github.com/helix-editor/helix/pull/8624))
- Update and improve comment highlighting ([#8564](https://github.com/helix-editor/helix/pull/8564), [#9253](https://github.com/helix-editor/helix/pull/9253), [#9800](https://github.com/helix-editor/helix/pull/9800), [#10014](https://github.com/helix-editor/helix/pull/10014))
- Highlight type parameters in Rust ([#8660](https://github.com/helix-editor/helix/pull/8660))
- Change KDL tree-sitter parsers ([#8652](https://github.com/helix-editor/helix/pull/8652))
- Update tree-sitter-markdown ([#8721](https://github.com/helix-editor/helix/pull/8721), [#10039](https://github.com/helix-editor/helix/pull/10039))
- Update tree-sitter-purescript ([#8712](https://github.com/helix-editor/helix/pull/8712))
- Add type parameter highlighting to TypeScript, Go, Haskell, OCaml and Kotlin ([#8718](https://github.com/helix-editor/helix/pull/8718))
- Add indentation queries for Scheme and lisps using tree-sitter-scheme ([#8720](https://github.com/helix-editor/helix/pull/8720))
- Recognize `meson_options.txt` as Meson ([#8794](https://github.com/helix-editor/helix/pull/8794))
- Add language server configuration for Nushell ([#8878](https://github.com/helix-editor/helix/pull/8878))
- Recognize `musicxml` as XML ([#8935](https://github.com/helix-editor/helix/pull/8935))
- Update tree-sitter-rescript ([#8962](https://github.com/helix-editor/helix/pull/8962))
- Update tree-sitter-python ([#8976](https://github.com/helix-editor/helix/pull/8976))
- Recognize `.envrc.local` and `.envrc.private` as env ([#8988](https://github.com/helix-editor/helix/pull/8988))
- Update tree-sitter-gleam ([#9003](https://github.com/helix-editor/helix/pull/9003), [9ceeea5](https://github.com/helix-editor/helix/commit/9ceeea5))
- Update tree-sitter-d ([#9021](https://github.com/helix-editor/helix/pull/9021))
- Fix R-markdown language name for LSP detection ([#9012](https://github.com/helix-editor/helix/pull/9012))
- Add haskell-language-server LSP configuration ([#9111](https://github.com/helix-editor/helix/pull/9111))
- Recognize `glif` as XML ([#9130](https://github.com/helix-editor/helix/pull/9130))
- Recognize `.prettierrc` as JSON ([#9214](https://github.com/helix-editor/helix/pull/9214))
- Add auto-pairs configuration for scheme ([#9232](https://github.com/helix-editor/helix/pull/9232))
- Add textobject queries for Scala ([#9191](https://github.com/helix-editor/helix/pull/9191))
- Add textobject queries for Protobuf ([#9184](https://github.com/helix-editor/helix/pull/9184))
- Update tree-sitter-wren ([#8544](https://github.com/helix-editor/helix/pull/8544))
- Add `spago.yaml` as an LSP root for PureScript ([#9362](https://github.com/helix-editor/helix/pull/9362))
- Improve highlight and indent queries for Bash, Make and CSS ([#9393](https://github.com/helix-editor/helix/pull/9393))
- Update tree-sitter-scala ([#9348](https://github.com/helix-editor/helix/pull/9348), [#9340](https://github.com/helix-editor/helix/pull/9340), [#9475](https://github.com/helix-editor/helix/pull/9475))
- Recognize `.bash_history` as Bash ([#9401](https://github.com/helix-editor/helix/pull/9401))
- Recognize Helix ignore files as ignore ([#9447](https://github.com/helix-editor/helix/pull/9447))
- Inject SQL into Scala SQL strings ([#9428](https://github.com/helix-editor/helix/pull/9428))
- Update gdscript textobjects ([#9288](https://github.com/helix-editor/helix/pull/9288))
- Update Go queries ([#9510](https://github.com/helix-editor/helix/pull/9510), [#9525](https://github.com/helix-editor/helix/pull/9525))
- Update tree-sitter-nushell ([#9502](https://github.com/helix-editor/helix/pull/9502))
- Update tree-sitter-unison, add indent queries ([#9505](https://github.com/helix-editor/helix/pull/9505))
- Update tree-sitter-slint ([#9551](https://github.com/helix-editor/helix/pull/9551), [#9698](https://github.com/helix-editor/helix/pull/9698))
- Update tree-sitter-swift ([#9586](https://github.com/helix-editor/helix/pull/9586))
- Add `fish_indent` as formatter for fish ([78ed3ad](https://github.com/helix-editor/helix/commit/78ed3ad))
- Recognize `zon` as Zig ([#9582](https://github.com/helix-editor/helix/pull/9582))
- Add a formatter for Odin ([#9537](https://github.com/helix-editor/helix/pull/9537))
- Update tree-sitter-erlang ([#9627](https://github.com/helix-editor/helix/pull/9627), [fdcd461](https://github.com/helix-editor/helix/commit/fdcd461))
- Capture Rust fields as argument textobjects ([#9637](https://github.com/helix-editor/helix/pull/9637))
- Improve Dart textobjects ([#9644](https://github.com/helix-editor/helix/pull/9644))
- Recognize `tmux.conf` as a bash file-type ([#9653](https://github.com/helix-editor/helix/pull/9653))
- Add textobjects queries for Nix ([#9659](https://github.com/helix-editor/helix/pull/9659))
- Add textobjects queries for HCL ([#9658](https://github.com/helix-editor/helix/pull/9658))
- Recognize osm and osc extensions as XML ([#9697](https://github.com/helix-editor/helix/pull/9697))
- Update tree-sitter-sql ([#9634](https://github.com/helix-editor/helix/pull/9634))
- Recognize pde Processing files as Java ([#9741](https://github.com/helix-editor/helix/pull/9741))
- Update tree-sitter-lua ([#9727](https://github.com/helix-editor/helix/pull/9727))
- Switch tree-sitter-nim parsers ([#9722](https://github.com/helix-editor/helix/pull/9722))
- Recognize GTK builder ui files as XML ([#9754](https://github.com/helix-editor/helix/pull/9754))
- Add configuration for markdown-oxide language server ([#9758](https://github.com/helix-editor/helix/pull/9758))
- Add a shebang for elvish ([#9779](https://github.com/helix-editor/helix/pull/9779))
- Fix precedence of Svelte TypeScript injection ([#9777](https://github.com/helix-editor/helix/pull/9777))
- Recognize common Dockerfile file types ([#9772](https://github.com/helix-editor/helix/pull/9772))
- Recognize NUON files as Nu ([#9839](https://github.com/helix-editor/helix/pull/9839))
- Add textobjects for Java native functions and constructors ([#9806](https://github.com/helix-editor/helix/pull/9806))
- Fix "braket" typeo in JSX highlights ([#9910](https://github.com/helix-editor/helix/pull/9910))
- Update tree-sitter-hurl ([#9775](https://github.com/helix-editor/helix/pull/9775))
- Add textobjects queries for Vala ([#8541](https://github.com/helix-editor/helix/pull/8541))
- Update tree-sitter-git-config ([9610254](https://github.com/helix-editor/helix/commit/9610254))
- Recognize 'mmd' as Mermaid ([459eb9a](https://github.com/helix-editor/helix/commit/459eb9a))
- Highlight Rust extern crate aliases ([c099dde](https://github.com/helix-editor/helix/commit/c099dde))
- Improve parameter highlighting in C++ ([f5d95de](https://github.com/helix-editor/helix/commit/f5d95de))
- Recognize 'rclone.conf' as INI ([#9959](https://github.com/helix-editor/helix/pull/9959))
- Add injections for GraphQL and ERB in Ruby heredocs ([#10036](https://github.com/helix-editor/helix/pull/10036))
- Add `main.odin` to Odin LSP roots ([#9968](https://github.com/helix-editor/helix/pull/9968))
Packaging:
- Allow user overlays in Nix grammars build ([#8749](https://github.com/helix-editor/helix/pull/8749))
- Set Cargo feature resolver to v2 ([#8917](https://github.com/helix-editor/helix/pull/8917))
- Use workspace inheritance for common Cargo metadata ([#8925](https://github.com/helix-editor/helix/pull/8925))
- Remove sourcehut-based tree-sitter grammars from default build ([#9316](https://github.com/helix-editor/helix/pull/9316), [#9326](https://github.com/helix-editor/helix/pull/9326))
- Add icon to Windows executable ([#9104](https://github.com/helix-editor/helix/pull/9104))
# 23.10 (2023-10-24) # 23.10 (2023-10-24)
A big shout out to all the contributors! We had 118 contributors in this release. A big shout out to all the contributors! We had 118 contributors in this release.

689
Cargo.lock generated

File diff suppressed because it is too large Load Diff

@ -23,8 +23,9 @@ default-members = [
# If working locally, use the local path dependency # If working locally, use the local path dependency
# steel-core = { path = "../../steel/crates/steel-core", version = "0.6.0", features = ["anyhow", "dylibs"] } # steel-core = { path = "../../steel/crates/steel-core", version = "0.6.0", features = ["anyhow", "dylibs"] }
steel-core = { git = "https://github.com/mattwparas/steel.git", version = "0.6.0", features = ["anyhow", "dylibs"] } steel-core = { git = "https://github.com/mattwparas/steel.git", version = "0.6.0", features = ["anyhow", "dylibs"] }
tree-sitter = { version = "0.20", git = "https://github.com/tree-sitter/tree-sitter", rev = "660481dbf71413eba5a928b0b0ab8da50c1109e0" } tree-sitter = { version = "0.22" }
nucleo = "0.2.0" nucleo = "0.2.0"
slotmap = "1.0.7"
[profile.release] [profile.release]
lto = "thin" lto = "thin"
@ -44,7 +45,7 @@ package.helix-tui.opt-level = 2
package.helix-term.opt-level = 2 package.helix-term.opt-level = 2
[workspace.package] [workspace.package]
version = "23.10.0" version = "24.3.0"
edition = "2021" edition = "2021"
authors = ["Blaž Hrastnik <blaz@mxxn.io>"] authors = ["Blaž Hrastnik <blaz@mxxn.io>"]
categories = ["editor"] categories = ["editor"]

@ -28,6 +28,8 @@
"label" = "magenta" "label" = "magenta"
"namespace" = "magenta" "namespace" = "magenta"
"ui.help" = { fg = "white", bg = "black" } "ui.help" = { fg = "white", bg = "black" }
"ui.virtual.jump-label" = { fg = "blue", modifiers = ["bold", "underlined"] }
"ui.virtual.ruler" = { bg = "black" }
"markup.heading" = "blue" "markup.heading" = "blue"
"markup.list" = "red" "markup.list" = "red"

@ -68,6 +68,7 @@ Its settings will be merged with the configuration directory `config.toml` and t
| `insert-final-newline` | Whether to automatically insert a trailing line-ending on write if missing | `true` | | `insert-final-newline` | Whether to automatically insert a trailing line-ending on write if missing | `true` |
| `popup-border` | Draw border around `popup`, `menu`, `all`, or `none` | `none` | | `popup-border` | Draw border around `popup`, `menu`, `all`, or `none` | `none` |
| `indent-heuristic` | How the indentation for a newly inserted line is computed: `simple` just copies the indentation level from the previous line, `tree-sitter` computes the indentation based on the syntax tree and `hybrid` combines both approaches. If the chosen heuristic is not available, a different one will be used as a fallback (the fallback order being `hybrid` -> `tree-sitter` -> `simple`). | `hybrid` | `indent-heuristic` | How the indentation for a newly inserted line is computed: `simple` just copies the indentation level from the previous line, `tree-sitter` computes the indentation based on the syntax tree and `hybrid` combines both approaches. If the chosen heuristic is not available, a different one will be used as a fallback (the fallback order being `hybrid` -> `tree-sitter` -> `simple`). | `hybrid`
| `jump-label-alphabet` | The characters that are used to generate two character jump labels. Characters at the start of the alphabet are used first. | `"abcdefghijklmnopqrstuvwxyz"`
### `[editor.statusline]` Section ### `[editor.statusline]` Section
@ -75,7 +76,7 @@ Allows configuring the statusline at the bottom of the editor.
The configuration distinguishes between three areas of the status line: The configuration distinguishes between three areas of the status line:
`[ ... ... LEFT ... ... | ... ... ... ... CENTER ... ... ... ... | ... ... RIGHT ... ... ]` `[ ... ... LEFT ... ... | ... ... ... CENTER ... ... ... | ... ... RIGHT ... ... ]`
Statusline elements can be defined as follows: Statusline elements can be defined as follows:
@ -108,6 +109,7 @@ The following statusline elements can be configured:
| `mode` | The current editor mode (`mode.normal`/`mode.insert`/`mode.select`) | | `mode` | The current editor mode (`mode.normal`/`mode.insert`/`mode.select`) |
| `spinner` | A progress spinner indicating LSP activity | | `spinner` | A progress spinner indicating LSP activity |
| `file-name` | The path/name of the opened file | | `file-name` | The path/name of the opened file |
| `file-absolute-path` | The absolute path/name of the opened file |
| `file-base-name` | The basename of the opened file | | `file-base-name` | The basename of the opened file |
| `file-modification-indicator` | The indicator to show whether the file is modified (a `[+]` appears when there are unsaved changes) | | `file-modification-indicator` | The indicator to show whether the file is modified (a `[+]` appears when there are unsaved changes) |
| `file-encoding` | The encoding of the opened file if it differs from UTF-8 | | `file-encoding` | The encoding of the opened file if it differs from UTF-8 |
@ -177,7 +179,7 @@ All git related options are only enabled in a git repository.
|`git-ignore` | Enables reading `.gitignore` files | `true` |`git-ignore` | Enables reading `.gitignore` files | `true`
|`git-global` | Enables reading global `.gitignore`, whose path is specified in git's config: `core.excludesfile` option | `true` |`git-global` | Enables reading global `.gitignore`, whose path is specified in git's config: `core.excludesfile` option | `true`
|`git-exclude` | Enables reading `.git/info/exclude` files | `true` |`git-exclude` | Enables reading `.git/info/exclude` files | `true`
|`max-depth` | Set with an integer value for maximum depth to recurse | Defaults to `None`. |`max-depth` | Set with an integer value for maximum depth to recurse | Unset by default
Ignore files can be placed locally as `.ignore` or put in your home directory as `~/.ignore`. They support the usual ignore and negative ignore (unignore) rules used in `.gitignore` files. Ignore files can be placed locally as `.ignore` or put in your home directory as `~/.ignore`. They support the usual ignore and negative ignore (unignore) rules used in `.gitignore` files.
@ -223,7 +225,7 @@ Additionally, this setting can be used in a language config. Unless
the editor setting is `false`, this will override the editor config in the editor setting is `false`, this will override the editor config in
documents with this language. documents with this language.
Example `languages.toml` that adds <> and removes '' Example `languages.toml` that adds `<>` and removes `''`
```toml ```toml
[[language]] [[language]]
@ -253,8 +255,8 @@ Options for rendering whitespace with visible characters. Use `:set whitespace.r
| Key | Description | Default | | Key | Description | Default |
|-----|-------------|---------| |-----|-------------|---------|
| `render` | Whether to render whitespace. May either be `"all"` or `"none"`, or a table with sub-keys `space`, `nbsp`, `tab`, and `newline` | `"none"` | | `render` | Whether to render whitespace. May either be `all` or `none`, or a table with sub-keys `space`, `nbsp`, `nnbsp`, `tab`, and `newline` | `none` |
| `characters` | Literal characters to use when rendering whitespace. Sub-keys may be any of `tab`, `space`, `nbsp`, `newline` or `tabpad` | See example below | | `characters` | Literal characters to use when rendering whitespace. Sub-keys may be any of `tab`, `space`, `nbsp`, `nnbsp`, `newline` or `tabpad` | See example below |
Example Example
@ -265,11 +267,14 @@ render = "all"
[editor.whitespace.render] [editor.whitespace.render]
space = "all" space = "all"
tab = "all" tab = "all"
nbsp = "none"
nnbsp = "none"
newline = "none" newline = "none"
[editor.whitespace.characters] [editor.whitespace.characters]
space = "·" space = "·"
nbsp = "⍽" nbsp = "⍽"
nnbsp = "␣"
tab = "→" tab = "→"
newline = "⏎" newline = "⏎"
tabpad = "·" # Tabs will look like "→···" (depending on tab width) tabpad = "·" # Tabs will look like "→···" (depending on tab width)
@ -340,7 +345,12 @@ Currently unused
#### `[editor.gutters.diff]` Section #### `[editor.gutters.diff]` Section
Currently unused The `diff` gutter option displays colored bars indicating whether a `git` diff represents that a line was added, removed or changed.
These colors are controlled by the theme attributes `diff.plus`, `diff.minus` and `diff.delta`.
Other diff providers will eventually be supported by a future plugin system.
There are currently no options for this section.
#### `[editor.gutters.spacer]` Section #### `[editor.gutters.spacer]` Section
@ -370,8 +380,25 @@ wrap-indicator = "" # set wrap-indicator to "" to hide it
### `[editor.smart-tab]` Section ### `[editor.smart-tab]` Section
Options for navigating and editing using tab key.
| Key | Description | Default | | Key | Description | Default |
|------------|-------------|---------| |------------|-------------|---------|
| `enable` | If set to true, then when the cursor is in a position with non-whitespace to its left, instead of inserting a tab, it will run `move_parent_node_end`. If there is only whitespace to the left, then it inserts a tab as normal. With the default bindings, to explicitly insert a tab character, press Shift-tab. | `true` | | `enable` | If set to true, then when the cursor is in a position with non-whitespace to its left, instead of inserting a tab, it will run `move_parent_node_end`. If there is only whitespace to the left, then it inserts a tab as normal. With the default bindings, to explicitly insert a tab character, press Shift-tab. | `true` |
| `supersede-menu` | Normally, when a menu is on screen, such as when auto complete is triggered, the tab key is bound to cycling through the items. This means when menus are on screen, one cannot use the tab key to trigger the `smart-tab` command. If this option is set to true, the `smart-tab` command always takes precedence, which means one cannot use the tab key to cycle through menu items. One of the other bindings must be used instead, such as arrow keys or `C-n`/`C-p`. | `false` | | `supersede-menu` | Normally, when a menu is on screen, such as when auto complete is triggered, the tab key is bound to cycling through the items. This means when menus are on screen, one cannot use the tab key to trigger the `smart-tab` command. If this option is set to true, the `smart-tab` command always takes precedence, which means one cannot use the tab key to cycle through menu items. One of the other bindings must be used instead, such as arrow keys or `C-n`/`C-p`. | `false` |
Due to lack of support for S-tab in some terminals, the default keybindings don't fully embrace smart-tab editing experience. If you enjoy smart-tab navigation and a terminal that supports the [Enhanced Keyboard protocol](https://github.com/helix-editor/helix/wiki/Terminal-Support#enhanced-keyboard-protocol), consider setting extra keybindings:
```
[keys.normal]
tab = "move_parent_node_end"
S-tab = "move_parent_node_start"
[keys.insert]
S-tab = "move_parent_node_start"
[keys.select]
tab = "extend_parent_node_end"
S-tab = "extend_parent_node_start"
```

@ -1,5 +1,7 @@
| Language | Syntax Highlighting | Treesitter Textobjects | Auto Indent | Default LSP | | Language | Syntax Highlighting | Treesitter Textobjects | Auto Indent | Default LSP |
| --- | --- | --- | --- | --- | | --- | --- | --- | --- | --- |
| ada | ✓ | ✓ | | `ada_language_server`, `ada_language_server` |
| adl | ✓ | ✓ | ✓ | |
| agda | ✓ | | | | | agda | ✓ | | | |
| astro | ✓ | | | | | astro | ✓ | | | |
| awk | ✓ | ✓ | | `awk-language-server` | | awk | ✓ | ✓ | | `awk-language-server` |
@ -8,12 +10,15 @@
| beancount | ✓ | | | | | beancount | ✓ | | | |
| bibtex | ✓ | | | `texlab` | | bibtex | ✓ | | | `texlab` |
| bicep | ✓ | | | `bicep-langserver` | | bicep | ✓ | | | `bicep-langserver` |
| bitbake | ✓ | | | `bitbake-language-server` |
| blade | ✓ | | | |
| blueprint | ✓ | | | `blueprint-compiler` | | blueprint | ✓ | | | `blueprint-compiler` |
| c | ✓ | ✓ | ✓ | `clangd` | | c | ✓ | ✓ | ✓ | `clangd` |
| c-sharp | ✓ | ✓ | | `OmniSharp` | | c-sharp | ✓ | ✓ | | `OmniSharp` |
| cabal | | | | `haskell-language-server-wrapper` | | cabal | | | | `haskell-language-server-wrapper` |
| cairo | ✓ | ✓ | ✓ | `cairo-language-server` | | cairo | ✓ | ✓ | ✓ | `cairo-language-server` |
| capnp | ✓ | | ✓ | | | capnp | ✓ | | ✓ | |
| cel | ✓ | | | |
| clojure | ✓ | | | `clojure-lsp` | | clojure | ✓ | | | `clojure-lsp` |
| cmake | ✓ | ✓ | ✓ | `cmake-language-server` | | cmake | ✓ | ✓ | ✓ | `cmake-language-server` |
| comment | ✓ | | | | | comment | ✓ | | | |
@ -29,9 +34,11 @@
| devicetree | ✓ | | | | | devicetree | ✓ | | | |
| dhall | ✓ | ✓ | | `dhall-lsp-server` | | dhall | ✓ | ✓ | | `dhall-lsp-server` |
| diff | ✓ | | | | | diff | ✓ | | | |
| docker-compose | ✓ | | ✓ | `docker-compose-langserver`, `yaml-language-server` |
| dockerfile | ✓ | | | `docker-langserver` | | dockerfile | ✓ | | | `docker-langserver` |
| dot | ✓ | | | `dot-language-server` | | dot | ✓ | | | `dot-language-server` |
| dtd | ✓ | | | | | dtd | ✓ | | | |
| earthfile | ✓ | ✓ | ✓ | `earthlyls` |
| edoc | ✓ | | | | | edoc | ✓ | | | |
| eex | ✓ | | | | | eex | ✓ | | | |
| ejs | ✓ | | | | | ejs | ✓ | | | |
@ -42,6 +49,7 @@
| erb | ✓ | | | | | erb | ✓ | | | |
| erlang | ✓ | ✓ | | `erlang_ls` | | erlang | ✓ | ✓ | | `erlang_ls` |
| esdl | ✓ | | | | | esdl | ✓ | | | |
| fidl | ✓ | | | |
| fish | ✓ | ✓ | ✓ | | | fish | ✓ | ✓ | ✓ | |
| forth | ✓ | | | `forth-lsp` | | forth | ✓ | | | `forth-lsp` |
| fortran | ✓ | | ✓ | `fortls` | | fortran | ✓ | | ✓ | `fortls` |
@ -55,6 +63,7 @@
| git-ignore | ✓ | | | | | git-ignore | ✓ | | | |
| git-rebase | ✓ | | | | | git-rebase | ✓ | | | |
| gleam | ✓ | ✓ | | `gleam` | | gleam | ✓ | ✓ | | `gleam` |
| glimmer | ✓ | | | `ember-language-server` |
| glsl | ✓ | ✓ | ✓ | | | glsl | ✓ | ✓ | ✓ | |
| gn | ✓ | | | | | gn | ✓ | | | |
| go | ✓ | ✓ | ✓ | `gopls`, `golangci-lint-langserver` | | go | ✓ | ✓ | ✓ | `gopls`, `golangci-lint-langserver` |
@ -62,16 +71,20 @@
| gomod | ✓ | | | `gopls` | | gomod | ✓ | | | `gopls` |
| gotmpl | ✓ | | | `gopls` | | gotmpl | ✓ | | | `gopls` |
| gowork | ✓ | | | `gopls` | | gowork | ✓ | | | `gopls` |
| graphql | ✓ | | | `graphql-lsp` | | graphql | ✓ | ✓ | | `graphql-lsp` |
| groovy | ✓ | | | |
| hare | ✓ | | | | | hare | ✓ | | | |
| haskell | ✓ | ✓ | | `haskell-language-server-wrapper` | | haskell | ✓ | ✓ | | `haskell-language-server-wrapper` |
| haskell-persistent | ✓ | | | | | haskell-persistent | ✓ | | | |
| hcl | ✓ | | ✓ | `terraform-ls` | | hcl | ✓ | | ✓ | `terraform-ls` |
| heex | ✓ | ✓ | | `elixir-ls` | | heex | ✓ | ✓ | | `elixir-ls` |
| helm | ✓ | | | `helm_ls` |
| hocon | ✓ | | ✓ | | | hocon | ✓ | | ✓ | |
| hoon | ✓ | | | |
| hosts | ✓ | | | | | hosts | ✓ | | | |
| html | ✓ | | | `vscode-html-language-server` | | html | ✓ | | | `vscode-html-language-server` |
| hurl | ✓ | | ✓ | | | hurl | ✓ | ✓ | ✓ | |
| hyprlang | ✓ | | ✓ | |
| idris | | | | `idris2-lsp` | | idris | | | | `idris2-lsp` |
| iex | ✓ | | | | | iex | ✓ | | | |
| ini | ✓ | | | | | ini | ✓ | | | |
@ -80,15 +93,19 @@
| javascript | ✓ | ✓ | ✓ | `typescript-language-server` | | javascript | ✓ | ✓ | ✓ | `typescript-language-server` |
| jinja | ✓ | | | | | jinja | ✓ | | | |
| jsdoc | ✓ | | | | | jsdoc | ✓ | | | |
| json | ✓ | | ✓ | `vscode-json-language-server` | | json | ✓ | | ✓ | `vscode-json-language-server` |
| json5 | ✓ | | | | | json5 | ✓ | | | |
| jsonc | ✓ | | ✓ | `vscode-json-language-server` |
| jsonnet | ✓ | | | `jsonnet-language-server` | | jsonnet | ✓ | | | `jsonnet-language-server` |
| jsx | ✓ | ✓ | ✓ | `typescript-language-server` | | jsx | ✓ | ✓ | ✓ | `typescript-language-server` |
| julia | ✓ | ✓ | ✓ | `julia` | | julia | ✓ | ✓ | ✓ | `julia` |
| just | ✓ | ✓ | ✓ | | | just | ✓ | ✓ | ✓ | |
| kdl | ✓ | ✓ | ✓ | | | kdl | ✓ | ✓ | ✓ | |
| koka | ✓ | | ✓ | `koka` |
| kotlin | ✓ | | | `kotlin-language-server` | | kotlin | ✓ | | | `kotlin-language-server` |
| latex | ✓ | ✓ | | `texlab` | | latex | ✓ | ✓ | | `texlab` |
| ld | ✓ | | ✓ | |
| ldif | ✓ | | | |
| lean | ✓ | | | `lean` | | lean | ✓ | | | `lean` |
| ledger | ✓ | | | | | ledger | ✓ | | | |
| llvm | ✓ | ✓ | ✓ | | | llvm | ✓ | ✓ | ✓ | |
@ -99,22 +116,24 @@
| lua | ✓ | ✓ | ✓ | `lua-language-server` | | lua | ✓ | ✓ | ✓ | `lua-language-server` |
| make | ✓ | | ✓ | | | make | ✓ | | ✓ | |
| markdoc | ✓ | | | `markdoc-ls` | | markdoc | ✓ | | | `markdoc-ls` |
| markdown | ✓ | | | `marksman` | | markdown | ✓ | | | `marksman`, `markdown-oxide` |
| markdown.inline | ✓ | | | | | markdown.inline | ✓ | | | |
| matlab | ✓ | ✓ | ✓ | | | matlab | ✓ | ✓ | ✓ | |
| mermaid | ✓ | | | | | mermaid | ✓ | | | |
| meson | ✓ | | ✓ | | | meson | ✓ | | ✓ | |
| mint | | | | `mint` | | mint | | | | `mint` |
| move | ✓ | | | |
| msbuild | ✓ | | ✓ | | | msbuild | ✓ | | ✓ | |
| nasm | ✓ | ✓ | | | | nasm | ✓ | ✓ | | |
| nickel | ✓ | | ✓ | `nls` | | nickel | ✓ | | ✓ | `nls` |
| nim | ✓ | ✓ | ✓ | `nimlangserver` | | nim | ✓ | ✓ | ✓ | `nimlangserver` |
| nix | ✓ | | | `nil` | | nix | ✓ | | | `nil` |
| nu | ✓ | | | `nu` | | nu | ✓ | | | `nu` |
| nunjucks | ✓ | | | | | nunjucks | ✓ | | | |
| ocaml | ✓ | | ✓ | `ocamllsp` | | ocaml | ✓ | | ✓ | `ocamllsp` |
| ocaml-interface | ✓ | | | `ocamllsp` | | ocaml-interface | ✓ | | | `ocamllsp` |
| odin | ✓ | | ✓ | `ols` | | odin | ✓ | | ✓ | `ols` |
| ohm | ✓ | ✓ | ✓ | |
| opencl | ✓ | ✓ | ✓ | `clangd` | | opencl | ✓ | ✓ | ✓ | `clangd` |
| openscad | ✓ | | | `openscad-lsp` | | openscad | ✓ | | | `openscad-lsp` |
| org | ✓ | | | | | org | ✓ | | | |
@ -122,10 +141,15 @@
| passwd | ✓ | | | | | passwd | ✓ | | | |
| pem | ✓ | | | | | pem | ✓ | | | |
| perl | ✓ | ✓ | ✓ | `perlnavigator` | | perl | ✓ | ✓ | ✓ | `perlnavigator` |
| pest | ✓ | ✓ | ✓ | `pest-language-server` |
| php | ✓ | ✓ | ✓ | `intelephense` | | php | ✓ | ✓ | ✓ | `intelephense` |
| php-only | ✓ | | | |
| pkgbuild | ✓ | ✓ | ✓ | `pkgbuild-language-server`, `bash-language-server` |
| pkl | ✓ | | ✓ | |
| po | ✓ | ✓ | | | | po | ✓ | ✓ | | |
| pod | ✓ | | | | | pod | ✓ | | | |
| ponylang | ✓ | ✓ | ✓ | | | ponylang | ✓ | ✓ | ✓ | |
| powershell | ✓ | | | |
| prisma | ✓ | | | `prisma-language-server` | | prisma | ✓ | | | `prisma-language-server` |
| prolog | | | | `swipl` | | prolog | | | | `swipl` |
| protobuf | ✓ | ✓ | ✓ | `bufls`, `pb` | | protobuf | ✓ | ✓ | ✓ | `bufls`, `pb` |
@ -148,35 +172,39 @@
| scala | ✓ | ✓ | ✓ | `metals` | | scala | ✓ | ✓ | ✓ | `metals` |
| scheme | ✓ | | ✓ | | | scheme | ✓ | | ✓ | |
| scss | ✓ | | | `vscode-css-language-server` | | scss | ✓ | | | `vscode-css-language-server` |
| slint | ✓ | | ✓ | `slint-lsp` | | slint | ✓ | | ✓ | `slint-lsp` |
| smali | ✓ | | ✓ | | | smali | ✓ | | ✓ | |
| smithy | ✓ | | | `cs` | | smithy | ✓ | | | `cs` |
| sml | ✓ | | | | | sml | ✓ | | | |
| solidity | ✓ | | | `solc` | | solidity | ✓ | ✓ | | `solc` |
| spicedb | ✓ | | | |
| sql | ✓ | | | | | sql | ✓ | | | |
| sshclientconfig | ✓ | | | | | sshclientconfig | ✓ | | | |
| starlark | ✓ | ✓ | | | | starlark | ✓ | ✓ | | |
| strace | ✓ | | | | | strace | ✓ | | | |
| supercollider | ✓ | | | |
| svelte | ✓ | | ✓ | `svelteserver` | | svelte | ✓ | | ✓ | `svelteserver` |
| sway | ✓ | ✓ | ✓ | `forc` | | sway | ✓ | ✓ | ✓ | `forc` |
| swift | ✓ | | | `sourcekit-lsp` | | swift | ✓ | | | `sourcekit-lsp` |
| t32 | ✓ | | | | | t32 | ✓ | | | |
| tablegen | ✓ | ✓ | ✓ | | | tablegen | ✓ | ✓ | ✓ | |
| tact | ✓ | ✓ | ✓ | |
| task | ✓ | | | | | task | ✓ | | | |
| tcl | ✓ | | ✓ | |
| templ | ✓ | | | `templ` | | templ | ✓ | | | `templ` |
| tfvars | ✓ | | ✓ | `terraform-ls` | | tfvars | ✓ | | ✓ | `terraform-ls` |
| todotxt | ✓ | | | | | todotxt | ✓ | | | |
| toml | ✓ | | | `taplo` | | toml | ✓ | | | `taplo` |
| tsq | ✓ | | | | | tsq | ✓ | | | |
| tsx | ✓ | ✓ | ✓ | `typescript-language-server` | | tsx | ✓ | ✓ | ✓ | `typescript-language-server` |
| twig | ✓ | | | | | twig | ✓ | | | |
| typescript | ✓ | ✓ | ✓ | `typescript-language-server` | | typescript | ✓ | ✓ | ✓ | `typescript-language-server` |
| typst | ✓ | | | `typst-lsp` | | typst | ✓ | | | `tinymist`, `typst-lsp` |
| ungrammar | ✓ | | | | | ungrammar | ✓ | | | |
| unison | ✓ | | | | | unison | ✓ | | | |
| uxntal | ✓ | | | | | uxntal | ✓ | | | |
| v | ✓ | ✓ | ✓ | `v-analyzer` | | v | ✓ | ✓ | ✓ | `v-analyzer` |
| vala | ✓ | | | `vala-language-server` | | vala | ✓ | | | `vala-language-server` |
| verilog | ✓ | ✓ | | `svlangserver` | | verilog | ✓ | ✓ | | `svlangserver` |
| vhdl | ✓ | | | `vhdl_ls` | | vhdl | ✓ | | | `vhdl_ls` |
| vhs | ✓ | | | | | vhs | ✓ | | | |
@ -189,6 +217,7 @@
| wren | ✓ | ✓ | ✓ | | | wren | ✓ | ✓ | ✓ | |
| xit | ✓ | | | | | xit | ✓ | | | |
| xml | ✓ | | ✓ | | | xml | ✓ | | ✓ | |
| xtc | ✓ | | | |
| yaml | ✓ | | ✓ | `yaml-language-server`, `ansible-language-server` | | yaml | ✓ | | ✓ | `yaml-language-server`, `ansible-language-server` |
| yuck | ✓ | | | | | yuck | ✓ | | | |
| zig | ✓ | ✓ | ✓ | `zls` | | zig | ✓ | ✓ | ✓ | `zls` |

@ -86,3 +86,5 @@
| `:clear-register` | Clear given register. If no argument is provided, clear all registers. | | `:clear-register` | Clear given register. If no argument is provided, clear all registers. |
| `:redraw` | Clear and re-render the whole UI | | `:redraw` | Clear and re-render the whole UI |
| `:move` | Move the current buffer and its corresponding file to a different path | | `:move` | Move the current buffer and its corresponding file to a different path |
| `:yank-diagnostic` | Yank diagnostic(s) under primary cursor to register, or clipboard by default |
| `:read`, `:r` | Load a file into buffer |

@ -25,6 +25,8 @@ The following [captures][tree-sitter-captures] are recognized:
| `parameter.inside` | | `parameter.inside` |
| `comment.inside` | | `comment.inside` |
| `comment.around` | | `comment.around` |
| `entry.inside` |
| `entry.around` |
[Example query files][textobject-examples] can be found in the helix GitHub repository. [Example query files][textobject-examples] can be found in the helix GitHub repository.
@ -44,4 +46,4 @@ doesn't make sense in a navigation context.
[tree-sitter-queries]: https://tree-sitter.github.io/tree-sitter/using-parsers#query-syntax [tree-sitter-queries]: https://tree-sitter.github.io/tree-sitter/using-parsers#query-syntax
[tree-sitter-captures]: https://tree-sitter.github.io/tree-sitter/using-parsers#capturing-nodes [tree-sitter-captures]: https://tree-sitter.github.io/tree-sitter/using-parsers#capturing-nodes
[textobject-examples]: https://github.com/search?q=repo%3Ahelix-editor%2Fhelix+filename%3Atextobjects.scm&type=Code&ref=advsearch&l=&l= [textobject-examples]: https://github.com/search?q=repo%3Ahelix-editor%2Fhelix+path%3A%2A%2A/textobjects.scm&type=Code&ref=advsearch&l=&l=

@ -76,6 +76,15 @@ Releases are available in the `extra` repository:
```sh ```sh
sudo pacman -S helix sudo pacman -S helix
``` ```
> 💡 When installed from the `extra` repository, run Helix with `helix` instead of `hx`.
>
> For example:
> ```sh
> helix --health
> ```
> to check health
Additionally, a [helix-git](https://aur.archlinux.org/packages/helix-git/) package is available Additionally, a [helix-git](https://aur.archlinux.org/packages/helix-git/) package is available
in the AUR, which builds the master branch. in the AUR, which builds the master branch.

@ -13,6 +13,8 @@
- [Window mode](#window-mode) - [Window mode](#window-mode)
- [Space mode](#space-mode) - [Space mode](#space-mode)
- [Popup](#popup) - [Popup](#popup)
- [Completion Menu](#completion-menu)
- [Signature-help Popup](#signature-help-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)
@ -23,6 +25,8 @@
> 💡 Mappings marked (**TS**) require a tree-sitter grammar for the file type. > 💡 Mappings marked (**TS**) require a tree-sitter grammar for the file type.
> ⚠️ Some terminals' default key mappings conflict with Helix's. If any of the mappings described on this page do not work as expected, check your terminal's mappings to ensure they do not conflict. See the (wiki)[https://github.com/helix-editor/helix/wiki/Terminal-Support] for known conflicts.
## Normal mode ## Normal mode
Normal mode is the default mode when you launch helix. You can return to it from other modes by pressing the `Escape` key. Normal mode is the default mode when you launch helix. You can return to it from other modes by pressing the `Escape` key.
@ -48,13 +52,13 @@ Normal mode is the default mode when you launch helix. You can return to it from
| `T` | Find 'till previous char | `till_prev_char` | | `T` | Find 'till previous char | `till_prev_char` |
| `F` | Find previous char | `find_prev_char` | | `F` | Find previous char | `find_prev_char` |
| `G` | Go to line number `<n>` | `goto_line` | | `G` | Go to line number `<n>` | `goto_line` |
| `Alt-.` | Repeat last motion (`f`, `t` or `m`) | `repeat_last_motion` | | `Alt-.` | Repeat last motion (`f`, `t`, `m`, `[` or `]`) | `repeat_last_motion` |
| `Home` | Move to the start of the line | `goto_line_start` | | `Home` | Move to the start of the line | `goto_line_start` |
| `End` | Move to the end of the line | `goto_line_end` | | `End` | Move to the end of the line | `goto_line_end` |
| `Ctrl-b`, `PageUp` | Move page up | `page_up` | | `Ctrl-b`, `PageUp` | Move page up | `page_up` |
| `Ctrl-f`, `PageDown` | Move page down | `page_down` | | `Ctrl-f`, `PageDown` | Move page down | `page_down` |
| `Ctrl-u` | Move half page up | `half_page_up` | | `Ctrl-u` | Move cursor and page half page up | `page_cursor_half_up` |
| `Ctrl-d` | Move half page down | `half_page_down` | | `Ctrl-d` | Move cursor and page half page down | `page_cursor_half_down` |
| `Ctrl-i` | Jump forward on the jumplist | `jump_forward` | | `Ctrl-i` | Jump forward on the jumplist | `jump_forward` |
| `Ctrl-o` | Jump backward on the jumplist | `jump_backward` | | `Ctrl-o` | Jump backward on the jumplist | `jump_backward` |
| `Ctrl-s` | Save the current selection to the jumplist | `save_selection` | | `Ctrl-s` | Save the current selection to the jumplist | `save_selection` |
@ -192,8 +196,8 @@ useful when you're simply looking over text and not actively editing it.
| `k`, `up` | Scroll the view upwards | `scroll_up` | | `k`, `up` | Scroll the view upwards | `scroll_up` |
| `Ctrl-f`, `PageDown` | Move page down | `page_down` | | `Ctrl-f`, `PageDown` | Move page down | `page_down` |
| `Ctrl-b`, `PageUp` | Move page up | `page_up` | | `Ctrl-b`, `PageUp` | Move page up | `page_up` |
| `Ctrl-d` | Move half page down | `half_page_down` | | `Ctrl-u` | Move cursor and page half page up | `page_cursor_half_up` |
| `Ctrl-u` | Move half page up | `half_page_up` | | `Ctrl-d` | Move cursor and page half page down | `page_cursor_half_down` |
#### Goto mode #### Goto mode
@ -223,6 +227,7 @@ Jumps to various locations.
| `.` | 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` | | `j` | Move down textual (instead of visual) line | `move_line_down` |
| `k` | Move up textual (instead of visual) line | `move_line_up` | | `k` | Move up textual (instead of visual) line | `move_line_up` |
| `w` | Show labels at each word and select the word that belongs to the entered labels | `goto_word` |
#### Match mode #### Match mode
@ -278,7 +283,8 @@ This layer is a kludge of mappings, mostly pickers.
| `F` | Open file picker at current working directory | `file_picker_in_current_directory` | | `F` | Open file picker at current working directory | `file_picker_in_current_directory` |
| `b` | Open buffer picker | `buffer_picker` | | `b` | Open buffer picker | `buffer_picker` |
| `j` | Open jumplist picker | `jumplist_picker` | | `j` | Open jumplist picker | `jumplist_picker` |
| `g` | Debug (experimental) | N/A | | `g` | Open changed file picker | `changed_file_picker` |
| `G` | Debug (experimental) | N/A |
| `k` | Show documentation for item under cursor in a [popup](#popup) (**LSP**) | `hover` | | `k` | Show documentation for item under cursor in a [popup](#popup) (**LSP**) | `hover` |
| `s` | Open document symbol picker (**LSP**) | `symbol_picker` | | `s` | Open document symbol picker (**LSP**) | `symbol_picker` |
| `S` | Open workspace symbol picker (**LSP**) | `workspace_symbol_picker` | | `S` | Open workspace symbol picker (**LSP**) | `workspace_symbol_picker` |
@ -289,6 +295,9 @@ This layer is a kludge of mappings, mostly pickers.
| `h` | Select symbol references (**LSP**) | `select_references_to_symbol_under_cursor` | | `h` | Select symbol references (**LSP**) | `select_references_to_symbol_under_cursor` |
| `'` | Open last fuzzy picker | `last_picker` | | `'` | Open last fuzzy picker | `last_picker` |
| `w` | Enter [window mode](#window-mode) | N/A | | `w` | Enter [window mode](#window-mode) | N/A |
| `c` | Comment/uncomment selections | `toggle_comments` |
| `C` | Block comment/uncomment selections | `toggle_block_comments` |
| `Alt-c` | Line comment/uncomment selections | `toggle_line_comments` |
| `p` | Paste system clipboard after selections | `paste_clipboard_after` | | `p` | Paste system clipboard after selections | `paste_clipboard_after` |
| `P` | Paste system clipboard before selections | `paste_clipboard_before` | | `P` | Paste system clipboard before selections | `paste_clipboard_before` |
| `y` | Yank selections to clipboard | `yank_to_clipboard` | | `y` | Yank selections to clipboard | `yank_to_clipboard` |
@ -301,13 +310,31 @@ This layer is a kludge of mappings, mostly pickers.
##### Popup ##### Popup
Displays documentation for item under cursor. Displays documentation for item under cursor. Remapping currently not supported.
| Key | Description | | Key | Description |
| ---- | ----------- | | ---- | ----------- |
| `Ctrl-u` | Scroll up | | `Ctrl-u` | Scroll up |
| `Ctrl-d` | Scroll down | | `Ctrl-d` | Scroll down |
##### Completion Menu
Displays documentation for the selected completion item. Remapping currently not supported.
| Key | Description |
| ---- | ----------- |
| `Shift-Tab`, `Ctrl-p`, `Up` | Previous entry |
| `Tab`, `Ctrl-n`, `Down` | Next entry |
##### Signature-help Popup
Displays the signature of the selected completion item. Remapping currently not supported.
| Key | Description |
| ---- | ----------- |
| `Alt-p` | Previous signature |
| `Alt-n` | Next signature |
#### Unimpaired #### Unimpaired
These mappings are in the style of [vim-unimpaired](https://github.com/tpope/vim-unimpaired). These mappings are in the style of [vim-unimpaired](https://github.com/tpope/vim-unimpaired).

@ -1,7 +1,7 @@
# Language Support # Language Support
The following languages and Language Servers are supported. To use The following languages and Language Servers are supported. To use
Language Server features, you must first [install][lsp-install-wiki] the Language Server features, you must first [configure][lsp-config-wiki] the
appropriate Language Server. appropriate Language Server.
You can check the language support in your installed helix version with `hx --health`. You can check the language support in your installed helix version with `hx --health`.
@ -11,6 +11,6 @@ Languages][adding-languages] guide for more language configuration information.
{{#include ./generated/lang-support.md}} {{#include ./generated/lang-support.md}}
[lsp-install-wiki]: https://github.com/helix-editor/helix/wiki/How-to-install-the-default-language-servers [lsp-config-wiki]: https://github.com/helix-editor/helix/wiki/Language-Server-Configurations
[lang-config]: ./languages.md [lang-config]: ./languages.md
[adding-languages]: ./guides/adding_languages.md [adding-languages]: ./guides/adding_languages.md

@ -42,7 +42,7 @@ name = "mylang"
scope = "source.mylang" scope = "source.mylang"
injection-regex = "mylang" injection-regex = "mylang"
file-types = ["mylang", "myl"] file-types = ["mylang", "myl"]
comment-token = "#" comment-tokens = "#"
indent = { tab-width = 2, unit = " " } indent = { tab-width = 2, unit = " " }
formatter = { command = "mylang-formatter" , args = ["--stdin"] } formatter = { command = "mylang-formatter" , args = ["--stdin"] }
language-servers = [ "mylang-lsp" ] language-servers = [ "mylang-lsp" ]
@ -61,7 +61,8 @@ These configuration keys are available:
| `roots` | A set of marker files to look for when trying to find the workspace root. For example `Cargo.lock`, `yarn.lock` | | `roots` | A set of marker files to look for when trying to find the workspace root. For example `Cargo.lock`, `yarn.lock` |
| `auto-format` | Whether to autoformat this language when saving | | `auto-format` | Whether to autoformat this language when saving |
| `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-tokens` | The tokens to use as a comment token, either a single token `"//"` or an array `["//", "///", "//!"]` (the first token will be used for commenting). Also configurable as `comment-token` for backwards compatibility|
| `block-comment-tokens`| The start and end tokens for a multiline comment either an array or single table of `{ start = "/*", end = "*/"}`. The first set of tokens will be used for commenting, any pairs in the array can be uncommented |
| `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-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) | | `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) |
| `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`) |
@ -78,24 +79,26 @@ from the above section. `file-types` is a list of strings or tables, for
example: example:
```toml ```toml
file-types = ["Makefile", "toml", { suffix = ".git/config" }] file-types = ["toml", { glob = "Makefile" }, { glob = ".git/config" }, { glob = ".github/workflows/*.yaml" } ]
``` ```
When determining a language configuration to use, Helix searches the file-types When determining a language configuration to use, Helix searches the file-types
with the following priorities: with the following priorities:
1. Exact match: if the filename of a file is an exact match of a string in a 1. Glob: values in `glob` tables are checked against the full path of the given
`file-types` list, that language wins. In the example above, `"Makefile"` file. Globs are standard Unix-style path globs (e.g. the kind you use in Shell)
will match against `Makefile` files. and can be used to match paths for a specific prefix, suffix, directory, etc.
2. Extension: if there are no exact matches, any `file-types` string that In the above example, the `{ glob = "Makefile" }` config would match files
matches the file extension of a given file wins. In the example above, the with the name `Makefile`, the `{ glob = ".git/config" }` config would match
`"toml"` matches files like `Cargo.toml` or `languages.toml`. `config` files in `.git` directories, and the `{ glob = ".github/workflows/*.yaml" }`
3. Suffix: if there are still no matches, any values in `suffix` tables config would match any `yaml` files in `.github/workflow` directories. Note
are checked against the full path of the given file. In the example above, that globs should always use the Unix path separator `/` even on Windows systems;
the `{ suffix = ".git/config" }` would match against any `config` files the matcher will automatically take the machine-specific separators into account.
in `.git` directories. Note: `/` is used as the directory separator but is If the glob isn't an absolute path or doesn't already start with a glob prefix,
replaced at runtime with the appropriate path separator for the operating `*/` will automatically be added to ensure it matches for any subdirectory.
system, so this rule would match against `.git\config` files on Windows. 2. Extension: if there are no glob matches, any `file-types` string that matches
the file extension of a given file wins. In the example above, the `"toml"`
config matches files like `Cargo.toml` or `languages.toml`.
## Language Server configuration ## Language Server configuration
@ -127,6 +130,7 @@ These are the available options for a language server.
| `config` | LSP initialization options | | `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` |
| `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" }` |
| `required-root-patterns` | A list of `glob` patterns to look for in the working directory. The language server is started if at least one of them is found. |
A `format` sub-table within `config` can be used to pass extra formatting options to A `format` sub-table within `config` can be used to pass extra formatting options to
[Document Formatting Requests](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_formatting). [Document Formatting Requests](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_formatting).
@ -146,6 +150,8 @@ They have to be defined in the `[language-server]` table as described in the pre
Different languages can use the same language server instance, e.g. `typescript-language-server` is used for javascript, jsx, tsx and typescript by default. Different languages can use the same language server instance, e.g. `typescript-language-server` is used for javascript, jsx, tsx and typescript by default.
The definition order of language servers affects the order in the results list of code action menu.
In case multiple language servers are specified in the `language-servers` attribute of a `language`, 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. it's often useful to only enable/disable certain language-server features for these language servers.

@ -36,13 +36,6 @@ For inspiration, you can find the default `theme.toml`
user-submitted themes user-submitted themes
[here](https://github.com/helix-editor/helix/blob/master/runtime/themes). [here](https://github.com/helix-editor/helix/blob/master/runtime/themes).
### Using the linter
Use the supplied linting tool to check for errors and missing scopes:
```sh
cargo xtask themelint onedark # replace onedark with <name>
```
## The details of theme creation ## The details of theme creation
@ -186,6 +179,7 @@ We use a similar set of scopes as
- `parameter` - Function parameters - `parameter` - Function parameters
- `other` - `other`
- `member` - Fields of composite data types (e.g. structs, unions) - `member` - Fields of composite data types (e.g. structs, unions)
- `private` - Private fields that use a unique syntax (currently just ECMAScript-based languages)
- `label` - `label`
@ -213,6 +207,7 @@ We use a similar set of scopes as
- `function` - `function`
- `builtin` - `builtin`
- `method` - `method`
- `private` - Private methods that use a unique syntax (currently just ECMAScript-based languages)
- `macro` - `macro`
- `special` (preprocessor in C) - `special` (preprocessor in C)
@ -251,6 +246,7 @@ We use a similar set of scopes as
- `gutter` - gutter indicator - `gutter` - gutter indicator
- `delta` - modifications - `delta` - modifications
- `moved` - renamed or moved files/changes - `moved` - renamed or moved files/changes
- `conflict` - merge conflicts
- `gutter` - gutter indicator - `gutter` - gutter indicator
#### Interface #### Interface
@ -314,6 +310,7 @@ These scopes are used for theming the editor interface:
| `ui.virtual.inlay-hint.parameter` | Style for inlay hints of kind `parameter` (LSPs are not required to set a kind) | | `ui.virtual.inlay-hint.parameter` | Style for inlay hints of kind `parameter` (LSPs are not required to set a kind) |
| `ui.virtual.inlay-hint.type` | Style for inlay hints of kind `type` (LSPs are not required to set a kind) | | `ui.virtual.inlay-hint.type` | Style for inlay hints of kind `type` (LSPs are not required to set a kind) |
| `ui.virtual.wrap` | Soft-wrap indicator (see the [`editor.soft-wrap` config][editor-section]) | | `ui.virtual.wrap` | Soft-wrap indicator (see the [`editor.soft-wrap` config][editor-section]) |
| `ui.virtual.jump-label` | Style for virtual jump labels |
| `ui.menu` | Code and command completion menus | | `ui.menu` | Code and command completion menus |
| `ui.menu.selected` | Selected autocomplete item | | `ui.menu.selected` | Selected autocomplete item |
| `ui.menu.scroll` | `fg` sets thumb color, `bg` sets track color of scrollbar | | `ui.menu.scroll` | `fg` sets thumb color, `bg` sets track color of scrollbar |
@ -333,5 +330,7 @@ These scopes are used for theming the editor interface:
| `diagnostic.info` | Diagnostics info (editing area) | | `diagnostic.info` | Diagnostics info (editing area) |
| `diagnostic.warning` | Diagnostics warning (editing area) | | `diagnostic.warning` | Diagnostics warning (editing area) |
| `diagnostic.error` | Diagnostics error (editing area) | | `diagnostic.error` | Diagnostics error (editing area) |
| `diagnostic.unnecessary` | Diagnostics with unnecessary tag (editing area) |
| `diagnostic.deprecated` | Diagnostics with deprecated tag (editing area) |
[editor-section]: ./configuration.md#editor-section [editor-section]: ./configuration.md#editor-section

@ -6,27 +6,26 @@
<name>Helix</name> <name>Helix</name>
<summary>A post-modern text editor</summary> <summary>A post-modern text editor</summary>
<summary xml:lang="ar">مُحَرِّرُ نُصُوصٍ سَابِقٌ لِعَهدِه</summary> <summary xml:lang="ar">مُحَرِّرُ نُصُوصٍ سَابِقٌ لِعَهدِه</summary>
<developer id="com.helix_editor">
<name>Blaž Hrastnik</name>
</developer>
<description> <description>
<p> <p>
Helix is a terminal-based text editor inspired by Kakoune / Neovim and written in Rust. Helix is a terminal-based text editor inspired by Kakoune / Neovim and written in Rust.
</p> </p>
<p xml:lang="ar">
مُحَرِّرُ نُصُوصٍ يَعمَلُ فِي الطَّرَفِيَّة، مُستَلهَمٌ مِن Kakoune وَ Neovim وَمَكتُوبٌ بِلُغَةِ رَست البَرمَجِيَّة.
</p>
<ul> <ul>
<li>Vim-like modal editing</li> <li>Vim-like modal editing</li>
<li xml:lang="ar">تَحرِيرٌ وَضعِيٌّ شَبيهٌ بِـVim</li>
<li>Multiple selections</li> <li>Multiple selections</li>
<li xml:lang="ar">تَحدِيدَاتٌ لِلنَّصِ مُتَعَدِّدَة</li>
<li>Built-in language server support</li> <li>Built-in language server support</li>
<li xml:lang="ar">دَعْمٌ مُدمَجٌ لِخَوادِمِ اللُّغَات</li>
<li>Smart, incremental syntax highlighting and code editing via tree-sitter</li> <li>Smart, incremental syntax highlighting and code editing via tree-sitter</li>
</ul> <li xml:lang="ar">تَحرِيرُ التَّعلِيمَاتِ البَّرمَجِيَّةِ مَعَ تَمييزٍ لِلتَّركِيبِ النَّحُويِّ بِواسِطَةِ tree-sitter</li>
</description>
<description xml:lang="ar">
<p>
مُحَرِّرُ نُصُوصٍ يَعمَلُ فِي الطَّرَفِيَّة، مُستَلهَمٌ مِن Kakoune وَ Neovim وَمَكتُوبٌ بِلُغَةِ رَست البَرمَجِيَّة.
</p>
<ul>
<li>تَحرِيرٌ وَضعِيٌّ شَبيهٌ بِـVim</li>
<li>تَحدِيدَاتٌ لِلنَّصِ مُتَعَدِّدَة</li>
<li>دَعْمٌ مُدمَجٌ لِخَوادِمِ اللُّغَات</li>
<li>تَحرِيرُ التَّعلِيمَاتِ البَّرمَجِيَّةِ مَعَ تَمييزٍ لِلتَّركِيبِ النَّحُويِّ بِواسِطَةِ tree-sitter</li>
</ul> </ul>
</description> </description>
@ -48,6 +47,9 @@
<content_rating type="oars-1.1" /> <content_rating type="oars-1.1" />
<releases> <releases>
<release version="24.03" date="2024-03-30">
<url>https://helix-editor.com/news/release-24-03-highlights/</url>
</release>
<release version="23.10" date="2023-10-24"> <release version="23.10" date="2023-10-24">
<url>https://helix-editor.com/news/release-23-10-highlights/</url> <url>https://helix-editor.com/news/release-23-10-highlights/</url>
</release> </release>
@ -71,9 +73,9 @@
</release> </release>
</releases> </releases>
<requires> <recommends>
<control>keyboard</control> <control>keyboard</control>
</requires> </recommends>
<categories> <categories>
<category>Utility</category> <category>Utility</category>

@ -5,19 +5,20 @@ _hx() {
# $1 command name # $1 command name
# $2 word being completed # $2 word being completed
# $3 word preceding # $3 word preceding
COMPREPLY=()
case "$3" in case "$3" in
-g | --grammar) -g | --grammar)
COMPREPLY=($(compgen -W "fetch build" -- $2)) COMPREPLY="$(compgen -W 'fetch build' -- $2)"
;; ;;
--health) --health)
local languages=$(hx --health |tail -n '+7' |awk '{print $1}' |sed 's/\x1b\[[0-9;]*m//g') local languages=$(hx --health |tail -n '+7' |awk '{print $1}' |sed 's/\x1b\[[0-9;]*m//g')
COMPREPLY=($(compgen -W "$languages" -- $2)) COMPREPLY="$(compgen -W """$languages""" -- $2)"
;; ;;
*) *)
COMPREPLY=($(compgen -fd -W "-h --help --tutor -V --version -v -vv -vvv --health -g --grammar --vsplit --hsplit -c --config --log" -- $2)) COMPREPLY="$(compgen -fd -W "-h --help --tutor -V --version -v -vv -vvv --health -g --grammar --vsplit --hsplit -c --config --log" -- """$2""")"
;; ;;
esac esac
} && complete -o filenames -F _hx hx
local IFS=$'\n'
COMPREPLY=($COMPREPLY)
} && complete -o filenames -F _hx hx

@ -7,11 +7,11 @@
] ]
}, },
"locked": { "locked": {
"lastModified": 1701025348, "lastModified": 1709610799,
"narHash": "sha256-42GHmYH+GF7VjwGSt+fVT1CQuNpGanJbNgVHTAZppUM=", "narHash": "sha256-5jfLQx0U9hXbi2skYMGodDJkIgffrjIOgMRjZqms2QE=",
"owner": "ipetkov", "owner": "ipetkov",
"repo": "crane", "repo": "crane",
"rev": "42afaeb1a0325194a7cdb526332d2cb92fddd07b", "rev": "81c393c776d5379c030607866afef6406ca1be57",
"type": "github" "type": "github"
}, },
"original": { "original": {
@ -25,11 +25,11 @@
"systems": "systems" "systems": "systems"
}, },
"locked": { "locked": {
"lastModified": 1694529238, "lastModified": 1709126324,
"narHash": "sha256-zsNZZGTGnMOf9YpHKJqMSsa0dXbfmxeoJ7xHlrt+xmY=", "narHash": "sha256-q6EQdSeUZOG26WelxqkmR7kArjgWCdw5sfJVHPH/7j8=",
"owner": "numtide", "owner": "numtide",
"repo": "flake-utils", "repo": "flake-utils",
"rev": "ff7b65b44d01cf9ba6a71320833626af21126384", "rev": "d465f4819400de7c8d874d50b982301f28a84605",
"type": "github" "type": "github"
}, },
"original": { "original": {
@ -40,11 +40,11 @@
}, },
"nixpkgs": { "nixpkgs": {
"locked": { "locked": {
"lastModified": 1700794826, "lastModified": 1709479366,
"narHash": "sha256-RyJTnTNKhO0yqRpDISk03I/4A67/dp96YRxc86YOPgU=", "narHash": "sha256-n6F0n8UV6lnTZbYPl1A9q1BS0p4hduAv1mGAP17CVd0=",
"owner": "nixos", "owner": "nixos",
"repo": "nixpkgs", "repo": "nixpkgs",
"rev": "5a09cb4b393d58f9ed0d9ca1555016a8543c2ac8", "rev": "b8697e57f10292a6165a20f03d2f42920dfaf973",
"type": "github" "type": "github"
}, },
"original": { "original": {
@ -72,11 +72,11 @@
] ]
}, },
"locked": { "locked": {
"lastModified": 1701137803, "lastModified": 1709604635,
"narHash": "sha256-0LcPAdql5IhQSUXJx3Zna0dYTgdIoYO7zUrsKgiBd04=", "narHash": "sha256-le4fwmWmjGRYWwkho0Gr7mnnZndOOe4XGbLw68OvF40=",
"owner": "oxalica", "owner": "oxalica",
"repo": "rust-overlay", "repo": "rust-overlay",
"rev": "9dd940c967502f844eacea52a61e9596268d4f70", "rev": "e86c0fb5d3a22a5f30d7f64ecad88643fe26449d",
"type": "github" "type": "github"
}, },
"original": { "original": {

@ -23,24 +23,23 @@ helix-loader = { path = "../helix-loader" }
ropey = { version = "1.6.1", default-features = false, features = ["simd"] } ropey = { version = "1.6.1", default-features = false, features = ["simd"] }
smallvec = "1.13" smallvec = "1.13"
smartstring = "1.0.1" smartstring = "1.0.1"
unicode-segmentation = "1.10" unicode-segmentation = "1.11"
unicode-width = "0.1" unicode-width = "0.1"
unicode-general-category = "0.6" unicode-general-category = "0.6"
# slab = "0.4.2" slotmap.workspace = true
slotmap = "1.0"
tree-sitter.workspace = true tree-sitter.workspace = true
once_cell = "1.19" once_cell = "1.19"
arc-swap = "1" arc-swap = "1"
regex = "1" regex = "1"
bitflags = "2.4" bitflags = "2.5"
ahash = "0.8.6" ahash = "0.8.11"
hashbrown = { version = "0.14.3", features = ["raw"] } hashbrown = { version = "0.14.3", features = ["raw"] }
dunce = "1.0" dunce = "1.0"
log = "0.4" log = "0.4"
serde = { version = "1.0", features = ["derive"] } serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0" serde_json = "1.0"
toml = "0.7" toml = "0.8"
imara-diff = "0.1.0" imara-diff = "0.1.0"
@ -49,12 +48,13 @@ 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.8" etcetera = "0.8"
textwrap = "0.16.0" textwrap = "0.16.1"
steel-core = { workspace = true, optional = true } steel-core = { workspace = true, optional = true }
nucleo.workspace = true nucleo.workspace = true
parking_lot = "0.12" parking_lot = "0.12"
globset = "0.4.14"
[dev-dependencies] [dev-dependencies]
quickcheck = { version = "1", default-features = false } quickcheck = { version = "1", default-features = false }
indoc = "2.0.4" indoc = "2.0.5"

@ -1,9 +1,12 @@
//! This module contains the functionality toggle comments on lines over the selection //! This module contains the functionality toggle comments on lines over the selection
//! using the comment character defined in the user's `languages.toml` //! using the comment character defined in the user's `languages.toml`
use smallvec::SmallVec;
use crate::{ use crate::{
find_first_non_whitespace_char, Change, Rope, RopeSlice, Selection, Tendril, Transaction, syntax::BlockCommentToken, Change, Range, Rope, RopeSlice, Selection, Tendril, Transaction,
}; };
use helix_stdx::rope::RopeSliceExt;
use std::borrow::Cow; use std::borrow::Cow;
/// Given text, a comment token, and a set of line indices, returns the following: /// Given text, a comment token, and a set of line indices, returns the following:
@ -22,12 +25,12 @@ fn find_line_comment(
) -> (bool, Vec<usize>, usize, usize) { ) -> (bool, Vec<usize>, usize, usize) {
let mut commented = true; let mut commented = true;
let mut to_change = Vec::new(); let mut to_change = Vec::new();
let mut min = usize::MAX; // minimum col for find_first_non_whitespace_char let mut min = usize::MAX; // minimum col for first_non_whitespace_char
let mut margin = 1; let mut margin = 1;
let token_len = token.chars().count(); let token_len = token.chars().count();
for line in lines { for line in lines {
let line_slice = text.line(line); let line_slice = text.line(line);
if let Some(pos) = find_first_non_whitespace_char(line_slice) { if let Some(pos) = line_slice.first_non_whitespace_char() {
let len = line_slice.len_chars(); let len = line_slice.len_chars();
if pos < min { if pos < min {
@ -94,6 +97,222 @@ pub fn toggle_line_comments(doc: &Rope, selection: &Selection, token: Option<&st
Transaction::change(doc, changes.into_iter()) Transaction::change(doc, changes.into_iter())
} }
#[derive(Debug, PartialEq, Eq)]
pub enum CommentChange {
Commented {
range: Range,
start_pos: usize,
end_pos: usize,
start_margin: bool,
end_margin: bool,
start_token: String,
end_token: String,
},
Uncommented {
range: Range,
start_pos: usize,
end_pos: usize,
start_token: String,
end_token: String,
},
Whitespace {
range: Range,
},
}
pub fn find_block_comments(
tokens: &[BlockCommentToken],
text: RopeSlice,
selection: &Selection,
) -> (bool, Vec<CommentChange>) {
let mut commented = true;
let mut only_whitespace = true;
let mut comment_changes = Vec::with_capacity(selection.len());
let default_tokens = tokens.first().cloned().unwrap_or_default();
// TODO: check if this can be removed on MSRV bump
#[allow(clippy::redundant_clone)]
let mut start_token = default_tokens.start.clone();
#[allow(clippy::redundant_clone)]
let mut end_token = default_tokens.end.clone();
let mut tokens = tokens.to_vec();
// sort the tokens by length, so longer tokens will match first
tokens.sort_by(|a, b| {
if a.start.len() == b.start.len() {
b.end.len().cmp(&a.end.len())
} else {
b.start.len().cmp(&a.start.len())
}
});
for range in selection {
let selection_slice = range.slice(text);
if let (Some(start_pos), Some(end_pos)) = (
selection_slice.first_non_whitespace_char(),
selection_slice.last_non_whitespace_char(),
) {
let mut line_commented = false;
let mut after_start = 0;
let mut before_end = 0;
let len = (end_pos + 1) - start_pos;
for BlockCommentToken { start, end } in &tokens {
let start_len = start.chars().count();
let end_len = end.chars().count();
after_start = start_pos + start_len;
before_end = end_pos.saturating_sub(end_len);
if len >= start_len + end_len {
let start_fragment = selection_slice.slice(start_pos..after_start);
let end_fragment = selection_slice.slice(before_end + 1..end_pos + 1);
// block commented with these tokens
if start_fragment == start.as_str() && end_fragment == end.as_str() {
start_token = start.to_string();
end_token = end.to_string();
line_commented = true;
break;
}
}
}
if !line_commented {
comment_changes.push(CommentChange::Uncommented {
range: *range,
start_pos,
end_pos,
start_token: default_tokens.start.clone(),
end_token: default_tokens.end.clone(),
});
commented = false;
} else {
comment_changes.push(CommentChange::Commented {
range: *range,
start_pos,
end_pos,
start_margin: selection_slice
.get_char(after_start)
.map_or(false, |c| c == ' '),
end_margin: after_start != before_end
&& selection_slice
.get_char(before_end)
.map_or(false, |c| c == ' '),
start_token: start_token.to_string(),
end_token: end_token.to_string(),
});
}
only_whitespace = false;
} else {
comment_changes.push(CommentChange::Whitespace { range: *range });
}
}
if only_whitespace {
commented = false;
}
(commented, comment_changes)
}
#[must_use]
pub fn create_block_comment_transaction(
doc: &Rope,
selection: &Selection,
commented: bool,
comment_changes: Vec<CommentChange>,
) -> (Transaction, SmallVec<[Range; 1]>) {
let mut changes: Vec<Change> = Vec::with_capacity(selection.len() * 2);
let mut ranges: SmallVec<[Range; 1]> = SmallVec::with_capacity(selection.len());
let mut offs = 0;
for change in comment_changes {
if commented {
if let CommentChange::Commented {
range,
start_pos,
end_pos,
start_token,
end_token,
start_margin,
end_margin,
} = change
{
let from = range.from();
changes.push((
from + start_pos,
from + start_pos + start_token.len() + start_margin as usize,
None,
));
changes.push((
from + end_pos - end_token.len() - end_margin as usize + 1,
from + end_pos + 1,
None,
));
}
} else {
// uncommented so manually map ranges through changes
match change {
CommentChange::Uncommented {
range,
start_pos,
end_pos,
start_token,
end_token,
} => {
let from = range.from();
changes.push((
from + start_pos,
from + start_pos,
Some(Tendril::from(format!("{} ", start_token))),
));
changes.push((
from + end_pos + 1,
from + end_pos + 1,
Some(Tendril::from(format!(" {}", end_token))),
));
let offset = start_token.chars().count() + end_token.chars().count() + 2;
ranges.push(
Range::new(from + offs, from + offs + end_pos + 1 + offset)
.with_direction(range.direction()),
);
offs += offset;
}
CommentChange::Commented { range, .. } | CommentChange::Whitespace { range } => {
ranges.push(Range::new(range.from() + offs, range.to() + offs));
}
}
}
}
(Transaction::change(doc, changes.into_iter()), ranges)
}
#[must_use]
pub fn toggle_block_comments(
doc: &Rope,
selection: &Selection,
tokens: &[BlockCommentToken],
) -> Transaction {
let text = doc.slice(..);
let (commented, comment_changes) = find_block_comments(tokens, text, selection);
let (mut transaction, ranges) =
create_block_comment_transaction(doc, selection, commented, comment_changes);
if !commented {
transaction = transaction.with_selection(Selection::new(ranges, selection.primary_index()));
}
transaction
}
pub fn split_lines_of_selection(text: RopeSlice, selection: &Selection) -> Selection {
let mut ranges = SmallVec::new();
for range in selection.ranges() {
let (line_start, line_end) = range.line_range(text.slice(..));
let mut pos = text.line_to_char(line_start);
for line in text.slice(pos..text.line_to_char(line_end + 1)).lines() {
let start = pos;
pos += line.len_chars();
ranges.push(Range::new(start, pos));
}
}
Selection::new(ranges, 0)
}
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use super::*; use super::*;
@ -149,4 +368,49 @@ mod test {
// TODO: account for uncommenting with uneven comment indentation // TODO: account for uncommenting with uneven comment indentation
} }
#[test]
fn test_find_block_comments() {
// three lines 5 characters.
let mut doc = Rope::from("1\n2\n3");
// select whole document
let selection = Selection::single(0, doc.len_chars());
let text = doc.slice(..);
let res = find_block_comments(&[BlockCommentToken::default()], text, &selection);
assert_eq!(
res,
(
false,
vec![CommentChange::Uncommented {
range: Range::new(0, 5),
start_pos: 0,
end_pos: 4,
start_token: "/*".to_string(),
end_token: "*/".to_string(),
}]
)
);
// comment
let transaction = toggle_block_comments(&doc, &selection, &[BlockCommentToken::default()]);
transaction.apply(&mut doc);
assert_eq!(doc, "/* 1\n2\n3 */");
// uncomment
let selection = Selection::single(0, doc.len_chars());
let transaction = toggle_block_comments(&doc, &selection, &[BlockCommentToken::default()]);
transaction.apply(&mut doc);
assert_eq!(doc, "1\n2\n3");
// don't panic when there is just a space in comment
doc = Rope::from("/* */");
let selection = Selection::single(0, doc.len_chars());
let transaction = toggle_block_comments(&doc, &selection, &[BlockCommentToken::default()]);
transaction.apply(&mut doc);
assert_eq!(doc, "");
}
} }

@ -1,10 +1,45 @@
/// Syntax configuration loader based on built-in languages.toml. use crate::syntax::{Configuration, Loader, LoaderError};
pub fn default_syntax_loader() -> crate::syntax::Configuration {
/// Language configuration based on built-in languages.toml.
pub fn default_lang_config() -> Configuration {
helix_loader::config::default_lang_config() helix_loader::config::default_lang_config()
.try_into() .try_into()
.expect("Could not serialize built-in languages.toml") .expect("Could not deserialize built-in languages.toml")
} }
/// Syntax configuration loader based on user configured languages.toml.
pub fn user_syntax_loader() -> Result<crate::syntax::Configuration, toml::de::Error> { /// Language configuration loader based on built-in languages.toml.
pub fn default_lang_loader() -> Loader {
Loader::new(default_lang_config()).expect("Could not compile loader for default config")
}
#[derive(Debug)]
pub enum LanguageLoaderError {
DeserializeError(toml::de::Error),
LoaderError(LoaderError),
}
impl std::fmt::Display for LanguageLoaderError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::DeserializeError(err) => write!(f, "Failed to parse language config: {err}"),
Self::LoaderError(err) => write!(f, "Failed to compile language config: {err}"),
}
}
}
impl std::error::Error for LanguageLoaderError {}
/// Language configuration based on user configured languages.toml.
pub fn user_lang_config() -> Result<Configuration, toml::de::Error> {
helix_loader::config::user_lang_config()?.try_into() helix_loader::config::user_lang_config()?.try_into()
} }
/// Language configuration loader based on user configured languages.toml.
pub fn user_lang_loader() -> Result<Loader, LanguageLoaderError> {
let config: Configuration = helix_loader::config::user_lang_config()
.map_err(LanguageLoaderError::DeserializeError)?
.try_into()
.map_err(LanguageLoaderError::DeserializeError)?;
Loader::new(config).map_err(LanguageLoaderError::LoaderError)
}

@ -1,4 +1,6 @@
//! LSP diagnostic utility types. //! LSP diagnostic utility types.
use std::fmt;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
/// Describes the severity level of a [`Diagnostic`]. /// Describes the severity level of a [`Diagnostic`].
@ -47,8 +49,25 @@ 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 provider: DiagnosticProvider,
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>,
} }
// TODO turn this into an enum + feature flag when lsp becomes optional
pub type DiagnosticProvider = LanguageServerId;
// while I would prefer having this in helix-lsp that necessitates a bunch of
// conversions I would rather not add. I think its fine since this just a very
// trivial newtype wrapper and we would need something similar once we define
// completions in core
slotmap::new_key_type! {
pub struct LanguageServerId;
}
impl fmt::Display for LanguageServerId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{:?}", self.0)
}
}

@ -116,7 +116,7 @@ impl Default for TextFormat {
#[derive(Debug)] #[derive(Debug)]
pub struct DocumentFormatter<'t> { pub struct DocumentFormatter<'t> {
text_fmt: &'t TextFormat, text_fmt: &'t TextFormat,
annotations: &'t TextAnnotations, annotations: &'t TextAnnotations<'t>,
/// The visual position at the end of the last yielded word boundary /// The visual position at the end of the last yielded word boundary
visual_pos: Position, visual_pos: Position,

@ -1,5 +1,3 @@
use std::rc::Rc;
use crate::doc_formatter::{DocumentFormatter, TextFormat}; use crate::doc_formatter::{DocumentFormatter, TextFormat};
use crate::text_annotations::{InlineAnnotation, Overlay, TextAnnotations}; use crate::text_annotations::{InlineAnnotation, Overlay, TextAnnotations};
@ -105,7 +103,7 @@ fn overlay_text(text: &str, char_pos: usize, softwrap: bool, overlays: &[Overlay
DocumentFormatter::new_at_prev_checkpoint( DocumentFormatter::new_at_prev_checkpoint(
text.into(), text.into(),
&TextFormat::new_test(softwrap), &TextFormat::new_test(softwrap),
TextAnnotations::default().add_overlay(overlays.into(), None), TextAnnotations::default().add_overlay(overlays, None),
char_pos, char_pos,
) )
.0 .0
@ -142,7 +140,7 @@ fn annotate_text(text: &str, softwrap: bool, annotations: &[InlineAnnotation]) -
DocumentFormatter::new_at_prev_checkpoint( DocumentFormatter::new_at_prev_checkpoint(
text.into(), text.into(),
&TextFormat::new_test(softwrap), &TextFormat::new_test(softwrap),
TextAnnotations::default().add_inline_annotations(annotations.into(), None), TextAnnotations::default().add_inline_annotations(annotations, None),
0, 0,
) )
.0 .0
@ -164,15 +162,24 @@ fn annotation() {
"foo foo foo foo \n.foo foo foo foo \n.foo foo foo " "foo foo foo foo \n.foo foo foo foo \n.foo foo foo "
); );
} }
#[test] #[test]
fn annotation_and_overlay() { fn annotation_and_overlay() {
let annotations = [InlineAnnotation {
char_idx: 0,
text: "fooo".into(),
}];
let overlay = [Overlay {
char_idx: 0,
grapheme: "\t".into(),
}];
assert_eq!( assert_eq!(
DocumentFormatter::new_at_prev_checkpoint( DocumentFormatter::new_at_prev_checkpoint(
"bbar".into(), "bbar".into(),
&TextFormat::new_test(false), &TextFormat::new_test(false),
TextAnnotations::default() TextAnnotations::default()
.add_inline_annotations(Rc::new([InlineAnnotation::new(0, "fooo")]), None) .add_inline_annotations(annotations.as_slice(), None)
.add_overlay(Rc::new([Overlay::new(0, "\t")]), None), .add_overlay(overlay.as_slice(), None),
0, 0,
) )
.0 .0

@ -278,23 +278,6 @@ pub fn ensure_grapheme_boundary_prev(slice: RopeSlice, char_idx: usize) -> usize
} }
} }
/// Returns the passed byte index if it's already a grapheme boundary,
/// or the next grapheme boundary byte index if not.
#[must_use]
#[inline]
pub fn ensure_grapheme_boundary_next_byte(slice: RopeSlice, byte_idx: usize) -> usize {
if byte_idx == 0 {
byte_idx
} else {
// TODO: optimize so we're not constructing grapheme cursor twice
if is_grapheme_boundary_byte(slice, byte_idx) {
byte_idx
} else {
next_grapheme_boundary_byte(slice, byte_idx)
}
}
}
/// Returns whether the given char position is a grapheme boundary. /// Returns whether the given char position is a grapheme boundary.
#[must_use] #[must_use]
pub fn is_grapheme_boundary(slice: RopeSlice, char_idx: usize) -> bool { pub fn is_grapheme_boundary(slice: RopeSlice, char_idx: usize) -> bool {
@ -425,6 +408,85 @@ impl<'a> Iterator for RopeGraphemes<'a> {
} }
} }
/// An iterator over the graphemes of a `RopeSlice` in reverse.
#[derive(Clone)]
pub struct RevRopeGraphemes<'a> {
text: RopeSlice<'a>,
chunks: Chunks<'a>,
cur_chunk: &'a str,
cur_chunk_start: usize,
cursor: GraphemeCursor,
}
impl<'a> fmt::Debug for RevRopeGraphemes<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("RevRopeGraphemes")
.field("text", &self.text)
.field("chunks", &self.chunks)
.field("cur_chunk", &self.cur_chunk)
.field("cur_chunk_start", &self.cur_chunk_start)
// .field("cursor", &self.cursor)
.finish()
}
}
impl<'a> RevRopeGraphemes<'a> {
#[must_use]
pub fn new(slice: RopeSlice) -> RevRopeGraphemes {
let (mut chunks, mut cur_chunk_start, _, _) = slice.chunks_at_byte(slice.len_bytes());
chunks.reverse();
let first_chunk = chunks.next().unwrap_or("");
cur_chunk_start -= first_chunk.len();
RevRopeGraphemes {
text: slice,
chunks,
cur_chunk: first_chunk,
cur_chunk_start,
cursor: GraphemeCursor::new(slice.len_bytes(), slice.len_bytes(), true),
}
}
}
impl<'a> Iterator for RevRopeGraphemes<'a> {
type Item = RopeSlice<'a>;
fn next(&mut self) -> Option<RopeSlice<'a>> {
let a = self.cursor.cur_cursor();
let b;
loop {
match self
.cursor
.prev_boundary(self.cur_chunk, self.cur_chunk_start)
{
Ok(None) => {
return None;
}
Ok(Some(n)) => {
b = n;
break;
}
Err(GraphemeIncomplete::PrevChunk) => {
self.cur_chunk = self.chunks.next().unwrap_or("");
self.cur_chunk_start -= self.cur_chunk.len();
}
Err(GraphemeIncomplete::PreContext(idx)) => {
let (chunk, byte_idx, _, _) = self.text.chunk_at_byte(idx.saturating_sub(1));
self.cursor.provide_context(chunk, byte_idx);
}
_ => unreachable!(),
}
}
if a >= self.cur_chunk_start + self.cur_chunk.len() {
Some(self.text.byte_slice(b..a))
} else {
let a2 = a - self.cur_chunk_start;
let b2 = b - self.cur_chunk_start;
Some((&self.cur_chunk[b2..a2]).into())
}
}
}
/// A highly compressed Cow<'a, str> that holds /// A highly compressed Cow<'a, str> that holds
/// atmost u31::MAX bytes and is readonly /// atmost u31::MAX bytes and is readonly
pub struct GraphemeStr<'a> { pub struct GraphemeStr<'a> {

@ -27,7 +27,7 @@ pub fn increment(selected_text: &str, amount: i64) -> Option<String> {
let date_time = NaiveDateTime::parse_from_str(date_time, format.fmt).ok()?; let date_time = NaiveDateTime::parse_from_str(date_time, format.fmt).ok()?;
Some( Some(
date_time date_time
.checked_add_signed(Duration::minutes(amount))? .checked_add_signed(Duration::try_minutes(amount)?)?
.format(format.fmt) .format(format.fmt)
.to_string(), .to_string(),
) )
@ -35,14 +35,15 @@ pub fn increment(selected_text: &str, amount: i64) -> Option<String> {
(true, false) => { (true, false) => {
let date = NaiveDate::parse_from_str(date_time, format.fmt).ok()?; let date = NaiveDate::parse_from_str(date_time, format.fmt).ok()?;
Some( Some(
date.checked_add_signed(Duration::days(amount))? date.checked_add_signed(Duration::try_days(amount)?)?
.format(format.fmt) .format(format.fmt)
.to_string(), .to_string(),
) )
} }
(false, true) => { (false, true) => {
let time = NaiveTime::parse_from_str(date_time, format.fmt).ok()?; let time = NaiveTime::parse_from_str(date_time, format.fmt).ok()?;
let (adjusted_time, _) = time.overflowing_add_signed(Duration::minutes(amount)); let (adjusted_time, _) =
time.overflowing_add_signed(Duration::try_minutes(amount)?);
Some(adjusted_time.format(format.fmt).to_string()) Some(adjusted_time.format(format.fmt).to_string())
} }
(false, false) => None, (false, false) => None,

@ -1,10 +1,10 @@
use std::{borrow::Cow, collections::HashMap}; use std::{borrow::Cow, collections::HashMap};
use helix_stdx::rope::RopeSliceExt;
use tree_sitter::{Query, QueryCursor, QueryPredicateArg}; use tree_sitter::{Query, QueryCursor, QueryPredicateArg};
use crate::{ use crate::{
chars::{char_is_line_ending, char_is_whitespace}, chars::{char_is_line_ending, char_is_whitespace},
find_first_non_whitespace_char,
graphemes::{grapheme_width, tab_width_at}, graphemes::{grapheme_width, tab_width_at},
syntax::{IndentationHeuristic, LanguageConfiguration, RopeProvider, Syntax}, syntax::{IndentationHeuristic, LanguageConfiguration, RopeProvider, Syntax},
tree_sitter::Node, tree_sitter::Node,
@ -247,41 +247,18 @@ fn add_indent_level(
} }
} }
/// Computes for node and all ancestors whether they are the first node on their line. /// Return true if only whitespace comes before the node on its line.
/// The first entry in the return value represents the root node, the last one the node itself /// If given, new_line_byte_pos is treated the same way as any existing newline.
fn get_first_in_line(mut node: Node, new_line_byte_pos: Option<usize>) -> Vec<bool> { fn is_first_in_line(node: Node, text: RopeSlice, new_line_byte_pos: Option<usize>) -> bool {
let mut first_in_line = Vec::new(); let mut line_start_byte_pos = text.line_to_byte(node.start_position().row);
loop { if let Some(pos) = new_line_byte_pos {
if let Some(prev) = node.prev_sibling() { if line_start_byte_pos < pos && pos <= node.start_byte() {
// If we insert a new line, the first node at/after the cursor is considered to be the first in its line line_start_byte_pos = pos;
let first = prev.end_position().row != node.start_position().row
|| new_line_byte_pos.map_or(false, |byte_pos| {
node.start_byte() >= byte_pos && prev.start_byte() < byte_pos
});
first_in_line.push(Some(first));
} else {
// Nodes that have no previous siblings are first in their line if and only if their parent is
// (which we don't know yet)
first_in_line.push(None);
} }
if let Some(parent) = node.parent() {
node = parent;
} else {
break;
} }
} text.byte_slice(line_start_byte_pos..node.start_byte())
.chars()
let mut result = Vec::with_capacity(first_in_line.len()); .all(|c| c.is_whitespace())
let mut parent_is_first = true; // The root node is by definition the first node in its line
for first in first_in_line.into_iter().rev() {
if let Some(first) = first {
result.push(first);
parent_is_first = first;
} else {
result.push(parent_is_first);
}
}
result
} }
/// The total indent for some line of code. /// The total indent for some line of code.
@ -852,7 +829,6 @@ pub fn treesitter_indent_for_pos<'a>(
byte_pos, byte_pos,
new_line_byte_pos, new_line_byte_pos,
)?; )?;
let mut first_in_line = get_first_in_line(node, new_line.then_some(byte_pos));
let mut result = Indentation::default(); let mut result = Indentation::default();
// We always keep track of all the indent changes on one line, in order to only indent once // We always keep track of all the indent changes on one line, in order to only indent once
@ -861,9 +837,7 @@ pub fn treesitter_indent_for_pos<'a>(
let mut indent_for_line_below = Indentation::default(); let mut indent_for_line_below = Indentation::default();
loop { loop {
// This can safely be unwrapped because `first_in_line` contains let is_first = is_first_in_line(node, text, new_line_byte_pos);
// one entry for each ancestor of the node (which is what we iterate over)
let is_first = *first_in_line.last().unwrap();
// Apply all indent definitions for this node. // Apply all indent definitions for this node.
// Since we only iterate over each node once, we can remove the // Since we only iterate over each node once, we can remove the
@ -906,7 +880,6 @@ pub fn treesitter_indent_for_pos<'a>(
} }
node = parent; node = parent;
first_in_line.pop();
} else { } else {
// Only add the indentation for the line below if that line // Only add the indentation for the line below if that line
// is not after the line that the indentation is calculated for. // is not after the line that the indentation is calculated for.
@ -970,7 +943,7 @@ pub fn indent_for_newline(
let mut num_attempts = 0; let mut num_attempts = 0;
for line_idx in (0..=line_before).rev() { for line_idx in (0..=line_before).rev() {
let line = text.line(line_idx); let line = text.line(line_idx);
let first_non_whitespace_char = match find_first_non_whitespace_char(line) { let first_non_whitespace_char = match line.first_non_whitespace_char() {
Some(i) => i, Some(i) => i,
None => { None => {
continue; continue;

@ -39,9 +39,6 @@ pub mod unicode {
pub use helix_loader::find_workspace; pub use helix_loader::find_workspace;
pub fn find_first_non_whitespace_char(line: RopeSlice) -> Option<usize> {
line.chars().position(|ch| !ch.is_whitespace())
}
mod rope_reader; mod rope_reader;
pub use rope_reader::RopeReader; pub use rope_reader::RopeReader;

@ -9,16 +9,32 @@ use crate::Syntax;
const MAX_PLAINTEXT_SCAN: usize = 10000; const MAX_PLAINTEXT_SCAN: usize = 10000;
const MATCH_LIMIT: usize = 16; const MATCH_LIMIT: usize = 16;
// Limit matching pairs to only ( ) { } [ ] < > ' ' " " pub const BRACKETS: [(char, char); 7] = [
const PAIRS: &[(char, char)] = &[
('(', ')'), ('(', ')'),
('{', '}'), ('{', '}'),
('[', ']'), ('[', ']'),
('<', '>'), ('<', '>'),
('\'', '\''), ('«', '»'),
('\"', '\"'), ('「', '」'),
('', ''),
]; ];
// The difference between BRACKETS and PAIRS is that we can find matching
// BRACKETS in a plain text file, but we can't do the same for PAIRs.
// PAIRS also contains all BRACKETS.
pub const PAIRS: [(char, char); BRACKETS.len() + 3] = {
let mut pairs = [(' ', ' '); BRACKETS.len() + 3];
let mut idx = 0;
while idx < BRACKETS.len() {
pairs[idx] = BRACKETS[idx];
idx += 1;
}
pairs[idx] = ('"', '"');
pairs[idx + 1] = ('\'', '\'');
pairs[idx + 2] = ('`', '`');
pairs
};
/// 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 /// If the cursor is on the opening bracket, the position of
@ -30,7 +46,7 @@ const PAIRS: &[(char, char)] = &[
/// If no matching bracket is found, `None` is returned. /// If no matching bracket is found, `None` is returned.
#[must_use] #[must_use]
pub fn find_matching_bracket(syntax: &Syntax, doc: RopeSlice, 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_pair(doc.char(pos)) {
return None; return None;
} }
find_pair(syntax, doc, pos, false) find_pair(syntax, doc, pos, false)
@ -67,7 +83,7 @@ fn find_pair(
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));
if is_valid_pair(doc, start_char, end_char) { if is_valid_pair_on_pos(doc, start_char, end_char) {
if end_byte == pos { if end_byte == pos {
return Some(start_char); return Some(start_char);
} }
@ -140,14 +156,22 @@ fn find_pair(
/// If no matching bracket is found, `None` is returned. /// If no matching bracket is found, `None` is returned.
#[must_use] #[must_use]
pub fn find_matching_bracket_plaintext(doc: RopeSlice, cursor_pos: usize) -> Option<usize> { 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.get_char(cursor_pos)?; let bracket = doc.get_char(cursor_pos)?;
let matching_bracket = {
let pair = get_pair(bracket);
if pair.0 == bracket {
pair.1
} else {
pair.0
}
};
// Don't do anything when the cursor is not on top of a bracket.
if !is_valid_bracket(bracket) { if !is_valid_bracket(bracket) {
return None; return None;
} }
// Determine the direction of the matching. // Determine the direction of the matching.
let is_fwd = is_forward_bracket(bracket); let is_fwd = is_open_bracket(bracket);
let chars_iter = if is_fwd { let chars_iter = if is_fwd {
doc.chars_at(cursor_pos + 1) doc.chars_at(cursor_pos + 1)
} else { } else {
@ -159,19 +183,7 @@ pub fn find_matching_bracket_plaintext(doc: RopeSlice, cursor_pos: usize) -> Opt
for (i, candidate) in chars_iter.take(MAX_PLAINTEXT_SCAN).enumerate() { for (i, candidate) in chars_iter.take(MAX_PLAINTEXT_SCAN).enumerate() {
if candidate == bracket { if candidate == bracket {
open_cnt += 1; open_cnt += 1;
} else if is_valid_pair( } else if candidate == matching_bracket {
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. // Return when all pending brackets have been closed.
if open_cnt == 1 { if open_cnt == 1 {
return Some(if is_fwd { return Some(if is_fwd {
@ -187,15 +199,49 @@ pub fn find_matching_bracket_plaintext(doc: RopeSlice, cursor_pos: usize) -> Opt
None None
} }
fn is_valid_bracket(c: char) -> bool { /// Returns the open and closing chars pair. If not found in
PAIRS.iter().any(|(l, r)| *l == c || *r == c) /// [`BRACKETS`] returns (ch, ch).
///
/// ```
/// use helix_core::match_brackets::get_pair;
///
/// assert_eq!(get_pair('['), ('[', ']'));
/// assert_eq!(get_pair('}'), ('{', '}'));
/// assert_eq!(get_pair('"'), ('"', '"'));
/// ```
pub fn get_pair(ch: char) -> (char, char) {
PAIRS
.iter()
.find(|(open, close)| *open == ch || *close == ch)
.copied()
.unwrap_or((ch, ch))
}
pub fn is_open_bracket(ch: char) -> bool {
BRACKETS.iter().any(|(l, _)| *l == ch)
}
pub fn is_close_bracket(ch: char) -> bool {
BRACKETS.iter().any(|(_, r)| *r == ch)
}
pub fn is_valid_bracket(ch: char) -> bool {
BRACKETS.iter().any(|(l, r)| *l == ch || *r == ch)
}
pub fn is_open_pair(ch: char) -> bool {
PAIRS.iter().any(|(l, _)| *l == ch)
}
pub fn is_close_pair(ch: char) -> bool {
PAIRS.iter().any(|(_, r)| *r == ch)
} }
fn is_forward_bracket(c: char) -> bool { pub fn is_valid_pair(ch: char) -> bool {
PAIRS.iter().any(|(l, _)| *l == c) PAIRS.iter().any(|(l, r)| *l == ch || *r == ch)
} }
fn is_valid_pair(doc: RopeSlice, start_char: usize, end_char: usize) -> bool { fn is_valid_pair_on_pos(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)))
} }

@ -1,76 +1,137 @@
use crate::{Range, RopeSlice, Selection, Syntax}; use crate::{movement::Direction, syntax::TreeCursor, Range, RopeSlice, Selection, Syntax};
use tree_sitter::Node;
pub fn expand_selection(syntax: &Syntax, text: RopeSlice, selection: Selection) -> Selection { pub fn expand_selection(syntax: &Syntax, text: RopeSlice, selection: Selection) -> Selection {
select_node_impl(syntax, text, selection, |mut node, from, to| { let cursor = &mut syntax.walk();
while node.start_byte() == from && node.end_byte() == to {
node = node.parent()?; selection.transform(|range| {
let from = text.char_to_byte(range.from());
let to = text.char_to_byte(range.to());
let byte_range = from..to;
cursor.reset_to_byte_range(from, to);
while cursor.node().byte_range() == byte_range {
if !cursor.goto_parent() {
break;
} }
Some(node) }
let node = cursor.node();
let from = text.byte_to_char(node.start_byte());
let to = text.byte_to_char(node.end_byte());
Range::new(to, from).with_direction(range.direction())
}) })
} }
pub fn shrink_selection(syntax: &Syntax, text: RopeSlice, selection: Selection) -> Selection { pub fn shrink_selection(syntax: &Syntax, text: RopeSlice, selection: Selection) -> Selection {
select_node_impl(syntax, text, selection, |descendant, _from, _to| { select_node_impl(
descendant.child(0).or(Some(descendant)) syntax,
}) text,
selection,
|cursor| {
cursor.goto_first_child();
},
None,
)
} }
pub fn select_sibling<F>( pub fn select_next_sibling(syntax: &Syntax, text: RopeSlice, selection: Selection) -> Selection {
syntax: &Syntax, select_node_impl(
text: RopeSlice, syntax,
selection: Selection, text,
sibling_fn: &F, selection,
) -> Selection |cursor| {
where while !cursor.goto_next_sibling() {
F: Fn(Node) -> Option<Node>, if !cursor.goto_parent() {
{ break;
select_node_impl(syntax, text, selection, |descendant, _from, _to| { }
find_sibling_recursive(descendant, sibling_fn) }
},
Some(Direction::Forward),
)
}
pub fn select_all_siblings(syntax: &Syntax, text: RopeSlice, selection: Selection) -> Selection {
selection.transform_iter(|range| {
let mut cursor = syntax.walk();
let (from, to) = range.into_byte_range(text);
cursor.reset_to_byte_range(from, to);
if !cursor.goto_parent_with(|parent| parent.child_count() > 1) {
return vec![range].into_iter();
}
select_children(&mut cursor, text, range).into_iter()
}) })
} }
fn find_sibling_recursive<F>(node: Node, sibling_fn: F) -> Option<Node> pub fn select_all_children(syntax: &Syntax, text: RopeSlice, selection: Selection) -> Selection {
where selection.transform_iter(|range| {
F: Fn(Node) -> Option<Node>, let mut cursor = syntax.walk();
{ let (from, to) = range.into_byte_range(text);
sibling_fn(node).or_else(|| { cursor.reset_to_byte_range(from, to);
node.parent() select_children(&mut cursor, text, range).into_iter()
.and_then(|node| find_sibling_recursive(node, sibling_fn))
}) })
} }
fn select_children<'n>(
cursor: &'n mut TreeCursor<'n>,
text: RopeSlice,
range: Range,
) -> Vec<Range> {
let children = cursor
.named_children()
.map(|child| Range::from_node(child, text, range.direction()))
.collect::<Vec<_>>();
if !children.is_empty() {
children
} else {
vec![range]
}
}
pub fn select_prev_sibling(syntax: &Syntax, text: RopeSlice, selection: Selection) -> Selection {
select_node_impl(
syntax,
text,
selection,
|cursor| {
while !cursor.goto_prev_sibling() {
if !cursor.goto_parent() {
break;
}
}
},
Some(Direction::Backward),
)
}
fn select_node_impl<F>( fn select_node_impl<F>(
syntax: &Syntax, syntax: &Syntax,
text: RopeSlice, text: RopeSlice,
selection: Selection, selection: Selection,
select_fn: F, motion: F,
direction: Option<Direction>,
) -> Selection ) -> Selection
where where
F: Fn(Node, usize, usize) -> Option<Node>, F: Fn(&mut TreeCursor),
{ {
let tree = syntax.tree(); let cursor = &mut syntax.walk();
selection.transform(|range| { selection.transform(|range| {
let from = text.char_to_byte(range.from()); let from = text.char_to_byte(range.from());
let to = text.char_to_byte(range.to()); let to = text.char_to_byte(range.to());
let node = match tree cursor.reset_to_byte_range(from, to);
.root_node()
.descendant_for_byte_range(from, to) motion(cursor);
.and_then(|node| select_fn(node, from, to))
{
Some(node) => node,
None => return range,
};
let node = cursor.node();
let from = text.byte_to_char(node.start_byte()); let from = text.byte_to_char(node.start_byte());
let to = text.byte_to_char(node.end_byte()); let to = text.byte_to_char(node.end_byte());
if range.head < range.anchor { Range::new(from, to).with_direction(direction.unwrap_or_else(|| range.direction()))
Range::new(to, from)
} else {
Range::new(from, to)
}
}) })
} }

@ -7,11 +7,14 @@ use crate::{
ensure_grapheme_boundary_next, ensure_grapheme_boundary_prev, next_grapheme_boundary, ensure_grapheme_boundary_next, ensure_grapheme_boundary_prev, next_grapheme_boundary,
prev_grapheme_boundary, prev_grapheme_boundary,
}, },
line_ending::get_line_ending,
movement::Direction, movement::Direction,
Assoc, ChangeSet, RopeGraphemes, RopeSlice, Assoc, ChangeSet, RopeGraphemes, RopeSlice,
}; };
use helix_stdx::rope::{self, RopeSliceExt};
use smallvec::{smallvec, SmallVec}; use smallvec::{smallvec, SmallVec};
use std::borrow::Cow; use std::borrow::Cow;
use tree_sitter::Node;
/// A single selection range. /// A single selection range.
/// ///
@ -71,6 +74,12 @@ impl Range {
Self::new(head, head) Self::new(head, head)
} }
pub fn from_node(node: Node, text: RopeSlice, direction: Direction) -> Self {
let from = text.byte_to_char(node.start_byte());
let to = text.byte_to_char(node.end_byte());
Range::new(from, to).with_direction(direction)
}
/// Start of the range. /// Start of the range.
#[inline] #[inline]
#[must_use] #[must_use]
@ -113,7 +122,7 @@ impl Range {
} }
/// `Direction::Backward` when head < anchor. /// `Direction::Backward` when head < anchor.
/// `Direction::Backward` otherwise. /// `Direction::Forward` otherwise.
#[inline] #[inline]
#[must_use] #[must_use]
pub fn direction(&self) -> Direction { pub fn direction(&self) -> Direction {
@ -374,6 +383,12 @@ impl Range {
let second = graphemes.next(); let second = graphemes.next();
first.is_some() && second.is_none() first.is_some() && second.is_none()
} }
/// Converts this char range into an in order byte range, discarding
/// direction.
pub fn into_byte_range(&self, text: RopeSlice) -> (usize, usize) {
(text.char_to_byte(self.from()), text.char_to_byte(self.to()))
}
} }
impl From<(usize, usize)> for Range { impl From<(usize, usize)> for Range {
@ -703,17 +718,26 @@ impl IntoIterator for Selection {
} }
} }
impl From<Range> for Selection {
fn from(range: Range) -> Self {
Self {
ranges: smallvec![range],
primary_index: 0,
}
}
}
// TODO: checkSelection -> check if valid for doc length && sorted // TODO: checkSelection -> check if valid for doc length && sorted
pub fn keep_or_remove_matches( pub fn keep_or_remove_matches(
text: RopeSlice, text: RopeSlice,
selection: &Selection, selection: &Selection,
regex: &crate::regex::Regex, regex: &rope::Regex,
remove: bool, remove: bool,
) -> Option<Selection> { ) -> Option<Selection> {
let result: SmallVec<_> = selection let result: SmallVec<_> = selection
.iter() .iter()
.filter(|range| regex.is_match(&range.fragment(text)) ^ remove) .filter(|range| regex.is_match(text.regex_input_at(range.from()..range.to())) ^ remove)
.copied() .copied()
.collect(); .collect();
@ -724,25 +748,20 @@ pub fn keep_or_remove_matches(
None None
} }
// TODO: support to split on capture #N instead of whole match
pub fn select_on_matches( pub fn select_on_matches(
text: RopeSlice, text: RopeSlice,
selection: &Selection, selection: &Selection,
regex: &crate::regex::Regex, regex: &rope::Regex,
) -> Option<Selection> { ) -> Option<Selection> {
let mut result = SmallVec::with_capacity(selection.len()); let mut result = SmallVec::with_capacity(selection.len());
for sel in selection { for sel in selection {
// TODO: can't avoid occasional allocations since Regex can't operate on chunks yet for mat in regex.find_iter(text.regex_input_at(sel.from()..sel.to())) {
let fragment = sel.fragment(text);
let sel_start = sel.from();
let start_byte = text.char_to_byte(sel_start);
for mat in regex.find_iter(&fragment) {
// TODO: retain range direction // TODO: retain range direction
let start = text.byte_to_char(start_byte + mat.start()); let start = text.byte_to_char(mat.start());
let end = text.byte_to_char(start_byte + mat.end()); let end = text.byte_to_char(mat.end());
let range = Range::new(start, end); let range = Range::new(start, end);
// Make sure the match is not right outside of the selection. // Make sure the match is not right outside of the selection.
@ -761,12 +780,7 @@ pub fn select_on_matches(
None None
} }
// TODO: support to split on capture #N instead of whole match pub fn split_on_newline(text: RopeSlice, selection: &Selection) -> Selection {
pub fn split_on_matches(
text: RopeSlice,
selection: &Selection,
regex: &crate::regex::Regex,
) -> Selection {
let mut result = SmallVec::with_capacity(selection.len()); let mut result = SmallVec::with_capacity(selection.len());
for sel in selection { for sel in selection {
@ -776,21 +790,49 @@ pub fn split_on_matches(
continue; continue;
} }
// TODO: can't avoid occasional allocations since Regex can't operate on chunks yet
let fragment = sel.fragment(text);
let sel_start = sel.from(); let sel_start = sel.from();
let sel_end = sel.to(); let sel_end = sel.to();
let start_byte = text.char_to_byte(sel_start); let mut start = sel_start;
for line in sel.slice(text).lines() {
let Some(line_ending) = get_line_ending(&line) else {
break;
};
let line_end = start + line.len_chars();
// TODO: retain range direction
result.push(Range::new(start, line_end - line_ending.len_chars()));
start = line_end;
}
if start < sel_end {
result.push(Range::new(start, sel_end));
}
}
// TODO: figure out a new primary index
Selection::new(result, 0)
}
pub fn split_on_matches(text: RopeSlice, selection: &Selection, regex: &rope::Regex) -> Selection {
let mut result = SmallVec::with_capacity(selection.len());
for sel in selection {
// Special case: zero-width selection.
if sel.from() == sel.to() {
result.push(*sel);
continue;
}
let sel_start = sel.from();
let sel_end = sel.to();
let mut start = sel_start; let mut start = sel_start;
for mat in regex.find_iter(&fragment) { for mat in regex.find_iter(text.regex_input_at(sel_start..sel_end)) {
// TODO: retain range direction // TODO: retain range direction
let end = text.byte_to_char(start_byte + mat.start()); let end = text.byte_to_char(mat.start());
result.push(Range::new(start, end)); result.push(Range::new(start, end));
start = text.byte_to_char(start_byte + mat.end()); start = text.byte_to_char(mat.end());
} }
if start < sel_end { if start < sel_end {
@ -1021,14 +1063,12 @@ mod test {
#[test] #[test]
fn test_select_on_matches() { fn test_select_on_matches() {
use crate::regex::{Regex, RegexBuilder};
let r = Rope::from_str("Nobody expects the Spanish inquisition"); let r = Rope::from_str("Nobody expects the Spanish inquisition");
let s = r.slice(..); let s = r.slice(..);
let selection = Selection::single(0, r.len_chars()); let selection = Selection::single(0, r.len_chars());
assert_eq!( assert_eq!(
select_on_matches(s, &selection, &Regex::new(r"[A-Z][a-z]*").unwrap()), select_on_matches(s, &selection, &rope::Regex::new(r"[A-Z][a-z]*").unwrap()),
Some(Selection::new( Some(Selection::new(
smallvec![Range::new(0, 6), Range::new(19, 26)], smallvec![Range::new(0, 6), Range::new(19, 26)],
0 0
@ -1038,8 +1078,14 @@ mod test {
let r = Rope::from_str("This\nString\n\ncontains multiple\nlines"); let r = Rope::from_str("This\nString\n\ncontains multiple\nlines");
let s = r.slice(..); let s = r.slice(..);
let start_of_line = RegexBuilder::new(r"^").multi_line(true).build().unwrap(); let start_of_line = rope::RegexBuilder::new()
let end_of_line = RegexBuilder::new(r"$").multi_line(true).build().unwrap(); .syntax(rope::Config::new().multi_line(true))
.build(r"^")
.unwrap();
let end_of_line = rope::RegexBuilder::new()
.syntax(rope::Config::new().multi_line(true))
.build(r"$")
.unwrap();
// line without ending // line without ending
assert_eq!( assert_eq!(
@ -1077,9 +1123,9 @@ mod test {
select_on_matches( select_on_matches(
s, s,
&Selection::single(0, s.len_chars()), &Selection::single(0, s.len_chars()),
&RegexBuilder::new(r"^[a-z ]*$") &rope::RegexBuilder::new()
.multi_line(true) .syntax(rope::Config::new().multi_line(true))
.build() .build(r"^[a-z ]*$")
.unwrap() .unwrap()
), ),
Some(Selection::new( Some(Selection::new(
@ -1171,13 +1217,15 @@ mod test {
#[test] #[test]
fn test_split_on_matches() { fn test_split_on_matches() {
use crate::regex::Regex;
let text = Rope::from(" abcd efg wrs xyz 123 456"); let text = Rope::from(" abcd efg wrs xyz 123 456");
let selection = Selection::new(smallvec![Range::new(0, 9), Range::new(11, 20),], 0); let selection = Selection::new(smallvec![Range::new(0, 9), Range::new(11, 20),], 0);
let result = split_on_matches(text.slice(..), &selection, &Regex::new(r"\s+").unwrap()); let result = split_on_matches(
text.slice(..),
&selection,
&rope::Regex::new(r"\s+").unwrap(),
);
assert_eq!( assert_eq!(
result.ranges(), result.ranges(),

@ -1,18 +1,16 @@
use std::fmt::Display; use std::fmt::Display;
use crate::{movement::Direction, search, Range, Selection}; use crate::{
graphemes::next_grapheme_boundary,
match_brackets::{
find_matching_bracket, find_matching_bracket_fuzzy, get_pair, is_close_bracket,
is_open_bracket,
},
movement::Direction,
search, Range, Selection, Syntax,
};
use ropey::RopeSlice; use ropey::RopeSlice;
pub const PAIRS: &[(char, char)] = &[
('(', ')'),
('[', ']'),
('{', '}'),
('<', '>'),
('«', '»'),
('「', '」'),
('', ''),
];
#[derive(Debug, PartialEq, Eq)] #[derive(Debug, PartialEq, Eq)]
pub enum Error { pub enum Error {
PairNotFound, PairNotFound,
@ -34,32 +32,68 @@ impl Display for Error {
type Result<T> = std::result::Result<T, Error>; type Result<T> = std::result::Result<T, Error>;
/// Given any char in [PAIRS], return the open and closing chars. If not found in /// Finds the position of surround pairs of any [`crate::match_brackets::PAIRS`]
/// [PAIRS] return (ch, ch). /// using tree-sitter when possible.
/// ///
/// ``` /// # Returns
/// use helix_core::surround::get_pair;
/// ///
/// assert_eq!(get_pair('['), ('[', ']')); /// Tuple `(anchor, head)`, meaning it is not always ordered.
/// assert_eq!(get_pair('}'), ('{', '}')); pub fn find_nth_closest_pairs_pos(
/// assert_eq!(get_pair('"'), ('"', '"')); syntax: Option<&Syntax>,
/// ``` text: RopeSlice,
pub fn get_pair(ch: char) -> (char, char) { range: Range,
PAIRS skip: usize,
.iter() ) -> Result<(usize, usize)> {
.find(|(open, close)| *open == ch || *close == ch) match syntax {
.copied() Some(syntax) => find_nth_closest_pairs_ts(syntax, text, range, skip),
.unwrap_or((ch, ch)) None => find_nth_closest_pairs_plain(text, range, skip),
}
} }
pub fn find_nth_closest_pairs_pos( fn find_nth_closest_pairs_ts(
syntax: &Syntax,
text: RopeSlice, text: RopeSlice,
range: Range, range: Range,
mut skip: usize, mut skip: usize,
) -> Result<(usize, usize)> { ) -> Result<(usize, usize)> {
let is_open_pair = |ch| PAIRS.iter().any(|(open, _)| *open == ch); let mut opening = range.from();
let is_close_pair = |ch| PAIRS.iter().any(|(_, close)| *close == ch); // We want to expand the selection if we are already on the found pair,
// otherwise we would need to subtract "-1" from "range.to()".
let mut closing = range.to();
while skip > 0 {
closing = find_matching_bracket_fuzzy(syntax, text, closing).ok_or(Error::PairNotFound)?;
opening = find_matching_bracket(syntax, text, closing).ok_or(Error::PairNotFound)?;
// If we're already on a closing bracket "find_matching_bracket_fuzzy" will return
// the position of the opening bracket.
if closing < opening {
(opening, closing) = (closing, opening);
}
// In case found brackets are partially inside current selection.
if range.from() < opening || closing < range.to() - 1 {
closing = next_grapheme_boundary(text, closing);
} else {
skip -= 1;
if skip != 0 {
closing = next_grapheme_boundary(text, closing);
}
}
}
// Keep the original direction.
if let Direction::Forward = range.direction() {
Ok((opening, closing))
} else {
Ok((closing, opening))
}
}
fn find_nth_closest_pairs_plain(
text: RopeSlice,
range: Range,
mut skip: usize,
) -> Result<(usize, usize)> {
let mut stack = Vec::with_capacity(2); let mut stack = Vec::with_capacity(2);
let pos = range.from(); let pos = range.from();
let mut close_pos = pos.saturating_sub(1); let mut close_pos = pos.saturating_sub(1);
@ -67,7 +101,7 @@ pub fn find_nth_closest_pairs_pos(
for ch in text.chars_at(pos) { for ch in text.chars_at(pos) {
close_pos += 1; close_pos += 1;
if is_open_pair(ch) { if is_open_bracket(ch) {
// Track open pairs encountered so that we can step over // Track open pairs encountered so that we can step over
// the corresponding close pairs that will come up further // the corresponding close pairs that will come up further
// down the loop. We want to find a lone close pair whose // down the loop. We want to find a lone close pair whose
@ -76,7 +110,7 @@ pub fn find_nth_closest_pairs_pos(
continue; continue;
} }
if !is_close_pair(ch) { if !is_close_bracket(ch) {
// We don't care if this character isn't a brace pair item, // We don't care if this character isn't a brace pair item,
// so short circuit here. // so short circuit here.
continue; continue;
@ -157,7 +191,11 @@ pub fn find_nth_pairs_pos(
) )
}; };
Option::zip(open, close).ok_or(Error::PairNotFound) // preserve original direction
match range.direction() {
Direction::Forward => Option::zip(open, close).ok_or(Error::PairNotFound),
Direction::Backward => Option::zip(close, open).ok_or(Error::PairNotFound),
}
} }
fn find_nth_open_pair( fn find_nth_open_pair(
@ -167,6 +205,10 @@ fn find_nth_open_pair(
mut pos: usize, mut pos: usize,
n: usize, n: usize,
) -> Option<usize> { ) -> Option<usize> {
if pos >= text.len_chars() {
return None;
}
let mut chars = text.chars_at(pos + 1); let mut chars = text.chars_at(pos + 1);
// Adjusts pos for the first iteration, and handles the case of the // Adjusts pos for the first iteration, and handles the case of the
@ -245,6 +287,7 @@ fn find_nth_close_pair(
/// are automatically detected around each cursor (note that this may result /// are automatically detected around each cursor (note that this may result
/// in them selecting different surround characters for each selection). /// in them selecting different surround characters for each selection).
pub fn get_surround_pos( pub fn get_surround_pos(
syntax: Option<&Syntax>,
text: RopeSlice, text: RopeSlice,
selection: &Selection, selection: &Selection,
ch: Option<char>, ch: Option<char>,
@ -253,14 +296,19 @@ pub fn get_surround_pos(
let mut change_pos = Vec::new(); let mut change_pos = Vec::new();
for &range in selection { for &range in selection {
let (open_pos, close_pos) = match ch { let (open_pos, close_pos) = {
let range_raw = match ch {
Some(ch) => find_nth_pairs_pos(text, ch, range, skip)?, Some(ch) => find_nth_pairs_pos(text, ch, range, skip)?,
None => find_nth_closest_pairs_pos(text, range, skip)?, None => find_nth_closest_pairs_pos(syntax, text, range, skip)?,
};
let range = Range::new(range_raw.0, range_raw.1);
(range.from(), range.to())
}; };
if change_pos.contains(&open_pos) || change_pos.contains(&close_pos) { if change_pos.contains(&open_pos) || change_pos.contains(&close_pos) {
return Err(Error::CursorOverlap); return Err(Error::CursorOverlap);
} }
change_pos.extend_from_slice(&[open_pos, close_pos]); // ensure the positions are always paired in the forward direction
change_pos.extend_from_slice(&[open_pos.min(close_pos), close_pos.max(open_pos)]);
} }
Ok(change_pos) Ok(change_pos)
} }
@ -283,7 +331,7 @@ mod test {
); );
assert_eq!( assert_eq!(
get_surround_pos(doc.slice(..), &selection, Some('('), 1).unwrap(), get_surround_pos(None, doc.slice(..), &selection, Some('('), 1).unwrap(),
expectations expectations
); );
} }
@ -298,7 +346,7 @@ mod test {
); );
assert_eq!( assert_eq!(
get_surround_pos(doc.slice(..), &selection, Some('('), 1), get_surround_pos(None, doc.slice(..), &selection, Some('('), 1),
Err(Error::PairNotFound) Err(Error::PairNotFound)
); );
} }
@ -313,7 +361,7 @@ mod test {
); );
assert_eq!( assert_eq!(
get_surround_pos(doc.slice(..), &selection, Some('('), 1), get_surround_pos(None, doc.slice(..), &selection, Some('('), 1),
Err(Error::PairNotFound) // overlapping surround chars Err(Error::PairNotFound) // overlapping surround chars
); );
} }
@ -328,7 +376,7 @@ mod test {
); );
assert_eq!( assert_eq!(
get_surround_pos(doc.slice(..), &selection, Some('['), 1), get_surround_pos(None, doc.slice(..), &selection, Some('['), 1),
Err(Error::CursorOverlap) Err(Error::CursorOverlap)
); );
} }
@ -382,6 +430,21 @@ mod test {
) )
} }
#[test]
fn test_find_nth_closest_pairs_pos_index_range_panic() {
#[rustfmt::skip]
let (doc, selection, _) =
rope_with_selections_and_expectations(
"(a)c)",
"^^^^^"
);
assert_eq!(
find_nth_closest_pairs_pos(None, doc.slice(..), selection.primary(), 1),
Err(Error::PairNotFound)
)
}
// Create a Rope and a matching Selection using a specification language. // Create a Rope and a matching Selection using a specification language.
// ^ is a single-point selection. // ^ is a single-point selection.
// _ is an expected index. These are returned as a Vec<usize> for use in assertions. // _ is an expected index. These are returned as a Vec<usize> for use in assertions.

@ -1,3 +1,5 @@
mod tree_cursor;
use crate::{ use crate::{
auto_pairs::AutoPairs, auto_pairs::AutoPairs,
chars::char_is_line_ending, chars::char_is_line_ending,
@ -10,7 +12,9 @@ use crate::{
use ahash::RandomState; use ahash::RandomState;
use arc_swap::{ArcSwap, Guard}; use arc_swap::{ArcSwap, Guard};
use bitflags::bitflags; use bitflags::bitflags;
use globset::GlobSet;
use hashbrown::raw::RawTable; use hashbrown::raw::RawTable;
use helix_stdx::rope::{self, RopeSliceExt};
use slotmap::{DefaultKey as LayerId, HopSlotMap}; use slotmap::{DefaultKey as LayerId, HopSlotMap};
use std::{ use std::{
@ -19,7 +23,7 @@ use std::{
collections::{HashMap, HashSet, VecDeque}, collections::{HashMap, HashSet, VecDeque},
fmt::{self, Display}, fmt::{self, Display},
hash::{Hash, Hasher}, hash::{Hash, Hasher},
mem::{replace, transmute}, mem::replace,
path::{Path, PathBuf}, path::{Path, PathBuf},
str::FromStr, str::FromStr,
sync::Arc, sync::Arc,
@ -30,6 +34,8 @@ use serde::{ser::SerializeSeq, Deserialize, Serialize};
use helix_loader::grammar::{get_language, load_runtime_file}; use helix_loader::grammar::{get_language, load_runtime_file};
pub use tree_cursor::TreeCursor;
fn deserialize_regex<'de, D>(deserializer: D) -> Result<Option<Regex>, D::Error> fn deserialize_regex<'de, D>(deserializer: D) -> Result<Option<Regex>, D::Error>
where where
D: serde::Deserializer<'de>, D: serde::Deserializer<'de>,
@ -82,12 +88,6 @@ pub struct Configuration {
pub language_server: HashMap<String, LanguageServerConfiguration>, pub language_server: HashMap<String, LanguageServerConfiguration>,
} }
impl Default for Configuration {
fn default() -> Self {
crate::config::default_syntax_loader()
}
}
// largely based on tree-sitter/cli/src/loader.rs // largely based on tree-sitter/cli/src/loader.rs
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case", deny_unknown_fields)] #[serde(rename_all = "kebab-case", deny_unknown_fields)]
@ -103,7 +103,19 @@ pub struct LanguageConfiguration {
pub shebangs: Vec<String>, // interpreter(s) associated with language pub shebangs: Vec<String>, // interpreter(s) associated with language
#[serde(default)] #[serde(default)]
pub roots: Vec<String>, // these indicate project roots <.git, Cargo.toml> pub roots: Vec<String>, // these indicate project roots <.git, Cargo.toml>
pub comment_token: Option<String>, #[serde(
default,
skip_serializing,
deserialize_with = "from_comment_tokens",
alias = "comment-token"
)]
pub comment_tokens: Option<Vec<String>>,
#[serde(
default,
skip_serializing,
deserialize_with = "from_block_comment_tokens"
)]
pub block_comment_tokens: Option<Vec<BlockCommentToken>>,
pub text_width: Option<usize>, pub text_width: Option<usize>,
pub soft_wrap: Option<SoftWrap>, pub soft_wrap: Option<SoftWrap>,
@ -164,9 +176,11 @@ pub enum FileType {
/// The extension of the file, either the `Path::extension` or the full /// The extension of the file, either the `Path::extension` or the full
/// filename if the file does not have an extension. /// filename if the file does not have an extension.
Extension(String), Extension(String),
/// The suffix of a file. This is compared to a given file's absolute /// A Unix-style path glob. This is compared to the file's absolute path, so
/// path, so it can be used to detect files based on their directories. /// it can be used to detect files based on their directories. If the glob
Suffix(String), /// is not an absolute path and does not already start with a glob pattern,
/// a glob pattern will be prepended to it.
Glob(globset::Glob),
} }
impl Serialize for FileType { impl Serialize for FileType {
@ -178,9 +192,9 @@ impl Serialize for FileType {
match self { match self {
FileType::Extension(extension) => serializer.serialize_str(extension), FileType::Extension(extension) => serializer.serialize_str(extension),
FileType::Suffix(suffix) => { FileType::Glob(glob) => {
let mut map = serializer.serialize_map(Some(1))?; let mut map = serializer.serialize_map(Some(1))?;
map.serialize_entry("suffix", &suffix.replace(std::path::MAIN_SEPARATOR, "/"))?; map.serialize_entry("glob", glob.glob())?;
map.end() map.end()
} }
} }
@ -213,9 +227,20 @@ 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, mut glob)) if key == "glob" => {
suffix.replace('/', std::path::MAIN_SEPARATOR_STR) // If the glob isn't an absolute path or already starts
})), // with a glob pattern, add a leading glob so we
// properly match relative paths.
if !glob.starts_with('/') && !glob.starts_with("*/") {
glob.insert_str(0, "*/");
}
globset::Glob::new(glob.as_str())
.map(FileType::Glob)
.map_err(|err| {
serde::de::Error::custom(format!("invalid `glob` pattern: {}", err))
})
}
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
@ -231,6 +256,59 @@ impl<'de> Deserialize<'de> for FileType {
} }
} }
fn from_comment_tokens<'de, D>(deserializer: D) -> Result<Option<Vec<String>>, D::Error>
where
D: serde::Deserializer<'de>,
{
#[derive(Deserialize)]
#[serde(untagged)]
enum CommentTokens {
Multiple(Vec<String>),
Single(String),
}
Ok(
Option::<CommentTokens>::deserialize(deserializer)?.map(|tokens| match tokens {
CommentTokens::Single(val) => vec![val],
CommentTokens::Multiple(vals) => vals,
}),
)
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct BlockCommentToken {
pub start: String,
pub end: String,
}
impl Default for BlockCommentToken {
fn default() -> Self {
BlockCommentToken {
start: "/*".to_string(),
end: "*/".to_string(),
}
}
}
fn from_block_comment_tokens<'de, D>(
deserializer: D,
) -> Result<Option<Vec<BlockCommentToken>>, D::Error>
where
D: serde::Deserializer<'de>,
{
#[derive(Deserialize)]
#[serde(untagged)]
enum BlockCommentTokens {
Multiple(Vec<BlockCommentToken>),
Single(BlockCommentToken),
}
Ok(
Option::<BlockCommentTokens>::deserialize(deserializer)?.map(|tokens| match tokens {
BlockCommentTokens::Single(val) => vec![val],
BlockCommentTokens::Multiple(vals) => vals,
}),
)
}
#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq, Eq, Hash)] #[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq, Eq, Hash)]
#[serde(rename_all = "kebab-case")] #[serde(rename_all = "kebab-case")]
pub enum LanguageServerFeature { pub enum LanguageServerFeature {
@ -358,6 +436,22 @@ where
serializer.end() serializer.end()
} }
fn deserialize_required_root_patterns<'de, D>(deserializer: D) -> Result<Option<GlobSet>, D::Error>
where
D: serde::Deserializer<'de>,
{
let patterns = Vec::<String>::deserialize(deserializer)?;
if patterns.is_empty() {
return Ok(None);
}
let mut builder = globset::GlobSetBuilder::new();
for pattern in patterns {
let glob = globset::Glob::new(&pattern).map_err(serde::de::Error::custom)?;
builder.add(glob);
}
builder.build().map(Some).map_err(serde::de::Error::custom)
}
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")] #[serde(rename_all = "kebab-case")]
pub struct LanguageServerConfiguration { pub struct LanguageServerConfiguration {
@ -371,6 +465,12 @@ pub struct LanguageServerConfiguration {
pub config: Option<serde_json::Value>, pub config: Option<serde_json::Value>,
#[serde(default = "default_timeout")] #[serde(default = "default_timeout")]
pub timeout: u64, pub timeout: u64,
#[serde(
default,
skip_serializing,
deserialize_with = "deserialize_required_root_patterns"
)]
pub required_root_patterns: Option<GlobSet>,
} }
#[derive(Debug, Clone, Serialize, Deserialize)] #[derive(Debug, Clone, Serialize, Deserialize)]
@ -709,7 +809,7 @@ impl LanguageConfiguration {
if query_text.is_empty() { if query_text.is_empty() {
return None; return None;
} }
let lang = self.highlight_config.get()?.as_ref()?.language; let lang = &self.highlight_config.get()?.as_ref()?.language;
Query::new(lang, &query_text) Query::new(lang, &query_text)
.map_err(|e| { .map_err(|e| {
log::error!( log::error!(
@ -752,6 +852,47 @@ pub struct SoftWrap {
pub wrap_at_text_width: Option<bool>, pub wrap_at_text_width: Option<bool>,
} }
#[derive(Debug)]
struct FileTypeGlob {
glob: globset::Glob,
language_id: usize,
}
impl FileTypeGlob {
fn new(glob: globset::Glob, language_id: usize) -> Self {
Self { glob, language_id }
}
}
#[derive(Debug)]
struct FileTypeGlobMatcher {
matcher: globset::GlobSet,
file_types: Vec<FileTypeGlob>,
}
impl FileTypeGlobMatcher {
fn new(file_types: Vec<FileTypeGlob>) -> Result<Self, globset::Error> {
let mut builder = globset::GlobSetBuilder::new();
for file_type in &file_types {
builder.add(file_type.glob.clone());
}
Ok(Self {
matcher: builder.build()?,
file_types,
})
}
fn language_id_for_path(&self, path: &Path) -> Option<&usize> {
self.matcher
.matches(path)
.iter()
.filter_map(|idx| self.file_types.get(*idx))
.max_by_key(|file_type| file_type.glob.glob().len())
.map(|file_type| &file_type.language_id)
}
}
// Expose loader as Lazy<> global since it's always static? // Expose loader as Lazy<> global since it's always static?
#[derive(Debug)] #[derive(Debug)]
@ -759,7 +900,7 @@ pub struct Loader {
// highlight_names ? // highlight_names ?
language_configs: Vec<Arc<LanguageConfiguration>>, language_configs: Vec<Arc<LanguageConfiguration>>,
language_config_ids_by_extension: HashMap<String, usize>, // Vec<usize> language_config_ids_by_extension: HashMap<String, usize>, // Vec<usize>
language_config_ids_by_suffix: HashMap<String, usize>, language_config_ids_glob_matcher: FileTypeGlobMatcher,
language_config_ids_by_shebang: HashMap<String, usize>, language_config_ids_by_shebang: HashMap<String, usize>,
language_server_configs: HashMap<String, LanguageServerConfiguration>, language_server_configs: HashMap<String, LanguageServerConfiguration>,
@ -767,66 +908,57 @@ pub struct Loader {
scopes: ArcSwap<Vec<String>>, scopes: ArcSwap<Vec<String>>,
} }
pub type LoaderError = globset::Error;
impl Loader { impl Loader {
pub fn new(config: Configuration) -> Self { pub fn new(config: Configuration) -> Result<Self, LoaderError> {
let mut loader = Self { let mut language_configs = Vec::new();
language_configs: Vec::new(), let mut language_config_ids_by_extension = HashMap::new();
language_server_configs: config.language_server, let mut language_config_ids_by_shebang = HashMap::new();
language_config_ids_by_extension: HashMap::new(), let mut file_type_globs = Vec::new();
language_config_ids_by_suffix: HashMap::new(),
language_config_ids_by_shebang: HashMap::new(),
scopes: ArcSwap::from_pointee(Vec::new()),
};
for config in config.language { for config in config.language {
// get the next id // get the next id
let language_id = loader.language_configs.len(); let language_id = language_configs.len();
for file_type in &config.file_types { for file_type in &config.file_types {
// entry().or_insert(Vec::new).push(language_id); // entry().or_insert(Vec::new).push(language_id);
match file_type { match file_type {
FileType::Extension(extension) => loader FileType::Extension(extension) => {
.language_config_ids_by_extension language_config_ids_by_extension.insert(extension.clone(), language_id);
.insert(extension.clone(), language_id), }
FileType::Suffix(suffix) => loader FileType::Glob(glob) => {
.language_config_ids_by_suffix file_type_globs.push(FileTypeGlob::new(glob.to_owned(), language_id));
.insert(suffix.clone(), language_id), }
}; };
} }
for shebang in &config.shebangs { for shebang in &config.shebangs {
loader language_config_ids_by_shebang.insert(shebang.clone(), language_id);
.language_config_ids_by_shebang
.insert(shebang.clone(), language_id);
} }
loader.language_configs.push(Arc::new(config)); language_configs.push(Arc::new(config));
} }
loader Ok(Self {
language_configs,
language_config_ids_by_extension,
language_config_ids_glob_matcher: FileTypeGlobMatcher::new(file_type_globs)?,
language_config_ids_by_shebang,
language_server_configs: config.language_server,
scopes: ArcSwap::from_pointee(Vec::new()),
})
} }
pub fn language_config_for_file_name(&self, path: &Path) -> Option<Arc<LanguageConfiguration>> { pub fn language_config_for_file_name(&self, path: &Path) -> Option<Arc<LanguageConfiguration>> {
// Find all the language configurations that match this file name // Find all the language configurations that match this file name
// or a suffix of the file name. // or a suffix of the file name.
let configuration_id = path let configuration_id = self
.file_name() .language_config_ids_glob_matcher
.and_then(|n| n.to_str()) .language_id_for_path(path)
.and_then(|file_name| self.language_config_ids_by_extension.get(file_name))
.or_else(|| { .or_else(|| {
path.extension() path.extension()
.and_then(|extension| extension.to_str()) .and_then(|extension| extension.to_str())
.and_then(|extension| self.language_config_ids_by_extension.get(extension)) .and_then(|extension| self.language_config_ids_by_extension.get(extension))
})
.or_else(|| {
self.language_config_ids_by_suffix
.iter()
.find_map(|(file_type, id)| {
if path.to_str()?.ends_with(file_type) {
Some(id)
} else {
None
}
})
}); });
configuration_id.and_then(|&id| self.language_configs.get(id).cloned()) configuration_id.and_then(|&id| self.language_configs.get(id).cloned())
@ -938,7 +1070,7 @@ thread_local! {
pub struct Syntax { pub struct Syntax {
layers: HopSlotMap<LayerId, LanguageLayer>, layers: HopSlotMap<LayerId, LanguageLayer>,
root: LayerId, root: LayerId,
loader: Arc<Loader>, loader: Arc<ArcSwap<Loader>>,
} }
fn byte_range_to_str(range: std::ops::Range<usize>, source: RopeSlice) -> Cow<str> { fn byte_range_to_str(range: std::ops::Range<usize>, source: RopeSlice) -> Cow<str> {
@ -949,7 +1081,7 @@ impl Syntax {
pub fn new( pub fn new(
source: RopeSlice, source: RopeSlice,
config: Arc<HighlightConfiguration>, config: Arc<HighlightConfiguration>,
loader: Arc<Loader>, loader: Arc<ArcSwap<Loader>>,
) -> Option<Self> { ) -> Option<Self> {
let root_layer = LanguageLayer { let root_layer = LanguageLayer {
tree: None, tree: None,
@ -962,6 +1094,7 @@ impl Syntax {
start_point: Point::new(0, 0), start_point: Point::new(0, 0),
end_point: Point::new(usize::MAX, usize::MAX), end_point: Point::new(usize::MAX, usize::MAX),
}], }],
parent: None,
}; };
// track scope_descriptor: a Vec of scopes for item in tree // track scope_descriptor: a Vec of scopes for item in tree
@ -993,9 +1126,10 @@ impl Syntax {
let mut queue = VecDeque::new(); let mut queue = VecDeque::new();
queue.push_back(self.root); queue.push_back(self.root);
let scopes = self.loader.scopes.load(); let loader = self.loader.load();
let scopes = loader.scopes.load();
let injection_callback = |language: &InjectionLanguageMarker| { let injection_callback = |language: &InjectionLanguageMarker| {
self.loader loader
.language_configuration_for_injection_string(language) .language_configuration_for_injection_string(language)
.and_then(|language_config| language_config.highlight_config(&scopes)) .and_then(|language_config| language_config.highlight_config(&scopes))
}; };
@ -1231,6 +1365,7 @@ impl Syntax {
depth, depth,
ranges, ranges,
flags: LayerUpdateFlags::empty(), flags: LayerUpdateFlags::empty(),
parent: Some(layer_id),
}; };
// Find an identical existing layer // Find an identical existing layer
@ -1364,6 +1499,12 @@ impl Syntax {
.descendant_for_byte_range(start, end) .descendant_for_byte_range(start, end)
} }
pub fn walk(&self) -> TreeCursor<'_> {
// data structure to find the smallest range that contains a point
// when some of the ranges in the structure can overlap.
TreeCursor::new(&self.layers, self.root)
}
// Commenting // Commenting
// comment_strings_for_pos // comment_strings_for_pos
// is_commented // is_commented
@ -1396,6 +1537,7 @@ pub struct LanguageLayer {
pub ranges: Vec<Range>, pub ranges: Vec<Range>,
pub depth: u32, pub depth: u32,
flags: LayerUpdateFlags, flags: LayerUpdateFlags,
parent: Option<LayerId>,
} }
/// This PartialEq implementation only checks if that /// This PartialEq implementation only checks if that
@ -1415,13 +1557,7 @@ impl PartialEq for LanguageLayer {
impl Hash for LanguageLayer { impl Hash for LanguageLayer {
fn hash<H: Hasher>(&self, state: &mut H) { fn hash<H: Hasher>(&self, state: &mut H) {
self.depth.hash(state); self.depth.hash(state);
// The transmute is necessary here because tree_sitter::Language does not derive Hash at the moment. self.config.language.hash(state);
// However it does use #[repr] transparent so the transmute here is safe
// as `Language` (which `Grammar` is an alias for) is just a newtype wrapper around a (thin) pointer.
// This is also compatible with the PartialEq implementation of language
// as that is just a pointer comparison.
let language: *const () = unsafe { transmute(self.config.language) };
language.hash(state);
self.ranges.hash(state); self.ranges.hash(state);
} }
} }
@ -1438,7 +1574,7 @@ impl LanguageLayer {
.map_err(|_| Error::InvalidRanges)?; .map_err(|_| Error::InvalidRanges)?;
parser parser
.set_language(self.config.language) .set_language(&self.config.language)
.map_err(|_| Error::InvalidLanguage)?; .map_err(|_| Error::InvalidLanguage)?;
// unsafe { syntax.parser.set_cancellation_flag(cancellation_flag) }; // unsafe { syntax.parser.set_cancellation_flag(cancellation_flag) };
@ -1597,7 +1733,7 @@ use std::sync::atomic::{AtomicUsize, Ordering};
use std::{iter, mem, ops, str, usize}; use std::{iter, mem, ops, str, usize};
use tree_sitter::{ use tree_sitter::{
Language as Grammar, Node, Parser, Point, Query, QueryCaptures, QueryCursor, QueryError, Language as Grammar, Node, Parser, Point, Query, QueryCaptures, QueryCursor, QueryError,
QueryMatch, Range, TextProvider, Tree, TreeCursor, QueryMatch, Range, TextProvider, Tree,
}; };
const CANCELLATION_CHECK_INTERVAL: usize = 100; const CANCELLATION_CHECK_INTERVAL: usize = 100;
@ -1738,7 +1874,7 @@ impl HighlightConfiguration {
// Construct a single query by concatenating the three query strings, but record the // Construct a single query by concatenating the three query strings, but record the
// range of pattern indices that belong to each individual string. // range of pattern indices that belong to each individual string.
let query = Query::new(language, &query_source)?; let query = Query::new(&language, &query_source)?;
let mut highlights_pattern_index = 0; let mut highlights_pattern_index = 0;
for i in 0..(query.pattern_count()) { for i in 0..(query.pattern_count()) {
let pattern_offset = query.start_byte_for_pattern(i); let pattern_offset = query.start_byte_for_pattern(i);
@ -1747,7 +1883,7 @@ impl HighlightConfiguration {
} }
} }
let injections_query = Query::new(language, injection_query)?; let injections_query = Query::new(&language, injection_query)?;
let combined_injections_patterns = (0..injections_query.pattern_count()) let combined_injections_patterns = (0..injections_query.pattern_count())
.filter(|&i| { .filter(|&i| {
injections_query injections_query
@ -1898,11 +2034,16 @@ impl HighlightConfiguration {
node_slice node_slice
}; };
static SHEBANG_REGEX: Lazy<Regex> = Lazy::new(|| Regex::new(SHEBANG).unwrap()); static SHEBANG_REGEX: Lazy<rope::Regex> =
Lazy::new(|| rope::Regex::new(SHEBANG).unwrap());
injection_capture = SHEBANG_REGEX injection_capture = SHEBANG_REGEX
.captures(&Cow::from(lines)) .captures_iter(lines.regex_input())
.map(|cap| InjectionLanguageMarker::Shebang(cap[1].to_owned())) .map(|cap| {
let cap = lines.byte_slice(cap.get_group(1).unwrap().range());
InjectionLanguageMarker::Shebang(cap.into())
})
.next()
} else if index == self.injection_content_capture_index { } else if index == self.injection_content_capture_index {
content_node = Some(capture.node); content_node = Some(capture.node);
} }
@ -2526,7 +2667,7 @@ pub fn pretty_print_tree<W: fmt::Write>(fmt: &mut W, node: Node) -> fmt::Result
fn pretty_print_tree_impl<W: fmt::Write>( fn pretty_print_tree_impl<W: fmt::Write>(
fmt: &mut W, fmt: &mut W,
cursor: &mut TreeCursor, cursor: &mut tree_sitter::TreeCursor,
depth: usize, depth: usize,
) -> fmt::Result { ) -> fmt::Result {
let node = cursor.node(); let node = cursor.node();
@ -2592,15 +2733,21 @@ mod test {
let loader = Loader::new(Configuration { let loader = Loader::new(Configuration {
language: vec![], language: vec![],
language_server: HashMap::new(), language_server: HashMap::new(),
}); })
.unwrap();
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();
let textobject = TextObjectQuery { query }; let textobject = TextObjectQuery { query };
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.slice(..), Arc::new(config), Arc::new(loader)).unwrap(); let syntax = Syntax::new(
source.slice(..),
Arc::new(config),
Arc::new(ArcSwap::from_pointee(loader)),
)
.unwrap();
let root = syntax.tree().root_node(); let root = syntax.tree().root_node();
let mut test = |capture, range| { let mut test = |capture, range| {
@ -2618,10 +2765,10 @@ mod test {
) )
}; };
test("quantified_nodes", 1..36); test("quantified_nodes", 1..37);
// NOTE: Enable after implementing proper node group capturing // NOTE: Enable after implementing proper node group capturing
// test("quantified_nodes_grouped", 1..36); // test("quantified_nodes_grouped", 1..37);
// test("multiple_nodes_grouped", 1..36); // test("multiple_nodes_grouped", 1..37);
} }
#[test] #[test]
@ -2654,7 +2801,8 @@ mod test {
let loader = Loader::new(Configuration { let loader = Loader::new(Configuration {
language: vec![], language: vec![],
language_server: HashMap::new(), language_server: HashMap::new(),
}); })
.unwrap();
let language = get_language("rust").unwrap(); let language = get_language("rust").unwrap();
let config = HighlightConfiguration::new( let config = HighlightConfiguration::new(
@ -2674,7 +2822,12 @@ mod test {
fn main() {} fn main() {}
", ",
); );
let syntax = Syntax::new(source.slice(..), Arc::new(config), Arc::new(loader)).unwrap(); let syntax = Syntax::new(
source.slice(..),
Arc::new(config),
Arc::new(ArcSwap::from_pointee(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");
@ -2760,11 +2913,17 @@ mod test {
let loader = Loader::new(Configuration { let loader = Loader::new(Configuration {
language: vec![], language: vec![],
language_server: HashMap::new(), language_server: HashMap::new(),
}); })
.unwrap();
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.slice(..), Arc::new(config), Arc::new(loader)).unwrap(); let syntax = Syntax::new(
source.slice(..),
Arc::new(config),
Arc::new(ArcSwap::from_pointee(loader)),
)
.unwrap();
let root = syntax let root = syntax
.tree() .tree()
@ -2780,7 +2939,7 @@ mod test {
#[test] #[test]
fn test_pretty_print() { fn test_pretty_print() {
let source = r#"/// Hello"#; let source = r#"// Hello"#;
assert_pretty_print("rust", source, "(line_comment)", 0, source.len()); assert_pretty_print("rust", source, "(line_comment)", 0, source.len());
// A large tree should be indented with fields: // A large tree should be indented with fields:
@ -2799,7 +2958,8 @@ mod test {
" (macro_invocation\n", " (macro_invocation\n",
" macro: (identifier)\n", " macro: (identifier)\n",
" (token_tree\n", " (token_tree\n",
" (string_literal))))))", " (string_literal\n",
" (string_content)))))))",
), ),
0, 0,
source.len(), source.len(),
@ -2818,7 +2978,7 @@ mod test {
// rule but `name` and `body` belong to an unnamed helper `_method_rest`. // rule but `name` and `body` belong to an unnamed helper `_method_rest`.
// This can cause a bug with a pretty-printing implementation that // This can cause a bug with a pretty-printing implementation that
// uses `Node::field_name_for_child` to determine field names but is // uses `Node::field_name_for_child` to determine field names but is
// fixed when using `TreeCursor::field_name`. // fixed when using `tree_sitter::TreeCursor::field_name`.
let source = "def self.method_name let source = "def self.method_name
true true
end"; end";

@ -0,0 +1,264 @@
use std::{cmp::Reverse, ops::Range};
use super::{LanguageLayer, LayerId};
use slotmap::HopSlotMap;
use tree_sitter::Node;
/// The byte range of an injection layer.
///
/// Injection ranges may overlap, but all overlapping parts are subsets of their parent ranges.
/// This allows us to sort the ranges ahead of time in order to efficiently find a range that
/// contains a point with maximum depth.
#[derive(Debug)]
struct InjectionRange {
start: usize,
end: usize,
layer_id: LayerId,
depth: u32,
}
pub struct TreeCursor<'a> {
layers: &'a HopSlotMap<LayerId, LanguageLayer>,
root: LayerId,
current: LayerId,
injection_ranges: Vec<InjectionRange>,
// TODO: Ideally this would be a `tree_sitter::TreeCursor<'a>` but
// that returns very surprising results in testing.
cursor: Node<'a>,
}
impl<'a> TreeCursor<'a> {
pub(super) fn new(layers: &'a HopSlotMap<LayerId, LanguageLayer>, root: LayerId) -> Self {
let mut injection_ranges = Vec::new();
for (layer_id, layer) in layers.iter() {
// Skip the root layer
if layer.parent.is_none() {
continue;
}
for byte_range in layer.ranges.iter() {
let range = InjectionRange {
start: byte_range.start_byte,
end: byte_range.end_byte,
layer_id,
depth: layer.depth,
};
injection_ranges.push(range);
}
}
injection_ranges.sort_unstable_by_key(|range| (range.end, Reverse(range.depth)));
let cursor = layers[root].tree().root_node();
Self {
layers,
root,
current: root,
injection_ranges,
cursor,
}
}
pub fn node(&self) -> Node<'a> {
self.cursor
}
pub fn goto_parent(&mut self) -> bool {
if let Some(parent) = self.node().parent() {
self.cursor = parent;
return true;
}
// If we are already on the root layer, we cannot ascend.
if self.current == self.root {
return false;
}
// Ascend to the parent layer.
let range = self.node().byte_range();
let parent_id = self.layers[self.current]
.parent
.expect("non-root layers have a parent");
self.current = parent_id;
let root = self.layers[self.current].tree().root_node();
self.cursor = root
.descendant_for_byte_range(range.start, range.end)
.unwrap_or(root);
true
}
pub fn goto_parent_with<P>(&mut self, predicate: P) -> bool
where
P: Fn(&Node) -> bool,
{
while self.goto_parent() {
if predicate(&self.node()) {
return true;
}
}
false
}
/// Finds the injection layer that has exactly the same range as the given `range`.
fn layer_id_of_byte_range(&self, search_range: Range<usize>) -> Option<LayerId> {
let start_idx = self
.injection_ranges
.partition_point(|range| range.end < search_range.end);
self.injection_ranges[start_idx..]
.iter()
.take_while(|range| range.end == search_range.end)
.find_map(|range| (range.start == search_range.start).then_some(range.layer_id))
}
fn goto_first_child_impl(&mut self, named: bool) -> bool {
// Check if the current node's range is an exact injection layer range.
if let Some(layer_id) = self
.layer_id_of_byte_range(self.node().byte_range())
.filter(|&layer_id| layer_id != self.current)
{
// Switch to the child layer.
self.current = layer_id;
self.cursor = self.layers[self.current].tree().root_node();
return true;
}
let child = if named {
self.cursor.named_child(0)
} else {
self.cursor.child(0)
};
if let Some(child) = child {
// Otherwise descend in the current tree.
self.cursor = child;
true
} else {
false
}
}
pub fn goto_first_child(&mut self) -> bool {
self.goto_first_child_impl(false)
}
pub fn goto_first_named_child(&mut self) -> bool {
self.goto_first_child_impl(true)
}
fn goto_next_sibling_impl(&mut self, named: bool) -> bool {
let sibling = if named {
self.cursor.next_named_sibling()
} else {
self.cursor.next_sibling()
};
if let Some(sibling) = sibling {
self.cursor = sibling;
true
} else {
false
}
}
pub fn goto_next_sibling(&mut self) -> bool {
self.goto_next_sibling_impl(false)
}
pub fn goto_next_named_sibling(&mut self) -> bool {
self.goto_next_sibling_impl(true)
}
fn goto_prev_sibling_impl(&mut self, named: bool) -> bool {
let sibling = if named {
self.cursor.prev_named_sibling()
} else {
self.cursor.prev_sibling()
};
if let Some(sibling) = sibling {
self.cursor = sibling;
true
} else {
false
}
}
pub fn goto_prev_sibling(&mut self) -> bool {
self.goto_prev_sibling_impl(false)
}
pub fn goto_prev_named_sibling(&mut self) -> bool {
self.goto_prev_sibling_impl(true)
}
/// Finds the injection layer that contains the given start-end range.
fn layer_id_containing_byte_range(&self, start: usize, end: usize) -> LayerId {
let start_idx = self
.injection_ranges
.partition_point(|range| range.end < end);
self.injection_ranges[start_idx..]
.iter()
.take_while(|range| range.start < end)
.find_map(|range| (range.start <= start).then_some(range.layer_id))
.unwrap_or(self.root)
}
pub fn reset_to_byte_range(&mut self, start: usize, end: usize) {
self.current = self.layer_id_containing_byte_range(start, end);
let root = self.layers[self.current].tree().root_node();
self.cursor = root.descendant_for_byte_range(start, end).unwrap_or(root);
}
/// Returns an iterator over the children of the node the TreeCursor is on
/// at the time this is called.
pub fn children(&'a mut self) -> ChildIter {
let parent = self.node();
ChildIter {
cursor: self,
parent,
named: false,
}
}
/// Returns an iterator over the named children of the node the TreeCursor is on
/// at the time this is called.
pub fn named_children(&'a mut self) -> ChildIter {
let parent = self.node();
ChildIter {
cursor: self,
parent,
named: true,
}
}
}
pub struct ChildIter<'n> {
cursor: &'n mut TreeCursor<'n>,
parent: Node<'n>,
named: bool,
}
impl<'n> Iterator for ChildIter<'n> {
type Item = Node<'n>;
fn next(&mut self) -> Option<Self::Item> {
// first iteration, just visit the first child
if self.cursor.node() == self.parent {
self.cursor
.goto_first_child_impl(self.named)
.then(|| self.cursor.node())
} else {
self.cursor
.goto_next_sibling_impl(self.named)
.then(|| self.cursor.node())
}
}
}

@ -1,6 +1,5 @@
use std::cell::Cell; use std::cell::Cell;
use std::ops::Range; use std::ops::Range;
use std::rc::Rc;
use crate::syntax::Highlight; use crate::syntax::Highlight;
use crate::Tendril; use crate::Tendril;
@ -92,23 +91,23 @@ pub struct LineAnnotation {
} }
#[derive(Debug)] #[derive(Debug)]
struct Layer<A, M> { struct Layer<'a, A, M> {
annotations: Rc<[A]>, annotations: &'a [A],
current_index: Cell<usize>, current_index: Cell<usize>,
metadata: M, metadata: M,
} }
impl<A, M: Clone> Clone for Layer<A, M> { impl<A, M: Clone> Clone for Layer<'_, A, M> {
fn clone(&self) -> Self { fn clone(&self) -> Self {
Layer { Layer {
annotations: self.annotations.clone(), annotations: self.annotations,
current_index: self.current_index.clone(), current_index: self.current_index.clone(),
metadata: self.metadata.clone(), metadata: self.metadata.clone(),
} }
} }
} }
impl<A, M> Layer<A, M> { impl<A, M> Layer<'_, A, M> {
pub fn reset_pos(&self, char_idx: usize, get_char_idx: impl Fn(&A) -> usize) { pub fn reset_pos(&self, char_idx: usize, get_char_idx: impl Fn(&A) -> usize) {
let new_index = self let new_index = self
.annotations .annotations
@ -128,8 +127,8 @@ impl<A, M> Layer<A, M> {
} }
} }
impl<A, M> From<(Rc<[A]>, M)> for Layer<A, M> { impl<'a, A, M> From<(&'a [A], M)> for Layer<'a, A, M> {
fn from((annotations, metadata): (Rc<[A]>, M)) -> Layer<A, M> { fn from((annotations, metadata): (&'a [A], M)) -> Layer<A, M> {
Layer { Layer {
annotations, annotations,
current_index: Cell::new(0), current_index: Cell::new(0),
@ -147,13 +146,13 @@ fn reset_pos<A, M>(layers: &[Layer<A, M>], pos: usize, get_pos: impl Fn(&A) -> u
/// Annotations that change that is displayed when the document is render. /// Annotations that change that is displayed when the document is render.
/// Also commonly called virtual text. /// Also commonly called virtual text.
#[derive(Default, Debug, Clone)] #[derive(Default, Debug, Clone)]
pub struct TextAnnotations { pub struct TextAnnotations<'a> {
inline_annotations: Vec<Layer<InlineAnnotation, Option<Highlight>>>, inline_annotations: Vec<Layer<'a, InlineAnnotation, Option<Highlight>>>,
overlays: Vec<Layer<Overlay, Option<Highlight>>>, overlays: Vec<Layer<'a, Overlay, Option<Highlight>>>,
line_annotations: Vec<Layer<LineAnnotation, ()>>, line_annotations: Vec<Layer<'a, LineAnnotation, ()>>,
} }
impl TextAnnotations { impl<'a> TextAnnotations<'a> {
/// Prepare the TextAnnotations for iteration starting at char_idx /// Prepare the TextAnnotations for iteration starting at char_idx
pub fn reset_pos(&self, char_idx: usize) { pub fn reset_pos(&self, char_idx: usize) {
reset_pos(&self.inline_annotations, char_idx, |annot| annot.char_idx); reset_pos(&self.inline_annotations, char_idx, |annot| annot.char_idx);
@ -194,7 +193,7 @@ impl TextAnnotations {
/// the annotations that belong to the layers added first will be shown first. /// the annotations that belong to the layers added first will be shown first.
pub fn add_inline_annotations( pub fn add_inline_annotations(
&mut self, &mut self,
layer: Rc<[InlineAnnotation]>, layer: &'a [InlineAnnotation],
highlight: Option<Highlight>, highlight: Option<Highlight>,
) -> &mut Self { ) -> &mut Self {
self.inline_annotations.push((layer, highlight).into()); self.inline_annotations.push((layer, highlight).into());
@ -211,7 +210,7 @@ impl TextAnnotations {
/// ///
/// If multiple layers contain overlay at the same position /// If multiple layers contain overlay at the same position
/// the overlay from the layer added last will be show. /// the overlay from the layer added last will be show.
pub fn add_overlay(&mut self, layer: Rc<[Overlay]>, highlight: Option<Highlight>) -> &mut Self { pub fn add_overlay(&mut self, layer: &'a [Overlay], highlight: Option<Highlight>) -> &mut Self {
self.overlays.push((layer, highlight).into()); self.overlays.push((layer, highlight).into());
self self
} }
@ -220,7 +219,7 @@ impl TextAnnotations {
/// ///
/// The line annotations **must be sorted** by their `char_idx`. /// The line annotations **must be sorted** by their `char_idx`.
/// Multiple line annotations with the same `char_idx` **are not allowed**. /// Multiple line annotations with the same `char_idx` **are not allowed**.
pub fn add_line_annotation(&mut self, layer: Rc<[LineAnnotation]>) -> &mut Self { pub fn add_line_annotation(&mut self, layer: &'a [LineAnnotation]) -> &mut Self {
self.line_annotations.push((layer, ()).into()); self.line_annotations.push((layer, ()).into());
self self
} }

@ -7,9 +7,9 @@ use crate::chars::{categorize_char, char_is_whitespace, CharCategory};
use crate::graphemes::{next_grapheme_boundary, prev_grapheme_boundary}; use crate::graphemes::{next_grapheme_boundary, prev_grapheme_boundary};
use crate::line_ending::rope_is_line_ending; use crate::line_ending::rope_is_line_ending;
use crate::movement::Direction; use crate::movement::Direction;
use crate::surround;
use crate::syntax::LanguageConfiguration; use crate::syntax::LanguageConfiguration;
use crate::Range; use crate::Range;
use crate::{surround, Syntax};
fn find_word_boundary(slice: RopeSlice, mut pos: usize, direction: Direction, long: bool) -> usize { fn find_word_boundary(slice: RopeSlice, mut pos: usize, direction: Direction, long: bool) -> usize {
use CharCategory::{Eol, Whitespace}; use CharCategory::{Eol, Whitespace};
@ -199,25 +199,28 @@ pub fn textobject_paragraph(
} }
pub fn textobject_pair_surround( pub fn textobject_pair_surround(
syntax: Option<&Syntax>,
slice: RopeSlice, slice: RopeSlice,
range: Range, range: Range,
textobject: TextObject, textobject: TextObject,
ch: char, ch: char,
count: usize, count: usize,
) -> Range { ) -> Range {
textobject_pair_surround_impl(slice, range, textobject, Some(ch), count) textobject_pair_surround_impl(syntax, slice, range, textobject, Some(ch), count)
} }
pub fn textobject_pair_surround_closest( pub fn textobject_pair_surround_closest(
syntax: Option<&Syntax>,
slice: RopeSlice, slice: RopeSlice,
range: Range, range: Range,
textobject: TextObject, textobject: TextObject,
count: usize, count: usize,
) -> Range { ) -> Range {
textobject_pair_surround_impl(slice, range, textobject, None, count) textobject_pair_surround_impl(syntax, slice, range, textobject, None, count)
} }
fn textobject_pair_surround_impl( fn textobject_pair_surround_impl(
syntax: Option<&Syntax>,
slice: RopeSlice, slice: RopeSlice,
range: Range, range: Range,
textobject: TextObject, textobject: TextObject,
@ -226,8 +229,7 @@ fn textobject_pair_surround_impl(
) -> Range { ) -> Range {
let pair_pos = match ch { let pair_pos = match ch {
Some(ch) => surround::find_nth_pairs_pos(slice, ch, range, count), Some(ch) => surround::find_nth_pairs_pos(slice, ch, range, count),
// Automatically find the closest surround pairs None => surround::find_nth_closest_pairs_pos(syntax, slice, range, count),
None => surround::find_nth_closest_pairs_pos(slice, range, count),
}; };
pair_pos pair_pos
.map(|(anchor, head)| match textobject { .map(|(anchor, head)| match textobject {
@ -574,7 +576,8 @@ mod test {
let slice = doc.slice(..); let slice = doc.slice(..);
for &case in scenario { for &case in scenario {
let (pos, objtype, expected_range, ch, count) = case; let (pos, objtype, expected_range, ch, count) = case;
let result = textobject_pair_surround(slice, Range::point(pos), objtype, ch, count); let result =
textobject_pair_surround(None, slice, Range::point(pos), objtype, ch, count);
assert_eq!( assert_eq!(
result, result,
expected_range.into(), expected_range.into(),

@ -1,10 +1,12 @@
use arc_swap::ArcSwap;
use helix_core::{ use helix_core::{
indent::{indent_level_for_line, treesitter_indent_for_pos, IndentStyle}, indent::{indent_level_for_line, treesitter_indent_for_pos, IndentStyle},
syntax::{Configuration, Loader}, syntax::{Configuration, Loader},
Syntax, Syntax,
}; };
use helix_stdx::rope::RopeSliceExt;
use ropey::Rope; use ropey::Rope;
use std::{ops::Range, path::PathBuf, process::Command}; use std::{ops::Range, path::PathBuf, process::Command, sync::Arc};
#[test] #[test]
fn test_treesitter_indent_rust() { fn test_treesitter_indent_rust() {
@ -186,7 +188,7 @@ fn test_treesitter_indent(
lang_scope: &str, lang_scope: &str,
ignored_lines: Vec<std::ops::Range<usize>>, ignored_lines: Vec<std::ops::Range<usize>>,
) { ) {
let loader = Loader::new(indent_tests_config()); let loader = Loader::new(indent_tests_config()).unwrap();
// set runtime path so we can find the queries // set runtime path so we can find the queries
let mut runtime = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")); let mut runtime = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"));
@ -197,7 +199,12 @@ fn test_treesitter_indent(
let indent_style = IndentStyle::from_str(&language_config.indent.as_ref().unwrap().unit); let indent_style = IndentStyle::from_str(&language_config.indent.as_ref().unwrap().unit);
let highlight_config = language_config.highlight_config(&[]).unwrap(); let highlight_config = language_config.highlight_config(&[]).unwrap();
let text = doc.slice(..); let text = doc.slice(..);
let syntax = Syntax::new(text, highlight_config, std::sync::Arc::new(loader)).unwrap(); let syntax = Syntax::new(
text,
highlight_config,
Arc::new(ArcSwap::from_pointee(loader)),
)
.unwrap();
let indent_query = language_config.indent_query().unwrap(); let indent_query = language_config.indent_query().unwrap();
for i in 0..doc.len_lines() { for i in 0..doc.len_lines() {
@ -205,7 +212,7 @@ fn test_treesitter_indent(
if ignored_lines.iter().any(|range| range.contains(&(i + 1))) { if ignored_lines.iter().any(|range| range.contains(&(i + 1))) {
continue; continue;
} }
if let Some(pos) = helix_core::find_first_non_whitespace_char(line) { if let Some(pos) = line.first_non_whitespace_char() {
let tab_width: usize = 4; let tab_width: usize = 4;
let suggested_indent = treesitter_indent_for_pos( let suggested_indent = treesitter_indent_for_pos(
indent_query, indent_query,

@ -2,7 +2,7 @@ use crate::{
requests::DisconnectArguments, requests::DisconnectArguments,
transport::{Payload, Request, Response, Transport}, transport::{Payload, Request, Response, Transport},
types::*, types::*,
Error, Result, ThreadId, Error, Result,
}; };
use helix_core::syntax::DebuggerQuirks; use helix_core::syntax::DebuggerQuirks;

@ -12,7 +12,7 @@ homepage.workspace = true
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
ahash = "0.8.3" ahash = "0.8.11"
hashbrown = "0.14.0" hashbrown = "0.14.0"
tokio = { version = "1", features = ["rt", "rt-multi-thread", "time", "sync", "parking_lot", "macros"] } tokio = { version = "1", features = ["rt", "rt-multi-thread", "time", "sync", "parking_lot", "macros"] }
# the event registry is essentially read only but must be an rwlock so we can # the event registry is essentially read only but must be an rwlock so we can

@ -19,7 +19,7 @@ helix-stdx = { path = "../helix-stdx" }
anyhow = "1" anyhow = "1"
serde = { version = "1.0", features = ["derive"] } serde = { version = "1.0", features = ["derive"] }
toml = "0.7" toml = "0.8"
etcetera = "0.8" etcetera = "0.8"
tree-sitter.workspace = true tree-sitter.workspace = true
once_cell = "1.19" once_cell = "1.19"
@ -30,7 +30,7 @@ log = "0.4"
# 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.9.0" tempfile = "3.10.1"
dunce = "1.0.4" dunce = "1.0.4"
[target.'cfg(not(target_arch = "wasm32"))'.dependencies] [target.'cfg(not(target_arch = "wasm32"))'.dependencies]

@ -53,7 +53,7 @@ fn prioritize_runtime_dirs() -> Vec<PathBuf> {
rt_dirs.push(conf_rt_dir); rt_dirs.push(conf_rt_dir);
if let Ok(dir) = std::env::var("HELIX_RUNTIME") { if let Ok(dir) = std::env::var("HELIX_RUNTIME") {
let dir = path::expand_tilde(dir); let dir = path::expand_tilde(Path::new(&dir));
rt_dirs.push(path::normalize(dir)); rt_dirs.push(path::normalize(dir));
} }
@ -126,7 +126,7 @@ pub fn config_dir() -> PathBuf {
pub fn cache_dir() -> PathBuf { pub fn cache_dir() -> PathBuf {
// TODO: allow env var override // TODO: allow env var override
let strategy = choose_base_strategy().expect("Unable to find the config directory!"); let strategy = choose_base_strategy().expect("Unable to find the cache directory!");
let mut path = strategy.cache_dir(); let mut path = strategy.cache_dir();
path.push("helix"); path.push("helix");
path path

@ -27,6 +27,8 @@ lsp-types = { version = "0.95" }
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.35", features = ["rt", "rt-multi-thread", "io-util", "io-std", "time", "process", "macros", "fs", "parking_lot", "sync"] } tokio = { version = "1.37", features = ["rt", "rt-multi-thread", "io-util", "io-std", "time", "process", "macros", "fs", "parking_lot", "sync"] }
tokio-stream = "0.1.14" tokio-stream = "0.1.15"
parking_lot = "0.12.1" parking_lot = "0.12.1"
arc-swap = "1"
slotmap.workspace = true

@ -2,11 +2,11 @@ use crate::{
file_operations::FileOperationsInterest, file_operations::FileOperationsInterest,
find_lsp_workspace, jsonrpc, find_lsp_workspace, jsonrpc,
transport::{Payload, Transport}, transport::{Payload, Transport},
Call, Error, OffsetEncoding, Result, Call, Error, LanguageServerId, OffsetEncoding, Result,
}; };
use helix_core::{find_workspace, syntax::LanguageServerFeature, ChangeSet, Rope}; use helix_core::{find_workspace, syntax::LanguageServerFeature, ChangeSet, Rope};
use helix_loader::{self, VERSION_AND_GIT_HASH}; use helix_loader::VERSION_AND_GIT_HASH;
use helix_stdx::path; use helix_stdx::path;
use lsp::{ use lsp::{
notification::DidChangeWorkspaceFolders, CodeActionCapabilityResolveSupport, notification::DidChangeWorkspaceFolders, CodeActionCapabilityResolveSupport,
@ -46,7 +46,7 @@ fn workspace_for_uri(uri: lsp::Url) -> WorkspaceFolder {
#[derive(Debug)] #[derive(Debug)]
pub struct Client { pub struct Client {
id: usize, id: LanguageServerId,
name: String, name: String,
_process: Child, _process: Child,
server_tx: UnboundedSender<Payload>, server_tx: UnboundedSender<Payload>,
@ -177,13 +177,16 @@ impl Client {
args: &[String], args: &[String],
config: Option<Value>, config: Option<Value>,
server_environment: HashMap<String, String>, server_environment: HashMap<String, String>,
root_markers: &[String], root_path: PathBuf,
manual_roots: &[PathBuf], root_uri: Option<lsp::Url>,
id: usize, id: LanguageServerId,
name: String, name: String,
req_timeout: u64, req_timeout: u64,
doc_path: Option<&std::path::PathBuf>, ) -> Result<(
) -> Result<(Self, UnboundedReceiver<(usize, Call)>, Arc<Notify>)> { Self,
UnboundedReceiver<(LanguageServerId, Call)>,
Arc<Notify>,
)> {
// Resolve path to the binary // Resolve path to the binary
let cmd = helix_stdx::env::which(cmd)?; let cmd = helix_stdx::env::which(cmd)?;
@ -206,22 +209,6 @@ impl Client {
let (server_rx, server_tx, initialize_notify) = let (server_rx, server_tx, initialize_notify) =
Transport::start(reader, writer, stderr, id, name.clone()); Transport::start(reader, writer, stderr, id, name.clone());
let (workspace, workspace_is_cwd) = find_workspace();
let workspace = path::normalize(workspace);
let root = find_lsp_workspace(
doc_path
.and_then(|x| x.parent().and_then(|x| x.to_str()))
.unwrap_or("."),
root_markers,
manual_roots,
&workspace,
workspace_is_cwd,
);
// `root_uri` and `workspace_folder` can be empty in case there is no workspace
// `root_url` can not, use `workspace` as a fallback
let root_path = root.clone().unwrap_or_else(|| workspace.clone());
let root_uri = root.and_then(|root| lsp::Url::from_file_path(root).ok());
let workspace_folders = root_uri let workspace_folders = root_uri
.clone() .clone()
@ -251,7 +238,7 @@ impl Client {
&self.name &self.name
} }
pub fn id(&self) -> usize { pub fn id(&self) -> LanguageServerId {
self.id self.id
} }
@ -410,6 +397,16 @@ impl Client {
&self, &self,
params: R::Params, params: R::Params,
) -> impl Future<Output = Result<Value>> ) -> impl Future<Output = Result<Value>>
where
R::Params: serde::Serialize,
{
self.call_with_ref::<R>(&params)
}
fn call_with_ref<R: lsp::request::Request>(
&self,
params: &R::Params,
) -> impl Future<Output = Result<Value>>
where where
R::Params: serde::Serialize, R::Params: serde::Serialize,
{ {
@ -418,7 +415,7 @@ impl Client {
fn call_with_timeout<R: lsp::request::Request>( fn call_with_timeout<R: lsp::request::Request>(
&self, &self,
params: R::Params, params: &R::Params,
timeout_secs: u64, timeout_secs: u64,
) -> impl Future<Output = Result<Value>> ) -> impl Future<Output = Result<Value>>
where where
@ -427,17 +424,16 @@ impl Client {
let server_tx = self.server_tx.clone(); let server_tx = self.server_tx.clone();
let id = self.next_request_id(); let id = self.next_request_id();
let params = serde_json::to_value(params);
async move { async move {
use std::time::Duration; use std::time::Duration;
use tokio::time::timeout; use tokio::time::timeout;
let params = serde_json::to_value(params)?;
let request = jsonrpc::MethodCall { let request = jsonrpc::MethodCall {
jsonrpc: Some(jsonrpc::Version::V2), jsonrpc: Some(jsonrpc::Version::V2),
id: id.clone(), id: id.clone(),
method: R::METHOD.to_string(), method: R::METHOD.to_string(),
params: Self::value_into_params(params), params: Self::value_into_params(params?),
}; };
let (tx, mut rx) = channel::<Result<Value>>(1); let (tx, mut rx) = channel::<Result<Value>>(1);
@ -648,6 +644,12 @@ impl Client {
}), }),
publish_diagnostics: Some(lsp::PublishDiagnosticsClientCapabilities { publish_diagnostics: Some(lsp::PublishDiagnosticsClientCapabilities {
version_support: Some(true), version_support: Some(true),
tag_support: Some(lsp::TagSupport {
value_set: vec![
lsp::DiagnosticTag::UNNECESSARY,
lsp::DiagnosticTag::DEPRECATED,
],
}),
..Default::default() ..Default::default()
}), }),
inlay_hint: Some(lsp::InlayHintClientCapabilities { inlay_hint: Some(lsp::InlayHintClientCapabilities {
@ -748,7 +750,7 @@ impl Client {
new_uri: url_from_path(new_path)?, new_uri: url_from_path(new_path)?,
}]; }];
let request = self.call_with_timeout::<lsp::request::WillRenameFiles>( let request = self.call_with_timeout::<lsp::request::WillRenameFiles>(
lsp::RenameFilesParams { files }, &lsp::RenameFilesParams { files },
5, 5,
); );
@ -1033,20 +1035,10 @@ impl Client {
pub fn resolve_completion_item( pub fn resolve_completion_item(
&self, &self,
completion_item: lsp::CompletionItem, completion_item: &lsp::CompletionItem,
) -> Option<impl Future<Output = Result<Value>>> { ) -> impl Future<Output = Result<lsp::CompletionItem>> {
let capabilities = self.capabilities.get().unwrap(); let res = self.call_with_ref::<lsp::request::ResolveCompletionItem>(completion_item);
async move { Ok(serde_json::from_value(res.await?)?) }
// Return early if the server does not support resolving completion items.
match capabilities.completion_provider {
Some(lsp::CompletionOptions {
resolve_provider: Some(true),
..
}) => (),
_ => return None,
}
Some(self.call::<lsp::request::ResolveCompletionItem>(completion_item))
} }
pub fn resolve_code_action( pub fn resolve_code_action(

@ -3,24 +3,24 @@ use std::{collections::HashMap, path::PathBuf, sync::Weak};
use globset::{GlobBuilder, GlobSetBuilder}; use globset::{GlobBuilder, GlobSetBuilder};
use tokio::sync::mpsc; use tokio::sync::mpsc;
use crate::{lsp, Client}; use crate::{lsp, Client, LanguageServerId};
enum Event { enum Event {
FileChanged { FileChanged {
path: PathBuf, path: PathBuf,
}, },
Register { Register {
client_id: usize, client_id: LanguageServerId,
client: Weak<Client>, client: Weak<Client>,
registration_id: String, registration_id: String,
options: lsp::DidChangeWatchedFilesRegistrationOptions, options: lsp::DidChangeWatchedFilesRegistrationOptions,
}, },
Unregister { Unregister {
client_id: usize, client_id: LanguageServerId,
registration_id: String, registration_id: String,
}, },
RemoveClient { RemoveClient {
client_id: usize, client_id: LanguageServerId,
}, },
} }
@ -59,7 +59,7 @@ impl Handler {
pub fn register( pub fn register(
&self, &self,
client_id: usize, client_id: LanguageServerId,
client: Weak<Client>, client: Weak<Client>,
registration_id: String, registration_id: String,
options: lsp::DidChangeWatchedFilesRegistrationOptions, options: lsp::DidChangeWatchedFilesRegistrationOptions,
@ -72,7 +72,7 @@ impl Handler {
}); });
} }
pub fn unregister(&self, client_id: usize, registration_id: String) { pub fn unregister(&self, client_id: LanguageServerId, registration_id: String) {
let _ = self.tx.send(Event::Unregister { let _ = self.tx.send(Event::Unregister {
client_id, client_id,
registration_id, registration_id,
@ -83,12 +83,12 @@ impl Handler {
let _ = self.tx.send(Event::FileChanged { path }); let _ = self.tx.send(Event::FileChanged { path });
} }
pub fn remove_client(&self, client_id: usize) { pub fn remove_client(&self, client_id: LanguageServerId) {
let _ = self.tx.send(Event::RemoveClient { client_id }); let _ = self.tx.send(Event::RemoveClient { client_id });
} }
async fn run(mut rx: mpsc::UnboundedReceiver<Event>) { async fn run(mut rx: mpsc::UnboundedReceiver<Event>) {
let mut state: HashMap<usize, ClientState> = HashMap::new(); let mut state: HashMap<LanguageServerId, ClientState> = HashMap::new();
while let Some(event) = rx.recv().await { while let Some(event) = rx.recv().await {
match event { match event {
Event::FileChanged { path } => { Event::FileChanged { path } => {

@ -5,6 +5,7 @@ pub mod jsonrpc;
pub mod snippet; pub mod snippet;
mod transport; mod transport;
use arc_swap::ArcSwap;
pub use client::Client; pub use client::Client;
pub use futures_executor::block_on; pub use futures_executor::block_on;
pub use jsonrpc::Call; pub use jsonrpc::Call;
@ -16,6 +17,7 @@ use helix_core::syntax::{
LanguageConfiguration, LanguageServerConfiguration, LanguageServerFeatures, LanguageConfiguration, LanguageServerConfiguration, LanguageServerFeatures,
}; };
use helix_stdx::path; use helix_stdx::path;
use slotmap::SlotMap;
use tokio::sync::mpsc::UnboundedReceiver; use tokio::sync::mpsc::UnboundedReceiver;
use std::{ use std::{
@ -27,8 +29,9 @@ use std::{
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, E = Error> = core::result::Result<T, E>;
pub type LanguageServerName = String; pub type LanguageServerName = String;
pub use helix_core::diagnostic::LanguageServerId;
#[derive(Error, Debug)] #[derive(Error, Debug)]
pub enum Error { pub enum Error {
@ -283,7 +286,8 @@ pub mod util {
.chars_at(cursor) .chars_at(cursor)
.skip(1) .skip(1)
.take_while(|ch| chars::char_is_word(*ch)) .take_while(|ch| chars::char_is_word(*ch))
.count(); .count()
+ 1;
} }
(start, end) (start, end)
} }
@ -538,6 +542,16 @@ pub mod util {
} else { } else {
return (0, 0, None); return (0, 0, None);
}; };
if start > end {
log::error!(
"Invalid LSP text edit start {:?} > end {:?}, discarding",
start,
end
);
return (0, 0, None);
}
(start, end, replacement) (start, end, replacement)
}), }),
) )
@ -639,38 +653,42 @@ impl Notification {
#[derive(Debug)] #[derive(Debug)]
pub struct Registry { pub struct Registry {
inner: HashMap<LanguageServerName, Vec<Arc<Client>>>, inner: SlotMap<LanguageServerId, Arc<Client>>,
syn_loader: Arc<helix_core::syntax::Loader>, inner_by_name: HashMap<LanguageServerName, Vec<Arc<Client>>>,
counter: usize, syn_loader: Arc<ArcSwap<helix_core::syntax::Loader>>,
pub incoming: SelectAll<UnboundedReceiverStream<(usize, Call)>>, pub incoming: SelectAll<UnboundedReceiverStream<(LanguageServerId, Call)>>,
pub file_event_handler: file_event::Handler, pub file_event_handler: file_event::Handler,
} }
impl Registry { impl Registry {
pub fn new(syn_loader: Arc<helix_core::syntax::Loader>) -> Self { pub fn new(syn_loader: Arc<ArcSwap<helix_core::syntax::Loader>>) -> Self {
Self { Self {
inner: HashMap::new(), inner: SlotMap::with_key(),
inner_by_name: HashMap::new(),
syn_loader, syn_loader,
counter: 0,
incoming: SelectAll::new(), incoming: SelectAll::new(),
file_event_handler: file_event::Handler::new(), file_event_handler: file_event::Handler::new(),
} }
} }
pub fn get_by_id(&self, id: usize) -> Option<&Client> { pub fn get_by_id(&self, id: LanguageServerId) -> Option<&Arc<Client>> {
self.inner self.inner.get(id)
.values()
.flatten()
.find(|client| client.id() == id)
.map(|client| &**client)
} }
pub fn remove_by_id(&mut self, id: usize) { pub fn remove_by_id(&mut self, id: LanguageServerId) {
let Some(client) = self.inner.remove(id) else {
log::error!("client was already removed");
return
};
self.file_event_handler.remove_client(id); self.file_event_handler.remove_client(id);
self.inner.retain(|_, language_servers| { let instances = self
language_servers.retain(|ls| id != ls.id()); .inner_by_name
!language_servers.is_empty() .get_mut(client.name())
}); .expect("inner and inner_by_name must be synced");
instances.retain(|ls| id != ls.id());
if instances.is_empty() {
self.inner_by_name.remove(client.name());
}
} }
fn start_client( fn start_client(
@ -680,15 +698,14 @@ 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<Arc<Client>> { ) -> Result<Arc<Client>, StartupError> {
let config = self let syn_loader = self.syn_loader.load();
.syn_loader let config = syn_loader
.language_server_configs() .language_server_configs()
.get(&name) .get(&name)
.ok_or_else(|| anyhow::anyhow!("Language server '{name}' not defined"))?; .ok_or_else(|| anyhow::anyhow!("Language server '{name}' not defined"))?;
let id = self.counter; let id = self.inner.try_insert_with_key(|id| {
self.counter += 1; start_client(
let NewClient(client, incoming) = start_client(
id, id,
name, name,
ls_config, ls_config,
@ -696,9 +713,13 @@ impl Registry {
doc_path, doc_path,
root_dirs, root_dirs,
enable_snippets, enable_snippets,
)?; )
self.incoming.push(UnboundedReceiverStream::new(incoming)); .map(|client| {
Ok(client) self.incoming.push(UnboundedReceiverStream::new(client.1));
client.0
})
})?;
Ok(self.inner[id].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, /// 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,
@ -715,7 +736,15 @@ impl Registry {
.language_servers .language_servers
.iter() .iter()
.filter_map(|LanguageServerFeatures { name, .. }| { .filter_map(|LanguageServerFeatures { name, .. }| {
if self.inner.contains_key(name) { if let Some(old_clients) = self.inner_by_name.remove(name) {
for old_client in old_clients {
self.file_event_handler.remove_client(old_client.id());
self.inner.remove(old_client.id());
tokio::spawn(async move {
let _ = old_client.force_shutdown().await;
});
}
}
let client = match self.start_client( let client = match self.start_client(
name.clone(), name.clone(),
language_config, language_config,
@ -724,32 +753,22 @@ impl Registry {
enable_snippets, enable_snippets,
) { ) {
Ok(client) => client, Ok(client) => client,
error => return Some(error), Err(StartupError::NoRequiredRootFound) => return None,
Err(StartupError::Error(err)) => return Some(Err(err)),
}; };
let old_clients = self self.inner_by_name
.inner .insert(name.to_owned(), vec![client.clone()]);
.insert(name.clone(), vec![client.clone()])
.unwrap();
for old_client in old_clients {
self.file_event_handler.remove_client(old_client.id());
tokio::spawn(async move {
let _ = old_client.force_shutdown().await;
});
}
Some(Ok(client)) Some(Ok(client))
} else {
None
}
}) })
.collect() .collect()
} }
pub fn stop(&mut self, name: &str) { pub fn stop(&mut self, name: &str) {
if let Some(clients) = self.inner.remove(name) { if let Some(clients) = self.inner_by_name.remove(name) {
for client in clients { for client in clients {
self.file_event_handler.remove_client(client.id()); self.file_event_handler.remove_client(client.id());
self.inner.remove(client.id());
tokio::spawn(async move { tokio::spawn(async move {
let _ = client.force_shutdown().await; let _ = client.force_shutdown().await;
}); });
@ -764,13 +783,13 @@ impl Registry {
root_dirs: &'a [PathBuf], root_dirs: &'a [PathBuf],
enable_snippets: bool, enable_snippets: bool,
) -> impl Iterator<Item = (LanguageServerName, Result<Arc<Client>>)> + 'a { ) -> impl Iterator<Item = (LanguageServerName, Result<Arc<Client>>)> + 'a {
language_config.language_servers.iter().map( language_config.language_servers.iter().filter_map(
move |LanguageServerFeatures { name, .. }| { move |LanguageServerFeatures { name, .. }| {
if let Some(clients) = self.inner.get(name) { if let Some(clients) = self.inner_by_name.get(name) {
if let Some((_, client)) = clients.iter().enumerate().find(|(i, client)| { if let Some((_, client)) = clients.iter().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 (name.to_owned(), Ok(client.clone())); return Some((name.to_owned(), Ok(client.clone())));
} }
} }
match self.start_client( match self.start_client(
@ -781,20 +800,21 @@ impl Registry {
enable_snippets, enable_snippets,
) { ) {
Ok(client) => { Ok(client) => {
self.inner self.inner_by_name
.entry(name.to_owned()) .entry(name.to_owned())
.or_default() .or_default()
.push(client.clone()); .push(client.clone());
(name.clone(), Ok(client)) Some((name.clone(), Ok(client)))
} }
Err(err) => (name.to_owned(), Err(err)), Err(StartupError::NoRequiredRootFound) => None,
Err(StartupError::Error(err)) => Some((name.to_owned(), Err(err))),
} }
}, },
) )
} }
pub fn iter_clients(&self) -> impl Iterator<Item = &Arc<Client>> { pub fn iter_clients(&self) -> impl Iterator<Item = &Arc<Client>> {
self.inner.values().flatten() self.inner.values()
} }
} }
@ -817,7 +837,7 @@ impl ProgressStatus {
/// Acts as a container for progress reported by language servers. Each server /// Acts as a container for progress reported by language servers. Each server
/// has a unique id assigned at creation through [`Registry`]. This id is then used /// has a unique id assigned at creation through [`Registry`]. This id is then used
/// to store the progress in this map. /// to store the progress in this map.
pub struct LspProgressMap(HashMap<usize, HashMap<lsp::ProgressToken, ProgressStatus>>); pub struct LspProgressMap(HashMap<LanguageServerId, HashMap<lsp::ProgressToken, ProgressStatus>>);
impl LspProgressMap { impl LspProgressMap {
pub fn new() -> Self { pub fn new() -> Self {
@ -825,28 +845,35 @@ impl LspProgressMap {
} }
/// Returns a map of all tokens corresponding to the language server with `id`. /// Returns a map of all tokens corresponding to the language server with `id`.
pub fn progress_map(&self, id: usize) -> Option<&HashMap<lsp::ProgressToken, ProgressStatus>> { pub fn progress_map(
&self,
id: LanguageServerId,
) -> Option<&HashMap<lsp::ProgressToken, ProgressStatus>> {
self.0.get(&id) self.0.get(&id)
} }
pub fn is_progressing(&self, id: usize) -> bool { pub fn is_progressing(&self, id: LanguageServerId) -> bool {
self.0.get(&id).map(|it| !it.is_empty()).unwrap_or_default() self.0.get(&id).map(|it| !it.is_empty()).unwrap_or_default()
} }
/// Returns last progress status for a given server with `id` and `token`. /// Returns last progress status for a given server with `id` and `token`.
pub fn progress(&self, id: usize, token: &lsp::ProgressToken) -> Option<&ProgressStatus> { pub fn progress(
&self,
id: LanguageServerId,
token: &lsp::ProgressToken,
) -> Option<&ProgressStatus> {
self.0.get(&id).and_then(|values| values.get(token)) self.0.get(&id).and_then(|values| values.get(token))
} }
/// Checks if progress `token` for server with `id` is created. /// Checks if progress `token` for server with `id` is created.
pub fn is_created(&mut self, id: usize, token: &lsp::ProgressToken) -> bool { pub fn is_created(&mut self, id: LanguageServerId, token: &lsp::ProgressToken) -> bool {
self.0 self.0
.get(&id) .get(&id)
.map(|values| values.get(token).is_some()) .map(|values| values.get(token).is_some())
.unwrap_or_default() .unwrap_or_default()
} }
pub fn create(&mut self, id: usize, token: lsp::ProgressToken) { pub fn create(&mut self, id: LanguageServerId, token: lsp::ProgressToken) {
self.0 self.0
.entry(id) .entry(id)
.or_default() .or_default()
@ -856,7 +883,7 @@ impl LspProgressMap {
/// Ends the progress by removing the `token` from server with `id`, if removed returns the value. /// Ends the progress by removing the `token` from server with `id`, if removed returns the value.
pub fn end_progress( pub fn end_progress(
&mut self, &mut self,
id: usize, id: LanguageServerId,
token: &lsp::ProgressToken, token: &lsp::ProgressToken,
) -> Option<ProgressStatus> { ) -> Option<ProgressStatus> {
self.0.get_mut(&id).and_then(|vals| vals.remove(token)) self.0.get_mut(&id).and_then(|vals| vals.remove(token))
@ -865,7 +892,7 @@ impl LspProgressMap {
/// Updates the progress of `token` for server with `id` to `status`, returns the value replaced or `None`. /// Updates the progress of `token` for server with `id` to `status`, returns the value replaced or `None`.
pub fn update( pub fn update(
&mut self, &mut self,
id: usize, id: LanguageServerId,
token: lsp::ProgressToken, token: lsp::ProgressToken,
status: lsp::WorkDoneProgress, status: lsp::WorkDoneProgress,
) -> Option<ProgressStatus> { ) -> Option<ProgressStatus> {
@ -876,30 +903,68 @@ impl LspProgressMap {
} }
} }
struct NewClient(Arc<Client>, UnboundedReceiver<(usize, Call)>); struct NewClient(Arc<Client>, UnboundedReceiver<(LanguageServerId, Call)>);
enum StartupError {
NoRequiredRootFound,
Error(Error),
}
impl<T: Into<Error>> From<T> for StartupError {
fn from(value: T) -> Self {
StartupError::Error(value.into())
}
}
/// 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: LanguageServerId,
name: String, 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<NewClient> { ) -> Result<NewClient, StartupError> {
let (workspace, workspace_is_cwd) = helix_loader::find_workspace();
let workspace = path::normalize(workspace);
let root = find_lsp_workspace(
doc_path
.and_then(|x| x.parent().and_then(|x| x.to_str()))
.unwrap_or("."),
&config.roots,
config.workspace_lsp_roots.as_deref().unwrap_or(root_dirs),
&workspace,
workspace_is_cwd,
);
// `root_uri` and `workspace_folder` can be empty in case there is no workspace
// `root_url` can not, use `workspace` as a fallback
let root_path = root.clone().unwrap_or_else(|| workspace.clone());
let root_uri = root.and_then(|root| lsp::Url::from_file_path(root).ok());
if let Some(globset) = &ls_config.required_root_patterns {
if !root_path
.read_dir()?
.flatten()
.map(|entry| entry.file_name())
.any(|entry| globset.is_match(entry))
{
return Err(StartupError::NoRequiredRootFound);
}
}
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,
ls_config.config.clone(), ls_config.config.clone(),
ls_config.environment.clone(), ls_config.environment.clone(),
&config.roots, root_path,
config.workspace_lsp_roots.as_deref().unwrap_or(root_dirs), root_uri,
id, id,
name, name,
ls_config.timeout, ls_config.timeout,
doc_path,
)?; )?;
let client = Arc::new(client); let client = Arc::new(client);

@ -1,4 +1,4 @@
use crate::{jsonrpc, Error, Result}; use crate::{jsonrpc, Error, LanguageServerId, Result};
use anyhow::Context; use anyhow::Context;
use log::{error, info}; use log::{error, info};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@ -37,7 +37,7 @@ enum ServerMessage {
#[derive(Debug)] #[derive(Debug)]
pub struct Transport { pub struct Transport {
id: usize, id: LanguageServerId,
name: String, name: String,
pending_requests: Mutex<HashMap<jsonrpc::Id, Sender<Result<Value>>>>, pending_requests: Mutex<HashMap<jsonrpc::Id, Sender<Result<Value>>>>,
} }
@ -47,10 +47,10 @@ impl Transport {
server_stdout: BufReader<ChildStdout>, server_stdout: BufReader<ChildStdout>,
server_stdin: BufWriter<ChildStdin>, server_stdin: BufWriter<ChildStdin>,
server_stderr: BufReader<ChildStderr>, server_stderr: BufReader<ChildStderr>,
id: usize, id: LanguageServerId,
name: String, name: String,
) -> ( ) -> (
UnboundedReceiver<(usize, jsonrpc::Call)>, UnboundedReceiver<(LanguageServerId, jsonrpc::Call)>,
UnboundedSender<Payload>, UnboundedSender<Payload>,
Arc<Notify>, Arc<Notify>,
) { ) {
@ -194,7 +194,7 @@ impl Transport {
async fn process_server_message( async fn process_server_message(
&self, &self,
client_tx: &UnboundedSender<(usize, jsonrpc::Call)>, client_tx: &UnboundedSender<(LanguageServerId, jsonrpc::Call)>,
msg: ServerMessage, msg: ServerMessage,
language_server_name: &str, language_server_name: &str,
) -> Result<()> { ) -> Result<()> {
@ -251,7 +251,7 @@ impl Transport {
async fn recv( async fn recv(
transport: Arc<Self>, transport: Arc<Self>,
mut server_stdout: BufReader<ChildStdout>, mut server_stdout: BufReader<ChildStdout>,
client_tx: UnboundedSender<(usize, jsonrpc::Call)>, client_tx: UnboundedSender<(LanguageServerId, jsonrpc::Call)>,
) { ) {
let mut recv_buffer = String::new(); let mut recv_buffer = String::new();
loop { loop {
@ -329,7 +329,7 @@ impl Transport {
async fn send( async fn send(
transport: Arc<Self>, transport: Arc<Self>,
mut server_stdin: BufWriter<ChildStdin>, mut server_stdin: BufWriter<ChildStdin>,
client_tx: UnboundedSender<(usize, jsonrpc::Call)>, client_tx: UnboundedSender<(LanguageServerId, jsonrpc::Call)>,
mut client_rx: UnboundedReceiver<Payload>, mut client_rx: UnboundedReceiver<Payload>,
initialize_notify: Arc<Notify>, initialize_notify: Arc<Notify>,
) { ) {

@ -16,6 +16,14 @@ dunce = "1.0"
etcetera = "0.8" etcetera = "0.8"
ropey = { version = "1.6.1", default-features = false } ropey = { version = "1.6.1", default-features = false }
which = "6.0" which = "6.0"
regex-cursor = "0.1.4"
bitflags = "2.4"
[target.'cfg(windows)'.dependencies]
windows-sys = { version = "0.52", features = ["Win32_Security", "Win32_Security_Authorization", "Win32_System_Threading"] }
[target.'cfg(unix)'.dependencies]
rustix = { version = "0.38", features = ["fs"] }
[dev-dependencies] [dev-dependencies]
tempfile = "3.9" tempfile = "3.10"

@ -42,8 +42,9 @@ pub fn binary_exists<T: AsRef<OsStr>>(binary_name: T) -> bool {
pub fn which<T: AsRef<OsStr>>( pub fn which<T: AsRef<OsStr>>(
binary_name: T, binary_name: T,
) -> Result<std::path::PathBuf, ExecutableNotFoundError> { ) -> Result<std::path::PathBuf, ExecutableNotFoundError> {
which::which(binary_name.as_ref()).map_err(|err| ExecutableNotFoundError { let binary_name = binary_name.as_ref();
command: binary_name.as_ref().to_string_lossy().into_owned(), which::which(binary_name).map_err(|err| ExecutableNotFoundError {
command: binary_name.to_string_lossy().into_owned(),
inner: err, inner: err,
}) })
} }

@ -0,0 +1,459 @@
//! From <https://github.com/Freaky/faccess>
use std::io;
use std::path::Path;
use bitflags::bitflags;
// Licensed under MIT from faccess
bitflags! {
/// Access mode flags for `access` function to test for.
pub struct AccessMode: u8 {
/// Path exists
const EXISTS = 0b0001;
/// Path can likely be read
const READ = 0b0010;
/// Path can likely be written to
const WRITE = 0b0100;
/// Path can likely be executed
const EXECUTE = 0b1000;
}
}
#[cfg(unix)]
mod imp {
use super::*;
use rustix::fs::Access;
use std::os::unix::fs::{MetadataExt, PermissionsExt};
pub fn access(p: &Path, mode: AccessMode) -> io::Result<()> {
let mut imode = Access::empty();
if mode.contains(AccessMode::EXISTS) {
imode |= Access::EXISTS;
}
if mode.contains(AccessMode::READ) {
imode |= Access::READ_OK;
}
if mode.contains(AccessMode::WRITE) {
imode |= Access::WRITE_OK;
}
if mode.contains(AccessMode::EXECUTE) {
imode |= Access::EXEC_OK;
}
rustix::fs::access(p, imode)?;
Ok(())
}
fn chown(p: &Path, uid: Option<u32>, gid: Option<u32>) -> io::Result<()> {
let uid = uid.map(|n| unsafe { rustix::fs::Uid::from_raw(n) });
let gid = gid.map(|n| unsafe { rustix::fs::Gid::from_raw(n) });
rustix::fs::chown(p, uid, gid)?;
Ok(())
}
pub fn copy_metadata(from: &Path, to: &Path) -> io::Result<()> {
let from_meta = std::fs::metadata(from)?;
let to_meta = std::fs::metadata(to)?;
let from_gid = from_meta.gid();
let to_gid = to_meta.gid();
let mut perms = from_meta.permissions();
perms.set_mode(perms.mode() & 0o0777);
if from_gid != to_gid && chown(to, None, Some(from_gid)).is_err() {
let new_perms = (perms.mode() & 0o0707) | ((perms.mode() & 0o07) << 3);
perms.set_mode(new_perms);
}
std::fs::set_permissions(to, perms)?;
Ok(())
}
}
// Licensed under MIT from faccess except for `chown`, `copy_metadata` and `is_acl_inherited`
#[cfg(windows)]
mod imp {
use windows_sys::Win32::Foundation::{CloseHandle, LocalFree, ERROR_SUCCESS, HANDLE, PSID};
use windows_sys::Win32::Security::Authorization::{
GetNamedSecurityInfoW, SetNamedSecurityInfoW, SE_FILE_OBJECT,
};
use windows_sys::Win32::Security::{
AccessCheck, AclSizeInformation, GetAce, GetAclInformation, GetSidIdentifierAuthority,
ImpersonateSelf, IsValidAcl, IsValidSid, MapGenericMask, RevertToSelf,
SecurityImpersonation, ACCESS_ALLOWED_CALLBACK_ACE, ACL, ACL_SIZE_INFORMATION,
DACL_SECURITY_INFORMATION, GENERIC_MAPPING, GROUP_SECURITY_INFORMATION, INHERITED_ACE,
LABEL_SECURITY_INFORMATION, OBJECT_SECURITY_INFORMATION, OWNER_SECURITY_INFORMATION,
PRIVILEGE_SET, PROTECTED_DACL_SECURITY_INFORMATION, PSECURITY_DESCRIPTOR,
SID_IDENTIFIER_AUTHORITY, TOKEN_DUPLICATE, TOKEN_QUERY,
};
use windows_sys::Win32::Storage::FileSystem::{
FILE_ACCESS_RIGHTS, FILE_ALL_ACCESS, FILE_GENERIC_EXECUTE, FILE_GENERIC_READ,
FILE_GENERIC_WRITE,
};
use windows_sys::Win32::System::Threading::{GetCurrentThread, OpenThreadToken};
use super::*;
use std::ffi::c_void;
use std::os::windows::{ffi::OsStrExt, fs::OpenOptionsExt};
struct SecurityDescriptor {
sd: PSECURITY_DESCRIPTOR,
owner: PSID,
group: PSID,
dacl: *mut ACL,
}
impl Drop for SecurityDescriptor {
fn drop(&mut self) {
if !self.sd.is_null() {
unsafe {
LocalFree(self.sd);
}
}
}
}
impl SecurityDescriptor {
fn for_path(p: &Path) -> io::Result<SecurityDescriptor> {
let path = std::fs::canonicalize(p)?;
let pathos = path.into_os_string();
let mut pathw: Vec<u16> = Vec::with_capacity(pathos.len() + 1);
pathw.extend(pathos.encode_wide());
pathw.push(0);
let mut sd = std::ptr::null_mut();
let mut owner = std::ptr::null_mut();
let mut group = std::ptr::null_mut();
let mut dacl = std::ptr::null_mut();
let err = unsafe {
GetNamedSecurityInfoW(
pathw.as_ptr(),
SE_FILE_OBJECT,
OWNER_SECURITY_INFORMATION
| GROUP_SECURITY_INFORMATION
| DACL_SECURITY_INFORMATION
| LABEL_SECURITY_INFORMATION,
&mut owner,
&mut group,
&mut dacl,
std::ptr::null_mut(),
&mut sd,
)
};
if err == ERROR_SUCCESS {
Ok(SecurityDescriptor {
sd,
owner,
group,
dacl,
})
} else {
Err(io::Error::last_os_error())
}
}
fn is_acl_inherited(&self) -> bool {
let mut acl_info: ACL_SIZE_INFORMATION = unsafe { ::core::mem::zeroed() };
let acl_info_ptr: *mut c_void = &mut acl_info as *mut _ as *mut c_void;
let mut ace: ACCESS_ALLOWED_CALLBACK_ACE = unsafe { ::core::mem::zeroed() };
unsafe {
GetAclInformation(
self.dacl,
acl_info_ptr,
std::mem::size_of_val(&acl_info) as u32,
AclSizeInformation,
)
};
for i in 0..acl_info.AceCount {
let mut ptr = &mut ace as *mut _ as *mut c_void;
unsafe { GetAce(self.dacl, i, &mut ptr) };
if (ace.Header.AceFlags as u32 & INHERITED_ACE) != 0 {
return true;
}
}
false
}
fn descriptor(&self) -> &PSECURITY_DESCRIPTOR {
&self.sd
}
fn owner(&self) -> &PSID {
&self.owner
}
}
struct ThreadToken(HANDLE);
impl Drop for ThreadToken {
fn drop(&mut self) {
unsafe {
CloseHandle(self.0);
}
}
}
impl ThreadToken {
fn new() -> io::Result<Self> {
unsafe {
if ImpersonateSelf(SecurityImpersonation) == 0 {
return Err(io::Error::last_os_error());
}
let token: *mut HANDLE = std::ptr::null_mut();
let err =
OpenThreadToken(GetCurrentThread(), TOKEN_DUPLICATE | TOKEN_QUERY, 0, token);
RevertToSelf();
if err == 0 {
return Err(io::Error::last_os_error());
}
Ok(Self(*token))
}
}
fn as_handle(&self) -> &HANDLE {
&self.0
}
}
// Based roughly on Tcl's NativeAccess()
// https://github.com/tcltk/tcl/blob/2ee77587e4dc2150deb06b48f69db948b4ab0584/win/tclWinFile.c
fn eaccess(p: &Path, mut mode: FILE_ACCESS_RIGHTS) -> io::Result<()> {
let md = p.metadata()?;
if !md.is_dir() {
// Read Only is ignored for directories
if mode & FILE_GENERIC_WRITE == FILE_GENERIC_WRITE && md.permissions().readonly() {
return Err(io::Error::new(
io::ErrorKind::PermissionDenied,
"File is read only",
));
}
// If it doesn't have the correct extension it isn't executable
if mode & FILE_GENERIC_EXECUTE == FILE_GENERIC_EXECUTE {
if let Some(ext) = p.extension().and_then(|s| s.to_str()) {
match ext {
"exe" | "com" | "bat" | "cmd" => (),
_ => {
return Err(io::Error::new(
io::ErrorKind::InvalidData,
"File not executable",
))
}
}
}
}
return std::fs::OpenOptions::new()
.access_mode(mode)
.open(p)
.map(|_| ());
}
let sd = SecurityDescriptor::for_path(p)?;
// Unmapped Samba users are assigned a top level authority of 22
// ACL tests are likely to be misleading
const SAMBA_UNMAPPED: SID_IDENTIFIER_AUTHORITY = SID_IDENTIFIER_AUTHORITY {
Value: [0, 0, 0, 0, 0, 22],
};
unsafe {
let owner = sd.owner();
if IsValidSid(*owner) != 0
&& (*GetSidIdentifierAuthority(*owner)).Value == SAMBA_UNMAPPED.Value
{
return Ok(());
}
}
let token = ThreadToken::new()?;
let mut privileges: PRIVILEGE_SET = unsafe { std::mem::zeroed() };
let mut granted_access: u32 = 0;
let mut privileges_length = std::mem::size_of::<PRIVILEGE_SET>() as u32;
let mut result = 0;
let mut mapping = GENERIC_MAPPING {
GenericRead: FILE_GENERIC_READ,
GenericWrite: FILE_GENERIC_WRITE,
GenericExecute: FILE_GENERIC_EXECUTE,
GenericAll: FILE_ALL_ACCESS,
};
unsafe { MapGenericMask(&mut mode, &mut mapping) };
if unsafe {
AccessCheck(
*sd.descriptor(),
*token.as_handle(),
mode,
&mut mapping,
&mut privileges,
&mut privileges_length,
&mut granted_access,
&mut result,
)
} != 0
{
if result == 0 {
Err(io::Error::new(
io::ErrorKind::PermissionDenied,
"Permission Denied",
))
} else {
Ok(())
}
} else {
Err(io::Error::last_os_error())
}
}
pub fn access(p: &Path, mode: AccessMode) -> io::Result<()> {
let mut imode = 0;
if mode.contains(AccessMode::READ) {
imode |= FILE_GENERIC_READ;
}
if mode.contains(AccessMode::WRITE) {
imode |= FILE_GENERIC_WRITE;
}
if mode.contains(AccessMode::EXECUTE) {
imode |= FILE_GENERIC_EXECUTE;
}
if imode == 0 {
if p.exists() {
Ok(())
} else {
Err(io::Error::new(io::ErrorKind::NotFound, "Not Found"))
}
} else {
eaccess(p, imode)
}
}
fn chown(p: &Path, sd: SecurityDescriptor) -> io::Result<()> {
let path = std::fs::canonicalize(p)?;
let pathos = path.as_os_str();
let mut pathw = Vec::with_capacity(pathos.len() + 1);
pathw.extend(pathos.encode_wide());
pathw.push(0);
let mut owner = std::ptr::null_mut();
let mut group = std::ptr::null_mut();
let mut dacl = std::ptr::null();
let mut si = OBJECT_SECURITY_INFORMATION::default();
if unsafe { IsValidSid(sd.owner) } != 0 {
si |= OWNER_SECURITY_INFORMATION;
owner = sd.owner;
}
if unsafe { IsValidSid(sd.group) } != 0 {
si |= GROUP_SECURITY_INFORMATION;
group = sd.group;
}
if unsafe { IsValidAcl(sd.dacl) } != 0 {
si |= DACL_SECURITY_INFORMATION;
if !sd.is_acl_inherited() {
si |= PROTECTED_DACL_SECURITY_INFORMATION;
}
dacl = sd.dacl as *const _;
}
let err = unsafe {
SetNamedSecurityInfoW(
pathw.as_ptr(),
SE_FILE_OBJECT,
si,
owner,
group,
dacl,
std::ptr::null(),
)
};
if err == ERROR_SUCCESS {
Ok(())
} else {
Err(io::Error::last_os_error())
}
}
pub fn copy_metadata(from: &Path, to: &Path) -> io::Result<()> {
let sd = SecurityDescriptor::for_path(from)?;
chown(to, sd)?;
let meta = std::fs::metadata(from)?;
let perms = meta.permissions();
std::fs::set_permissions(to, perms)?;
Ok(())
}
}
// Licensed under MIT from faccess except for `copy_metadata`
#[cfg(not(any(unix, windows)))]
mod imp {
use super::*;
pub fn access(p: &Path, mode: AccessMode) -> io::Result<()> {
if mode.contains(AccessMode::WRITE) {
if std::fs::metadata(p)?.permissions().readonly() {
return Err(io::Error::new(
io::ErrorKind::PermissionDenied,
"Path is read only",
));
} else {
return Ok(());
}
}
if p.exists() {
Ok(())
} else {
Err(io::Error::new(io::ErrorKind::NotFound, "Path not found"))
}
}
pub fn copy_metadata(from: &path, to: &Path) -> io::Result<()> {
let meta = std::fs::metadata(from)?;
let perms = meta.permissions();
std::fs::set_permissions(to, perms)?;
Ok(())
}
}
pub fn readonly(p: &Path) -> bool {
match imp::access(p, AccessMode::WRITE) {
Ok(_) => false,
Err(err) if err.kind() == std::io::ErrorKind::NotFound => false,
Err(_) => true,
}
}
pub fn copy_metadata(from: &Path, to: &Path) -> io::Result<()> {
imp::copy_metadata(from, to)
}

@ -1,3 +1,4 @@
pub mod env; pub mod env;
pub mod faccess;
pub mod path; pub mod path;
pub mod rope; pub mod rope;

@ -1,37 +1,52 @@
pub use etcetera::home_dir; pub use etcetera::home_dir;
use std::path::{Component, Path, PathBuf}; use std::{
borrow::Cow,
ffi::OsString,
path::{Component, Path, PathBuf, MAIN_SEPARATOR_STR},
};
use crate::env::current_working_dir; use crate::env::current_working_dir;
/// Replaces users home directory from `path` with tilde `~` if the directory /// Replaces users home directory from `path` with tilde `~` if the directory
/// is available, otherwise returns the path unchanged. /// is available, otherwise returns the path unchanged.
pub fn fold_home_dir(path: &Path) -> PathBuf { pub fn fold_home_dir<'a, P>(path: P) -> Cow<'a, Path>
where
P: Into<Cow<'a, Path>>,
{
let path = path.into();
if let Ok(home) = home_dir() { if let Ok(home) = home_dir() {
if let Ok(stripped) = path.strip_prefix(&home) { if let Ok(stripped) = path.strip_prefix(&home) {
return PathBuf::from("~").join(stripped); let mut path = OsString::with_capacity(2 + stripped.as_os_str().len());
path.push("~");
path.push(MAIN_SEPARATOR_STR);
path.push(stripped);
return Cow::Owned(PathBuf::from(path));
} }
} }
path.to_path_buf() path
} }
/// Expands tilde `~` into users home directory if available, otherwise returns the path /// Expands tilde `~` into users home directory if available, otherwise returns the path
/// unchanged. The tilde will only be expanded when present as the first component of the path /// unchanged. The tilde will only be expanded when present as the first component of the path
/// and only slash follows it. /// and only slash follows it.
pub fn expand_tilde(path: impl AsRef<Path>) -> PathBuf { pub fn expand_tilde<'a, P>(path: P) -> Cow<'a, Path>
let path = path.as_ref(); where
let mut components = path.components().peekable(); P: Into<Cow<'a, Path>>,
if let Some(Component::Normal(c)) = components.peek() { {
if c == &"~" { let path = path.into();
if let Ok(home) = home_dir() { let mut components = path.components();
// it's ok to unwrap, the path starts with `~` if let Some(Component::Normal(c)) = components.next() {
return home.join(path.strip_prefix("~").unwrap()); if c == "~" {
if let Ok(mut buf) = home_dir() {
buf.push(components);
return Cow::Owned(buf);
} }
} }
} }
path.to_path_buf() path
} }
/// Normalize a path without resolving symlinks. /// Normalize a path without resolving symlinks.
@ -109,9 +124,9 @@ pub fn normalize(path: impl AsRef<Path>) -> PathBuf {
/// This function is used instead of [`std::fs::canonicalize`] because we don't want to verify /// This function is used instead of [`std::fs::canonicalize`] because we don't want to verify
/// here if the path exists, just normalize it's components. /// here if the path exists, just normalize it's components.
pub fn canonicalize(path: impl AsRef<Path>) -> PathBuf { pub fn canonicalize(path: impl AsRef<Path>) -> PathBuf {
let path = expand_tilde(path); let path = expand_tilde(path.as_ref());
let path = if path.is_relative() { let path = if path.is_relative() {
current_working_dir().join(path) Cow::Owned(current_working_dir().join(path))
} else { } else {
path path
}; };
@ -119,18 +134,21 @@ pub fn canonicalize(path: impl AsRef<Path>) -> PathBuf {
normalize(path) normalize(path)
} }
pub fn get_relative_path(path: impl AsRef<Path>) -> PathBuf { pub fn get_relative_path<'a, P>(path: P) -> Cow<'a, Path>
let path = PathBuf::from(path.as_ref()); where
let path = if path.is_absolute() { P: Into<Cow<'a, Path>>,
{
let path = path.into();
if path.is_absolute() {
let cwdir = normalize(current_working_dir()); let cwdir = normalize(current_working_dir());
normalize(&path) if let Ok(stripped) = normalize(&path).strip_prefix(cwdir) {
.strip_prefix(cwdir) return Cow::Owned(PathBuf::from(stripped));
.map(PathBuf::from) }
.unwrap_or(path)
} else { return fold_home_dir(path);
}
path path
};
fold_home_dir(&path)
} }
/// Returns a truncated filepath where the basepart of the path is reduced to the first /// Returns a truncated filepath where the basepart of the path is reduced to the first
@ -164,22 +182,50 @@ pub fn get_relative_path(path: impl AsRef<Path>) -> PathBuf {
/// ///
pub fn get_truncated_path(path: impl AsRef<Path>) -> PathBuf { pub fn get_truncated_path(path: impl AsRef<Path>) -> PathBuf {
let cwd = current_working_dir(); let cwd = current_working_dir();
let path = path let path = path.as_ref();
.as_ref() let path = path.strip_prefix(cwd).unwrap_or(path);
.strip_prefix(cwd)
.unwrap_or_else(|_| path.as_ref());
let file = path.file_name().unwrap_or_default(); let file = path.file_name().unwrap_or_default();
let base = path.parent().unwrap_or_else(|| Path::new("")); let base = path.parent().unwrap_or_else(|| Path::new(""));
let mut ret = PathBuf::new(); let mut ret = PathBuf::with_capacity(file.len());
// A char can't be directly pushed to a PathBuf
let mut first_char_buffer = String::new();
for d in base { for d in base {
ret.push( let Some(first_char) = d.to_string_lossy().chars().next() else {
d.to_string_lossy() break;
.chars() };
.next() first_char_buffer.push(first_char);
.unwrap_or_default() ret.push(&first_char_buffer);
.to_string(), first_char_buffer.clear();
);
} }
ret.push(file); ret.push(file);
ret ret
} }
#[cfg(test)]
mod tests {
use std::{
ffi::OsStr,
path::{Component, Path},
};
use crate::path;
#[test]
fn expand_tilde() {
for path in ["~", "~/foo"] {
let expanded = path::expand_tilde(Path::new(path));
let tilde = Component::Normal(OsStr::new("~"));
let mut component_count = 0;
for component in expanded.components() {
// No tilde left.
assert_ne!(component, tilde);
component_count += 1;
}
// The path was at least expanded to something.
assert_ne!(component_count, 0);
}
}
}

@ -1,11 +1,42 @@
use std::ops::{Bound, RangeBounds};
pub use regex_cursor::engines::meta::{Builder as RegexBuilder, Regex};
pub use regex_cursor::regex_automata::util::syntax::Config;
use regex_cursor::{Input as RegexInput, RopeyCursor};
use ropey::str_utils::byte_to_char_idx;
use ropey::RopeSlice; use ropey::RopeSlice;
pub trait RopeSliceExt: Sized { pub trait RopeSliceExt<'a>: Sized {
fn ends_with(self, text: &str) -> bool; fn ends_with(self, text: &str) -> bool;
fn starts_with(self, text: &str) -> bool; fn starts_with(self, text: &str) -> bool;
fn regex_input(self) -> RegexInput<RopeyCursor<'a>>;
fn regex_input_at_bytes<R: RangeBounds<usize>>(
self,
byte_range: R,
) -> RegexInput<RopeyCursor<'a>>;
fn regex_input_at<R: RangeBounds<usize>>(self, char_range: R) -> RegexInput<RopeyCursor<'a>>;
fn first_non_whitespace_char(self) -> Option<usize>;
fn last_non_whitespace_char(self) -> Option<usize>;
/// returns the char idx of `byte_idx`, if `byte_idx` is a char boundary
/// this function behaves the same as `byte_to_char` but if `byte_idx` is
/// not a valid char boundary (so within a char) this will return the next
/// char index.
///
/// # Example
///
/// ```
/// # use ropey::RopeSlice;
/// # use helix_stdx::rope::RopeSliceExt;
/// let text = RopeSlice::from("😆");
/// for i in 1..text.len_bytes() {
/// assert_eq!(text.byte_to_char(i), 0);
/// assert_eq!(text.byte_to_next_char(i), 1);
/// }
/// ```
fn byte_to_next_char(self, byte_idx: usize) -> usize;
} }
impl RopeSliceExt for RopeSlice<'_> { impl<'a> RopeSliceExt<'a> for RopeSlice<'a> {
fn ends_with(self, text: &str) -> bool { fn ends_with(self, text: &str) -> bool {
let len = self.len_bytes(); let len = self.len_bytes();
if len < text.len() { if len < text.len() {
@ -23,4 +54,87 @@ impl RopeSliceExt for RopeSlice<'_> {
self.get_byte_slice(..len - text.len()) self.get_byte_slice(..len - text.len())
.map_or(false, |start| start == text) .map_or(false, |start| start == text)
} }
fn regex_input(self) -> RegexInput<RopeyCursor<'a>> {
RegexInput::new(self)
}
fn regex_input_at<R: RangeBounds<usize>>(self, char_range: R) -> RegexInput<RopeyCursor<'a>> {
let start_bound = match char_range.start_bound() {
Bound::Included(&val) => Bound::Included(self.char_to_byte(val)),
Bound::Excluded(&val) => Bound::Excluded(self.char_to_byte(val)),
Bound::Unbounded => Bound::Unbounded,
};
let end_bound = match char_range.end_bound() {
Bound::Included(&val) => Bound::Included(self.char_to_byte(val)),
Bound::Excluded(&val) => Bound::Excluded(self.char_to_byte(val)),
Bound::Unbounded => Bound::Unbounded,
};
self.regex_input_at_bytes((start_bound, end_bound))
}
fn regex_input_at_bytes<R: RangeBounds<usize>>(
self,
byte_range: R,
) -> RegexInput<RopeyCursor<'a>> {
let input = match byte_range.start_bound() {
Bound::Included(&pos) | Bound::Excluded(&pos) => {
RegexInput::new(RopeyCursor::at(self, pos))
}
Bound::Unbounded => RegexInput::new(self),
};
input.range(byte_range)
}
fn first_non_whitespace_char(self) -> Option<usize> {
self.chars().position(|ch| !ch.is_whitespace())
}
fn last_non_whitespace_char(self) -> Option<usize> {
self.chars_at(self.len_chars())
.reversed()
.position(|ch| !ch.is_whitespace())
.map(|pos| self.len_chars() - pos - 1)
}
/// returns the char idx of `byte_idx`, if `byte_idx` is
/// a char boundary this function behaves the same as `byte_to_char`
fn byte_to_next_char(self, mut byte_idx: usize) -> usize {
let (chunk, chunk_byte_off, chunk_char_off, _) = self.chunk_at_byte(byte_idx);
byte_idx -= chunk_byte_off;
let is_char_boundary =
is_utf8_char_boundary(chunk.as_bytes().get(byte_idx).copied().unwrap_or(0));
chunk_char_off + byte_to_char_idx(chunk, byte_idx) + !is_char_boundary as usize
}
}
// copied from std
#[inline]
const fn is_utf8_char_boundary(b: u8) -> bool {
// This is bit magic equivalent to: b < 128 || b >= 192
(b as i8) >= -0x40
}
#[cfg(test)]
mod tests {
use ropey::RopeSlice;
use crate::rope::RopeSliceExt;
#[test]
fn next_char_at_byte() {
for i in 0..=6 {
assert_eq!(RopeSlice::from("foobar").byte_to_next_char(i), i);
}
for char_idx in 0..10 {
let len = "😆".len();
assert_eq!(
RopeSlice::from("😆😆😆😆😆😆😆😆😆😆").byte_to_next_char(char_idx * len),
char_idx
);
for i in 1..=len {
assert_eq!(
RopeSlice::from("😆😆😆😆😆😆😆😆😆😆").byte_to_next_char(char_idx * len + i),
char_idx + 1
);
}
}
}
} }

@ -42,7 +42,8 @@ crossterm = { version = "0.27", features = ["event-stream"] }
signal-hook = "0.3" signal-hook = "0.3"
tokio-stream = "0.1" tokio-stream = "0.1"
futures-util = { version = "0.3", features = ["std", "async-await"], default-features = false } futures-util = { version = "0.3", features = ["std", "async-await"], default-features = false }
arc-swap = { version = "1.6.0" } arc-swap = { version = "1.7.1" }
termini = "1"
# Logging # Logging
fern = "0.6" fern = "0.6"
@ -53,16 +54,16 @@ log = "0.4"
nucleo.workspace = true nucleo.workspace = true
ignore = "0.4" ignore = "0.4"
# markdown doc rendering # markdown doc rendering
pulldown-cmark = { version = "0.9", default-features = false } pulldown-cmark = { version = "0.10", default-features = false }
# file type detection # file type detection
content_inspector = "0.2.4" content_inspector = "0.2.4"
# opening URLs # opening URLs
open = "5.0.1" open = "5.1.2"
url = "2.5.0" url = "2.5.0"
# config # config
toml = "0.7" toml = "0.8"
serde_json = "1.0" serde_json = "1.0"
serde = { version = "1.0", features = ["derive"] } serde = { version = "1.0", features = ["derive"] }
@ -76,7 +77,7 @@ steel-core = { workspace = true, optional = true }
[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.152" libc = "0.2.153"
[target.'cfg(target_os = "macos")'.dependencies] [target.'cfg(target_os = "macos")'.dependencies]
crossterm = { version = "0.27", features = ["event-stream", "use-dev-tty"] } crossterm = { version = "0.27", features = ["event-stream", "use-dev-tty"] }
@ -86,5 +87,5 @@ helix-loader = { path = "../helix-loader" }
[dev-dependencies] [dev-dependencies]
smallvec = "1.13" smallvec = "1.13"
indoc = "2.0.4" indoc = "2.0.5"
tempfile = "3.9.0" tempfile = "3.10.1"

@ -4,7 +4,7 @@ use helix_core::{diagnostic::Severity, pos_at_coords, syntax, Selection};
use helix_lsp::{ use helix_lsp::{
lsp::{self, notification::Notification}, lsp::{self, notification::Notification},
util::lsp_range_to_range, util::lsp_range_to_range,
LspProgressMap, LanguageServerId, LspProgressMap,
}; };
use helix_stdx::path::get_relative_path; use helix_stdx::path::get_relative_path;
use helix_view::{ use helix_view::{
@ -66,7 +66,7 @@ pub struct Application {
#[allow(dead_code)] #[allow(dead_code)]
theme_loader: Arc<theme::Loader>, theme_loader: Arc<theme::Loader>,
#[allow(dead_code)] #[allow(dead_code)]
syn_loader: Arc<syntax::Loader>, syn_loader: Arc<ArcSwap<syntax::Loader>>,
signals: Signals, signals: Signals,
jobs: Jobs, jobs: Jobs,
@ -96,11 +96,7 @@ fn setup_integration_logging() {
} }
impl Application { impl Application {
pub fn new( pub fn new(args: Args, config: Config, lang_loader: syntax::Loader) -> Result<Self, Error> {
args: Args,
config: Config,
syn_loader_conf: syntax::Configuration,
) -> Result<Self, Error> {
#[cfg(feature = "integration")] #[cfg(feature = "integration")]
setup_integration_logging(); setup_integration_logging();
@ -126,7 +122,7 @@ impl Application {
}) })
.unwrap_or_else(|| theme_loader.default_theme(true_color)); .unwrap_or_else(|| theme_loader.default_theme(true_color));
let syn_loader = std::sync::Arc::new(syntax::Loader::new(syn_loader_conf)); let syn_loader = Arc::new(ArcSwap::from_pointee(lang_loader));
#[cfg(not(feature = "integration"))] #[cfg(not(feature = "integration"))]
let backend = CrosstermBackend::new(stdout(), &config.editor); let backend = CrosstermBackend::new(stdout(), &config.editor);
@ -415,10 +411,9 @@ impl Application {
/// refresh language config after config change /// refresh language config after config change
fn refresh_language_config(&mut self) -> Result<(), Error> { fn refresh_language_config(&mut self) -> Result<(), Error> {
let syntax_config = helix_core::config::user_syntax_loader() let lang_loader = helix_core::config::user_lang_loader()?;
.map_err(|err| anyhow::anyhow!("Failed to load language config: {}", err))?;
self.syn_loader = std::sync::Arc::new(syntax::Loader::new(syntax_config)); self.syn_loader.store(Arc::new(lang_loader));
self.editor.syn_loader = self.syn_loader.clone(); self.editor.syn_loader = self.syn_loader.clone();
for document in self.editor.documents.values_mut() { for document in self.editor.documents.values_mut() {
document.detect_language(self.syn_loader.clone()); document.detect_language(self.syn_loader.clone());
@ -681,7 +676,7 @@ impl Application {
pub async fn handle_language_server_message( pub async fn handle_language_server_message(
&mut self, &mut self,
call: helix_lsp::Call, call: helix_lsp::Call,
server_id: usize, server_id: LanguageServerId,
) { ) {
use helix_lsp::{Call, MethodCall, Notification}; use helix_lsp::{Call, MethodCall, Notification};
@ -750,7 +745,7 @@ impl Application {
} }
Notification::PublishDiagnostics(mut params) => { Notification::PublishDiagnostics(mut params) => {
let path = match params.uri.to_file_path() { let path = match params.uri.to_file_path() {
Ok(path) => path, Ok(path) => helix_stdx::path::normalize(&path),
Err(_) => { Err(_) => {
log::error!("Unsupported file URI: {}", params.uri); log::error!("Unsupported file URI: {}", params.uri);
return; return;
@ -779,9 +774,7 @@ impl Application {
let lang_conf = doc.language.clone(); let lang_conf = doc.language.clone();
if let Some(lang_conf) = &lang_conf { if let Some(lang_conf) = &lang_conf {
if let Some(old_diagnostics) = if let Some(old_diagnostics) = self.editor.diagnostics.get(&path) {
self.editor.diagnostics.get(&params.uri)
{
if !lang_conf.persistent_diagnostic_sources.is_empty() { if !lang_conf.persistent_diagnostic_sources.is_empty() {
// Sort diagnostics first by severity and then by line numbers. // Sort diagnostics first by severity and then by line numbers.
// Note: The `lsp::DiagnosticSeverity` enum is already defined in decreasing order // Note: The `lsp::DiagnosticSeverity` enum is already defined in decreasing order
@ -814,7 +807,7 @@ impl Application {
// 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.
let diagnostics = match self.editor.diagnostics.entry(params.uri) { let diagnostics = match self.editor.diagnostics.entry(path) {
Entry::Occupied(o) => { Entry::Occupied(o) => {
let current_diagnostics = o.into_mut(); let current_diagnostics = o.into_mut();
// there may entries of other language servers, which is why we can't overwrite the whole entry // there may entries of other language servers, which is why we can't overwrite the whole entry
@ -1058,12 +1051,7 @@ impl Application {
Ok(json!(result)) Ok(json!(result))
} }
Ok(MethodCall::RegisterCapability(params)) => { Ok(MethodCall::RegisterCapability(params)) => {
if let Some(client) = self if let Some(client) = self.editor.language_servers.get_by_id(server_id) {
.editor
.language_servers
.iter_clients()
.find(|client| client.id() == server_id)
{
for reg in params.registrations { for reg in params.registrations {
match reg.method.as_str() { match reg.method.as_str() {
lsp::notification::DidChangeWatchedFiles::METHOD => { lsp::notification::DidChangeWatchedFiles::METHOD => {

File diff suppressed because it is too large Load Diff

@ -8,7 +8,7 @@ use dap::{StackFrame, Thread, ThreadStates};
use helix_core::syntax::{DebugArgumentValue, DebugConfigCompletion, DebugTemplate}; use helix_core::syntax::{DebugArgumentValue, DebugConfigCompletion, DebugTemplate};
use helix_dap::{self as dap, Client}; use helix_dap::{self as dap, Client};
use helix_lsp::block_on; use helix_lsp::block_on;
use helix_view::{editor::Breakpoint, graphics::Margin}; use helix_view::editor::Breakpoint;
use serde_json::{to_value, Value}; use serde_json::{to_value, Value};
use tokio_stream::wrappers::UnboundedReceiverStream; use tokio_stream::wrappers::UnboundedReceiverStream;
@ -581,12 +581,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 margin = if cx.editor.popup_border() { let popup = Popup::new("dap-variables", contents);
Margin::all(1)
} else {
Margin::none()
};
let popup = Popup::new("dap-variables", contents).margin(margin);
cx.replace_or_push_layer("dap-variables", popup); cx.replace_or_push_layer("dap-variables", popup);
} }

@ -2,7 +2,6 @@ use arc_swap::ArcSwapAny;
use helix_core::{ use helix_core::{
extensions::steel_implementations::{rope_module, SteelRopeSlice}, extensions::steel_implementations::{rope_module, SteelRopeSlice},
graphemes, graphemes,
regex::Regex,
shellwords::Shellwords, shellwords::Shellwords,
syntax::{AutoPairConfig, SoftWrap}, syntax::{AutoPairConfig, SoftWrap},
Range, Selection, Tendril, Range, Selection, Tendril,
@ -24,7 +23,7 @@ use once_cell::sync::Lazy;
use steel::{ use steel::{
gc::unsafe_erased_pointers::CustomReference, gc::unsafe_erased_pointers::CustomReference,
rerrs::ErrorKind, rerrs::ErrorKind,
rvals::{as_underlying_type, FromSteelVal, IntoSteelVal, SteelString}, rvals::{as_underlying_type, IntoSteelVal, SteelString},
steel_vm::{engine::Engine, register_fn::RegisterFn}, steel_vm::{engine::Engine, register_fn::RegisterFn},
steelerr, SteelErr, SteelVal, steelerr, SteelErr, SteelVal,
}; };
@ -2572,14 +2571,16 @@ pub fn custom_insert_newline(cx: &mut Context, indent: String) {
} }
fn search_in_directory(cx: &mut Context, directory: String) { fn search_in_directory(cx: &mut Context, directory: String) {
let search_path = expand_tilde(&PathBuf::from(directory)); let buf = PathBuf::from(directory);
crate::commands::search_in_directory(cx, search_path); let search_path = expand_tilde(&buf);
let path = search_path.to_path_buf();
crate::commands::search_in_directory(cx, path);
} }
// TODO: Result should create unrecoverable result, and should have a special // TODO: Result should create unrecoverable result, and should have a special
// recoverable result - that way we can handle both, not one in particular // recoverable result - that way we can handle both, not one in particular
fn regex_selection(cx: &mut Context, regex: String) { fn regex_selection(cx: &mut Context, regex: String) {
if let Ok(regex) = Regex::new(&regex) { if let Ok(regex) = helix_stdx::rope::Regex::new(&regex) {
let (view, doc) = current!(cx.editor); let (view, doc) = current!(cx.editor);
let text = doc.text().slice(..); let text = doc.text().slice(..);
if let Some(selection) = if let Some(selection) =

@ -1,4 +1,4 @@
use futures_util::{stream::FuturesUnordered, FutureExt}; use futures_util::{stream::FuturesOrdered, FutureExt};
use helix_lsp::{ use helix_lsp::{
block_on, block_on,
lsp::{ lsp::{
@ -6,7 +6,7 @@ use helix_lsp::{
NumberOrString, NumberOrString,
}, },
util::{diagnostic_to_lsp_diagnostic, lsp_range_to_range, range_to_lsp_range}, util::{diagnostic_to_lsp_diagnostic, lsp_range_to_range, range_to_lsp_range},
Client, OffsetEncoding, Client, LanguageServerId, OffsetEncoding,
}; };
use tokio_stream::StreamExt; use tokio_stream::StreamExt;
use tui::{ use tui::{
@ -21,7 +21,6 @@ use helix_stdx::path;
use helix_view::{ use helix_view::{
document::{DocumentInlayHints, DocumentInlayHintsId}, document::{DocumentInlayHints, DocumentInlayHintsId},
editor::Action, editor::Action,
graphics::Margin,
handlers::lsp::SignatureHelpInvoked, handlers::lsp::SignatureHelpInvoked,
theme::Style, theme::Style,
Document, View, Document, View,
@ -38,7 +37,7 @@ use std::{
collections::{BTreeMap, HashSet}, collections::{BTreeMap, HashSet},
fmt::Write, fmt::Write,
future::Future, future::Future,
path::PathBuf, path::{Path, PathBuf},
}; };
/// Gets the first language server that is attached to a document which supports a specific feature. /// Gets the first language server that is attached to a document which supports a specific feature.
@ -134,7 +133,7 @@ struct DiagnosticStyles {
} }
struct PickerDiagnostic { struct PickerDiagnostic {
url: lsp::Url, path: PathBuf,
diag: lsp::Diagnostic, diag: lsp::Diagnostic,
offset_encoding: OffsetEncoding, offset_encoding: OffsetEncoding,
} }
@ -167,8 +166,7 @@ impl ui::menu::Item for PickerDiagnostic {
let path = match format { let path = match format {
DiagnosticsFormat::HideSourcePath => String::new(), DiagnosticsFormat::HideSourcePath => String::new(),
DiagnosticsFormat::ShowSourcePath => { DiagnosticsFormat::ShowSourcePath => {
let file_path = self.url.to_file_path().unwrap(); let path = path::get_truncated_path(&self.path);
let path = path::get_truncated_path(file_path);
format!("{}: ", path.to_string_lossy()) format!("{}: ", path.to_string_lossy())
} }
}; };
@ -208,22 +206,31 @@ fn jump_to_location(
return; return;
} }
}; };
jump_to_position(editor, &path, location.range, offset_encoding, action);
}
let doc = match editor.open(&path, action) { fn jump_to_position(
editor: &mut Editor,
path: &Path,
range: lsp::Range,
offset_encoding: OffsetEncoding,
action: Action,
) {
let doc = match editor.open(path, action) {
Ok(id) => doc_mut!(editor, &id), Ok(id) => doc_mut!(editor, &id),
Err(err) => { Err(err) => {
let err = format!("failed to open path: {:?}: {:?}", location.uri, err); let err = format!("failed to open path: {:?}: {:?}", path, err);
editor.set_error(err); editor.set_error(err);
return; return;
} }
}; };
let view = view_mut!(editor); let view = view_mut!(editor);
// TODO: convert inside server // TODO: convert inside server
let new_range = let new_range = if let Some(new_range) = lsp_range_to_range(doc.text(), range, offset_encoding)
if let Some(new_range) = lsp_range_to_range(doc.text(), location.range, offset_encoding) { {
new_range new_range
} else { } else {
log::warn!("lsp position out of bounds - {:?}", location.range); log::warn!("lsp position out of bounds - {:?}", range);
return; return;
}; };
// we flip the range so that the cursor sits on the start of the symbol // we flip the range so that the cursor sits on the start of the symbol
@ -258,21 +265,20 @@ enum DiagnosticsFormat {
fn diag_picker( fn diag_picker(
cx: &Context, cx: &Context,
diagnostics: BTreeMap<lsp::Url, Vec<(lsp::Diagnostic, usize)>>, diagnostics: BTreeMap<PathBuf, Vec<(lsp::Diagnostic, LanguageServerId)>>,
_current_path: Option<lsp::Url>,
format: DiagnosticsFormat, format: DiagnosticsFormat,
) -> Picker<PickerDiagnostic> { ) -> Picker<PickerDiagnostic> {
// TODO: drop current_path comparison and instead use workspace: bool flag? // TODO: drop current_path comparison and instead use workspace: bool flag?
// flatten the map to a vec of (url, diag) pairs // flatten the map to a vec of (url, diag) pairs
let mut flat_diag = Vec::new(); let mut flat_diag = Vec::new();
for (url, diags) in diagnostics { for (path, diags) in diagnostics {
flat_diag.reserve(diags.len()); flat_diag.reserve(diags.len());
for (diag, ls) in diags { for (diag, ls) in diags {
if let Some(ls) = cx.editor.language_server_by_id(ls) { if let Some(ls) = cx.editor.language_server_by_id(ls) {
flat_diag.push(PickerDiagnostic { flat_diag.push(PickerDiagnostic {
url: url.clone(), path: path.clone(),
diag, diag,
offset_encoding: ls.offset_encoding(), offset_encoding: ls.offset_encoding(),
}); });
@ -292,22 +298,17 @@ fn diag_picker(
(styles, format), (styles, format),
move |cx, move |cx,
PickerDiagnostic { PickerDiagnostic {
url, path,
diag, diag,
offset_encoding, offset_encoding,
}, },
action| { action| {
jump_to_location( jump_to_position(cx.editor, path, diag.range, *offset_encoding, action)
cx.editor,
&lsp::Location::new(url.clone(), diag.range),
*offset_encoding,
action,
)
}, },
) )
.with_preview(move |_editor, PickerDiagnostic { url, diag, .. }| { .with_preview(move |_editor, PickerDiagnostic { path, diag, .. }| {
let location = lsp::Location::new(url.clone(), diag.range); let line = Some((diag.range.start.line as usize, diag.range.end.line as usize));
Some(location_to_file_location(&location)) Some((path.clone().into(), line))
}) })
.truncate_start(false) .truncate_start(false)
} }
@ -339,7 +340,7 @@ pub fn symbol_picker(cx: &mut Context) {
let mut seen_language_servers = HashSet::new(); let mut seen_language_servers = HashSet::new();
let mut futures: FuturesUnordered<_> = doc let mut futures: FuturesOrdered<_> = doc
.language_servers_with_feature(LanguageServerFeature::DocumentSymbols) .language_servers_with_feature(LanguageServerFeature::DocumentSymbols)
.filter(|ls| seen_language_servers.insert(ls.id())) .filter(|ls| seen_language_servers.insert(ls.id()))
.map(|language_server| { .map(|language_server| {
@ -414,7 +415,7 @@ pub fn workspace_symbol_picker(cx: &mut Context) {
let get_symbols = move |pattern: String, editor: &mut Editor| { let get_symbols = move |pattern: String, editor: &mut Editor| {
let doc = doc!(editor); let doc = doc!(editor);
let mut seen_language_servers = HashSet::new(); let mut seen_language_servers = HashSet::new();
let mut futures: FuturesUnordered<_> = doc let mut futures: FuturesOrdered<_> = doc
.language_servers_with_feature(LanguageServerFeature::WorkspaceSymbols) .language_servers_with_feature(LanguageServerFeature::WorkspaceSymbols)
.filter(|ls| seen_language_servers.insert(ls.id())) .filter(|ls| seen_language_servers.insert(ls.id()))
.map(|language_server| { .map(|language_server| {
@ -470,17 +471,16 @@ pub fn workspace_symbol_picker(cx: &mut Context) {
pub fn diagnostics_picker(cx: &mut Context) { pub fn diagnostics_picker(cx: &mut Context) {
let doc = doc!(cx.editor); let doc = doc!(cx.editor);
if let Some(current_url) = doc.url() { if let Some(current_path) = doc.path() {
let diagnostics = cx let diagnostics = cx
.editor .editor
.diagnostics .diagnostics
.get(&current_url) .get(current_path)
.cloned() .cloned()
.unwrap_or_default(); .unwrap_or_default();
let picker = diag_picker( let picker = diag_picker(
cx, cx,
[(current_url.clone(), diagnostics)].into(), [(current_path.clone(), diagnostics)].into(),
Some(current_url),
DiagnosticsFormat::HideSourcePath, DiagnosticsFormat::HideSourcePath,
); );
cx.push_layer(Box::new(overlaid(picker))); cx.push_layer(Box::new(overlaid(picker)));
@ -488,22 +488,15 @@ pub fn diagnostics_picker(cx: &mut Context) {
} }
pub fn workspace_diagnostics_picker(cx: &mut Context) { pub fn workspace_diagnostics_picker(cx: &mut Context) {
let doc = doc!(cx.editor);
let current_url = doc.url();
// TODO not yet filtered by LanguageServerFeature, need to do something similar as Document::shown_diagnostics here for all open documents // TODO not yet filtered by LanguageServerFeature, need to do something similar as Document::shown_diagnostics here for all open documents
let diagnostics = cx.editor.diagnostics.clone(); let diagnostics = cx.editor.diagnostics.clone();
let picker = diag_picker( let picker = diag_picker(cx, diagnostics, DiagnosticsFormat::ShowSourcePath);
cx,
diagnostics,
current_url,
DiagnosticsFormat::ShowSourcePath,
);
cx.push_layer(Box::new(overlaid(picker))); cx.push_layer(Box::new(overlaid(picker)));
} }
struct CodeActionOrCommandItem { struct CodeActionOrCommandItem {
lsp_item: lsp::CodeActionOrCommand, lsp_item: lsp::CodeActionOrCommand,
language_server_id: usize, language_server_id: LanguageServerId,
} }
impl ui::menu::Item for CodeActionOrCommandItem { impl ui::menu::Item for CodeActionOrCommandItem {
@ -580,7 +573,7 @@ pub fn code_action(cx: &mut Context) {
let mut seen_language_servers = HashSet::new(); let mut seen_language_servers = HashSet::new();
let mut futures: FuturesUnordered<_> = doc let mut futures: FuturesOrdered<_> = doc
.language_servers_with_feature(LanguageServerFeature::CodeAction) .language_servers_with_feature(LanguageServerFeature::CodeAction)
.filter(|ls| seen_language_servers.insert(ls.id())) .filter(|ls| seen_language_servers.insert(ls.id()))
// TODO this should probably already been filtered in something like "language_servers_with_feature" // TODO this should probably already been filtered in something like "language_servers_with_feature"
@ -739,15 +732,7 @@ pub fn code_action(cx: &mut Context) {
}); });
picker.move_down(); // pre-select the first item picker.move_down(); // pre-select the first item
let margin = if editor.menu_border() { let popup = Popup::new("code-action", picker).with_scrollbar(false);
Margin::vertical(1)
} else {
Margin::none()
};
let popup = Popup::new("code-action", picker)
.with_scrollbar(false)
.margin(margin);
compositor.replace_or_push("code-action", popup); compositor.replace_or_push("code-action", popup);
}; };
@ -763,7 +748,11 @@ impl ui::menu::Item for lsp::Command {
} }
} }
pub fn execute_lsp_command(editor: &mut Editor, language_server_id: usize, cmd: lsp::Command) { pub fn execute_lsp_command(
editor: &mut Editor,
language_server_id: LanguageServerId,
cmd: lsp::Command,
) {
// the command is executed on the server and communicated back // the command is executed on the server and communicated back
// to the client asynchronously using workspace edits // to the client asynchronously using workspace edits
let future = match editor let future = match editor
@ -1040,7 +1029,7 @@ pub fn rename_symbol(cx: &mut Context) {
fn create_rename_prompt( fn create_rename_prompt(
editor: &Editor, editor: &Editor,
prefill: String, prefill: String,
language_server_id: Option<usize>, language_server_id: Option<LanguageServerId>,
) -> Box<ui::Prompt> { ) -> Box<ui::Prompt> {
let prompt = ui::Prompt::new( let prompt = ui::Prompt::new(
"rename-to:".into(), "rename-to:".into(),
@ -1321,11 +1310,11 @@ fn compute_inlay_hints_for_view(
view_id, view_id,
DocumentInlayHints { DocumentInlayHints {
id: new_doc_inlay_hints_id, id: new_doc_inlay_hints_id,
type_inlay_hints: type_inlay_hints.into(), type_inlay_hints,
parameter_inlay_hints: parameter_inlay_hints.into(), parameter_inlay_hints,
other_inlay_hints: other_inlay_hints.into(), other_inlay_hints,
padding_before_inlay_hints: padding_before_inlay_hints.into(), padding_before_inlay_hints,
padding_after_inlay_hints: padding_after_inlay_hints.into(), padding_after_inlay_hints,
}, },
); );
doc.inlay_hints_oudated = false; doc.inlay_hints_oudated = false;

@ -1,3 +1,4 @@
use std::io::BufReader;
use std::ops::Deref; use std::ops::Deref;
use crate::job::Job; use crate::job::Job;
@ -6,9 +7,9 @@ use super::*;
use helix_core::fuzzy::fuzzy_match; use helix_core::fuzzy::fuzzy_match;
use helix_core::indent::MAX_INDENT; use helix_core::indent::MAX_INDENT;
use helix_core::{encoding, line_ending, shellwords::Shellwords}; use helix_core::{line_ending, shellwords::Shellwords};
use helix_view::document::DEFAULT_LANGUAGE_NAME; use helix_view::document::{read_to_string, DEFAULT_LANGUAGE_NAME};
use helix_view::editor::{Action, CloseError, ConfigEvent}; use helix_view::editor::{CloseError, ConfigEvent};
use serde_json::Value; use serde_json::Value;
use ui::completers::{self, Completer}; use ui::completers::{self, Completer};
@ -111,14 +112,14 @@ fn open(cx: &mut compositor::Context, args: &[Cow<str>], event: PromptEvent) ->
ensure!(!args.is_empty(), "wrong argument count"); ensure!(!args.is_empty(), "wrong argument count");
for arg in args { for arg in args {
let (path, pos) = args::parse_file(arg); let (path, pos) = args::parse_file(arg);
let path = helix_stdx::path::expand_tilde(&path); let path = helix_stdx::path::expand_tilde(path);
// If the path is a directory, open a file picker on that directory and update the status // If the path is a directory, open a file picker on that directory and update the status
// message // message
if let Ok(true) = std::fs::canonicalize(&path).map(|p| p.is_dir()) { if let Ok(true) = std::fs::canonicalize(&path).map(|p| p.is_dir()) {
let callback = async move { let callback = async move {
let call: job::Callback = job::Callback::EditorCompositor(Box::new( let call: job::Callback = job::Callback::EditorCompositor(Box::new(
move |editor: &mut Editor, compositor: &mut Compositor| { move |editor: &mut Editor, compositor: &mut Compositor| {
let picker = ui::file_picker(path, &editor.config()); let picker = ui::file_picker(path.into_owned(), &editor.config());
compositor.push(Box::new(overlaid(picker))); compositor.push(Box::new(overlaid(picker)));
}, },
)); ));
@ -310,7 +311,7 @@ fn buffer_next(
return Ok(()); return Ok(());
} }
goto_buffer(cx.editor, Direction::Forward); goto_buffer(cx.editor, Direction::Forward, 1);
Ok(()) Ok(())
} }
@ -323,7 +324,7 @@ fn buffer_previous(
return Ok(()); return Ok(());
} }
goto_buffer(cx.editor, Direction::Backward); goto_buffer(cx.editor, Direction::Backward, 1);
Ok(()) Ok(())
} }
@ -1087,11 +1088,11 @@ fn change_current_directory(
return Ok(()); return Ok(());
} }
let dir = helix_stdx::path::expand_tilde( let dir = args
args.first() .first()
.context("target directory not provided")? .context("target directory not provided")?
.as_ref(), .as_ref();
); let dir = helix_stdx::path::expand_tilde(Path::new(dir));
helix_stdx::env::set_current_working_dir(dir)?; helix_stdx::env::set_current_working_dir(dir)?;
@ -1327,7 +1328,11 @@ fn reload_all(
// Ensure that the view is synced with the document's history. // Ensure that the view is synced with the document's history.
view.sync_changes(doc); view.sync_changes(doc);
doc.reload(view, &cx.editor.diff_providers)?; if let Err(error) = doc.reload(view, &cx.editor.diff_providers) {
cx.editor.set_error(format!("{}", error));
continue;
}
if let Some(path) = doc.path() { if let Some(path) = doc.path() {
cx.editor cx.editor
.language_servers .language_servers
@ -2266,7 +2271,7 @@ fn run_shell_command_text(
let args = args.join(" "); let args = args.join(" ");
let callback = async move { let callback = async move {
let (output, success) = shell_impl_async(&shell, &args, None).await?; let output = shell_impl_async(&shell, &args, None).await?;
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| {
if !output.is_empty() { if !output.is_empty() {
@ -2276,11 +2281,7 @@ fn run_shell_command_text(
)); ));
compositor.replace_or_push("shell", popup); compositor.replace_or_push("shell", popup);
} }
if success {
editor.set_status("Command succeeded"); editor.set_status("Command succeeded");
} else {
editor.set_error("Command failed");
}
}, },
)); ));
Ok(call) Ok(call)
@ -2303,7 +2304,7 @@ fn run_shell_command(
let args = args.join(" "); let args = args.join(" ");
let callback = async move { let callback = async move {
let (output, success) = shell_impl_async(&shell, &args, None).await?; let output = shell_impl_async(&shell, &args, None).await?;
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| {
if !output.is_empty() { if !output.is_empty() {
@ -2316,11 +2317,7 @@ fn run_shell_command(
)); ));
compositor.replace_or_push("shell", popup); compositor.replace_or_push("shell", popup);
} }
if success {
editor.set_status("Command succeeded"); editor.set_status("Command succeeded");
} else {
editor.set_error("Command failed");
}
}, },
)); ));
Ok(call) Ok(call)
@ -2460,6 +2457,79 @@ fn move_buffer(
Ok(()) Ok(())
} }
fn yank_diagnostic(
cx: &mut compositor::Context,
args: &[Cow<str>],
event: PromptEvent,
) -> anyhow::Result<()> {
if event != PromptEvent::Validate {
return Ok(());
}
let reg = match args.first() {
Some(s) => {
ensure!(s.chars().count() == 1, format!("Invalid register {s}"));
s.chars().next().unwrap()
}
None => '+',
};
let (view, doc) = current_ref!(cx.editor);
let primary = doc.selection(view.id).primary();
// Look only for diagnostics that intersect with the primary selection
let diag: Vec<_> = doc
.diagnostics()
.iter()
.filter(|d| primary.overlaps(&helix_core::Range::new(d.range.start, d.range.end)))
.map(|d| d.message.clone())
.collect();
let n = diag.len();
if n == 0 {
bail!("No diagnostics under primary selection");
}
cx.editor.registers.write(reg, diag)?;
cx.editor.set_status(format!(
"Yanked {n} diagnostic{} to register {reg}",
if n == 1 { "" } else { "s" }
));
Ok(())
}
fn read(cx: &mut compositor::Context, args: &[Cow<str>], event: PromptEvent) -> anyhow::Result<()> {
if event != PromptEvent::Validate {
return Ok(());
}
let scrolloff = cx.editor.config().scrolloff;
let (view, doc) = current!(cx.editor);
ensure!(!args.is_empty(), "file name is expected");
ensure!(args.len() == 1, "only the file name is expected");
let filename = args.get(0).unwrap();
let path = PathBuf::from(filename.to_string());
ensure!(
path.exists() && path.is_file(),
"path is not a file: {:?}",
path
);
let file = std::fs::File::open(path).map_err(|err| anyhow!("error opening file: {}", err))?;
let mut reader = BufReader::new(file);
let (contents, _, _) = read_to_string(&mut reader, Some(doc.encoding()))
.map_err(|err| anyhow!("error reading file: {}", err))?;
let contents = Tendril::from(contents);
let selection = doc.selection(view.id);
let transaction = Transaction::insert(doc.text(), selection, contents);
doc.apply(&transaction, view.id);
doc.append_changes_to_history(view);
view.ensure_cursor_in_view(doc, scrolloff);
Ok(())
}
pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[ pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[
TypableCommand { TypableCommand {
name: "quit", name: "quit",
@ -3060,7 +3130,7 @@ pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[
aliases: &[], aliases: &[],
doc: "Clear given register. If no argument is provided, clear all registers.", doc: "Clear given register. If no argument is provided, clear all registers.",
fun: clear_register, fun: clear_register,
signature: CommandSignature::none(), signature: CommandSignature::all(completers::register),
}, },
TypableCommand { TypableCommand {
name: "redraw", name: "redraw",
@ -3076,6 +3146,20 @@ pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[
fun: move_buffer, fun: move_buffer,
signature: CommandSignature::positional(&[completers::filename]), signature: CommandSignature::positional(&[completers::filename]),
}, },
TypableCommand {
name: "yank-diagnostic",
aliases: &[],
doc: "Yank diagnostic(s) under primary cursor to register, or clipboard by default",
fun: yank_diagnostic,
signature: CommandSignature::all(completers::register),
},
TypableCommand {
name: "read",
aliases: &["r"],
doc: "Load a file into buffer",
fun: read,
signature: CommandSignature::positional(&[completers::filename]),
},
]; ];
pub static TYPABLE_COMMAND_MAP: Lazy<HashMap<&'static str, &'static TypableCommand>> = pub static TYPABLE_COMMAND_MAP: Lazy<HashMap<&'static str, &'static TypableCommand>> =

@ -9,10 +9,9 @@ use crate::handlers::completion::CompletionHandler;
use crate::handlers::signature_help::SignatureHelpHandler; use crate::handlers::signature_help::SignatureHelpHandler;
pub use completion::trigger_auto_completion; pub use completion::trigger_auto_completion;
pub use helix_view::handlers::lsp::SignatureHelpInvoked;
pub use helix_view::handlers::Handlers; pub use helix_view::handlers::Handlers;
mod completion; pub mod completion;
mod signature_help; mod signature_help;
pub fn setup(config: Arc<ArcSwap<Config>>) -> Handlers { pub fn setup(config: Arc<ArcSwap<Config>>) -> Handlers {

@ -30,6 +30,8 @@ use crate::ui::lsp::SignatureHelp;
use crate::ui::{self, CompletionItem, Popup}; use crate::ui::{self, CompletionItem, Popup};
use super::Handlers; use super::Handlers;
pub use resolve::ResolveHandler;
mod resolve;
#[derive(Debug, PartialEq, Eq, Clone, Copy)] #[derive(Debug, PartialEq, Eq, Clone, Copy)]
enum TriggerKind { enum TriggerKind {
@ -221,10 +223,18 @@ fn request_completion(
.iter() .iter()
.find(|&trigger| trigger_text.ends_with(trigger)) .find(|&trigger| trigger_text.ends_with(trigger))
}); });
if trigger_char.is_some() {
lsp::CompletionContext { lsp::CompletionContext {
trigger_kind: lsp::CompletionTriggerKind::TRIGGER_CHARACTER, trigger_kind: lsp::CompletionTriggerKind::TRIGGER_CHARACTER,
trigger_character: trigger_char.cloned(), trigger_character: trigger_char.cloned(),
} }
} else {
lsp::CompletionContext {
trigger_kind: lsp::CompletionTriggerKind::INVOKED,
trigger_character: None,
}
}
}; };
let completion_response = ls.completion(doc_id, pos, None, context).unwrap(); let completion_response = ls.completion(doc_id, pos, None, context).unwrap();
@ -243,7 +253,7 @@ fn request_completion(
.into_iter() .into_iter()
.map(|item| CompletionItem { .map(|item| CompletionItem {
item, item,
language_server_id, provider: language_server_id,
resolved: false, resolved: false,
}) })
.collect(); .collect();

@ -0,0 +1,153 @@
use std::sync::Arc;
use helix_lsp::lsp;
use tokio::sync::mpsc::Sender;
use tokio::time::{Duration, Instant};
use helix_event::{send_blocking, AsyncHook, CancelRx};
use helix_view::Editor;
use crate::handlers::completion::CompletionItem;
use crate::job;
/// A hook for resolving incomplete completion items.
///
/// From the [LSP spec](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_completion):
///
/// > If computing full completion items is expensive, servers can additionally provide a
/// > handler for the completion item resolve request. ...
/// > A typical use case is for example: the `textDocument/completion` request doesn't fill
/// > in the `documentation` property for returned completion items since it is expensive
/// > to compute. When the item is selected in the user interface then a
/// > 'completionItem/resolve' request is sent with the selected completion item as a parameter.
/// > The returned completion item should have the documentation property filled in.
pub struct ResolveHandler {
last_request: Option<Arc<CompletionItem>>,
resolver: Sender<ResolveRequest>,
}
impl ResolveHandler {
pub fn new() -> ResolveHandler {
ResolveHandler {
last_request: None,
resolver: ResolveTimeout {
next_request: None,
in_flight: None,
}
.spawn(),
}
}
pub fn ensure_item_resolved(&mut self, editor: &mut Editor, item: &mut CompletionItem) {
if item.resolved {
return;
}
let needs_resolve = item.item.documentation.is_none()
|| item.item.detail.is_none()
|| item.item.additional_text_edits.is_none();
if !needs_resolve {
item.resolved = true;
return;
}
if self.last_request.as_deref().is_some_and(|it| it == item) {
return;
}
let Some(ls) = editor.language_servers.get_by_id(item.provider).cloned() else {
item.resolved = true;
return;
};
if matches!(
ls.capabilities().completion_provider,
Some(lsp::CompletionOptions {
resolve_provider: Some(true),
..
})
) {
let item = Arc::new(item.clone());
self.last_request = Some(item.clone());
send_blocking(&self.resolver, ResolveRequest { item, ls })
} else {
item.resolved = true;
}
}
}
struct ResolveRequest {
item: Arc<CompletionItem>,
ls: Arc<helix_lsp::Client>,
}
#[derive(Default)]
struct ResolveTimeout {
next_request: Option<ResolveRequest>,
in_flight: Option<(helix_event::CancelTx, Arc<CompletionItem>)>,
}
impl AsyncHook for ResolveTimeout {
type Event = ResolveRequest;
fn handle_event(
&mut self,
request: Self::Event,
timeout: Option<tokio::time::Instant>,
) -> Option<tokio::time::Instant> {
if self
.next_request
.as_ref()
.is_some_and(|old_request| old_request.item == request.item)
{
timeout
} else if self
.in_flight
.as_ref()
.is_some_and(|(_, old_request)| old_request.item == request.item.item)
{
self.next_request = None;
None
} else {
self.next_request = Some(request);
Some(Instant::now() + Duration::from_millis(150))
}
}
fn finish_debounce(&mut self) {
let Some(request) = self.next_request.take() else { return };
let (tx, rx) = helix_event::cancelation();
self.in_flight = Some((tx, request.item.clone()));
tokio::spawn(request.execute(rx));
}
}
impl ResolveRequest {
async fn execute(self, cancel: CancelRx) {
let future = self.ls.resolve_completion_item(&self.item.item);
let Some(resolved_item) = helix_event::cancelable_future(future, cancel).await else {
return;
};
job::dispatch(move |_, compositor| {
if let Some(completion) = &mut compositor
.find::<crate::ui::EditorView>()
.unwrap()
.completion
{
let resolved_item = match resolved_item {
Ok(item) => CompletionItem {
item,
resolved: true,
..*self.item
},
Err(err) => {
log::error!("completion resolve request failed: {err}");
// set item to resolved so we don't request it again
// we could also remove it but that oculd be odd ui
let mut item = (*self.item).clone();
item.resolved = true;
item
}
};
completion.replace_item(&self.item, resolved_item);
};
})
.await
}
}

@ -5,7 +5,7 @@ use helix_core::syntax::LanguageServerFeature;
use helix_event::{ use helix_event::{
cancelable_future, cancelation, register_hook, send_blocking, CancelRx, CancelTx, cancelable_future, cancelation, register_hook, send_blocking, CancelRx, CancelTx,
}; };
use helix_lsp::lsp; use helix_lsp::lsp::{self, SignatureInformation};
use helix_stdx::rope::RopeSliceExt; use helix_stdx::rope::RopeSliceExt;
use helix_view::document::Mode; use helix_view::document::Mode;
use helix_view::events::{DocumentDidChange, SelectionDidChange}; use helix_view::events::{DocumentDidChange, SelectionDidChange};
@ -18,7 +18,7 @@ use crate::commands::Open;
use crate::compositor::Compositor; use crate::compositor::Compositor;
use crate::events::{OnModeSwitch, PostInsertChar}; use crate::events::{OnModeSwitch, PostInsertChar};
use crate::handlers::Handlers; use crate::handlers::Handlers;
use crate::ui::lsp::SignatureHelp; use crate::ui::lsp::{Signature, SignatureHelp};
use crate::ui::Popup; use crate::ui::Popup;
use crate::{job, ui}; use crate::{job, ui};
@ -82,6 +82,7 @@ impl helix_event::AsyncHook for SignatureHelpHandler {
} }
} }
self.state = if open { State::Open } else { State::Closed }; self.state = if open { State::Open } else { State::Closed };
return timeout; return timeout;
} }
} }
@ -138,6 +139,31 @@ pub fn request_signature_help(
}); });
} }
fn active_param_range(
signature: &SignatureInformation,
response_active_parameter: Option<u32>,
) -> Option<(usize, usize)> {
let param_idx = signature
.active_parameter
.or(response_active_parameter)
.unwrap_or(0) as usize;
let param = signature.parameters.as_ref()?.get(param_idx)?;
match &param.label {
lsp::ParameterLabel::Simple(string) => {
let start = signature.label.find(string.as_str())?;
Some((start, start + string.len()))
}
lsp::ParameterLabel::LabelOffsets([start, end]) => {
// LS sends offsets based on utf-16 based string representation
// but highlighting in helix is done using byte offset.
use helix_core::str_utils::char_to_byte_idx;
let from = char_to_byte_idx(&signature.label, *start as usize);
let to = char_to_byte_idx(&signature.label, *end as usize);
Some((from, to))
}
}
}
pub fn show_signature_help( pub fn show_signature_help(
editor: &mut Editor, editor: &mut Editor,
compositor: &mut Compositor, compositor: &mut Compositor,
@ -184,54 +210,64 @@ pub fn show_signature_help(
let doc = doc!(editor); let doc = doc!(editor);
let language = doc.language_name().unwrap_or(""); let language = doc.language_name().unwrap_or("");
let signature = match response if response.signatures.is_empty() {
return;
}
let signatures: Vec<Signature> = response
.signatures .signatures
.get(response.active_signature.unwrap_or(0) as usize) .into_iter()
{ .map(|s| {
Some(s) => s, let active_param_range = active_param_range(&s, response.active_parameter);
None => return,
};
let mut contents = SignatureHelp::new(
signature.label.clone(),
language.to_string(),
Arc::clone(&editor.syn_loader),
);
let signature_doc = if config.lsp.display_signature_help_docs { let signature_doc = if config.lsp.display_signature_help_docs {
signature.documentation.as_ref().map(|doc| match doc { s.documentation.map(|doc| match doc {
lsp::Documentation::String(s) => s.clone(), lsp::Documentation::String(s) => s,
lsp::Documentation::MarkupContent(markup) => markup.value.clone(), lsp::Documentation::MarkupContent(markup) => markup.value,
}) })
} else { } else {
None None
}; };
contents.set_signature_doc(signature_doc); Signature {
signature: s.label,
let active_param_range = || -> Option<(usize, usize)> { signature_doc,
let param_idx = signature active_param_range,
.active_parameter
.or(response.active_parameter)
.unwrap_or(0) as usize;
let param = signature.parameters.as_ref()?.get(param_idx)?;
match &param.label {
lsp::ParameterLabel::Simple(string) => {
let start = signature.label.find(string.as_str())?;
Some((start, start + string.len()))
}
lsp::ParameterLabel::LabelOffsets([start, end]) => {
// LS sends offsets based on utf-16 based string representation
// but highlighting in helix is done using byte offset.
use helix_core::str_utils::char_to_byte_idx;
let from = char_to_byte_idx(&signature.label, *start as usize);
let to = char_to_byte_idx(&signature.label, *end as usize);
Some((from, to))
}
} }
}; })
contents.set_active_param_range(active_param_range()); .collect();
let old_popup = compositor.find_id::<Popup<SignatureHelp>>(SignatureHelp::ID); let old_popup = compositor.find_id::<Popup<SignatureHelp>>(SignatureHelp::ID);
let lsp_signature = response.active_signature.map(|s| s as usize);
// take the new suggested lsp signature if changed
// otherwise take the old signature if possible
// otherwise the last one (in case there is less signatures than before)
let active_signature = old_popup
.as_ref()
.map(|popup| {
let old_lsp_sig = popup.contents().lsp_signature();
let old_sig = popup
.contents()
.active_signature()
.min(signatures.len() - 1);
if old_lsp_sig != lsp_signature {
lsp_signature.unwrap_or(old_sig)
} else {
old_sig
}
})
.unwrap_or(lsp_signature.unwrap_or_default());
let contents = SignatureHelp::new(
language.to_string(),
Arc::clone(&editor.syn_loader),
active_signature,
lsp_signature,
signatures,
);
let mut popup = Popup::new(SignatureHelp::ID, contents) let mut popup = Popup::new(SignatureHelp::ID, contents)
.position(old_popup.and_then(|p| p.get_position())) .position(old_popup.and_then(|p| p.get_position()))
.position_bias(Open::Above) .position_bias(Open::Above)

@ -2,7 +2,7 @@ use crossterm::{
style::{Color, Print, Stylize}, style::{Color, Print, Stylize},
tty::IsTty, tty::IsTty,
}; };
use helix_core::config::{default_syntax_loader, user_syntax_loader}; use helix_core::config::{default_lang_config, user_lang_config};
use helix_loader::grammar::load_runtime_file; use helix_loader::grammar::load_runtime_file;
use helix_view::clipboard::get_clipboard_provider; use helix_view::clipboard::get_clipboard_provider;
use std::io::Write; use std::io::Write;
@ -128,7 +128,7 @@ pub fn languages_all() -> std::io::Result<()> {
let stdout = std::io::stdout(); let stdout = std::io::stdout();
let mut stdout = stdout.lock(); let mut stdout = stdout.lock();
let mut syn_loader_conf = match user_syntax_loader() { let mut syn_loader_conf = match user_lang_config() {
Ok(conf) => conf, Ok(conf) => conf,
Err(err) => { Err(err) => {
let stderr = std::io::stderr(); let stderr = std::io::stderr();
@ -141,7 +141,7 @@ pub fn languages_all() -> std::io::Result<()> {
err err
)?; )?;
writeln!(stderr, "{}", "Using default language config".yellow())?; writeln!(stderr, "{}", "Using default language config".yellow())?;
default_syntax_loader() default_lang_config()
} }
}; };
@ -234,7 +234,7 @@ pub fn language(lang_str: String) -> std::io::Result<()> {
let stdout = std::io::stdout(); let stdout = std::io::stdout();
let mut stdout = stdout.lock(); let mut stdout = stdout.lock();
let syn_loader_conf = match user_syntax_loader() { let syn_loader_conf = match user_lang_config() {
Ok(conf) => conf, Ok(conf) => conf,
Err(err) => { Err(err) => {
let stderr = std::io::stderr(); let stderr = std::io::stderr();
@ -247,7 +247,7 @@ pub fn language(lang_str: String) -> std::io::Result<()> {
err err
)?; )?;
writeln!(stderr, "{}", "Using default language config".yellow())?; writeln!(stderr, "{}", "Using default language config".yellow())?;
default_syntax_loader() default_lang_config()
} }
}; };

@ -303,6 +303,15 @@ impl Keymaps {
self.sticky.as_ref() self.sticky.as_ref()
} }
pub fn contains_key(&self, mode: Mode, key: KeyEvent) -> bool {
let keymaps = &*self.map();
let keymap = &keymaps[&mode];
keymap
.search(self.pending())
.and_then(KeyTrie::node)
.is_some_and(|node| node.contains_key(&key))
}
pub(crate) fn get_with_map( pub(crate) fn get_with_map(
&mut self, &mut self,
keymaps: &HashMap<Mode, KeyTrie>, keymaps: &HashMap<Mode, KeyTrie>,

@ -58,6 +58,7 @@ pub fn default() -> HashMap<Mode, KeyTrie> {
"k" => move_line_up, "k" => move_line_up,
"j" => move_line_down, "j" => move_line_down,
"." => goto_last_modification, "." => goto_last_modification,
"w" => goto_word,
}, },
":" => command_mode, ":" => command_mode,
@ -86,10 +87,12 @@ pub fn default() -> HashMap<Mode, KeyTrie> {
"A-;" => flip_selections, "A-;" => flip_selections,
"A-o" | "A-up" => expand_selection, "A-o" | "A-up" => expand_selection,
"A-i" | "A-down" => shrink_selection, "A-i" | "A-down" => shrink_selection,
"A-I" | "A-S-down" => select_all_children,
"A-p" | "A-left" => select_prev_sibling, "A-p" | "A-left" => select_prev_sibling,
"A-n" | "A-right" => select_next_sibling, "A-n" | "A-right" => select_next_sibling,
"A-e" => move_parent_node_end, "A-e" => move_parent_node_end,
"A-b" => move_parent_node_start, "A-b" => move_parent_node_start,
"A-a" => select_all_siblings,
"%" => select_all, "%" => select_all,
"x" => extend_line_below, "x" => extend_line_below,
@ -113,6 +116,7 @@ pub fn default() -> HashMap<Mode, KeyTrie> {
"t" => goto_prev_class, "t" => goto_prev_class,
"a" => goto_prev_parameter, "a" => goto_prev_parameter,
"c" => goto_prev_comment, "c" => goto_prev_comment,
"e" => goto_prev_entry,
"T" => goto_prev_test, "T" => goto_prev_test,
"p" => goto_prev_paragraph, "p" => goto_prev_paragraph,
"space" => add_newline_above, "space" => add_newline_above,
@ -126,6 +130,7 @@ pub fn default() -> HashMap<Mode, KeyTrie> {
"t" => goto_next_class, "t" => goto_next_class,
"a" => goto_next_parameter, "a" => goto_next_parameter,
"c" => goto_next_comment, "c" => goto_next_comment,
"e" => goto_next_entry,
"T" => goto_next_test, "T" => goto_next_test,
"p" => goto_next_paragraph, "p" => goto_next_paragraph,
"space" => add_newline_below, "space" => add_newline_below,
@ -178,8 +183,8 @@ pub fn default() -> HashMap<Mode, KeyTrie> {
"esc" => normal_mode, "esc" => normal_mode,
"C-b" | "pageup" => page_up, "C-b" | "pageup" => page_up,
"C-f" | "pagedown" => page_down, "C-f" | "pagedown" => page_down,
"C-u" => half_page_up, "C-u" => page_cursor_half_up,
"C-d" => half_page_down, "C-d" => page_cursor_half_down,
"C-w" => { "Window" "C-w" => { "Window"
"C-w" | "w" => rotate_view, "C-w" | "w" => rotate_view,
@ -222,9 +227,10 @@ pub fn default() -> HashMap<Mode, KeyTrie> {
"S" => workspace_symbol_picker, "S" => workspace_symbol_picker,
"d" => diagnostics_picker, "d" => diagnostics_picker,
"D" => workspace_diagnostics_picker, "D" => workspace_diagnostics_picker,
"g" => changed_file_picker,
"a" => code_action, "a" => code_action,
"'" => last_picker, "'" => last_picker,
"g" => { "Debug (experimental)" sticky=true "G" => { "Debug (experimental)" sticky=true
"l" => dap_launch, "l" => dap_launch,
"r" => dap_restart, "r" => dap_restart,
"b" => dap_toggle_breakpoint, "b" => dap_toggle_breakpoint,
@ -276,6 +282,9 @@ pub fn default() -> HashMap<Mode, KeyTrie> {
"k" => hover, "k" => hover,
"r" => rename_symbol, "r" => rename_symbol,
"h" => select_references_to_symbol_under_cursor, "h" => select_references_to_symbol_under_cursor,
"c" => toggle_comments,
"C" => toggle_block_comments,
"A-c" => toggle_line_comments,
"?" => command_palette, "?" => command_palette,
}, },
"z" => { "View" "z" => { "View"
@ -287,8 +296,8 @@ pub fn default() -> HashMap<Mode, KeyTrie> {
"j" | "down" => scroll_down, "j" | "down" => scroll_down,
"C-b" | "pageup" => page_up, "C-b" | "pageup" => page_up,
"C-f" | "pagedown" => page_down, "C-f" | "pagedown" => page_down,
"C-u" | "backspace" => half_page_up, "C-u" | "backspace" => page_cursor_half_up,
"C-d" | "space" => half_page_down, "C-d" | "space" => page_cursor_half_down,
"/" => search, "/" => search,
"?" => rsearch, "?" => rsearch,
@ -304,8 +313,8 @@ pub fn default() -> HashMap<Mode, KeyTrie> {
"j" | "down" => scroll_down, "j" | "down" => scroll_down,
"C-b" | "pageup" => page_up, "C-b" | "pageup" => page_up,
"C-f" | "pagedown" => page_down, "C-f" | "pagedown" => page_down,
"C-u" | "backspace" => half_page_up, "C-u" | "backspace" => page_cursor_half_up,
"C-d" | "space" => half_page_down, "C-d" | "space" => page_cursor_half_down,
"/" => search, "/" => search,
"?" => rsearch, "?" => rsearch,
@ -357,6 +366,7 @@ pub fn default() -> HashMap<Mode, KeyTrie> {
"g" => { "Goto" "g" => { "Goto"
"k" => extend_line_up, "k" => extend_line_up,
"j" => extend_line_down, "j" => extend_line_down,
"w" => extend_to_word,
}, },
})); }));
let insert = keymap!({ "Insert mode" let insert = keymap!({ "Insert mode"

@ -20,25 +20,39 @@ mod handlers;
use ignore::DirEntry; use ignore::DirEntry;
use url::Url; use url::Url;
pub use keymap::macros::*; #[cfg(windows)]
fn true_color() -> bool {
true
}
#[cfg(not(windows))] #[cfg(not(windows))]
fn true_color() -> bool { fn true_color() -> bool {
std::env::var("COLORTERM") if matches!(
.map(|v| matches!(v.as_str(), "truecolor" | "24bit")) std::env::var("COLORTERM").map(|v| matches!(v.as_str(), "truecolor" | "24bit")),
.unwrap_or(false) Ok(true)
) {
return true;
}
match termini::TermInfo::from_env() {
Ok(t) => {
t.extended_cap("RGB").is_some()
|| t.extended_cap("Tc").is_some()
|| (t.extended_cap("setrgbf").is_some() && t.extended_cap("setrgbb").is_some())
}
Err(_) => false,
} }
#[cfg(windows)]
fn true_color() -> bool {
true
} }
/// Function used for filtering dir entries in the various file pickers. /// Function used for filtering dir entries in the various file pickers.
fn filter_picker_entry(entry: &DirEntry, root: &Path, dedup_symlinks: bool) -> bool { fn filter_picker_entry(entry: &DirEntry, root: &Path, dedup_symlinks: bool) -> bool {
// We always want to ignore the .git directory, otherwise if // We always want to ignore popular VCS directories, otherwise if
// `ignore` is turned off, we end up with a lot of noise // `ignore` is turned off, we end up with a lot of noise
// in our picker. // in our picker.
if entry.file_name() == ".git" { if matches!(
entry.file_name().to_str(),
Some(".git" | ".pijul" | ".jj" | ".hg" | ".svn")
) {
return false; return false;
} }

@ -148,18 +148,18 @@ FLAGS:
} }
}; };
let syn_loader_conf = helix_core::config::user_syntax_loader().unwrap_or_else(|err| { let lang_loader = helix_core::config::user_lang_loader().unwrap_or_else(|err| {
eprintln!("Bad language config: {}", err); eprintln!("{}", err);
eprintln!("Press <ENTER> to continue with default language config"); eprintln!("Press <ENTER> to continue with default language config");
use std::io::Read; use std::io::Read;
// This waits for an enter press. // This waits for an enter press.
let _ = std::io::stdin().read(&mut []); let _ = std::io::stdin().read(&mut []);
helix_core::config::default_syntax_loader() helix_core::config::default_lang_loader()
}); });
// TODO: use the thread local executor to spawn the application task separately from the work pool // TODO: use the thread local executor to spawn the application task separately from the work pool
let mut app = Application::new(args, config, syn_loader_conf) let mut app =
.context("unable to create new application")?; Application::new(args, config, lang_loader).context("unable to create new application")?;
let exit_code = app.run(&mut EventStream::new()).await?; let exit_code = app.run(&mut EventStream::new()).await?;

@ -1,11 +1,10 @@
use crate::{ use crate::{
compositor::{Component, Context, Event, EventResult}, compositor::{Component, Context, Event, EventResult},
handlers::trigger_auto_completion, handlers::{completion::ResolveHandler, trigger_auto_completion},
}; };
use helix_view::{ use helix_view::{
document::SavePoint, document::SavePoint,
editor::CompleteAction, editor::CompleteAction,
graphics::Margin,
handlers::lsp::SignatureHelpInvoked, handlers::lsp::SignatureHelpInvoked,
theme::{Modifier, Style}, theme::{Modifier, Style},
ViewId, ViewId,
@ -17,10 +16,9 @@ use std::{borrow::Cow, sync::Arc};
use helix_core::{chars, Change, Transaction}; use helix_core::{chars, Change, Transaction};
use helix_view::{graphics::Rect, Document, Editor}; use helix_view::{graphics::Rect, Document, Editor};
use crate::commands;
use crate::ui::{menu, Markdown, Menu, Popup, PromptEvent}; use crate::ui::{menu, Markdown, Menu, Popup, PromptEvent};
use helix_lsp::{lsp, util, OffsetEncoding}; use helix_lsp::{lsp, util, LanguageServerId, OffsetEncoding};
impl menu::Item for CompletionItem { impl menu::Item for CompletionItem {
type Data = (); type Data = ();
@ -92,7 +90,7 @@ impl menu::Item for CompletionItem {
#[derive(Debug, PartialEq, Default, Clone)] #[derive(Debug, PartialEq, Default, Clone)]
pub struct CompletionItem { pub struct CompletionItem {
pub item: lsp::CompletionItem, pub item: lsp::CompletionItem,
pub language_server_id: usize, pub provider: LanguageServerId,
pub resolved: bool, pub resolved: bool,
} }
@ -102,6 +100,7 @@ pub struct Completion {
#[allow(dead_code)] #[allow(dead_code)]
trigger_offset: usize, trigger_offset: usize,
filter: String, filter: String,
resolve_handler: ResolveHandler,
} }
impl Completion { impl Completion {
@ -221,7 +220,7 @@ impl Completion {
($item:expr) => { ($item:expr) => {
match editor match editor
.language_servers .language_servers
.get_by_id($item.language_server_id) .get_by_id($item.provider)
{ {
Some(ls) => ls, Some(ls) => ls,
None => { None => {
@ -282,14 +281,8 @@ impl Completion {
// let language_server = language_server!(item); // let language_server = language_server!(item);
// let offset_encoding = language_server.offset_encoding(); // let offset_encoding = language_server.offset_encoding();
// let language_server = editor
// .language_servers
// .get_by_id(item.language_server_id)
// .unwrap();
let mut language_server_option = None; let mut language_server_option = None;
// resolve item if not yet resolved
if !item.resolved { if !item.resolved {
let language_server = language_server!(item); let language_server = language_server!(item);
// let offset_encoding = language_server.offset_encoding(); // let offset_encoding = language_server.offset_encoding();
@ -354,16 +347,9 @@ impl Completion {
} }
}); });
let margin = if editor.menu_border() {
Margin::vertical(1)
} else {
Margin::none()
};
let popup = Popup::new(Self::ID, menu) let popup = Popup::new(Self::ID, menu)
.with_scrollbar(false) .with_scrollbar(false)
.ignore_escape_key(true) .ignore_escape_key(true);
.margin(margin);
let (view, doc) = current_ref!(editor); let (view, doc) = current_ref!(editor);
let text = doc.text().slice(..); let text = doc.text().slice(..);
@ -382,6 +368,7 @@ impl Completion {
// TODO: expand nucleo api to allow moving straight to a Utf32String here // TODO: expand nucleo api to allow moving straight to a Utf32String here
// and avoid allocation during matching // and avoid allocation during matching
filter: String::from(fragment), filter: String::from(fragment),
resolve_handler: ResolveHandler::new(),
}; };
// need to recompute immediately in case start_offset != trigger_offset // need to recompute immediately in case start_offset != trigger_offset
@ -393,14 +380,25 @@ impl Completion {
completion completion
} }
/// Synchronously resolve the given completion item. This is used when
/// accepting a completion.
fn resolve_completion_item( fn resolve_completion_item(
language_server: &helix_lsp::Client, language_server: &helix_lsp::Client,
completion_item: lsp::CompletionItem, completion_item: lsp::CompletionItem,
) -> Option<lsp::CompletionItem> { ) -> Option<lsp::CompletionItem> {
let future = language_server.resolve_completion_item(completion_item)?; if !matches!(
language_server.capabilities().completion_provider,
Some(lsp::CompletionOptions {
resolve_provider: Some(true),
..
})
) {
return None;
}
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 {
Ok(value) => serde_json::from_value(value).ok(), Ok(item) => Some(item),
Err(err) => { Err(err) => {
log::error!("Failed to resolve completion item: {}", err); log::error!("Failed to resolve completion item: {}", err);
None None
@ -430,66 +428,10 @@ impl Completion {
self.popup.contents().is_empty() self.popup.contents().is_empty()
} }
fn replace_item(&mut self, old_item: CompletionItem, new_item: CompletionItem) { pub 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);
} }
/// Asynchronously requests that the currently selection completion item is
/// resolved through LSP `completionItem/resolve`.
pub fn ensure_item_resolved(&mut self, cx: &mut commands::Context) -> bool {
// > If computing full completion items is expensive, servers can additionally provide a
// > handler for the completion item resolve request. ...
// > A typical use case is for example: the `textDocument/completion` request doesn't fill
// > in the `documentation` property for returned completion items since it is expensive
// > to compute. When the item is selected in the user interface then a
// > 'completionItem/resolve' request is sent with the selected completion item as a parameter.
// > 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
let current_item = match self.popup.contents().selection() {
Some(item) if !item.resolved => item.clone(),
_ => return false,
};
let Some(language_server) = cx
.editor
.language_server_by_id(current_item.language_server_id)
else {
return false;
};
// This method should not block the compositor so we handle the response asynchronously.
let Some(future) = language_server.resolve_completion_item(current_item.item.clone())
else {
return false;
};
cx.callback(
future,
move |_editor, compositor, response: Option<lsp::CompletionItem>| {
let resolved_item = match response {
Some(item) => item,
None => return,
};
if let Some(completion) = &mut compositor
.find::<crate::ui::EditorView>()
.unwrap()
.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);
}
},
);
true
}
pub fn area(&mut self, viewport: Rect, editor: &Editor) -> Rect { pub fn area(&mut self, viewport: Rect, editor: &Editor) -> Rect {
self.popup.area(viewport, editor) self.popup.area(viewport, editor)
} }
@ -508,10 +450,13 @@ impl Component for Completion {
self.popup.render(area, surface, cx); self.popup.render(area, surface, cx);
// if we have a selection, render a markdown popup on top/below with info // if we have a selection, render a markdown popup on top/below with info
let option = match self.popup.contents().selection() { let option = match self.popup.contents_mut().selection_mut() {
Some(option) => option, Some(option) => option,
None => return, None => return,
}; };
if !option.resolved {
self.resolve_handler.ensure_item_resolved(cx.editor, option);
}
// need to render: // need to render:
// option.detail // option.detail
// --- // ---
@ -559,12 +504,7 @@ impl Component for Completion {
None => return, None => return,
}; };
let popup_area = { let popup_area = self.popup.area(area, cx.editor);
let (popup_x, popup_y) = self.popup.get_rel_position(area, cx.editor);
let (popup_width, popup_height) = self.popup.get_size();
Rect::new(popup_x, popup_y, popup_width, popup_height)
};
let doc_width_available = area.width.saturating_sub(popup_area.right()); let doc_width_available = area.width.saturating_sub(popup_area.right());
let doc_area = if doc_width_available > 30 { let doc_area = if doc_width_available > 30 {
let mut doc_width = doc_width_available; let mut doc_width = doc_width_available;

@ -7,6 +7,7 @@ use helix_core::syntax::Highlight;
use helix_core::syntax::HighlightEvent; use helix_core::syntax::HighlightEvent;
use helix_core::text_annotations::TextAnnotations; use helix_core::text_annotations::TextAnnotations;
use helix_core::{visual_offset_from_block, Position, RopeSlice}; use helix_core::{visual_offset_from_block, Position, RopeSlice};
use helix_stdx::rope::RopeSliceExt;
use helix_view::editor::{WhitespaceConfig, WhitespaceRenderValue}; use helix_view::editor::{WhitespaceConfig, WhitespaceRenderValue};
use helix_view::graphics::Rect; use helix_view::graphics::Rect;
use helix_view::theme::Style; use helix_view::theme::Style;
@ -32,14 +33,27 @@ impl<F: FnMut(&mut TextRenderer, LinePos)> LineDecoration for F {
} }
} }
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
enum StyleIterKind {
/// base highlights (usually emitted by TS), byte indices (potentially not codepoint aligned)
BaseHighlights,
/// overlay highlights (emitted by custom code from selections), char indices
Overlay,
}
/// A wrapper around a HighlightIterator /// A wrapper around a HighlightIterator
/// that merges the layered highlights to create the final text style /// that merges the layered highlights to create the final text style
/// and yields the active text style and the char_idx where the active /// and yields the active text style and the char_idx where the active
/// style will have to be recomputed. /// style will have to be recomputed.
///
/// TODO(ropey2): hopefully one day helix and ropey will operate entirely
/// on byte ranges and we can remove this
struct StyleIter<'a, H: Iterator<Item = HighlightEvent>> { struct StyleIter<'a, H: Iterator<Item = HighlightEvent>> {
text_style: Style, text_style: Style,
active_highlights: Vec<Highlight>, active_highlights: Vec<Highlight>,
highlight_iter: H, highlight_iter: H,
kind: StyleIterKind,
text: RopeSlice<'a>,
theme: &'a Theme, theme: &'a Theme,
} }
@ -54,7 +68,7 @@ impl<H: Iterator<Item = HighlightEvent>> Iterator for StyleIter<'_, H> {
HighlightEvent::HighlightEnd => { HighlightEvent::HighlightEnd => {
self.active_highlights.pop(); self.active_highlights.pop();
} }
HighlightEvent::Source { start, end } => { HighlightEvent::Source { start, mut end } => {
if start == end { if start == end {
continue; continue;
} }
@ -64,7 +78,9 @@ impl<H: Iterator<Item = HighlightEvent>> Iterator for StyleIter<'_, H> {
.fold(self.text_style, |acc, span| { .fold(self.text_style, |acc, span| {
acc.patch(self.theme.highlight(span.0)) acc.patch(self.theme.highlight(span.0))
}); });
if self.kind == StyleIterKind::BaseHighlights {
end = self.text.byte_to_next_char(end);
}
return Some((style, end)); return Some((style, end));
} }
} }
@ -188,13 +204,17 @@ pub fn render_text<'t>(
text_style: renderer.text_style, text_style: renderer.text_style,
active_highlights: Vec::with_capacity(64), active_highlights: Vec::with_capacity(64),
highlight_iter: syntax_highlight_iter, highlight_iter: syntax_highlight_iter,
kind: StyleIterKind::BaseHighlights,
theme, theme,
text,
}; };
let mut overlay_styles = StyleIter { let mut overlay_styles = StyleIter {
text_style: Style::default(), text_style: Style::default(),
active_highlights: Vec::with_capacity(64), active_highlights: Vec::with_capacity(64),
highlight_iter: overlay_highlight_iter, highlight_iter: overlay_highlight_iter,
kind: StyleIterKind::Overlay,
theme, theme,
text,
}; };
let mut last_line_pos = LinePos { let mut last_line_pos = LinePos {
@ -344,6 +364,7 @@ pub struct TextRenderer<'a> {
pub indent_guide_style: Style, pub indent_guide_style: Style,
pub newline: String, pub newline: String,
pub nbsp: String, pub nbsp: String,
pub nnbsp: String,
pub space: String, pub space: String,
pub tab: String, pub tab: String,
pub virtual_tab: String, pub virtual_tab: String,
@ -398,6 +419,11 @@ impl<'a> TextRenderer<'a> {
} else { } else {
" ".to_owned() " ".to_owned()
}; };
let nnbsp = if ws_render.nnbsp() == WhitespaceRenderValue::All {
ws_chars.nnbsp.into()
} else {
" ".to_owned()
};
let text_style = theme.get("ui.text"); let text_style = theme.get("ui.text");
@ -408,6 +434,7 @@ impl<'a> TextRenderer<'a> {
indent_guide_char: editor_config.indent_guides.character.into(), indent_guide_char: editor_config.indent_guides.character.into(),
newline, newline,
nbsp, nbsp,
nnbsp,
space, space,
tab, tab,
virtual_tab, virtual_tab,
@ -451,6 +478,7 @@ impl<'a> TextRenderer<'a> {
let width = grapheme.width(); let width = grapheme.width();
let space = if is_virtual { " " } else { &self.space }; let space = if is_virtual { " " } else { &self.space };
let nbsp = if is_virtual { " " } else { &self.nbsp }; let nbsp = if is_virtual { " " } else { &self.nbsp };
let nnbsp = if is_virtual { " " } else { &self.nnbsp };
let tab = if is_virtual { let tab = if is_virtual {
&self.virtual_tab &self.virtual_tab
} else { } else {
@ -464,6 +492,7 @@ impl<'a> TextRenderer<'a> {
// TODO special rendering for other whitespaces? // TODO special rendering for other whitespaces?
Grapheme::Other { ref g } if g == " " => space, Grapheme::Other { ref g } if g == " " => space,
Grapheme::Other { ref g } if g == "\u{00A0}" => nbsp, Grapheme::Other { ref g } if g == "\u{00A0}" => nbsp,
Grapheme::Other { ref g } if g == "\u{202F}" => nnbsp,
Grapheme::Other { ref g } => g, Grapheme::Other { ref g } => g,
Grapheme::Newline => &self.newline, Grapheme::Newline => &self.newline,
}; };

@ -12,9 +12,7 @@ use crate::{
use helix_core::{ use helix_core::{
diagnostic::NumberOrString, diagnostic::NumberOrString,
graphemes::{ graphemes::{next_grapheme_boundary, prev_grapheme_boundary},
ensure_grapheme_boundary_next_byte, next_grapheme_boundary, prev_grapheme_boundary,
},
movement::Direction, movement::Direction,
syntax::{self, HighlightEvent}, syntax::{self, HighlightEvent},
text_annotations::TextAnnotations, text_annotations::TextAnnotations,
@ -315,26 +313,14 @@ impl EditorView {
let iter = syntax let iter = syntax
// TODO: range doesn't actually restrict source, just highlight range // TODO: range doesn't actually restrict source, just highlight range
.highlight_iter(text.slice(..), Some(range), None) .highlight_iter(text.slice(..), Some(range), None)
.map(|event| event.unwrap()) .map(|event| event.unwrap());
.map(move |event| match event {
// TODO: use byte slices directly
// convert byte offsets to char offset
HighlightEvent::Source { start, end } => {
let start =
text.byte_to_char(ensure_grapheme_boundary_next_byte(text, start));
let end =
text.byte_to_char(ensure_grapheme_boundary_next_byte(text, end));
HighlightEvent::Source { start, end }
}
event => event,
});
Box::new(iter) Box::new(iter)
} }
None => Box::new( None => Box::new(
[HighlightEvent::Source { [HighlightEvent::Source {
start: text.byte_to_char(range.start), start: range.start,
end: text.byte_to_char(range.end), end: range.end,
}] }]
.into_iter(), .into_iter(),
), ),
@ -350,7 +336,8 @@ impl EditorView {
let text = doc.text().slice(..); let text = doc.text().slice(..);
let row = text.char_to_line(anchor.min(text.len_chars())); let row = text.char_to_line(anchor.min(text.len_chars()));
let range = Self::viewport_byte_range(text, row, height); let mut range = Self::viewport_byte_range(text, row, height);
range = text.byte_to_char(range.start)..text.byte_to_char(range.end);
text_annotations.collect_overlay_highlights(range) text_annotations.collect_overlay_highlights(range)
} }
@ -359,8 +346,8 @@ impl EditorView {
pub fn doc_diagnostics_highlights( pub fn doc_diagnostics_highlights(
doc: &Document, doc: &Document,
theme: &Theme, theme: &Theme,
) -> [Vec<(usize, std::ops::Range<usize>)>; 5] { ) -> [Vec<(usize, std::ops::Range<usize>)>; 7] {
use helix_core::diagnostic::Severity; use helix_core::diagnostic::{DiagnosticTag, Range, Severity};
let get_scope_of = |scope| { let get_scope_of = |scope| {
theme theme
.find_scope_index_exact(scope) .find_scope_index_exact(scope)
@ -380,11 +367,34 @@ impl EditorView {
let error = get_scope_of("diagnostic.error"); let error = get_scope_of("diagnostic.error");
let r#default = get_scope_of("diagnostic"); // this is a bit redundant but should be fine let r#default = get_scope_of("diagnostic"); // this is a bit redundant but should be fine
// Diagnostic tags
let unnecessary = theme.find_scope_index_exact("diagnostic.unnecessary");
let deprecated = theme.find_scope_index_exact("diagnostic.deprecated");
let mut default_vec: Vec<(usize, std::ops::Range<usize>)> = Vec::new(); let mut default_vec: Vec<(usize, std::ops::Range<usize>)> = Vec::new();
let mut info_vec = Vec::new(); let mut info_vec = Vec::new();
let mut hint_vec = Vec::new(); let mut hint_vec = Vec::new();
let mut warning_vec = Vec::new(); let mut warning_vec = Vec::new();
let mut error_vec = Vec::new(); let mut error_vec = Vec::new();
let mut unnecessary_vec = Vec::new();
let mut deprecated_vec = Vec::new();
let push_diagnostic =
|vec: &mut Vec<(usize, std::ops::Range<usize>)>, scope, range: Range| {
// If any diagnostic overlaps ranges with the prior diagnostic,
// merge the two together. Otherwise push a new span.
match vec.last_mut() {
Some((_, existing_range)) if range.start <= existing_range.end => {
// This branch merges overlapping diagnostics, assuming that the current
// diagnostic starts on range.start or later. If this assertion fails,
// we will discard some part of `diagnostic`. This implies that
// `doc.diagnostics()` is not sorted by `diagnostic.range`.
debug_assert!(existing_range.start <= range.start);
existing_range.end = range.end.max(existing_range.end)
}
_ => vec.push((scope, range.start..range.end)),
}
};
for diagnostic in doc.diagnostics() { for diagnostic in doc.diagnostics() {
// Separate diagnostics into different Vecs by severity. // Separate diagnostics into different Vecs by severity.
@ -396,22 +406,44 @@ impl EditorView {
_ => (&mut default_vec, r#default), _ => (&mut default_vec, r#default),
}; };
// If any diagnostic overlaps ranges with the prior diagnostic, // If the diagnostic has tags and a non-warning/error severity, skip rendering
// merge the two together. Otherwise push a new span. // the diagnostic as info/hint/default and only render it as unnecessary/deprecated
match vec.last_mut() { // instead. For warning/error diagnostics, render both the severity highlight and
Some((_, range)) if diagnostic.range.start <= range.end => { // the tag highlight.
// This branch merges overlapping diagnostics, assuming that the current if diagnostic.tags.is_empty()
// diagnostic starts on range.start or later. If this assertion fails, || matches!(
// we will discard some part of `diagnostic`. This implies that diagnostic.severity,
// `doc.diagnostics()` is not sorted by `diagnostic.range`. Some(Severity::Warning | Severity::Error)
debug_assert!(range.start <= diagnostic.range.start); )
range.end = diagnostic.range.end.max(range.end) {
push_diagnostic(vec, scope, diagnostic.range);
}
for tag in &diagnostic.tags {
match tag {
DiagnosticTag::Unnecessary => {
if let Some(scope) = unnecessary {
push_diagnostic(&mut unnecessary_vec, scope, diagnostic.range)
}
}
DiagnosticTag::Deprecated => {
if let Some(scope) = deprecated {
push_diagnostic(&mut deprecated_vec, scope, diagnostic.range)
}
}
} }
_ => vec.push((scope, diagnostic.range.start..diagnostic.range.end)),
} }
} }
[default_vec, info_vec, hint_vec, warning_vec, error_vec] [
default_vec,
unnecessary_vec,
deprecated_vec,
info_vec,
hint_vec,
warning_vec,
error_vec,
]
} }
/// Get highlight spans for selections in a document view. /// Get highlight spans for selections in a document view.
@ -716,7 +748,8 @@ impl EditorView {
} }
} }
let paragraph = Paragraph::new(lines) let text = Text::from(lines);
let paragraph = Paragraph::new(&text)
.alignment(Alignment::Right) .alignment(Alignment::Right)
.wrap(Wrap { trim: true }); .wrap(Wrap { trim: true });
let width = 100.min(viewport.width); let width = 100.min(viewport.width);
@ -906,11 +939,15 @@ impl EditorView {
fn command_mode(&mut self, mode: Mode, cxt: &mut commands::Context, event: KeyEvent) { fn command_mode(&mut self, mode: Mode, cxt: &mut commands::Context, event: KeyEvent) {
match (event, cxt.editor.count) { match (event, cxt.editor.count) {
// count handling // If the count is already started and the input is a number, always continue the count.
(key!(i @ '0'), Some(_)) | (key!(i @ '1'..='9'), _) => { (key!(i @ '0'..='9'), Some(count)) => {
let i = i.to_digit(10).unwrap() as usize;
cxt.editor.count = NonZeroUsize::new(count.get() * 10 + i);
}
// A non-zero digit will start the count if that number isn't used by a keymap.
(key!(i @ '1'..='9'), None) if !self.keymaps.contains_key(mode, event) => {
let i = i.to_digit(10).unwrap() as usize; let i = i.to_digit(10).unwrap() as usize;
cxt.editor.count = cxt.editor.count = NonZeroUsize::new(i);
std::num::NonZeroUsize::new(cxt.editor.count.map_or(i, |c| c.get() * 10 + i));
} }
// special handling for repeat operator // special handling for repeat operator
(key!('.'), _) if self.keymaps.pending().is_empty() => { (key!('.'), _) if self.keymaps.pending().is_empty() => {
@ -1001,7 +1038,6 @@ impl EditorView {
self.last_insert.1.push(InsertEvent::TriggerCompletion); self.last_insert.1.push(InsertEvent::TriggerCompletion);
// TODO : propagate required size on resize to completion too // TODO : propagate required size on resize to completion too
completion.required_size((size.width, size.height));
self.completion = Some(completion); self.completion = Some(completion);
Some(area) Some(area)
} }
@ -1029,26 +1065,38 @@ impl EditorView {
pub fn handle_idle_timeout(&mut self, cx: &mut commands::Context) -> EventResult { pub fn handle_idle_timeout(&mut self, cx: &mut commands::Context) -> EventResult {
commands::compute_inlay_hints_for_all_views(cx.editor, cx.jobs); commands::compute_inlay_hints_for_all_views(cx.editor, cx.jobs);
if let Some(completion) = &mut self.completion {
return if completion.ensure_item_resolved(cx) {
EventResult::Consumed(None)
} else {
EventResult::Ignored(None) EventResult::Ignored(None)
}; }
} }
EventResult::Ignored(None) impl EditorView {
/// must be called whenever the editor processed input that
/// is not a `KeyEvent`. In these cases any pending keys/on next
/// key callbacks must be canceled.
fn handle_non_key_input(&mut self, cxt: &mut commands::Context) {
cxt.editor.status_msg = None;
cxt.editor.reset_idle_timer();
// HACKS: create a fake key event that will never trigger any actual map
// and therefore simply acts as "dismiss"
let null_key_event = KeyEvent {
code: KeyCode::Null,
modifiers: KeyModifiers::empty(),
};
// dismiss any pending keys
if let Some(on_next_key) = self.on_next_key.take() {
on_next_key(cxt, null_key_event);
} }
self.handle_keymap_event(cxt.editor.mode, cxt, null_key_event);
self.pseudo_pending.clear();
} }
impl EditorView {
fn handle_mouse_event( fn handle_mouse_event(
&mut self, &mut self,
event: &MouseEvent, event: &MouseEvent,
cxt: &mut commands::Context, cxt: &mut commands::Context,
) -> EventResult { ) -> EventResult {
if event.kind != MouseEventKind::Moved { if event.kind != MouseEventKind::Moved {
cxt.editor.reset_idle_timer(); self.handle_non_key_input(cxt)
} }
let config = cxt.editor.config(); let config = cxt.editor.config();
@ -1090,6 +1138,15 @@ impl EditorView {
if modifiers == KeyModifiers::ALT { if modifiers == KeyModifiers::ALT {
let selection = doc.selection(view_id).clone(); let selection = doc.selection(view_id).clone();
doc.set_selection(view_id, selection.push(Range::point(pos))); doc.set_selection(view_id, selection.push(Range::point(pos)));
} else if editor.mode == Mode::Select {
// Discards non-primary selections for consistent UX with normal mode
let primary = doc.selection(view_id).primary().put_cursor(
doc.text().slice(..),
pos,
true,
);
editor.mouse_down_range = Some(primary);
doc.set_selection(view_id, Selection::single(primary.anchor, primary.head));
} else { } else {
doc.set_selection(view_id, Selection::point(pos)); doc.set_selection(view_id, Selection::point(pos));
} }
@ -1158,7 +1215,7 @@ impl EditorView {
} }
let offset = config.scroll_lines.unsigned_abs(); let offset = config.scroll_lines.unsigned_abs();
commands::scroll(cxt, offset, direction); commands::scroll(cxt, offset, direction, false);
cxt.editor.tree.focus = current_view; cxt.editor.tree.focus = current_view;
cxt.editor.ensure_cursor_in_view(current_view); cxt.editor.ensure_cursor_in_view(current_view);
@ -1173,40 +1230,51 @@ impl EditorView {
let (view, doc) = current!(cxt.editor); let (view, doc) = current!(cxt.editor);
if doc let should_yank = match cxt.editor.mouse_down_range.take() {
.selection(view.id) Some(down_range) => doc.selection(view.id).primary() != down_range,
None => {
// This should not happen under normal cases. We fall back to the original
// behavior of yanking on non-single-char selections.
doc.selection(view.id)
.primary() .primary()
.slice(doc.text().slice(..)) .slice(doc.text().slice(..))
.len_chars() .len_chars()
<= 1 > 1
{
return EventResult::Ignored(None);
} }
};
commands::MappableCommand::yank_main_selection_to_primary_clipboard.execute(cxt); if should_yank {
commands::MappableCommand::yank_main_selection_to_primary_clipboard
.execute(cxt);
EventResult::Consumed(None) EventResult::Consumed(None)
} else {
EventResult::Ignored(None)
}
} }
MouseEventKind::Up(MouseButton::Right) => { MouseEventKind::Up(MouseButton::Right) => {
if let Some((coords, view_id)) = gutter_coords_and_view(cxt.editor, row, column) { if let Some((pos, view_id)) = gutter_coords_and_view(cxt.editor, row, column) {
cxt.editor.focus(view_id); cxt.editor.focus(view_id);
if let Some((pos, _)) = pos_and_view(cxt.editor, row, column, true) {
doc_mut!(cxt.editor).set_selection(view_id, Selection::point(pos));
} else {
let (view, doc) = current!(cxt.editor); let (view, doc) = current!(cxt.editor);
if let Some(pos) =
view.pos_at_visual_coords(doc, coords.row as u16, coords.col as u16, true) if let Some(pos) = view.pos_at_visual_coords(doc, pos.row as u16, 0, true) {
{
doc.set_selection(view_id, Selection::point(pos)); doc.set_selection(view_id, Selection::point(pos));
if modifiers == KeyModifiers::ALT { match modifiers {
commands::MappableCommand::dap_edit_log.execute(cxt); KeyModifiers::ALT => {
} else { commands::MappableCommand::dap_edit_log.execute(cxt)
commands::MappableCommand::dap_edit_condition.execute(cxt);
} }
_ => commands::MappableCommand::dap_edit_condition.execute(cxt),
return EventResult::Consumed(None); };
} }
} }
cxt.editor.ensure_cursor_in_view(view_id);
return EventResult::Consumed(None);
}
EventResult::Ignored(None) EventResult::Ignored(None)
} }
@ -1257,6 +1325,7 @@ impl Component for EditorView {
match event { match event {
Event::Paste(contents) => { Event::Paste(contents) => {
self.handle_non_key_input(&mut cx);
cx.count = cx.editor.count; cx.count = cx.editor.count;
commands::paste_bracketed_value(&mut cx, contents.clone()); commands::paste_bracketed_value(&mut cx, contents.clone());
cx.editor.count = None; cx.editor.count = None;

@ -2,6 +2,7 @@ use crate::compositor::{Component, Context};
use helix_view::graphics::{Margin, Rect}; use helix_view::graphics::{Margin, Rect};
use helix_view::info::Info; use helix_view::info::Info;
use tui::buffer::Buffer as Surface; use tui::buffer::Buffer as Surface;
use tui::text::Text;
use tui::widgets::{Block, Borders, Paragraph, Widget}; use tui::widgets::{Block, Borders, Paragraph, Widget};
impl Component for Info { impl Component for Info {
@ -31,7 +32,7 @@ impl Component for Info {
let inner = block.inner(area).inner(&margin); let inner = block.inner(area).inner(&margin);
block.render(area, surface); block.render(area, surface);
Paragraph::new(self.text.as_str()) Paragraph::new(&Text::from(self.text.as_str()))
.style(text_style) .style(text_style)
.render(inner, surface); .render(inner, surface);
} }

@ -1,57 +1,104 @@
use std::sync::Arc; use std::sync::Arc;
use arc_swap::ArcSwap;
use helix_core::syntax; use helix_core::syntax;
use helix_view::graphics::{Margin, Rect, Style}; use helix_view::graphics::{Margin, Rect, Style};
use helix_view::input::Event;
use tui::buffer::Buffer; use tui::buffer::Buffer;
use tui::layout::Alignment;
use tui::text::Text;
use tui::widgets::{BorderType, Paragraph, Widget, Wrap}; use tui::widgets::{BorderType, Paragraph, Widget, Wrap};
use crate::compositor::{Component, Compositor, Context}; use crate::compositor::{Component, Compositor, Context, EventResult};
use crate::alt;
use crate::ui::Markdown; use crate::ui::Markdown;
use super::Popup; use super::Popup;
pub struct SignatureHelp { pub struct Signature {
signature: String, pub signature: String,
signature_doc: Option<String>, pub signature_doc: Option<String>,
/// Part of signature text /// Part of signature text
active_param_range: Option<(usize, usize)>, pub active_param_range: Option<(usize, usize)>,
}
pub struct SignatureHelp {
language: String, language: String,
config_loader: Arc<syntax::Loader>, config_loader: Arc<ArcSwap<syntax::Loader>>,
active_signature: usize,
lsp_signature: Option<usize>,
signatures: Vec<Signature>,
} }
impl SignatureHelp { impl SignatureHelp {
pub const ID: &'static str = "signature-help"; pub const ID: &'static str = "signature-help";
pub fn new(signature: String, language: String, config_loader: Arc<syntax::Loader>) -> Self { pub fn new(
language: String,
config_loader: Arc<ArcSwap<syntax::Loader>>,
active_signature: usize,
lsp_signature: Option<usize>,
signatures: Vec<Signature>,
) -> Self {
Self { Self {
signature,
signature_doc: None,
active_param_range: None,
language, language,
config_loader, config_loader,
active_signature,
lsp_signature,
signatures,
} }
} }
pub fn set_signature_doc(&mut self, signature_doc: Option<String>) { pub fn active_signature(&self) -> usize {
self.signature_doc = signature_doc; self.active_signature
} }
pub fn set_active_param_range(&mut self, offset: Option<(usize, usize)>) { pub fn lsp_signature(&self) -> Option<usize> {
self.active_param_range = offset; self.lsp_signature
} }
pub fn visible_popup(compositor: &mut Compositor) -> Option<&mut Popup<Self>> { pub fn visible_popup(compositor: &mut Compositor) -> Option<&mut Popup<Self>> {
compositor.find_id::<Popup<Self>>(Self::ID) compositor.find_id::<Popup<Self>>(Self::ID)
} }
fn signature_index(&self) -> String {
format!("({}/{})", self.active_signature + 1, self.signatures.len())
}
} }
impl Component for SignatureHelp { impl Component for SignatureHelp {
fn handle_event(&mut self, event: &Event, _cx: &mut Context) -> EventResult {
let Event::Key(event) = event else {
return EventResult::Ignored(None);
};
if self.signatures.len() <= 1 {
return EventResult::Ignored(None);
}
match event {
alt!('p') => {
self.active_signature = self
.active_signature
.checked_sub(1)
.unwrap_or(self.signatures.len() - 1);
EventResult::Consumed(None)
}
alt!('n') => {
self.active_signature = (self.active_signature + 1) % self.signatures.len();
EventResult::Consumed(None)
}
_ => EventResult::Ignored(None),
}
}
fn render(&mut self, area: Rect, surface: &mut Buffer, cx: &mut Context) { fn render(&mut self, area: Rect, surface: &mut Buffer, cx: &mut Context) {
let margin = Margin::horizontal(1); let margin = Margin::horizontal(1);
let active_param_span = self.active_param_range.map(|(start, end)| { let signature = &self.signatures[self.active_signature];
let active_param_span = signature.active_param_range.map(|(start, end)| {
vec![( vec![(
cx.editor cx.editor
.theme .theme
@ -61,21 +108,29 @@ impl Component for SignatureHelp {
)] )]
}); });
let sig = &self.signatures[self.active_signature];
let sig_text = crate::ui::markdown::highlighted_code_block( let sig_text = crate::ui::markdown::highlighted_code_block(
&self.signature, sig.signature.as_str(),
&self.language, &self.language,
Some(&cx.editor.theme), Some(&cx.editor.theme),
Arc::clone(&self.config_loader), Arc::clone(&self.config_loader),
active_param_span, active_param_span,
); );
if self.signatures.len() > 1 {
let signature_index = self.signature_index();
let text = Text::from(signature_index);
let paragraph = Paragraph::new(&text).alignment(Alignment::Right);
paragraph.render(area.clip_top(1).with_height(1).clip_right(1), surface);
}
let (_, sig_text_height) = crate::ui::text::required_size(&sig_text, area.width); let (_, sig_text_height) = crate::ui::text::required_size(&sig_text, area.width);
let sig_text_area = area.clip_top(1).with_height(sig_text_height); let sig_text_area = area.clip_top(1).with_height(sig_text_height);
let sig_text_area = sig_text_area.inner(&margin).intersection(surface.area); let sig_text_area = sig_text_area.inner(&margin).intersection(surface.area);
let sig_text_para = Paragraph::new(sig_text).wrap(Wrap { trim: false }); let sig_text_para = Paragraph::new(&sig_text).wrap(Wrap { trim: false });
sig_text_para.render(sig_text_area, surface); sig_text_para.render(sig_text_area, surface);
if self.signature_doc.is_none() { if sig.signature_doc.is_none() {
return; return;
} }
@ -87,7 +142,7 @@ impl Component for SignatureHelp {
} }
} }
let sig_doc = match &self.signature_doc { let sig_doc = match &sig.signature_doc {
None => return, None => return,
Some(doc) => Markdown::new(doc.clone(), Arc::clone(&self.config_loader)), Some(doc) => Markdown::new(doc.clone(), Arc::clone(&self.config_loader)),
}; };
@ -95,7 +150,7 @@ impl Component for SignatureHelp {
let sig_doc_area = area let sig_doc_area = area
.clip_top(sig_text_area.height + 2) .clip_top(sig_text_area.height + 2)
.clip_bottom(u16::from(cx.editor.popup_border())); .clip_bottom(u16::from(cx.editor.popup_border()));
let sig_doc_para = Paragraph::new(sig_doc) let sig_doc_para = Paragraph::new(&sig_doc)
.wrap(Wrap { trim: false }) .wrap(Wrap { trim: false })
.scroll((cx.scroll.unwrap_or_default() as u16, 0)); .scroll((cx.scroll.unwrap_or_default() as u16, 0));
sig_doc_para.render(sig_doc_area.inner(&margin), surface); sig_doc_para.render(sig_doc_area.inner(&margin), surface);
@ -105,13 +160,12 @@ impl Component for SignatureHelp {
const PADDING: u16 = 2; const PADDING: u16 = 2;
const SEPARATOR_HEIGHT: u16 = 1; const SEPARATOR_HEIGHT: u16 = 1;
if PADDING >= viewport.1 || PADDING >= viewport.0 { let sig = &self.signatures[self.active_signature];
return None;
} let max_text_width = viewport.0.saturating_sub(PADDING).clamp(10, 120);
let max_text_width = (viewport.0 - PADDING).min(120);
let signature_text = crate::ui::markdown::highlighted_code_block( let signature_text = crate::ui::markdown::highlighted_code_block(
&self.signature, sig.signature.as_str(),
&self.language, &self.language,
None, None,
Arc::clone(&self.config_loader), Arc::clone(&self.config_loader),
@ -120,7 +174,7 @@ impl Component for SignatureHelp {
let (sig_width, sig_height) = let (sig_width, sig_height) =
crate::ui::text::required_size(&signature_text, max_text_width); crate::ui::text::required_size(&signature_text, max_text_width);
let (width, height) = match self.signature_doc { let (width, height) = match sig.signature_doc {
Some(ref doc) => { Some(ref doc) => {
let doc_md = Markdown::new(doc.clone(), Arc::clone(&self.config_loader)); let doc_md = Markdown::new(doc.clone(), Arc::clone(&self.config_loader));
let doc_text = doc_md.parse(None); let doc_text = doc_md.parse(None);
@ -134,6 +188,12 @@ impl Component for SignatureHelp {
None => (sig_width, sig_height), None => (sig_width, sig_height),
}; };
Some((width + PADDING, height + PADDING)) let sig_index_width = if self.signatures.len() > 1 {
self.signature_index().len() + 1
} else {
0
};
Some((width + PADDING + sig_index_width as u16, height + PADDING))
} }
} }

@ -1,4 +1,5 @@
use crate::compositor::{Component, Context}; use crate::compositor::{Component, Context};
use arc_swap::ArcSwap;
use tui::{ use tui::{
buffer::Buffer as Surface, buffer::Buffer as Surface,
text::{Span, Spans, Text}, text::{Span, Spans, Text},
@ -6,7 +7,7 @@ use tui::{
use std::sync::Arc; use std::sync::Arc;
use pulldown_cmark::{CodeBlockKind, Event, HeadingLevel, Options, Parser, Tag}; use pulldown_cmark::{CodeBlockKind, Event, HeadingLevel, Options, Parser, Tag, TagEnd};
use helix_core::{ use helix_core::{
syntax::{self, HighlightEvent, InjectionLanguageMarker, Syntax}, syntax::{self, HighlightEvent, InjectionLanguageMarker, Syntax},
@ -31,7 +32,7 @@ pub fn highlighted_code_block<'a>(
text: &str, text: &str,
language: &str, language: &str,
theme: Option<&Theme>, theme: Option<&Theme>,
config_loader: Arc<syntax::Loader>, config_loader: Arc<ArcSwap<syntax::Loader>>,
additional_highlight_spans: Option<Vec<(usize, std::ops::Range<usize>)>>, additional_highlight_spans: Option<Vec<(usize, std::ops::Range<usize>)>>,
) -> Text<'a> { ) -> Text<'a> {
let mut spans = Vec::new(); let mut spans = Vec::new();
@ -48,6 +49,7 @@ pub fn highlighted_code_block<'a>(
let ropeslice = RopeSlice::from(text); let ropeslice = RopeSlice::from(text);
let syntax = config_loader let syntax = config_loader
.load()
.language_configuration_for_injection_string(&InjectionLanguageMarker::Name( .language_configuration_for_injection_string(&InjectionLanguageMarker::Name(
language.into(), language.into(),
)) ))
@ -121,7 +123,7 @@ pub fn highlighted_code_block<'a>(
pub struct Markdown { pub struct Markdown {
contents: String, contents: String,
config_loader: Arc<syntax::Loader>, config_loader: Arc<ArcSwap<syntax::Loader>>,
} }
// TODO: pre-render and self reference via Pin // TODO: pre-render and self reference via Pin
@ -140,7 +142,7 @@ impl Markdown {
]; ];
const INDENT: &'static str = " "; const INDENT: &'static str = " ";
pub fn new(contents: String, config_loader: Arc<syntax::Loader>) -> Self { pub fn new(contents: String, config_loader: Arc<ArcSwap<syntax::Loader>>) -> Self {
Self { Self {
contents, contents,
config_loader, config_loader,
@ -209,7 +211,7 @@ impl Markdown {
list_stack.push(list); list_stack.push(list);
} }
Event::End(Tag::List(_)) => { Event::End(TagEnd::List(_)) => {
list_stack.pop(); list_stack.pop();
// whenever top-level list closes, empty line // whenever top-level list closes, empty line
@ -249,7 +251,10 @@ impl Markdown {
Event::End(tag) => { Event::End(tag) => {
tags.pop(); tags.pop();
match tag { match tag {
Tag::Heading(_, _, _) | Tag::Paragraph | Tag::CodeBlock(_) | Tag::Item => { TagEnd::Heading(_)
| TagEnd::Paragraph
| TagEnd::CodeBlock
| TagEnd::Item => {
push_line(&mut spans, &mut lines); push_line(&mut spans, &mut lines);
} }
_ => (), _ => (),
@ -257,7 +262,7 @@ impl Markdown {
// whenever heading, code block or paragraph closes, empty line // whenever heading, code block or paragraph closes, empty line
match tag { match tag {
Tag::Heading(_, _, _) | Tag::Paragraph | Tag::CodeBlock(_) => { TagEnd::Heading(_) | TagEnd::Paragraph | TagEnd::CodeBlock => {
lines.push(Spans::default()); lines.push(Spans::default());
} }
_ => (), _ => (),
@ -279,7 +284,7 @@ impl Markdown {
lines.extend(tui_text.lines.into_iter()); lines.extend(tui_text.lines.into_iter());
} else { } else {
let style = match tags.last() { let style = match tags.last() {
Some(Tag::Heading(level, ..)) => match level { Some(Tag::Heading { level, .. }) => match level {
HeadingLevel::H1 => heading_styles[0], HeadingLevel::H1 => heading_styles[0],
HeadingLevel::H2 => heading_styles[1], HeadingLevel::H2 => heading_styles[1],
HeadingLevel::H3 => heading_styles[2], HeadingLevel::H3 => heading_styles[2],
@ -341,7 +346,7 @@ impl Component for Markdown {
let text = self.parse(Some(&cx.editor.theme)); let text = self.parse(Some(&cx.editor.theme));
let par = Paragraph::new(text) let par = Paragraph::new(&text)
.wrap(Wrap { trim: false }) .wrap(Wrap { trim: false })
.scroll((cx.scroll.unwrap_or_default() as u16, 0)); .scroll((cx.scroll.unwrap_or_default() as u16, 0));

@ -7,18 +7,11 @@ use crate::{
use helix_core::fuzzy::MATCHER; use helix_core::fuzzy::MATCHER;
use nucleo::pattern::{Atom, AtomKind, CaseMatching}; use nucleo::pattern::{Atom, AtomKind, CaseMatching};
use nucleo::{Config, Utf32Str}; use nucleo::{Config, Utf32Str};
use tui::{ use tui::{buffer::Buffer as Surface, widgets::Table};
buffer::Buffer as Surface,
widgets::{Block, Borders, Table, Widget},
};
pub use tui::widgets::{Cell, Row}; pub use tui::widgets::{Cell, Row};
use helix_view::{ use helix_view::{editor::SmartTabConfig, graphics::Rect, Editor};
editor::SmartTabConfig,
graphics::{Margin, Rect},
Editor,
};
use tui::layout::Constraint; use tui::layout::Constraint;
pub trait Item: Sync + Send + 'static { pub trait Item: Sync + Send + 'static {
@ -241,9 +234,9 @@ impl<T: Item> Menu<T> {
} }
impl<T: Item + PartialEq> Menu<T> { impl<T: Item + PartialEq> Menu<T> {
pub fn replace_option(&mut self, old_option: T, new_option: T) { pub fn replace_option(&mut self, old_option: &T, new_option: T) {
for option in &mut self.options { for option in &mut self.options {
if old_option == *option { if old_option == option {
*option = new_option; *option = new_option;
break; break;
} }
@ -341,16 +334,8 @@ impl<T: Item + 'static> Component for Menu<T> {
.try_get("ui.menu") .try_get("ui.menu")
.unwrap_or_else(|| theme.get("ui.text")); .unwrap_or_else(|| theme.get("ui.text"));
let selected = theme.get("ui.menu.selected"); let selected = theme.get("ui.menu.selected");
surface.clear_with(area, style);
let render_borders = cx.editor.menu_border();
let area = if render_borders { surface.clear_with(area, style);
Widget::render(Block::default().borders(Borders::ALL), area, surface);
area.inner(&Margin::vertical(1))
} else {
area
};
let scroll = self.scroll; let scroll = self.scroll;
@ -427,6 +412,7 @@ impl<T: Item + 'static> Component for Menu<T> {
cell.set_fg(scroll_style.fg.unwrap_or(helix_view::theme::Color::Reset)); cell.set_fg(scroll_style.fg.unwrap_or(helix_view::theme::Color::Reset));
} else if !render_borders { } else if !render_borders {
// Draw scroll track // Draw scroll track
cell.set_symbol(half_block);
cell.set_fg(scroll_style.bg.unwrap_or(helix_view::theme::Color::Reset)); cell.set_fg(scroll_style.bg.unwrap_or(helix_view::theme::Color::Reset));
} }
} }

@ -14,11 +14,12 @@ mod spinner;
mod statusline; mod statusline;
mod text; mod text;
use crate::compositor::{Component, Compositor}; use crate::compositor::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, CompletionItem}; pub use completion::{Completion, CompletionItem};
pub use editor::EditorView; pub use editor::EditorView;
use helix_stdx::rope;
pub use markdown::Markdown; pub use markdown::Markdown;
pub use menu::Menu; pub use menu::Menu;
pub use picker::{DynamicPicker, FileLocation, Picker}; pub use picker::{DynamicPicker, FileLocation, Picker};
@ -27,8 +28,6 @@ pub use prompt::{Prompt, PromptEvent};
pub use spinner::{ProgressSpinners, Spinner}; pub use spinner::{ProgressSpinners, Spinner};
pub use text::Text; pub use text::Text;
use helix_core::regex::Regex;
use helix_core::regex::RegexBuilder;
use helix_view::Editor; use helix_view::Editor;
use std::path::PathBuf; use std::path::PathBuf;
@ -64,7 +63,22 @@ pub fn regex_prompt(
prompt: std::borrow::Cow<'static, str>, prompt: std::borrow::Cow<'static, str>,
history_register: Option<char>, history_register: Option<char>,
completion_fn: impl FnMut(&Editor, &str) -> Vec<prompt::Completion> + 'static, completion_fn: impl FnMut(&Editor, &str) -> Vec<prompt::Completion> + 'static,
fun: impl Fn(&mut crate::compositor::Context, Regex, PromptEvent) + 'static, fun: impl Fn(&mut crate::compositor::Context, rope::Regex, PromptEvent) + 'static,
) {
raw_regex_prompt(
cx,
prompt,
history_register,
completion_fn,
move |cx, regex, _, event| fun(cx, regex, event),
);
}
pub fn raw_regex_prompt(
cx: &mut crate::commands::Context,
prompt: std::borrow::Cow<'static, str>,
history_register: Option<char>,
completion_fn: impl FnMut(&Editor, &str) -> Vec<prompt::Completion> + 'static,
fun: impl Fn(&mut crate::compositor::Context, rope::Regex, &str, PromptEvent) + 'static,
) { ) {
let (view, doc) = current!(cx.editor); let (view, doc) = current!(cx.editor);
let doc_id = view.doc; let doc_id = view.doc;
@ -95,10 +109,13 @@ pub fn regex_prompt(
false false
}; };
match RegexBuilder::new(input) match rope::RegexBuilder::new()
.syntax(
rope::Config::new()
.case_insensitive(case_insensitive) .case_insensitive(case_insensitive)
.multi_line(true) .multi_line(true),
.build() )
.build(input)
{ {
Ok(regex) => { Ok(regex) => {
let (view, doc) = current!(cx.editor); let (view, doc) = current!(cx.editor);
@ -111,7 +128,7 @@ pub fn regex_prompt(
view.jumps.push((doc_id, snapshot.clone())); view.jumps.push((doc_id, snapshot.clone()));
} }
fun(cx, regex, event); fun(cx, regex, input, event);
let (view, doc) = current!(cx.editor); let (view, doc) = current!(cx.editor);
view.ensure_cursor_in_view(doc, config.scrolloff); view.ensure_cursor_in_view(doc, config.scrolloff);
@ -127,14 +144,12 @@ pub fn regex_prompt(
move |_editor: &mut Editor, compositor: &mut Compositor| { move |_editor: &mut Editor, compositor: &mut Compositor| {
let contents = Text::new(format!("{}", err)); let contents = Text::new(format!("{}", err));
let size = compositor.size(); let size = compositor.size();
let mut popup = Popup::new("invalid-regex", contents) let popup = Popup::new("invalid-regex", contents)
.position(Some(helix_core::Position::new( .position(Some(helix_core::Position::new(
size.height as usize - 2, // 2 = statusline + commandline size.height as usize - 2, // 2 = statusline + commandline
0, 0,
))) )))
.auto_close(true); .auto_close(true);
popup.required_size((size.width, size.height));
compositor.replace_or_push("invalid-regex", popup); compositor.replace_or_push("invalid-regex", popup);
}, },
)); ));
@ -338,8 +353,8 @@ pub mod completers {
pub fn language(editor: &Editor, input: &str) -> Vec<Completion> { pub fn language(editor: &Editor, input: &str) -> Vec<Completion> {
let text: String = "text".into(); let text: String = "text".into();
let language_ids = editor let loader = editor.syn_loader.load();
.syn_loader let language_ids = loader
.language_configs() .language_configs()
.map(|config| &config.language_id) .map(|config| &config.language_id)
.chain(std::iter::once(&text)); .chain(std::iter::once(&text));
@ -430,9 +445,9 @@ pub mod completers {
path path
} else { } else {
match path.parent() { match path.parent() {
Some(path) if !path.as_os_str().is_empty() => path.to_path_buf(), Some(path) if !path.as_os_str().is_empty() => Cow::Borrowed(path),
// Path::new("h")'s parent is Some("")... // Path::new("h")'s parent is Some("")...
_ => helix_stdx::env::current_working_dir(), _ => Cow::Owned(helix_stdx::env::current_working_dir()),
} }
}; };
@ -494,4 +509,18 @@ pub mod completers {
files files
} }
} }
pub fn register(editor: &Editor, input: &str) -> Vec<Completion> {
let iter = editor
.registers
.iter_preview()
// Exclude special registers that shouldn't be written to
.filter(|(ch, _)| !matches!(ch, '%' | '#' | '.'))
.map(|(ch, _)| ch.to_string());
fuzzy_match(input, iter, false)
.into_iter()
.map(|(name, _)| ((0..), name.into()))
.collect()
}
} }

@ -461,14 +461,17 @@ impl<T: Item + 'static> Picker<T> {
// Then attempt to highlight it if it has no language set // Then attempt to highlight it if it has no language set
if doc.language_config().is_none() { if doc.language_config().is_none() {
if let Some(language_config) = doc.detect_language_config(&cx.editor.syn_loader) { if let Some(language_config) = doc.detect_language_config(&cx.editor.syn_loader.load())
{
doc.language = Some(language_config.clone()); doc.language = Some(language_config.clone());
let text = doc.text().clone(); let text = doc.text().clone();
let loader = cx.editor.syn_loader.clone(); let loader = cx.editor.syn_loader.clone();
let job = tokio::task::spawn_blocking(move || { let job = tokio::task::spawn_blocking(move || {
let syntax = language_config.highlight_config(&loader.scopes()).and_then( let syntax = language_config
|highlight_config| Syntax::new(text.slice(..), highlight_config, loader), .highlight_config(&loader.load().scopes())
); .and_then(|highlight_config| {
Syntax::new(text.slice(..), highlight_config, loader)
});
let callback = move |editor: &mut Editor, compositor: &mut Compositor| { let callback = move |editor: &mut Editor, compositor: &mut Compositor| {
let Some(syntax) = syntax else { let Some(syntax) = syntax else {
log::info!("highlighting picker item failed"); log::info!("highlighting picker item failed");

@ -11,20 +11,30 @@ use tui::{
use helix_core::Position; use helix_core::Position;
use helix_view::{ use helix_view::{
graphics::{Margin, Rect}, graphics::{Margin, Rect},
input::{MouseEvent, MouseEventKind},
Editor, Editor,
}; };
const MIN_HEIGHT: u16 = 6;
const MAX_HEIGHT: u16 = 26;
const MAX_WIDTH: u16 = 120;
struct RenderInfo {
area: Rect,
child_height: u16,
render_borders: bool,
is_menu: bool,
}
// TODO: share logic with Menu, it's essentially Popup(render_fn), but render fn needs to return // TODO: share logic with Menu, it's essentially Popup(render_fn), but render fn needs to return
// a width/height hint. maybe Popup(Box<Component>) // a width/height hint. maybe Popup(Box<Component>)
pub struct Popup<T: Component> { pub struct Popup<T: Component> {
contents: T, contents: T,
position: Option<Position>, position: Option<Position>,
margin: Margin, area: Rect,
size: (u16, u16),
child_size: (u16, u16),
position_bias: Open, position_bias: Open,
scroll: usize, scroll_half_pages: usize,
auto_close: bool, auto_close: bool,
ignore_escape_key: bool, ignore_escape_key: bool,
id: &'static str, id: &'static str,
@ -36,11 +46,9 @@ impl<T: Component> Popup<T> {
Self { Self {
contents, contents,
position: None, position: None,
margin: Margin::none(),
size: (0, 0),
position_bias: Open::Below, position_bias: Open::Below,
child_size: (0, 0), area: Rect::new(0, 0, 0, 0),
scroll: 0, scroll_half_pages: 0,
auto_close: false, auto_close: false,
ignore_escape_key: false, ignore_escape_key: false,
id, id,
@ -70,11 +78,6 @@ impl<T: Component> Popup<T> {
self self
} }
pub fn margin(mut self, margin: Margin) -> Self {
self.margin = margin;
self
}
pub fn auto_close(mut self, auto_close: bool) -> Self { pub fn auto_close(mut self, auto_close: bool) -> Self {
self.auto_close = auto_close; self.auto_close = auto_close;
self self
@ -92,28 +95,65 @@ impl<T: Component> Popup<T> {
self self
} }
/// Calculate the position where the popup should be rendered and return the coordinates of the pub fn scroll_half_page_down(&mut self) {
/// top left corner. self.scroll_half_pages += 1;
pub fn get_rel_position(&mut self, viewport: Rect, editor: &Editor) -> (u16, u16) { }
let position = self
pub fn scroll_half_page_up(&mut self) {
self.scroll_half_pages = self.scroll_half_pages.saturating_sub(1);
}
/// Toggles the Popup's scrollbar.
/// Consider disabling the scrollbar in case the child
/// already has its own.
pub fn with_scrollbar(mut self, enable_scrollbar: bool) -> Self {
self.has_scrollbar = enable_scrollbar;
self
}
pub fn contents(&self) -> &T {
&self.contents
}
pub fn contents_mut(&mut self) -> &mut T {
&mut self.contents
}
pub fn area(&mut self, viewport: Rect, editor: &Editor) -> Rect {
self.render_info(viewport, editor).area
}
fn render_info(&mut self, viewport: Rect, editor: &Editor) -> RenderInfo {
let mut position = editor.cursor().0.unwrap_or_default();
if let Some(old_position) = self
.position .position
.get_or_insert_with(|| editor.cursor().0.unwrap_or_default()); .filter(|old_position| old_position.row == position.row)
{
position = old_position;
} else {
self.position = Some(position);
}
let (width, height) = self.size; let is_menu = self
.contents
.type_name()
.starts_with("helix_term::ui::menu::Menu");
// if there's a orientation preference, use that let mut render_borders = if is_menu {
// if we're on the top part of the screen, do below editor.menu_border()
// if we're on the bottom part, do above } else {
editor.popup_border()
};
// -- make sure frame doesn't stick out of bounds // -- make sure frame doesn't stick out of bounds
let mut rel_x = position.col as u16; let mut rel_x = position.col as u16;
let mut rel_y = position.row as u16; let mut rel_y = position.row as u16;
if viewport.width <= rel_x + width {
rel_x = rel_x.saturating_sub((rel_x + width).saturating_sub(viewport.width));
}
let can_put_below = viewport.height > rel_y + height; // if there's a orientation preference, use that
let can_put_above = rel_y.checked_sub(height).is_some(); // if we're on the top part of the screen, do below
// if we're on the bottom part, do above
let can_put_below = viewport.height > rel_y + MIN_HEIGHT;
let can_put_above = rel_y.checked_sub(MIN_HEIGHT).is_some();
let final_pos = match self.position_bias { let final_pos = match self.position_bias {
Open::Below => match can_put_below { Open::Below => match can_put_below {
true => Open::Below, true => Open::Below,
@ -125,51 +165,87 @@ impl<T: Component> Popup<T> {
}, },
}; };
rel_y = match final_pos { // compute maximum space available for child
Open::Above => rel_y.saturating_sub(height), let mut max_height = match final_pos {
Open::Below => rel_y + 1, Open::Above => rel_y,
Open::Below => viewport.height.saturating_sub(1 + rel_y),
}; };
max_height = max_height.min(MAX_HEIGHT);
(rel_x, rel_y) let mut max_width = viewport.width.saturating_sub(2).min(MAX_WIDTH);
render_borders = render_borders && max_height > 3 && max_width > 3;
if render_borders {
max_width -= 2;
max_height -= 2;
} }
pub fn get_size(&self) -> (u16, u16) { // compute required child size and reclamp
(self.size.0, self.size.1) let (mut width, child_height) = self
} .contents
.required_size((max_width, max_height))
.expect("Component needs required_size implemented in order to be embedded in a popup");
pub fn scroll(&mut self, offset: usize, direction: bool) { width = width.min(MAX_WIDTH);
if direction { let height = if render_borders {
let max_offset = self.child_size.1.saturating_sub(self.size.1); (child_height + 2).min(MAX_HEIGHT)
self.scroll = (self.scroll + offset).min(max_offset as usize);
} else { } else {
self.scroll = self.scroll.saturating_sub(offset); child_height.min(MAX_HEIGHT)
};
if render_borders {
width += 2;
} }
if viewport.width <= rel_x + width + 2 {
rel_x = viewport.width.saturating_sub(width + 2);
width = viewport.width.saturating_sub(rel_x + 2)
} }
/// Toggles the Popup's scrollbar. let area = match final_pos {
/// Consider disabling the scrollbar in case the child Open::Above => {
/// already has its own. rel_y = rel_y.saturating_sub(height);
pub fn with_scrollbar(mut self, enable_scrollbar: bool) -> Self { Rect::new(rel_x, rel_y, width, position.row as u16 - rel_y)
self.has_scrollbar = enable_scrollbar;
self
} }
Open::Below => {
pub fn contents(&self) -> &T { rel_y += 1;
&self.contents let y_max = viewport.bottom().min(height + rel_y);
Rect::new(rel_x, rel_y, width, y_max - rel_y)
} }
};
pub fn contents_mut(&mut self) -> &mut T { RenderInfo {
&mut self.contents area,
child_height,
render_borders,
is_menu,
}
}
fn handle_mouse_event(
&mut self,
&MouseEvent {
kind,
column: x,
row: y,
..
}: &MouseEvent,
) -> EventResult {
let mouse_is_within_popup = x >= self.area.left()
&& x < self.area.right()
&& y >= self.area.top()
&& y < self.area.bottom();
if !mouse_is_within_popup {
return EventResult::Ignored(None);
} }
pub fn area(&mut self, viewport: Rect, editor: &Editor) -> Rect { match kind {
// trigger required_size so we recalculate if the child changed MouseEventKind::ScrollDown if self.has_scrollbar => {
self.required_size((viewport.width, viewport.height)); self.scroll_half_page_down();
EventResult::Consumed(None)
let (rel_x, rel_y) = self.get_rel_position(viewport, editor); }
MouseEventKind::ScrollUp if self.has_scrollbar => {
// clip to viewport self.scroll_half_page_up();
viewport.intersection(Rect::new(rel_x, rel_y, self.size.0, self.size.1)) EventResult::Consumed(None)
}
_ => EventResult::Ignored(None),
}
} }
} }
@ -177,6 +253,7 @@ impl<T: Component> Component for Popup<T> {
fn handle_event(&mut self, event: &Event, cx: &mut Context) -> EventResult { fn handle_event(&mut self, event: &Event, cx: &mut Context) -> EventResult {
let key = match event { let key = match event {
Event::Key(event) => *event, Event::Key(event) => *event,
Event::Mouse(event) => return self.handle_mouse_event(event),
Event::Resize(_, _) => { Event::Resize(_, _) => {
// TODO: calculate inner area, call component's handle_event with that area // TODO: calculate inner area, call component's handle_event with that area
return EventResult::Ignored(None); return EventResult::Ignored(None);
@ -200,11 +277,11 @@ impl<T: Component> Component for Popup<T> {
EventResult::Consumed(Some(close_fn)) EventResult::Consumed(Some(close_fn))
} }
ctrl!('d') => { ctrl!('d') => {
self.scroll(self.size.1 as usize / 2, true); self.scroll_half_page_down();
EventResult::Consumed(None) EventResult::Consumed(None)
} }
ctrl!('u') => { ctrl!('u') => {
self.scroll(self.size.1 as usize / 2, false); self.scroll_half_page_up();
EventResult::Consumed(None) EventResult::Consumed(None)
} }
_ => { _ => {
@ -223,63 +300,48 @@ impl<T: Component> Component for Popup<T> {
// tab/enter/ctrl-k or whatever will confirm the selection/ ctrl-n/ctrl-p for scroll. // tab/enter/ctrl-k or whatever will confirm the selection/ ctrl-n/ctrl-p for scroll.
} }
fn required_size(&mut self, viewport: (u16, u16)) -> Option<(u16, u16)> {
let max_width = 120.min(viewport.0);
let max_height = 26.min(viewport.1.saturating_sub(2)); // add some spacing in the viewport
let inner = Rect::new(0, 0, max_width, max_height).inner(&self.margin);
let (width, height) = self
.contents
.required_size((inner.width, inner.height))
.expect("Component needs required_size implemented in order to be embedded in a popup");
self.child_size = (width, height);
self.size = (
(width + self.margin.width()).min(max_width),
(height + self.margin.height()).min(max_height),
);
// re-clamp scroll offset
let max_offset = self.child_size.1.saturating_sub(self.size.1);
self.scroll = self.scroll.min(max_offset as usize);
Some(self.size)
}
fn render(&mut self, viewport: Rect, surface: &mut Surface, cx: &mut Context) { fn render(&mut self, viewport: Rect, surface: &mut Surface, cx: &mut Context) {
let area = self.area(viewport, cx.editor); let RenderInfo {
cx.scroll = Some(self.scroll); area,
child_height,
render_borders,
is_menu,
} = self.render_info(viewport, cx.editor);
self.area = area;
// clear area // clear area
let background = cx.editor.theme.get("ui.popup"); let background = if is_menu {
surface.clear_with(area, background); // TODO: consistently style menu
cx.editor
let render_borders = cx.editor.popup_border(); .theme
.try_get("ui.menu")
let inner = if self .unwrap_or_else(|| cx.editor.theme.get("ui.text"))
.contents
.type_name()
.starts_with("helix_term::ui::menu::Menu")
{
area
} else { } else {
area.inner(&self.margin) cx.editor.theme.get("ui.popup")
}; };
surface.clear_with(area, background);
let border = usize::from(render_borders); let mut inner = area;
if render_borders { if render_borders {
inner = area.inner(&Margin::all(1));
Widget::render(Block::default().borders(Borders::ALL), area, surface); Widget::render(Block::default().borders(Borders::ALL), area, surface);
} }
let border = usize::from(render_borders);
let max_offset = child_height.saturating_sub(inner.height) as usize;
let half_page_size = (inner.height / 2) as usize;
let scroll = max_offset.min(self.scroll_half_pages * half_page_size);
if half_page_size > 0 {
self.scroll_half_pages = scroll / half_page_size;
}
cx.scroll = Some(scroll);
self.contents.render(inner, surface, cx); self.contents.render(inner, surface, cx);
// render scrollbar if contents do not fit // render scrollbar if contents do not fit
if self.has_scrollbar { if self.has_scrollbar {
let win_height = (inner.height as usize).saturating_sub(2 * border); let win_height = inner.height as usize;
let len = (self.child_size.1 as usize).saturating_sub(2 * border); let len = child_height as usize;
let fits = len <= win_height; let fits = len <= win_height;
let scroll = self.scroll;
let scroll_style = cx.editor.theme.get("ui.menu.scroll"); let scroll_style = cx.editor.theme.get("ui.menu.scroll");
const fn div_ceil(a: usize, b: usize) -> usize { const fn div_ceil(a: usize, b: usize) -> usize {
@ -293,7 +355,8 @@ impl<T: Component> Component for Popup<T> {
let mut cell; let mut cell;
for i in 0..win_height { for i in 0..win_height {
cell = &mut surface[(inner.right() - 1, inner.top() + (border + i) as u16)]; cell =
&mut surface[(inner.right() - 1 + border as u16, inner.top() + i as u16)];
let half_block = if render_borders { "▌" } else { "▐" }; let half_block = if render_borders { "▌" } else { "▐" };
@ -303,6 +366,7 @@ impl<T: Component> Component for Popup<T> {
cell.set_fg(scroll_style.fg.unwrap_or(helix_view::theme::Color::Reset)); cell.set_fg(scroll_style.fg.unwrap_or(helix_view::theme::Color::Reset));
} else if !render_borders { } else if !render_borders {
// Draw scroll track // Draw scroll track
cell.set_symbol(half_block);
cell.set_fg(scroll_style.bg.unwrap_or(helix_view::theme::Color::Reset)); cell.set_fg(scroll_style.bg.unwrap_or(helix_view::theme::Color::Reset));
} }
} }

@ -1,5 +1,6 @@
use crate::compositor::{Component, Compositor, Context, Event, EventResult}; use crate::compositor::{Component, Compositor, Context, Event, EventResult};
use crate::{alt, ctrl, key, shift, ui}; use crate::{alt, ctrl, key, shift, ui};
use arc_swap::ArcSwap;
use helix_core::syntax; use helix_core::syntax;
use helix_view::input::KeyEvent; use helix_view::input::KeyEvent;
use helix_view::keyboard::KeyCode; use helix_view::keyboard::KeyCode;
@ -34,7 +35,7 @@ pub struct Prompt {
callback_fn: CallbackFn, callback_fn: CallbackFn,
pub doc_fn: DocFn, pub doc_fn: DocFn,
next_char_handler: Option<PromptCharHandler>, next_char_handler: Option<PromptCharHandler>,
language: Option<(&'static str, Arc<syntax::Loader>)>, language: Option<(&'static str, Arc<ArcSwap<syntax::Loader>>)>,
} }
#[derive(Clone, Copy, PartialEq, Eq)] #[derive(Clone, Copy, PartialEq, Eq)]
@ -98,7 +99,11 @@ impl Prompt {
self self
} }
pub fn with_language(mut self, language: &'static str, loader: Arc<syntax::Loader>) -> Self { pub fn with_language(
mut self,
language: &'static str,
loader: Arc<ArcSwap<syntax::Loader>>,
) -> Self {
self.language = Some((language, loader)); self.language = Some((language, loader));
self self
} }
@ -393,7 +398,7 @@ impl Prompt {
height, height,
); );
if !self.completion.is_empty() { if completion_area.height > 0 && !self.completion.is_empty() {
let area = completion_area; let area = completion_area;
let background = theme.get("ui.menu"); let background = theme.get("ui.menu");

@ -1,16 +1,18 @@
use std::{collections::HashMap, time::Instant}; use std::{collections::HashMap, time::Instant};
use helix_lsp::LanguageServerId;
#[derive(Default, Debug)] #[derive(Default, Debug)]
pub struct ProgressSpinners { pub struct ProgressSpinners {
inner: HashMap<usize, Spinner>, inner: HashMap<LanguageServerId, Spinner>,
} }
impl ProgressSpinners { impl ProgressSpinners {
pub fn get(&self, id: usize) -> Option<&Spinner> { pub fn get(&self, id: LanguageServerId) -> Option<&Spinner> {
self.inner.get(&id) self.inner.get(&id)
} }
pub fn get_or_create(&mut self, id: usize) -> &mut Spinner { pub fn get_or_create(&mut self, id: LanguageServerId) -> &mut Spinner {
self.inner.entry(id).or_default() self.inner.entry(id).or_default()
} }
} }

@ -4,7 +4,6 @@ use helix_view::document::DEFAULT_LANGUAGE_NAME;
use helix_view::{ use helix_view::{
document::{Mode, SCRATCH_BUFFER_NAME}, document::{Mode, SCRATCH_BUFFER_NAME},
graphics::Rect, graphics::Rect,
theme::Style,
Document, Editor, View, Document, Editor, View,
}; };
@ -20,7 +19,6 @@ pub struct RenderContext<'a> {
pub view: &'a View, pub view: &'a View,
pub focused: bool, pub focused: bool,
pub spinners: &'a ProgressSpinners, pub spinners: &'a ProgressSpinners,
pub parts: RenderBuffer<'a>,
} }
impl<'a> RenderContext<'a> { impl<'a> RenderContext<'a> {
@ -37,18 +35,10 @@ impl<'a> RenderContext<'a> {
view, view,
focused, focused,
spinners, spinners,
parts: RenderBuffer::default(),
} }
} }
} }
#[derive(Default)]
pub struct RenderBuffer<'a> {
pub left: Spans<'a>,
pub center: Spans<'a>,
pub right: Spans<'a>,
}
pub fn render(context: &mut RenderContext, viewport: Rect, surface: &mut Surface) { pub fn render(context: &mut RenderContext, viewport: Rect, surface: &mut Surface) {
let base_style = if context.focused { let base_style = if context.focused {
context.editor.theme.get("ui.statusline") context.editor.theme.get("ui.statusline")
@ -58,90 +48,93 @@ pub fn render(context: &mut RenderContext, viewport: Rect, surface: &mut Surface
surface.set_style(viewport.with_height(1), base_style); surface.set_style(viewport.with_height(1), base_style);
let write_left = |context: &mut RenderContext, text, style| { let statusline = render_statusline(context, viewport.width as usize);
append(&mut context.parts.left, text, &base_style, style)
};
let write_center = |context: &mut RenderContext, text, style| {
append(&mut context.parts.center, text, &base_style, style)
};
let write_right = |context: &mut RenderContext, text, style| {
append(&mut context.parts.right, text, &base_style, style)
};
// Left side of the status line.
let config = context.editor.config();
let element_ids = &config.statusline.left;
element_ids
.iter()
.map(|element_id| get_render_function(*element_id))
.for_each(|render| render(context, write_left));
surface.set_spans( surface.set_spans(
viewport.x, viewport.x,
viewport.y, viewport.y,
&context.parts.left, &statusline,
context.parts.left.width() as u16, statusline.width() as u16,
); );
}
// Right side of the status line. pub fn render_statusline<'a>(context: &mut RenderContext, width: usize) -> Spans<'a> {
let config = context.editor.config();
let element_ids = &config.statusline.right; let element_ids = &config.statusline.left;
element_ids let mut left = element_ids
.iter() .iter()
.map(|element_id| get_render_function(*element_id)) .map(|element_id| get_render_function(*element_id))
.for_each(|render| render(context, write_right)); .flat_map(|render| render(context).0)
.collect::<Vec<Span>>();
surface.set_spans(
viewport.x
+ viewport
.width
.saturating_sub(context.parts.right.width() as u16),
viewport.y,
&context.parts.right,
context.parts.right.width() as u16,
);
// Center of the status line.
let element_ids = &config.statusline.center; let element_ids = &config.statusline.center;
element_ids let mut center = element_ids
.iter() .iter()
.map(|element_id| get_render_function(*element_id)) .map(|element_id| get_render_function(*element_id))
.for_each(|render| render(context, write_center)); .flat_map(|render| render(context).0)
.collect::<Vec<Span>>();
// Width of the empty space between the left and center area and between the center and right area. let element_ids = &config.statusline.right;
let spacing = 1u16; let mut right = element_ids
.iter()
let edge_width = context.parts.left.width().max(context.parts.right.width()) as u16; .map(|element_id| get_render_function(*element_id))
let center_max_width = viewport.width.saturating_sub(2 * edge_width + 2 * spacing); .flat_map(|render| render(context).0)
let center_width = center_max_width.min(context.parts.center.width() as u16); .collect::<Vec<Span>>();
let left_area_width: usize = left.iter().map(|s| s.width()).sum();
let center_area_width: usize = center.iter().map(|s| s.width()).sum();
let right_area_width: usize = right.iter().map(|s| s.width()).sum();
let min_spacing_between_areas = 1usize;
let sides_space_required = left_area_width + right_area_width + min_spacing_between_areas;
let total_space_required = sides_space_required + center_area_width + min_spacing_between_areas;
let mut statusline: Vec<Span> = vec![];
if center_area_width > 0 && total_space_required <= width {
// SAFETY: this subtraction cannot underflow because `left_area_width + center_area_width + right_area_width`
// is smaller than `total_space_required`, which is smaller than `width` in this branch.
let total_spacers = width - (left_area_width + center_area_width + right_area_width);
// This is how much padding space it would take on either side to align the center area to the middle.
let center_margin = (width - center_area_width) / 2;
let left_spacers = if left_area_width < center_margin && right_area_width < center_margin {
// Align the center area to the middle if there is enough space on both sides.
center_margin - left_area_width
} else {
// Otherwise split the available space evenly and use it as margin.
// The center element won't be aligned to the middle but it will be evenly
// spaced between the left and right areas.
total_spacers / 2
};
let right_spacers = total_spacers - left_spacers;
surface.set_spans( statusline.append(&mut left);
viewport.x + viewport.width / 2 - center_width / 2, statusline.push(" ".repeat(left_spacers).into());
viewport.y, statusline.append(&mut center);
&context.parts.center, statusline.push(" ".repeat(right_spacers).into());
center_width, statusline.append(&mut right);
); } else if right_area_width > 0 && sides_space_required <= width {
let side_areas_width = left_area_width + right_area_width;
statusline.append(&mut left);
statusline.push(" ".repeat(width - side_areas_width).into());
statusline.append(&mut right);
} else if left_area_width <= width {
statusline.append(&mut left);
} }
fn append(buffer: &mut Spans, text: String, base_style: &Style, style: Option<Style>) { statusline.into()
buffer.0.push(Span::styled(
text,
style.map_or(*base_style, |s| (*base_style).patch(s)),
));
} }
fn get_render_function<F>(element_id: StatusLineElementID) -> impl Fn(&mut RenderContext, F) fn get_render_function<'a>(
where element_id: StatusLineElementID,
F: Fn(&mut RenderContext, String, Option<Style>) + Copy, ) -> impl Fn(&RenderContext) -> Spans<'a> {
{
match element_id { match element_id {
helix_view::editor::StatusLineElement::Mode => render_mode, helix_view::editor::StatusLineElement::Mode => render_mode,
helix_view::editor::StatusLineElement::Spinner => render_lsp_spinner, helix_view::editor::StatusLineElement::Spinner => render_lsp_spinner,
helix_view::editor::StatusLineElement::FileBaseName => render_file_base_name, helix_view::editor::StatusLineElement::FileBaseName => render_file_base_name,
helix_view::editor::StatusLineElement::FileName => render_file_name, helix_view::editor::StatusLineElement::FileName => render_file_name,
helix_view::editor::StatusLineElement::FileAbsolutePath => render_file_absolute_path,
helix_view::editor::StatusLineElement::FileModificationIndicator => { helix_view::editor::StatusLineElement::FileModificationIndicator => {
render_file_modification_indicator render_file_modification_indicator
} }
@ -165,48 +158,40 @@ where
} }
} }
fn render_mode<F>(context: &mut RenderContext, write: F) fn render_mode<'a>(context: &RenderContext) -> Spans<'a> {
where
F: Fn(&mut RenderContext, String, Option<Style>) + Copy,
{
let visible = context.focused; let visible = context.focused;
let config = context.editor.config(); let config = context.editor.config();
let modenames = &config.statusline.mode; let modenames = &config.statusline.mode;
write( let modename = if visible {
context,
format!(
" {} ",
if visible {
match context.editor.mode() { match context.editor.mode() {
Mode::Insert => &modenames.insert, Mode::Insert => modenames.insert.clone(),
Mode::Select => &modenames.select, Mode::Select => modenames.select.clone(),
Mode::Normal => &modenames.normal, Mode::Normal => modenames.normal.clone(),
} }
} else { } else {
// If not focused, explicitly leave an empty space instead of returning None. // If not focused, explicitly leave an empty space.
" " " ".into()
} };
), let modename = format!(" {} ", modename);
if visible && config.color_modes { if visible && config.color_modes {
Span::styled(
modename,
match context.editor.mode() { match context.editor.mode() {
Mode::Insert => Some(context.editor.theme.get("ui.statusline.insert")), Mode::Insert => context.editor.theme.get("ui.statusline.insert"),
Mode::Select => Some(context.editor.theme.get("ui.statusline.select")), Mode::Select => context.editor.theme.get("ui.statusline.select"),
Mode::Normal => Some(context.editor.theme.get("ui.statusline.normal")), Mode::Normal => context.editor.theme.get("ui.statusline.normal"),
}
} else {
None
}, },
); )
.into()
} else {
Span::raw(modename).into()
}
} }
// TODO think about handling multiple language servers // TODO think about handling multiple language servers
fn render_lsp_spinner<F>(context: &mut RenderContext, write: F) fn render_lsp_spinner<'a>(context: &RenderContext) -> Spans<'a> {
where
F: Fn(&mut RenderContext, String, Option<Style>) + Copy,
{
let language_server = context.doc.language_servers().next(); let language_server = context.doc.language_servers().next();
write( Span::raw(
context,
language_server language_server
.and_then(|srv| { .and_then(|srv| {
context context
@ -217,14 +202,11 @@ where
// Even if there's no spinner; reserve its space to avoid elements frequently shifting. // Even if there's no spinner; reserve its space to avoid elements frequently shifting.
.unwrap_or(" ") .unwrap_or(" ")
.to_string(), .to_string(),
None, )
); .into()
} }
fn render_diagnostics<F>(context: &mut RenderContext, write: F) fn render_diagnostics<'a>(context: &RenderContext) -> Spans<'a> {
where
F: Fn(&mut RenderContext, String, Option<Style>) + Copy,
{
let (warnings, errors) = context let (warnings, errors) = context
.doc .doc
.diagnostics() .diagnostics()
@ -239,29 +221,28 @@ where
counts counts
}); });
let mut output = Spans::default();
if warnings > 0 { if warnings > 0 {
write( output.0.push(Span::styled(
context,
"●".to_string(), "●".to_string(),
Some(context.editor.theme.get("warning")), context.editor.theme.get("warning"),
); ));
write(context, format!(" {} ", warnings), None); output.0.push(Span::raw(format!(" {} ", warnings)));
} }
if errors > 0 { if errors > 0 {
write( output.0.push(Span::styled(
context,
"●".to_string(), "●".to_string(),
Some(context.editor.theme.get("error")), context.editor.theme.get("error"),
); ));
write(context, format!(" {} ", errors), None); output.0.push(Span::raw(format!(" {} ", errors)));
} }
output
} }
fn render_workspace_diagnostics<F>(context: &mut RenderContext, write: F) fn render_workspace_diagnostics<'a>(context: &RenderContext) -> Spans<'a> {
where
F: Fn(&mut RenderContext, String, Option<Style>) + Copy,
{
let (warnings, errors) = let (warnings, errors) =
context context
.editor .editor
@ -277,51 +258,49 @@ where
counts counts
}); });
let mut output = Spans::default();
if warnings > 0 || errors > 0 { if warnings > 0 || errors > 0 {
write(context, " W ".into(), None); output.0.push(Span::raw(" W "));
} }
if warnings > 0 { if warnings > 0 {
write( output.0.push(Span::styled(
context,
"●".to_string(), "●".to_string(),
Some(context.editor.theme.get("warning")), context.editor.theme.get("warning"),
); ));
write(context, format!(" {} ", warnings), None); output.0.push(Span::raw(format!(" {} ", warnings)));
} }
if errors > 0 { if errors > 0 {
write( output.0.push(Span::styled(
context,
"●".to_string(), "●".to_string(),
Some(context.editor.theme.get("error")), context.editor.theme.get("error"),
); ));
write(context, format!(" {} ", errors), None); output.0.push(Span::raw(format!(" {} ", errors)));
} }
output
} }
fn render_selections<F>(context: &mut RenderContext, write: F) fn render_selections<'a>(context: &RenderContext) -> Spans<'a> {
where
F: Fn(&mut RenderContext, String, Option<Style>) + Copy,
{
let count = context.doc.selection(context.view.id).len(); let count = context.doc.selection(context.view.id).len();
write( Span::raw(format!(
context, " {} sel{} ",
format!(" {} sel{} ", count, if count == 1 { "" } else { "s" }), count,
None, if count == 1 { "" } else { "s" }
); ))
.into()
} }
fn render_primary_selection_length<F>(context: &mut RenderContext, write: F) fn render_primary_selection_length<'a>(context: &RenderContext) -> Spans<'a> {
where
F: Fn(&mut RenderContext, String, Option<Style>) + Copy,
{
let tot_sel = context.doc.selection(context.view.id).primary().len(); let tot_sel = context.doc.selection(context.view.id).primary().len();
write( Span::raw(format!(
context, " {} char{} ",
format!(" {} char{} ", tot_sel, if tot_sel == 1 { "" } else { "s" }), tot_sel,
None, if tot_sel == 1 { "" } else { "s" }
); ))
.into()
} }
fn get_position(context: &RenderContext) -> Position { fn get_position(context: &RenderContext) -> Position {
@ -335,55 +314,33 @@ fn get_position(context: &RenderContext) -> Position {
) )
} }
fn render_position<F>(context: &mut RenderContext, write: F) fn render_position<'a>(context: &RenderContext) -> Spans<'a> {
where
F: Fn(&mut RenderContext, String, Option<Style>) + Copy,
{
let position = get_position(context); let position = get_position(context);
write( Span::raw(format!(" {}:{} ", position.row + 1, position.col + 1)).into()
context,
format!(" {}:{} ", position.row + 1, position.col + 1),
None,
);
} }
fn render_total_line_numbers<F>(context: &mut RenderContext, write: F) fn render_total_line_numbers<'a>(context: &RenderContext) -> Spans<'a> {
where
F: Fn(&mut RenderContext, String, Option<Style>) + Copy,
{
let total_line_numbers = context.doc.text().len_lines(); let total_line_numbers = context.doc.text().len_lines();
Span::raw(format!(" {} ", total_line_numbers)).into()
write(context, format!(" {} ", total_line_numbers), None);
} }
fn render_position_percentage<F>(context: &mut RenderContext, write: F) fn render_position_percentage<'a>(context: &RenderContext) -> Spans<'a> {
where
F: Fn(&mut RenderContext, String, Option<Style>) + Copy,
{
let position = get_position(context); let position = get_position(context);
let maxrows = context.doc.text().len_lines(); let maxrows = context.doc.text().len_lines();
write( Span::raw(format!("{}%", (position.row + 1) * 100 / maxrows)).into()
context,
format!("{}%", (position.row + 1) * 100 / maxrows),
None,
);
} }
fn render_file_encoding<F>(context: &mut RenderContext, write: F) fn render_file_encoding<'a>(context: &RenderContext) -> Spans<'a> {
where
F: Fn(&mut RenderContext, String, Option<Style>) + Copy,
{
let enc = context.doc.encoding(); let enc = context.doc.encoding();
if enc != encoding::UTF_8 { if enc != encoding::UTF_8 {
write(context, format!(" {} ", enc.name()), None); Span::raw(format!(" {} ", enc.name())).into()
} else {
Spans::default()
} }
} }
fn render_file_line_ending<F>(context: &mut RenderContext, write: F) fn render_file_line_ending<'a>(context: &RenderContext) -> Spans<'a> {
where
F: Fn(&mut RenderContext, String, Option<Style>) + Copy,
{
use helix_core::LineEnding::*; use helix_core::LineEnding::*;
let line_ending = match context.doc.line_ending { let line_ending = match context.doc.line_ending {
Crlf => "CRLF", Crlf => "CRLF",
@ -402,22 +359,16 @@ where
PS => "PS", // U+2029 -- ParagraphSeparator PS => "PS", // U+2029 -- ParagraphSeparator
}; };
write(context, format!(" {} ", line_ending), None); Span::raw(format!(" {} ", line_ending)).into()
} }
fn render_file_type<F>(context: &mut RenderContext, write: F) fn render_file_type<'a>(context: &RenderContext) -> Spans<'a> {
where
F: Fn(&mut RenderContext, String, Option<Style>) + Copy,
{
let file_type = context.doc.language_name().unwrap_or(DEFAULT_LANGUAGE_NAME); let file_type = context.doc.language_name().unwrap_or(DEFAULT_LANGUAGE_NAME);
write(context, format!(" {} ", file_type), None); Span::raw(format!(" {} ", file_type)).into()
} }
fn render_file_name<F>(context: &mut RenderContext, write: F) fn render_file_name<'a>(context: &RenderContext) -> Spans<'a> {
where
F: Fn(&mut RenderContext, String, Option<Style>) + Copy,
{
let title = { let title = {
let rel_path = context.doc.relative_path(); let rel_path = context.doc.relative_path();
let path = rel_path let path = rel_path
@ -428,13 +379,23 @@ where
format!(" {} ", path) format!(" {} ", path)
}; };
write(context, title, None); Span::raw(title).into()
}
fn render_file_absolute_path<'a>(context: &RenderContext) -> Spans<'a> {
let title = {
let path = context.doc.path();
let path = path
.as_ref()
.map(|p| p.to_string_lossy())
.unwrap_or_else(|| SCRATCH_BUFFER_NAME.into());
format!(" {} ", path)
};
Span::raw(title).into()
} }
fn render_file_modification_indicator<F>(context: &mut RenderContext, write: F) fn render_file_modification_indicator<'a>(context: &RenderContext) -> Spans<'a> {
where
F: Fn(&mut RenderContext, String, Option<Style>) + Copy,
{
let title = (if context.doc.is_modified() { let title = (if context.doc.is_modified() {
"[+]" "[+]"
} else { } else {
@ -442,77 +403,61 @@ where
}) })
.to_string(); .to_string();
write(context, title, None); Span::raw(title).into()
} }
fn render_read_only_indicator<F>(context: &mut RenderContext, write: F) fn render_read_only_indicator<'a>(context: &RenderContext) -> Spans<'a> {
where
F: Fn(&mut RenderContext, String, Option<Style>) + Copy,
{
let title = if context.doc.readonly { let title = if context.doc.readonly {
" [readonly] " " [readonly] "
} else { } else {
"" ""
} }
.to_string(); .to_string();
write(context, title, None); Span::raw(title).into()
} }
fn render_file_base_name<F>(context: &mut RenderContext, write: F) fn render_file_base_name<'a>(context: &RenderContext) -> Spans<'a> {
where
F: Fn(&mut RenderContext, String, Option<Style>) + Copy,
{
let title = { let title = {
let rel_path = context.doc.relative_path(); let rel_path = context.doc.relative_path();
let path = rel_path let path = rel_path
.as_ref() .as_ref()
.and_then(|p| p.as_path().file_name().map(|s| s.to_string_lossy())) .and_then(|p| p.file_name().map(|s| s.to_string_lossy()))
.or_else(|| context.doc.name.as_ref().map(|x| x.into())) .or_else(|| context.doc.name.as_ref().map(|x| x.into()))
.unwrap_or_else(|| SCRATCH_BUFFER_NAME.into()); .unwrap_or_else(|| SCRATCH_BUFFER_NAME.into());
format!(" {} ", path) format!(" {} ", path)
}; };
write(context, title, None); Span::raw(title).into()
} }
fn render_separator<F>(context: &mut RenderContext, write: F) fn render_separator<'a>(context: &RenderContext) -> Spans<'a> {
where
F: Fn(&mut RenderContext, String, Option<Style>) + Copy,
{
let sep = &context.editor.config().statusline.separator; let sep = &context.editor.config().statusline.separator;
write( Span::styled(
context,
sep.to_string(), sep.to_string(),
Some(context.editor.theme.get("ui.statusline.separator")), context.editor.theme.get("ui.statusline.separator"),
); )
.into()
} }
fn render_spacer<F>(context: &mut RenderContext, write: F) fn render_spacer<'a>(_context: &RenderContext) -> Spans<'a> {
where Span::raw(" ").into()
F: Fn(&mut RenderContext, String, Option<Style>) + Copy,
{
write(context, String::from(" "), None);
} }
fn render_version_control<F>(context: &mut RenderContext, write: F) fn render_version_control<'a>(context: &RenderContext) -> Spans<'a> {
where
F: Fn(&mut RenderContext, String, Option<Style>) + Copy,
{
let head = context let head = context
.doc .doc
.version_control_head() .version_control_head()
.unwrap_or_default() .unwrap_or_default()
.to_string(); .to_string();
write(context, head, None); Span::raw(head).into()
} }
fn render_register<F>(context: &mut RenderContext, write: F) fn render_register<'a>(context: &RenderContext) -> Spans<'a> {
where
F: Fn(&mut RenderContext, String, Option<Style>) + Copy,
{
if let Some(reg) = context.editor.selected_register { if let Some(reg) = context.editor.selected_register {
write(context, format!(" reg={} ", reg), None) Span::raw(format!(" reg={} ", reg)).into()
} else {
Spans::default()
} }
} }

@ -33,7 +33,7 @@ impl Component for Text {
fn render(&mut self, area: Rect, surface: &mut Surface, _cx: &mut Context) { fn render(&mut self, area: Rect, surface: &mut Surface, _cx: &mut Context) {
use tui::widgets::{Paragraph, Widget, Wrap}; use tui::widgets::{Paragraph, Widget, Wrap};
let par = Paragraph::new(self.contents.clone()).wrap(Wrap { trim: false }); let par = Paragraph::new(&self.contents).wrap(Wrap { trim: false });
// .scroll(x, y) offsets // .scroll(x, y) offsets
par.render(area, surface); par.render(area, surface);

@ -6,13 +6,13 @@ async fn auto_indent_c() -> anyhow::Result<()> {
AppBuilder::new().with_file("foo.c", None), AppBuilder::new().with_file("foo.c", None),
// switches to append mode? // switches to append mode?
( (
helpers::platform_line("void foo() {#[|}]#"), "void foo() {#[|}]#",
"i<ret><esc>", "i<ret><esc>",
helpers::platform_line(indoc! {"\ indoc! {"\
void foo() { void foo() {
#[|\n]#\ #[|\n]#\
} }
"}), "},
), ),
) )
.await?; .await?;

@ -19,6 +19,7 @@ async fn insert_basic() -> anyhow::Result<()> {
format!("#[{}|]#", LINE_END), format!("#[{}|]#", LINE_END),
format!("i{}", pair.0), format!("i{}", pair.0),
format!("{}#[|{}]#{}", pair.0, pair.1, LINE_END), format!("{}#[|{}]#{}", pair.0, pair.1, LINE_END),
LineFeedHandling::AsIs,
)) ))
.await?; .await?;
} }
@ -46,6 +47,7 @@ async fn insert_configured_multi_byte_chars() -> anyhow::Result<()> {
format!("#[{}|]#", LINE_END), format!("#[{}|]#", LINE_END),
format!("i{}", open), format!("i{}", open),
format!("{}#[|{}]#{}", open, close, LINE_END), format!("{}#[|{}]#{}", open, close, LINE_END),
LineFeedHandling::AsIs,
), ),
) )
.await?; .await?;
@ -56,6 +58,7 @@ async fn insert_configured_multi_byte_chars() -> anyhow::Result<()> {
format!("{}#[{}|]#{}", open, close, LINE_END), format!("{}#[{}|]#{}", open, close, LINE_END),
format!("i{}", close), format!("i{}", close),
format!("{}{}#[|{}]#", open, close, LINE_END), format!("{}{}#[|{}]#", open, close, LINE_END),
LineFeedHandling::AsIs,
), ),
) )
.await?; .await?;
@ -71,6 +74,7 @@ async fn insert_after_word() -> anyhow::Result<()> {
format!("foo#[{}|]#", LINE_END), format!("foo#[{}|]#", LINE_END),
format!("i{}", pair.0), format!("i{}", pair.0),
format!("foo{}#[|{}]#{}", pair.0, pair.1, LINE_END), format!("foo{}#[|{}]#{}", pair.0, pair.1, LINE_END),
LineFeedHandling::AsIs,
)) ))
.await?; .await?;
} }
@ -80,6 +84,7 @@ async fn insert_after_word() -> anyhow::Result<()> {
format!("foo#[{}|]#", LINE_END), format!("foo#[{}|]#", LINE_END),
format!("i{}", pair.0), format!("i{}", pair.0),
format!("foo{}#[|{}]#", pair.0, LINE_END), format!("foo{}#[|{}]#", pair.0, LINE_END),
LineFeedHandling::AsIs,
)) ))
.await?; .await?;
} }
@ -94,6 +99,7 @@ async fn insert_before_word() -> anyhow::Result<()> {
format!("#[f|]#oo{}", LINE_END), format!("#[f|]#oo{}", LINE_END),
format!("i{}", pair.0), format!("i{}", pair.0),
format!("{}#[|f]#oo{}", pair.0, LINE_END), format!("{}#[|f]#oo{}", pair.0, LINE_END),
LineFeedHandling::AsIs,
)) ))
.await?; .await?;
} }
@ -108,6 +114,7 @@ async fn insert_before_word_selection() -> anyhow::Result<()> {
format!("#[foo|]#{}", LINE_END), format!("#[foo|]#{}", LINE_END),
format!("i{}", pair.0), format!("i{}", pair.0),
format!("{}#[|foo]#{}", pair.0, LINE_END), format!("{}#[|foo]#{}", pair.0, LINE_END),
LineFeedHandling::AsIs,
)) ))
.await?; .await?;
} }
@ -122,6 +129,7 @@ async fn insert_before_word_selection_trailing_word() -> anyhow::Result<()> {
format!("foo#[ wor|]#{}", LINE_END), format!("foo#[ wor|]#{}", LINE_END),
format!("i{}", pair.0), format!("i{}", pair.0),
format!("foo{}#[|{} wor]#{}", pair.0, pair.1, LINE_END), format!("foo{}#[|{} wor]#{}", pair.0, pair.1, LINE_END),
LineFeedHandling::AsIs,
)) ))
.await?; .await?;
} }
@ -136,6 +144,7 @@ async fn insert_closer_selection_trailing_word() -> anyhow::Result<()> {
format!("foo{}#[|{} wor]#{}", pair.0, pair.1, LINE_END), format!("foo{}#[|{} wor]#{}", pair.0, pair.1, LINE_END),
format!("i{}", pair.1), format!("i{}", pair.1),
format!("foo{}{}#[| wor]#{}", pair.0, pair.1, LINE_END), format!("foo{}{}#[| wor]#{}", pair.0, pair.1, LINE_END),
LineFeedHandling::AsIs,
)) ))
.await?; .await?;
} }
@ -155,6 +164,7 @@ async fn insert_before_eol() -> anyhow::Result<()> {
open = pair.0, open = pair.0,
close = pair.1 close = pair.1
), ),
LineFeedHandling::AsIs,
)) ))
.await?; .await?;
} }
@ -177,6 +187,7 @@ async fn insert_auto_pairs_disabled() -> anyhow::Result<()> {
format!("#[{}|]#", LINE_END), format!("#[{}|]#", LINE_END),
format!("i{}", pair.0), format!("i{}", pair.0),
format!("{}#[|{}]#", pair.0, LINE_END), format!("{}#[|{}]#", pair.0, LINE_END),
LineFeedHandling::AsIs,
), ),
) )
.await?; .await?;
@ -197,6 +208,7 @@ async fn insert_multi_range() -> anyhow::Result<()> {
close = pair.1, close = pair.1,
eol = LINE_END eol = LINE_END
), ),
LineFeedHandling::AsIs,
)) ))
.await?; .await?;
} }
@ -211,6 +223,7 @@ async fn insert_before_multi_code_point_graphemes() -> anyhow::Result<()> {
format!("hello #[👨‍👩‍👧‍👦|]# goodbye{}", LINE_END), format!("hello #[👨‍👩‍👧‍👦|]# goodbye{}", LINE_END),
format!("i{}", pair.1), format!("i{}", pair.1),
format!("hello {}#[|👨‍👩‍👧‍👦]# goodbye{}", pair.1, LINE_END), format!("hello {}#[|👨‍👩‍👧‍👦]# goodbye{}", pair.1, LINE_END),
LineFeedHandling::AsIs,
)) ))
.await?; .await?;
} }
@ -226,6 +239,7 @@ async fn insert_at_end_of_document() -> anyhow::Result<()> {
in_keys: format!("i{}", pair.0), in_keys: format!("i{}", pair.0),
out_text: format!("{}{}{}", LINE_END, pair.0, pair.1), out_text: format!("{}{}{}", LINE_END, pair.0, pair.1),
out_selection: Selection::single(LINE_END.len() + 1, LINE_END.len() + 2), out_selection: Selection::single(LINE_END.len() + 1, LINE_END.len() + 2),
line_feed_handling: LineFeedHandling::AsIs,
}) })
.await?; .await?;
@ -235,6 +249,7 @@ async fn insert_at_end_of_document() -> anyhow::Result<()> {
in_keys: format!("i{}", pair.0), in_keys: format!("i{}", pair.0),
out_text: format!("foo{}{}{}", LINE_END, pair.0, pair.1), out_text: format!("foo{}{}{}", LINE_END, pair.0, pair.1),
out_selection: Selection::single(LINE_END.len() + 4, LINE_END.len() + 5), out_selection: Selection::single(LINE_END.len() + 4, LINE_END.len() + 5),
line_feed_handling: LineFeedHandling::AsIs,
}) })
.await?; .await?;
} }
@ -259,6 +274,7 @@ async fn insert_close_inside_pair() -> anyhow::Result<()> {
close = pair.1, close = pair.1,
eol = LINE_END eol = LINE_END
), ),
LineFeedHandling::AsIs,
)) ))
.await?; .await?;
} }
@ -283,6 +299,7 @@ async fn insert_close_inside_pair_multi() -> anyhow::Result<()> {
close = pair.1, close = pair.1,
eol = LINE_END eol = LINE_END
), ),
LineFeedHandling::AsIs,
)) ))
.await?; .await?;
} }
@ -307,6 +324,7 @@ async fn insert_nested_open_inside_pair() -> anyhow::Result<()> {
close = pair.1, close = pair.1,
eol = LINE_END eol = LINE_END
), ),
LineFeedHandling::AsIs,
)) ))
.await?; .await?;
} }
@ -338,6 +356,7 @@ async fn insert_nested_open_inside_pair_multi() -> anyhow::Result<()> {
inner_close = inner_pair.1, inner_close = inner_pair.1,
eol = LINE_END eol = LINE_END
), ),
LineFeedHandling::AsIs,
)) ))
.await?; .await?;
} }
@ -358,6 +377,7 @@ async fn append_basic() -> anyhow::Result<()> {
close = pair.1, close = pair.1,
eol = LINE_END eol = LINE_END
), ),
LineFeedHandling::AsIs,
)) ))
.await?; .await?;
} }
@ -377,6 +397,7 @@ async fn append_multi_range() -> anyhow::Result<()> {
close = pair.1, close = pair.1,
eol = LINE_END eol = LINE_END
), ),
LineFeedHandling::AsIs,
)) ))
.await?; .await?;
} }
@ -401,6 +422,7 @@ async fn append_close_inside_pair() -> anyhow::Result<()> {
close = pair.1, close = pair.1,
eol = LINE_END eol = LINE_END
), ),
LineFeedHandling::AsIs,
)) ))
.await?; .await?;
} }
@ -425,6 +447,7 @@ async fn append_close_inside_pair_multi() -> anyhow::Result<()> {
close = pair.1, close = pair.1,
eol = LINE_END eol = LINE_END
), ),
LineFeedHandling::AsIs,
)) ))
.await?; .await?;
} }
@ -444,6 +467,7 @@ async fn append_end_of_word() -> anyhow::Result<()> {
close = pair.1, close = pair.1,
eol = LINE_END eol = LINE_END
), ),
LineFeedHandling::AsIs,
)) ))
.await?; .await?;
} }
@ -458,6 +482,7 @@ async fn append_middle_of_word() -> anyhow::Result<()> {
format!("#[wo|]#rd{}", LINE_END), format!("#[wo|]#rd{}", LINE_END),
format!("a{}", pair.1), format!("a{}", pair.1),
format!("#[wo{}r|]#d{}", pair.1, LINE_END), format!("#[wo{}r|]#d{}", pair.1, LINE_END),
LineFeedHandling::AsIs,
)) ))
.await?; .await?;
} }
@ -477,6 +502,7 @@ async fn append_end_of_word_multi() -> anyhow::Result<()> {
close = pair.1, close = pair.1,
eol = LINE_END eol = LINE_END
), ),
LineFeedHandling::AsIs,
)) ))
.await?; .await?;
} }
@ -501,6 +527,7 @@ async fn append_inside_nested_pair() -> anyhow::Result<()> {
close = pair.1, close = pair.1,
eol = LINE_END eol = LINE_END
), ),
LineFeedHandling::AsIs,
)) ))
.await?; .await?;
} }
@ -532,6 +559,7 @@ async fn append_inside_nested_pair_multi() -> anyhow::Result<()> {
inner_close = inner_pair.1, inner_close = inner_pair.1,
eol = LINE_END eol = LINE_END
), ),
LineFeedHandling::AsIs,
)) ))
.await?; .await?;
} }

@ -9,89 +9,89 @@ mod write;
async fn test_selection_duplication() -> anyhow::Result<()> { async fn test_selection_duplication() -> anyhow::Result<()> {
// Forward // Forward
test(( test((
platform_line(indoc! {"\ indoc! {"\
#[lo|]#rem #[lo|]#rem
ipsum ipsum
dolor dolor
"}), "},
"CC", "CC",
platform_line(indoc! {"\ indoc! {"\
#(lo|)#rem #(lo|)#rem
#(ip|)#sum #(ip|)#sum
#[do|]#lor #[do|]#lor
"}), "},
)) ))
.await?; .await?;
// Backward // Backward
test(( test((
platform_line(indoc! {"\ indoc! {"\
#[|lo]#rem #[|lo]#rem
ipsum ipsum
dolor dolor
"}), "},
"CC", "CC",
platform_line(indoc! {"\ indoc! {"\
#(|lo)#rem #(|lo)#rem
#(|ip)#sum #(|ip)#sum
#[|do]#lor #[|do]#lor
"}), "},
)) ))
.await?; .await?;
// Copy the selection to previous line, skipping the first line in the file // Copy the selection to previous line, skipping the first line in the file
test(( test((
platform_line(indoc! {"\ indoc! {"\
test test
#[testitem|]# #[testitem|]#
"}), "},
"<A-C>", "<A-C>",
platform_line(indoc! {"\ indoc! {"\
test test
#[testitem|]# #[testitem|]#
"}), "},
)) ))
.await?; .await?;
// Copy the selection to previous line, including the first line in the file // Copy the selection to previous line, including the first line in the file
test(( test((
platform_line(indoc! {"\ indoc! {"\
test test
#[test|]# #[test|]#
"}), "},
"<A-C>", "<A-C>",
platform_line(indoc! {"\ indoc! {"\
#[test|]# #[test|]#
#(test|)# #(test|)#
"}), "},
)) ))
.await?; .await?;
// Copy the selection to next line, skipping the last line in the file // Copy the selection to next line, skipping the last line in the file
test(( test((
platform_line(indoc! {"\ indoc! {"\
#[testitem|]# #[testitem|]#
test test
"}), "},
"C", "C",
platform_line(indoc! {"\ indoc! {"\
#[testitem|]# #[testitem|]#
test test
"}), "},
)) ))
.await?; .await?;
// Copy the selection to next line, including the last line in the file // Copy the selection to next line, including the last line in the file
test(( test((
platform_line(indoc! {"\ indoc! {"\
#[test|]# #[test|]#
test test
"}), "},
"C", "C",
platform_line(indoc! {"\ indoc! {"\
#(test|)# #(test|)#
#[test|]# #[test|]#
"}), "},
)) ))
.await?; .await?;
Ok(()) Ok(())
@ -153,23 +153,45 @@ async fn test_goto_file_impl() -> anyhow::Result<()> {
) )
.await?; .await?;
// ';' is behind the path
test_key_sequence(
&mut AppBuilder::new().with_file(file.path(), None).build()?,
Some("iimport 'one.js';<esc>B;gf"),
Some(&|app| {
assert_eq!(1, match_paths(app, vec!["one.js"]));
}),
false,
)
.await?;
// allow numeric values in path
test_key_sequence(
&mut AppBuilder::new().with_file(file.path(), None).build()?,
Some("iimport 'one123.js'<esc>B;gf"),
Some(&|app| {
assert_eq!(1, match_paths(app, vec!["one123.js"]));
}),
false,
)
.await?;
Ok(()) Ok(())
} }
#[tokio::test(flavor = "multi_thread")] #[tokio::test(flavor = "multi_thread")]
async fn test_multi_selection_paste() -> anyhow::Result<()> { async fn test_multi_selection_paste() -> anyhow::Result<()> {
test(( test((
platform_line(indoc! {"\ indoc! {"\
#[|lorem]# #[|lorem]#
#(|ipsum)# #(|ipsum)#
#(|dolor)# #(|dolor)#
"}), "},
"yp", "yp",
platform_line(indoc! {"\ indoc! {"\
lorem#[|lorem]# lorem#[|lorem]#
ipsum#(|ipsum)# ipsum#(|ipsum)#
dolor#(|dolor)# dolor#(|dolor)#
"}), "},
)) ))
.await?; .await?;
@ -180,58 +202,58 @@ async fn test_multi_selection_paste() -> anyhow::Result<()> {
async fn test_multi_selection_shell_commands() -> anyhow::Result<()> { async fn test_multi_selection_shell_commands() -> anyhow::Result<()> {
// pipe // pipe
test(( test((
platform_line(indoc! {"\ indoc! {"\
#[|lorem]# #[|lorem]#
#(|ipsum)# #(|ipsum)#
#(|dolor)# #(|dolor)#
"}), "},
"|echo foo<ret>", "|echo foo<ret>",
platform_line(indoc! {"\ indoc! {"\
#[|foo\n]# #[|foo\n]#
#(|foo\n)# #(|foo\n)#
#(|foo\n)# #(|foo\n)#
"}), "},
)) ))
.await?; .await?;
// insert-output // insert-output
test(( test((
platform_line(indoc! {"\ indoc! {"\
#[|lorem]# #[|lorem]#
#(|ipsum)# #(|ipsum)#
#(|dolor)# #(|dolor)#
"}), "},
"!echo foo<ret>", "!echo foo<ret>",
platform_line(indoc! {"\ indoc! {"\
#[|foo\n]# #[|foo\n]#
lorem lorem
#(|foo\n)# #(|foo\n)#
ipsum ipsum
#(|foo\n)# #(|foo\n)#
dolor dolor
"}), "},
)) ))
.await?; .await?;
// append-output // append-output
test(( test((
platform_line(indoc! {"\ indoc! {"\
#[|lorem]# #[|lorem]#
#(|ipsum)# #(|ipsum)#
#(|dolor)# #(|dolor)#
"}), "},
"<A-!>echo foo<ret>", "<A-!>echo foo<ret>",
platform_line(indoc! {"\ indoc! {"\
lorem#[|foo\n]# lorem#[|foo\n]#
ipsum#(|foo\n)# ipsum#(|foo\n)#
dolor#(|foo\n)# dolor#(|foo\n)#
"}), "},
)) ))
.await?; .await?;
@ -247,7 +269,13 @@ async fn test_undo_redo() -> anyhow::Result<()> {
// * u Undo the two newlines. We're now on line 1. // * u Undo the two newlines. We're now on line 1.
// * <C-o><C-i> Jump forward an back again in the jumplist. This would panic // * <C-o><C-i> Jump forward an back again in the jumplist. This would panic
// if the jumplist were not being updated correctly. // if the jumplist were not being updated correctly.
test(("#[|]#", "2[<space><C-s>u<C-o><C-i>", "#[|]#")).await?; test((
"#[|]#",
"2[<space><C-s>u<C-o><C-i>",
"#[|]#",
LineFeedHandling::AsIs,
))
.await?;
// A jumplist selection is passed through an edit and then an undo and then a redo. // A jumplist selection is passed through an edit and then an undo and then a redo.
// //
@ -258,10 +286,22 @@ async fn test_undo_redo() -> anyhow::Result<()> {
// * <C-o> Jump back in the jumplist. This would panic if the jumplist were not being // * <C-o> Jump back in the jumplist. This would panic if the jumplist were not being
// updated correctly. // updated correctly.
// * <C-i> Jump forward to line 1. // * <C-i> Jump forward to line 1.
test(("#[|]#", "[<space><C-s>kduU<C-o><C-i>", "#[|]#")).await?; test((
"#[|]#",
"[<space><C-s>kduU<C-o><C-i>",
"#[|]#",
LineFeedHandling::AsIs,
))
.await?;
// In this case we 'redo' manually to ensure that the transactions are composing correctly. // In this case we 'redo' manually to ensure that the transactions are composing correctly.
test(("#[|]#", "[<space>u[<space>u", "#[|]#")).await?; test((
"#[|]#",
"[<space>u[<space>u",
"#[|]#",
LineFeedHandling::AsIs,
))
.await?;
Ok(()) Ok(())
} }
@ -270,35 +310,35 @@ async fn test_undo_redo() -> anyhow::Result<()> {
async fn test_extend_line() -> anyhow::Result<()> { async fn test_extend_line() -> anyhow::Result<()> {
// extend with line selected then count // extend with line selected then count
test(( test((
platform_line(indoc! {"\ indoc! {"\
#[l|]#orem #[l|]#orem
ipsum ipsum
dolor dolor
"}), "},
"x2x", "x2x",
platform_line(indoc! {"\ indoc! {"\
#[lorem #[lorem
ipsum ipsum
dolor\n|]# dolor\n|]#
"}), "},
)) ))
.await?; .await?;
// extend with count on partial selection // extend with count on partial selection
test(( test((
platform_line(indoc! {"\ indoc! {"\
#[l|]#orem #[l|]#orem
ipsum ipsum
"}), "},
"2x", "2x",
platform_line(indoc! {"\ indoc! {"\
#[lorem #[lorem
ipsum\n|]# ipsum\n|]#
"}), "},
)) ))
.await?; .await?;
@ -366,16 +406,11 @@ async fn test_character_info() -> anyhow::Result<()> {
#[tokio::test(flavor = "multi_thread")] #[tokio::test(flavor = "multi_thread")]
async fn test_delete_char_backward() -> anyhow::Result<()> { async fn test_delete_char_backward() -> anyhow::Result<()> {
// don't panic when deleting overlapping ranges // don't panic when deleting overlapping ranges
test(("#(x|)# #[x|]#", "c<space><backspace><esc>", "#[\n|]#")).await?;
test(( test((
platform_line("#(x|)# #[x|]#"), "#( |)##( |)#a#( |)#axx#[x|]#a",
"c<space><backspace><esc>",
platform_line("#[\n|]#"),
))
.await?;
test((
platform_line("#( |)##( |)#a#( |)#axx#[x|]#a"),
"li<backspace><esc>", "li<backspace><esc>",
platform_line("#(a|)##(|a)#xx#[|a]#"), "#(a|)##(|a)#xx#[|a]#",
)) ))
.await?; .await?;
@ -385,43 +420,33 @@ async fn test_delete_char_backward() -> anyhow::Result<()> {
#[tokio::test(flavor = "multi_thread")] #[tokio::test(flavor = "multi_thread")]
async fn test_delete_word_backward() -> anyhow::Result<()> { async fn test_delete_word_backward() -> anyhow::Result<()> {
// don't panic when deleting overlapping ranges // don't panic when deleting overlapping ranges
test(( test(("fo#[o|]#ba#(r|)#", "a<C-w><esc>", "#[\n|]#")).await?;
platform_line("fo#[o|]#ba#(r|)#"),
"a<C-w><esc>",
platform_line("#[\n|]#"),
))
.await?;
Ok(()) Ok(())
} }
#[tokio::test(flavor = "multi_thread")] #[tokio::test(flavor = "multi_thread")]
async fn test_delete_word_forward() -> anyhow::Result<()> { async fn test_delete_word_forward() -> anyhow::Result<()> {
// don't panic when deleting overlapping ranges // don't panic when deleting overlapping ranges
test(( test(("fo#[o|]#b#(|ar)#", "i<A-d><esc>", "fo#[\n|]#")).await?;
platform_line("fo#[o|]#b#(|ar)#"),
"i<A-d><esc>",
platform_line("fo#[\n|]#"),
))
.await?;
Ok(()) Ok(())
} }
#[tokio::test(flavor = "multi_thread")] #[tokio::test(flavor = "multi_thread")]
async fn test_delete_char_forward() -> anyhow::Result<()> { async fn test_delete_char_forward() -> anyhow::Result<()> {
test(( test((
platform_line(indoc! {"\ indoc! {"\
#[abc|]#def #[abc|]#def
#(abc|)#ef #(abc|)#ef
#(abc|)#f #(abc|)#f
#(abc|)# #(abc|)#
"}), "},
"a<del><esc>", "a<del><esc>",
platform_line(indoc! {"\ indoc! {"\
#[abc|]#ef #[abc|]#ef
#(abc|)#f #(abc|)#f
#(abc|)# #(abc|)#
#(abc|)# #(abc|)#
"}), "},
)) ))
.await?; .await?;
@ -430,33 +455,37 @@ async fn test_delete_char_forward() -> anyhow::Result<()> {
#[tokio::test(flavor = "multi_thread")] #[tokio::test(flavor = "multi_thread")]
async fn test_insert_with_indent() -> anyhow::Result<()> { async fn test_insert_with_indent() -> anyhow::Result<()> {
const INPUT: &str = "\ const INPUT: &str = indoc! { "
#[f|]#n foo() { #[f|]#n foo() {
if let Some(_) = None { if let Some(_) = None {
} }
\x20
} }
fn bar() { fn bar() {
}"; }
"
};
// insert_at_line_start // insert_at_line_start
test(( test((
INPUT, INPUT,
":lang rust<ret>%<A-s>I", ":lang rust<ret>%<A-s>I",
"\ indoc! { "
#[f|]#n foo() { #[f|]#n foo() {
#(i|)#f let Some(_) = None { #(i|)#f let Some(_) = None {
#(\n|)#\ #(\n|)#
\x20 #(}|)#
#(\x20|)#
#(}|)# #(}|)#
#(\n|)#\ #( |)#
#(}|)#
#(\n|)#
#(f|)#n bar() { #(f|)#n bar() {
#(\n|)#\ #(\n|)#
#(}|)#", #(}|)#
"
},
)) ))
.await?; .await?;
@ -464,17 +493,19 @@ fn bar() {
test(( test((
INPUT, INPUT,
":lang rust<ret>%<A-s>A", ":lang rust<ret>%<A-s>A",
"\ indoc! { "
fn foo() {#[\n|]#\ fn foo() {#[\n|]#
\x20 if let Some(_) = None {#(\n|)#\ if let Some(_) = None {#(\n|)#
\x20 #(\n|)#\ #(\n|)#
\x20 }#(\n|)#\ }#(\n|)#
\x20#(\n|)#\ #(\n|)#
}#(\n|)#\ }#(\n|)#
#(\n|)#\ #(\n|)#
fn bar() {#(\n|)#\ fn bar() {#(\n|)#
\x20 #(\n|)#\ #(\n|)#
}#(|)#", }#(\n|)#
"
},
)) ))
.await?; .await?;
@ -485,42 +516,209 @@ fn bar() {#(\n|)#\
async fn test_join_selections() -> anyhow::Result<()> { async fn test_join_selections() -> anyhow::Result<()> {
// normal join // normal join
test(( test((
platform_line(indoc! {"\ indoc! {"\
#[a|]#bc #[a|]#bc
def def
"}), "},
"J", "J",
platform_line(indoc! {"\ indoc! {"\
#[a|]#bc def #[a|]#bc def
"}), "},
)) ))
.await?; .await?;
// join with empty line // join with empty line
test(( test((
platform_line(indoc! {"\ indoc! {"\
#[a|]#bc #[a|]#bc
def def
"}), "},
"JJ", "JJ",
platform_line(indoc! {"\ indoc! {"\
#[a|]#bc def #[a|]#bc def
"}), "},
)) ))
.await?; .await?;
// join with additional space in non-empty line // join with additional space in non-empty line
test(( test((
platform_line(indoc! {"\ indoc! {"\
#[a|]#bc #[a|]#bc
def def
"}), "},
"JJ", "JJ",
platform_line(indoc! {"\ indoc! {"\
#[a|]#bc def #[a|]#bc def
"}), "},
))
.await?;
Ok(())
}
#[tokio::test(flavor = "multi_thread")]
async fn test_join_selections_space() -> anyhow::Result<()> {
// join with empty lines panic
test((
indoc! {"\
#[a
b
c
d
e|]#
"},
"<A-J>",
indoc! {"\
a#[ |]#b#( |)#c#( |)#d#( |)#e
"},
))
.await?;
// normal join
test((
indoc! {"\
#[a|]#bc
def
"},
"<A-J>",
indoc! {"\
abc#[ |]#def
"},
))
.await?;
// join with empty line
test((
indoc! {"\
#[a|]#bc
def
"},
"<A-J>",
indoc! {"\
#[a|]#bc
def
"},
))
.await?;
// join with additional space in non-empty line
test((
indoc! {"\
#[a|]#bc
def
"},
"<A-J><A-J>",
indoc! {"\
abc#[ |]#def
"},
))
.await?;
// join with retained trailing spaces
test((
indoc! {"\
#[aaa
bb
c |]#
"},
"<A-J>",
indoc! {"\
aaa #[ |]#bb #( |)#c
"},
))
.await?;
Ok(())
}
#[tokio::test(flavor = "multi_thread")]
async fn test_read_file() -> anyhow::Result<()> {
let mut file = tempfile::NamedTempFile::new()?;
let contents_to_read = "some contents";
let output_file = helpers::temp_file_with_contents(contents_to_read)?;
test_key_sequence(
&mut helpers::AppBuilder::new()
.with_file(file.path(), None)
.build()?,
Some(&format!(":r {:?}<ret><esc>:w<ret>", output_file.path())),
Some(&|app| {
assert!(!app.editor.is_err(), "error: {:?}", app.editor.get_status());
}),
false,
)
.await?;
let expected_contents = LineFeedHandling::Native.apply(contents_to_read);
helpers::assert_file_has_content(&mut file, &expected_contents)?;
Ok(())
}
#[tokio::test(flavor = "multi_thread")]
async fn surround_delete() -> anyhow::Result<()> {
// Test `surround_delete` when head < anchor
test(("(#[| ]#)", "mdm", "#[| ]#")).await?;
test(("(#[| ]#)", "md(", "#[| ]#")).await?;
Ok(())
}
#[tokio::test(flavor = "multi_thread")]
async fn surround_replace_ts() -> anyhow::Result<()> {
const INPUT: &str = r#"\
fn foo() {
if let Some(_) = None {
todo!("f#[|o]#o)");
}
}
"#;
test((
INPUT,
":lang rust<ret>mrm'",
r#"\
fn foo() {
if let Some(_) = None {
todo!('f#[|o]#o)');
}
}
"#,
))
.await?;
test((
INPUT,
":lang rust<ret>3mrm[",
r#"\
fn foo() {
if let Some(_) = None [
todo!("f#[|o]#o)");
]
}
"#,
))
.await?;
test((
INPUT,
":lang rust<ret>2mrm{",
r#"\
fn foo() {
if let Some(_) = None {
todo!{"f#[|o]#o)"};
}
}
"#,
)) ))
.await?; .await?;

@ -6,7 +6,7 @@ async fn test_move_parent_node_end() -> anyhow::Result<()> {
// single cursor stays single cursor, first goes to end of current // single cursor stays single cursor, first goes to end of current
// node, then parent // node, then parent
( (
helpers::platform_line(indoc! {r##" indoc! {r##"
fn foo() { fn foo() {
let result = if true { let result = if true {
"yes" "yes"
@ -14,9 +14,9 @@ async fn test_move_parent_node_end() -> anyhow::Result<()> {
"no#["|]# "no#["|]#
} }
} }
"##}), "##},
"<A-e>", "<A-e>",
helpers::platform_line(indoc! {"\ indoc! {"\
fn foo() { fn foo() {
let result = if true { let result = if true {
\"yes\" \"yes\"
@ -24,10 +24,10 @@ async fn test_move_parent_node_end() -> anyhow::Result<()> {
\"no\"#[\n|]# \"no\"#[\n|]#
} }
} }
"}), "},
), ),
( (
helpers::platform_line(indoc! {"\ indoc! {"\
fn foo() { fn foo() {
let result = if true { let result = if true {
\"yes\" \"yes\"
@ -35,9 +35,9 @@ async fn test_move_parent_node_end() -> anyhow::Result<()> {
\"no\"#[\n|]# \"no\"#[\n|]#
} }
} }
"}), "},
"<A-e>", "<A-e>",
helpers::platform_line(indoc! {"\ indoc! {"\
fn foo() { fn foo() {
let result = if true { let result = if true {
\"yes\" \"yes\"
@ -45,11 +45,11 @@ async fn test_move_parent_node_end() -> anyhow::Result<()> {
\"no\" \"no\"
}#[\n|]# }#[\n|]#
} }
"}), "},
), ),
// select mode extends // select mode extends
( (
helpers::platform_line(indoc! {r##" indoc! {r##"
fn foo() { fn foo() {
let result = if true { let result = if true {
"yes" "yes"
@ -57,9 +57,9 @@ async fn test_move_parent_node_end() -> anyhow::Result<()> {
#["no"|]# #["no"|]#
} }
} }
"##}), "##},
"v<A-e><A-e>", "v<A-e><A-e>",
helpers::platform_line(indoc! {"\ indoc! {"\
fn foo() { fn foo() {
let result = if true { let result = if true {
\"yes\" \"yes\"
@ -67,7 +67,7 @@ async fn test_move_parent_node_end() -> anyhow::Result<()> {
#[\"no\" #[\"no\"
}\n|]# }\n|]#
} }
"}), "},
), ),
]; ];
@ -84,7 +84,7 @@ async fn test_move_parent_node_start() -> anyhow::Result<()> {
// single cursor stays single cursor, first goes to end of current // single cursor stays single cursor, first goes to end of current
// node, then parent // node, then parent
( (
helpers::platform_line(indoc! {r##" indoc! {r##"
fn foo() { fn foo() {
let result = if true { let result = if true {
"yes" "yes"
@ -92,9 +92,9 @@ async fn test_move_parent_node_start() -> anyhow::Result<()> {
"no#["|]# "no#["|]#
} }
} }
"##}), "##},
"<A-b>", "<A-b>",
helpers::platform_line(indoc! {"\ indoc! {"\
fn foo() { fn foo() {
let result = if true { let result = if true {
\"yes\" \"yes\"
@ -102,10 +102,10 @@ async fn test_move_parent_node_start() -> anyhow::Result<()> {
#[\"|]#no\" #[\"|]#no\"
} }
} }
"}), "},
), ),
( (
helpers::platform_line(indoc! {"\ indoc! {"\
fn foo() { fn foo() {
let result = if true { let result = if true {
\"yes\" \"yes\"
@ -113,9 +113,9 @@ async fn test_move_parent_node_start() -> anyhow::Result<()> {
\"no\"#[\n|]# \"no\"#[\n|]#
} }
} }
"}), "},
"<A-b>", "<A-b>",
helpers::platform_line(indoc! {"\ indoc! {"\
fn foo() { fn foo() {
let result = if true { let result = if true {
\"yes\" \"yes\"
@ -123,10 +123,10 @@ async fn test_move_parent_node_start() -> anyhow::Result<()> {
\"no\" \"no\"
} }
} }
"}), "},
), ),
( (
helpers::platform_line(indoc! {"\ indoc! {"\
fn foo() { fn foo() {
let result = if true { let result = if true {
\"yes\" \"yes\"
@ -134,9 +134,9 @@ async fn test_move_parent_node_start() -> anyhow::Result<()> {
\"no\" \"no\"
} }
} }
"}), "},
"<A-b>", "<A-b>",
helpers::platform_line(indoc! {"\ indoc! {"\
fn foo() { fn foo() {
let result = if true { let result = if true {
\"yes\" \"yes\"
@ -144,11 +144,11 @@ async fn test_move_parent_node_start() -> anyhow::Result<()> {
\"no\" \"no\"
} }
} }
"}), "},
), ),
// select mode extends // select mode extends
( (
helpers::platform_line(indoc! {r##" indoc! {r##"
fn foo() { fn foo() {
let result = if true { let result = if true {
"yes" "yes"
@ -156,9 +156,9 @@ async fn test_move_parent_node_start() -> anyhow::Result<()> {
#["no"|]# #["no"|]#
} }
} }
"##}), "##},
"v<A-b><A-b>", "v<A-b><A-b>",
helpers::platform_line(indoc! {"\ indoc! {"\
fn foo() { fn foo() {
let result = if true { let result = if true {
\"yes\" \"yes\"
@ -166,10 +166,10 @@ async fn test_move_parent_node_start() -> anyhow::Result<()> {
]#\"no\" ]#\"no\"
} }
} }
"}), "},
), ),
( (
helpers::platform_line(indoc! {r##" indoc! {r##"
fn foo() { fn foo() {
let result = if true { let result = if true {
"yes" "yes"
@ -177,9 +177,9 @@ async fn test_move_parent_node_start() -> anyhow::Result<()> {
#["no"|]# #["no"|]#
} }
} }
"##}), "##},
"v<A-b><A-b><A-b>", "v<A-b><A-b><A-b>",
helpers::platform_line(indoc! {"\ indoc! {"\
fn foo() { fn foo() {
let result = if true { let result = if true {
\"yes\" \"yes\"
@ -187,7 +187,7 @@ async fn test_move_parent_node_start() -> anyhow::Result<()> {
]#\"no\" ]#\"no\"
} }
} }
"}), "},
), ),
]; ];
@ -204,7 +204,7 @@ async fn test_smart_tab_move_parent_node_end() -> anyhow::Result<()> {
// single cursor stays single cursor, first goes to end of current // single cursor stays single cursor, first goes to end of current
// node, then parent // node, then parent
( (
helpers::platform_line(indoc! {r##" indoc! {r##"
fn foo() { fn foo() {
let result = if true { let result = if true {
"yes" "yes"
@ -212,9 +212,9 @@ async fn test_smart_tab_move_parent_node_end() -> anyhow::Result<()> {
"no#["|]# "no#["|]#
} }
} }
"##}), "##},
"i<tab>", "i<tab>",
helpers::platform_line(indoc! {"\ indoc! {"\
fn foo() { fn foo() {
let result = if true { let result = if true {
\"yes\" \"yes\"
@ -222,10 +222,10 @@ async fn test_smart_tab_move_parent_node_end() -> anyhow::Result<()> {
\"no\"#[|\n]# \"no\"#[|\n]#
} }
} }
"}), "},
), ),
( (
helpers::platform_line(indoc! {"\ indoc! {"\
fn foo() { fn foo() {
let result = if true { let result = if true {
\"yes\" \"yes\"
@ -233,9 +233,9 @@ async fn test_smart_tab_move_parent_node_end() -> anyhow::Result<()> {
\"no\"#[\n|]# \"no\"#[\n|]#
} }
} }
"}), "},
"i<tab>", "i<tab>",
helpers::platform_line(indoc! {"\ indoc! {"\
fn foo() { fn foo() {
let result = if true { let result = if true {
\"yes\" \"yes\"
@ -243,12 +243,12 @@ async fn test_smart_tab_move_parent_node_end() -> anyhow::Result<()> {
\"no\" \"no\"
}#[|\n]# }#[|\n]#
} }
"}), "},
), ),
// appending to the end of a line should still look at the current // appending to the end of a line should still look at the current
// line, not the next one // line, not the next one
( (
helpers::platform_line(indoc! {"\ indoc! {"\
fn foo() { fn foo() {
let result = if true { let result = if true {
\"yes\" \"yes\"
@ -256,9 +256,9 @@ async fn test_smart_tab_move_parent_node_end() -> anyhow::Result<()> {
\"no#[\"|]# \"no#[\"|]#
} }
} }
"}), "},
"a<tab>", "a<tab>",
helpers::platform_line(indoc! {"\ indoc! {"\
fn foo() { fn foo() {
let result = if true { let result = if true {
\"yes\" \"yes\"
@ -266,11 +266,11 @@ async fn test_smart_tab_move_parent_node_end() -> anyhow::Result<()> {
\"no\" \"no\"
}#[\n|]# }#[\n|]#
} }
"}), "},
), ),
// before cursor is all whitespace, so insert tab // before cursor is all whitespace, so insert tab
( (
helpers::platform_line(indoc! {"\ indoc! {"\
fn foo() { fn foo() {
let result = if true { let result = if true {
\"yes\" \"yes\"
@ -278,9 +278,9 @@ async fn test_smart_tab_move_parent_node_end() -> anyhow::Result<()> {
#[\"no\"|]# #[\"no\"|]#
} }
} }
"}), "},
"i<tab>", "i<tab>",
helpers::platform_line(indoc! {"\ indoc! {"\
fn foo() { fn foo() {
let result = if true { let result = if true {
\"yes\" \"yes\"
@ -288,12 +288,12 @@ async fn test_smart_tab_move_parent_node_end() -> anyhow::Result<()> {
#[|\"no\"]# #[|\"no\"]#
} }
} }
"}), "},
), ),
// if selection spans multiple lines, it should still only look at the // if selection spans multiple lines, it should still only look at the
// line on which the head is // line on which the head is
( (
helpers::platform_line(indoc! {"\ indoc! {"\
fn foo() { fn foo() {
let result = if true { let result = if true {
#[\"yes\" #[\"yes\"
@ -301,9 +301,9 @@ async fn test_smart_tab_move_parent_node_end() -> anyhow::Result<()> {
\"no\"|]# \"no\"|]#
} }
} }
"}), "},
"a<tab>", "a<tab>",
helpers::platform_line(indoc! {"\ indoc! {"\
fn foo() { fn foo() {
let result = if true { let result = if true {
\"yes\" \"yes\"
@ -311,10 +311,10 @@ async fn test_smart_tab_move_parent_node_end() -> anyhow::Result<()> {
\"no\" \"no\"
}#[\n|]# }#[\n|]#
} }
"}), "},
), ),
( (
helpers::platform_line(indoc! {"\ indoc! {"\
fn foo() { fn foo() {
let result = if true { let result = if true {
#[\"yes\" #[\"yes\"
@ -322,9 +322,9 @@ async fn test_smart_tab_move_parent_node_end() -> anyhow::Result<()> {
\"no\"|]# \"no\"|]#
} }
} }
"}), "},
"i<tab>", "i<tab>",
helpers::platform_line(indoc! {"\ indoc! {"\
fn foo() { fn foo() {
let result = if true { let result = if true {
#[|\"yes\" #[|\"yes\"
@ -332,10 +332,10 @@ async fn test_smart_tab_move_parent_node_end() -> anyhow::Result<()> {
\"no\"]# \"no\"]#
} }
} }
"}), "},
), ),
( (
helpers::platform_line(indoc! {"\ indoc! {"\
fn foo() { fn foo() {
#[l|]#et result = if true { #[l|]#et result = if true {
#(\"yes\" #(\"yes\"
@ -343,9 +343,9 @@ async fn test_smart_tab_move_parent_node_end() -> anyhow::Result<()> {
\"no\"|)# \"no\"|)#
} }
} }
"}), "},
"i<tab>", "i<tab>",
helpers::platform_line(indoc! {"\ indoc! {"\
fn foo() { fn foo() {
#[|l]#et result = if true { #[|l]#et result = if true {
#(|\"yes\" #(|\"yes\"
@ -353,10 +353,10 @@ async fn test_smart_tab_move_parent_node_end() -> anyhow::Result<()> {
\"no\")# \"no\")#
} }
} }
"}), "},
), ),
( (
helpers::platform_line(indoc! {"\ indoc! {"\
fn foo() { fn foo() {
let result = if true { let result = if true {
\"yes\"#[\n|]# \"yes\"#[\n|]#
@ -364,9 +364,9 @@ async fn test_smart_tab_move_parent_node_end() -> anyhow::Result<()> {
\"no\"#(\n|)# \"no\"#(\n|)#
} }
} }
"}), "},
"i<tab>", "i<tab>",
helpers::platform_line(indoc! {"\ indoc! {"\
fn foo() { fn foo() {
let result = if true { let result = if true {
\"yes\" \"yes\"
@ -374,10 +374,10 @@ async fn test_smart_tab_move_parent_node_end() -> anyhow::Result<()> {
\"no\" \"no\"
}#(|\n)# }#(|\n)#
} }
"}), "},
), ),
( (
helpers::platform_line(indoc! {"\ indoc! {"\
fn foo() { fn foo() {
let result = if true { let result = if true {
#[\"yes\"|]# #[\"yes\"|]#
@ -385,9 +385,9 @@ async fn test_smart_tab_move_parent_node_end() -> anyhow::Result<()> {
#(\"no\"|)# #(\"no\"|)#
} }
} }
"}), "},
"i<tab>", "i<tab>",
helpers::platform_line(indoc! {"\ indoc! {"\
fn foo() { fn foo() {
let result = if true { let result = if true {
#[|\"yes\"]# #[|\"yes\"]#
@ -395,12 +395,12 @@ async fn test_smart_tab_move_parent_node_end() -> anyhow::Result<()> {
#(|\"no\")# #(|\"no\")#
} }
} }
"}), "},
), ),
// if any cursors are not preceded by all whitespace, then do the // if any cursors are not preceded by all whitespace, then do the
// smart_tab action // smart_tab action
( (
helpers::platform_line(indoc! {"\ indoc! {"\
fn foo() { fn foo() {
let result = if true { let result = if true {
#[\"yes\"\n|]# #[\"yes\"\n|]#
@ -408,9 +408,9 @@ async fn test_smart_tab_move_parent_node_end() -> anyhow::Result<()> {
\"no#(\"\n|)# \"no#(\"\n|)#
} }
} }
"}), "},
"i<tab>", "i<tab>",
helpers::platform_line(indoc! {"\ indoc! {"\
fn foo() { fn foo() {
let result = if true { let result = if true {
\"yes\" \"yes\"
@ -418,11 +418,11 @@ async fn test_smart_tab_move_parent_node_end() -> anyhow::Result<()> {
\"no\" \"no\"
}#(|\n)# }#(|\n)#
} }
"}), "},
), ),
// Ctrl-tab always inserts a tab // Ctrl-tab always inserts a tab
( (
helpers::platform_line(indoc! {"\ indoc! {"\
fn foo() { fn foo() {
let result = if true { let result = if true {
#[\"yes\"\n|]# #[\"yes\"\n|]#
@ -430,9 +430,9 @@ async fn test_smart_tab_move_parent_node_end() -> anyhow::Result<()> {
\"no#(\"\n|)# \"no#(\"\n|)#
} }
} }
"}), "},
"i<S-tab>", "i<S-tab>",
helpers::platform_line(indoc! {"\ indoc! {"\
fn foo() { fn foo() {
let result = if true { let result = if true {
#[|\"yes\"\n]# #[|\"yes\"\n]#
@ -440,7 +440,363 @@ async fn test_smart_tab_move_parent_node_end() -> anyhow::Result<()> {
\"no #(|\"\n)# \"no #(|\"\n)#
} }
} }
"}), "},
),
];
for test in tests {
test_with_config(AppBuilder::new().with_file("foo.rs", None), test).await?;
}
Ok(())
}
#[tokio::test(flavor = "multi_thread")]
async fn select_all_siblings() -> anyhow::Result<()> {
let tests = vec![
// basic tests
(
indoc! {r##"
let foo = bar(#[a|]#, b, c);
"##},
"<A-a>",
indoc! {r##"
let foo = bar(#[a|]#, #(b|)#, #(c|)#);
"##},
),
(
indoc! {r##"
let a = [
#[1|]#,
2,
3,
4,
5,
];
"##},
"<A-a>",
indoc! {r##"
let a = [
#[1|]#,
#(2|)#,
#(3|)#,
#(4|)#,
#(5|)#,
];
"##},
),
// direction is preserved
(
indoc! {r##"
let a = [
#[|1]#,
2,
3,
4,
5,
];
"##},
"<A-a>",
indoc! {r##"
let a = [
#[|1]#,
#(|2)#,
#(|3)#,
#(|4)#,
#(|5)#,
];
"##},
),
// can't pick any more siblings - selection stays the same
(
indoc! {r##"
let a = [
#[1|]#,
#(2|)#,
#(3|)#,
#(4|)#,
#(5|)#,
];
"##},
"<A-a>",
indoc! {r##"
let a = [
#[1|]#,
#(2|)#,
#(3|)#,
#(4|)#,
#(5|)#,
];
"##},
),
// each cursor does the sibling select independently
(
indoc! {r##"
let a = [
#[1|]#,
2,
3,
4,
5,
];
let b = [
#("one"|)#,
"two",
"three",
"four",
"five",
];
"##},
"<A-a>",
indoc! {r##"
let a = [
#[1|]#,
#(2|)#,
#(3|)#,
#(4|)#,
#(5|)#,
];
let b = [
#("one"|)#,
#("two"|)#,
#("three"|)#,
#("four"|)#,
#("five"|)#,
];
"##},
),
// conflicting sibling selections get normalized. Here, the primary
// selection would choose every list item, but because the secondary
// range covers more than one item, the descendent is the entire list,
// which means the sibling is the assignment. The list item ranges just
// get normalized out since the list itself becomes selected.
(
indoc! {r##"
let a = [
#[1|]#,
2,
#(3,
4|)#,
5,
];
"##},
"<A-a>",
indoc! {r##"
let #(a|)# = #[[
1,
2,
3,
4,
5,
]|]#;
"##},
),
];
for test in tests {
test_with_config(AppBuilder::new().with_file("foo.rs", None), test).await?;
}
Ok(())
}
#[tokio::test(flavor = "multi_thread")]
async fn select_all_children() -> anyhow::Result<()> {
let tests = vec![
// basic tests
(
indoc! {r##"
let foo = bar#[(a, b, c)|]#;
"##},
"<A-I>",
indoc! {r##"
let foo = bar(#[a|]#, #(b|)#, #(c|)#);
"##},
),
(
indoc! {r##"
let a = #[[
1,
2,
3,
4,
5,
]|]#;
"##},
"<A-I>",
indoc! {r##"
let a = [
#[1|]#,
#(2|)#,
#(3|)#,
#(4|)#,
#(5|)#,
];
"##},
),
// direction is preserved
(
indoc! {r##"
let a = #[|[
1,
2,
3,
4,
5,
]]#;
"##},
"<A-I>",
indoc! {r##"
let a = [
#[|1]#,
#(|2)#,
#(|3)#,
#(|4)#,
#(|5)#,
];
"##},
),
// can't pick any more children - selection stays the same
(
indoc! {r##"
let a = [
#[1|]#,
#(2|)#,
#(3|)#,
#(4|)#,
#(5|)#,
];
"##},
"<A-I>",
indoc! {r##"
let a = [
#[1|]#,
#(2|)#,
#(3|)#,
#(4|)#,
#(5|)#,
];
"##},
),
// each cursor does the sibling select independently
(
indoc! {r##"
let a = #[|[
1,
2,
3,
4,
5,
]]#;
let b = #([
"one",
"two",
"three",
"four",
"five",
]|)#;
"##},
"<A-I>",
indoc! {r##"
let a = [
#[|1]#,
#(|2)#,
#(|3)#,
#(|4)#,
#(|5)#,
];
let b = [
#("one"|)#,
#("two"|)#,
#("three"|)#,
#("four"|)#,
#("five"|)#,
];
"##},
),
];
for test in tests {
test_with_config(AppBuilder::new().with_file("foo.rs", None), test).await?;
}
Ok(())
}
#[tokio::test(flavor = "multi_thread")]
async fn test_select_next_sibling() -> anyhow::Result<()> {
let tests = vec![
// basic test
(
indoc! {r##"
fn inc(x: usize) -> usize { x + 1 #[}|]#
fn dec(x: usize) -> usize { x - 1 }
fn ident(x: usize) -> usize { x }
"##},
"<A-n>",
indoc! {r##"
fn inc(x: usize) -> usize { x + 1 }
#[fn dec(x: usize) -> usize { x - 1 }|]#
fn ident(x: usize) -> usize { x }
"##},
),
// direction is not preserved and is always forward.
(
indoc! {r##"
fn inc(x: usize) -> usize { x + 1 #[}|]#
fn dec(x: usize) -> usize { x - 1 }
fn ident(x: usize) -> usize { x }
"##},
"<A-n><A-;><A-n>",
indoc! {r##"
fn inc(x: usize) -> usize { x + 1 }
fn dec(x: usize) -> usize { x - 1 }
#[fn ident(x: usize) -> usize { x }|]#
"##},
),
];
for test in tests {
test_with_config(AppBuilder::new().with_file("foo.rs", None), test).await?;
}
Ok(())
}
#[tokio::test(flavor = "multi_thread")]
async fn test_select_prev_sibling() -> anyhow::Result<()> {
let tests = vec![
// basic test
(
indoc! {r##"
fn inc(x: usize) -> usize { x + 1 }
fn dec(x: usize) -> usize { x - 1 }
#[|f]#n ident(x: usize) -> usize { x }
"##},
"<A-p>",
indoc! {r##"
fn inc(x: usize) -> usize { x + 1 }
#[|fn dec(x: usize) -> usize { x - 1 }]#
fn ident(x: usize) -> usize { x }
"##},
),
// direction is not preserved and is always backward.
(
indoc! {r##"
fn inc(x: usize) -> usize { x + 1 }
fn dec(x: usize) -> usize { x - 1 }
#[|f]#n ident(x: usize) -> usize { x }
"##},
"<A-p><A-;><A-p>",
indoc! {r##"
#[|fn inc(x: usize) -> usize { x + 1 }]#
fn dec(x: usize) -> usize { x - 1 }
fn ident(x: usize) -> usize { x }
"##},
), ),
]; ];

@ -94,7 +94,10 @@ async fn test_buffer_close_concurrent() -> anyhow::Result<()> {
) )
.await?; .await?;
helpers::assert_file_has_content(file.as_file_mut(), &platform_line(&RANGE.end().to_string()))?; helpers::assert_file_has_content(
&mut file,
&LineFeedHandling::Native.apply(&RANGE.end().to_string()),
)?;
Ok(()) Ok(())
} }
@ -114,14 +117,12 @@ async fn test_write() -> anyhow::Result<()> {
) )
.await?; .await?;
file.as_file_mut().flush()?; reload_file(&mut file).unwrap();
file.as_file_mut().sync_all()?;
let mut file_content = String::new(); let mut file_content = String::new();
file.as_file_mut().read_to_string(&mut file_content)?; file.as_file_mut().read_to_string(&mut file_content)?;
assert_eq!( assert_eq!(
helpers::platform_line("the gostak distims the doshes"), LineFeedHandling::Native.apply("the gostak distims the doshes"),
file_content file_content
); );
@ -138,24 +139,18 @@ async fn test_overwrite_protection() -> anyhow::Result<()> {
helpers::run_event_loop_until_idle(&mut app).await; helpers::run_event_loop_until_idle(&mut app).await;
file.as_file_mut() file.as_file_mut()
.write_all(helpers::platform_line("extremely important content").as_bytes())?; .write_all("extremely important content".as_bytes())?;
file.as_file_mut().flush()?; file.as_file_mut().flush()?;
file.as_file_mut().sync_all()?; file.as_file_mut().sync_all()?;
test_key_sequence(&mut app, Some(":x<ret>"), None, false).await?; test_key_sequence(&mut app, Some(":x<ret>"), None, false).await?;
file.as_file_mut().flush()?; reload_file(&mut file).unwrap();
file.as_file_mut().sync_all()?;
file.rewind()?;
let mut file_content = String::new(); let mut file_content = String::new();
file.as_file_mut().read_to_string(&mut file_content)?; file.read_to_string(&mut file_content)?;
assert_eq!( assert_eq!("extremely important content", file_content);
helpers::platform_line("extremely important content"),
file_content
);
Ok(()) Ok(())
} }
@ -175,14 +170,13 @@ async fn test_write_quit() -> anyhow::Result<()> {
) )
.await?; .await?;
file.as_file_mut().flush()?; reload_file(&mut file).unwrap();
file.as_file_mut().sync_all()?;
let mut file_content = String::new(); let mut file_content = String::new();
file.as_file_mut().read_to_string(&mut file_content)?; file.read_to_string(&mut file_content)?;
assert_eq!( assert_eq!(
helpers::platform_line("the gostak distims the doshes"), LineFeedHandling::Native.apply("the gostak distims the doshes"),
file_content file_content
); );
@ -205,12 +199,13 @@ async fn test_write_concurrent() -> anyhow::Result<()> {
test_key_sequence(&mut app, Some(&command), None, false).await?; test_key_sequence(&mut app, Some(&command), None, false).await?;
file.as_file_mut().flush()?; reload_file(&mut file).unwrap();
file.as_file_mut().sync_all()?;
let mut file_content = String::new(); let mut file_content = String::new();
file.as_file_mut().read_to_string(&mut file_content)?; file.read_to_string(&mut file_content)?;
assert_eq!(platform_line(&RANGE.end().to_string()), file_content); assert_eq!(
LineFeedHandling::Native.apply(&RANGE.end().to_string()),
file_content
);
Ok(()) Ok(())
} }
@ -276,7 +271,7 @@ async fn test_write_scratch_to_new_path() -> anyhow::Result<()> {
) )
.await?; .await?;
helpers::assert_file_has_content(file.as_file_mut(), &helpers::platform_line("hello"))?; helpers::assert_file_has_content(&mut file, &LineFeedHandling::Native.apply("hello"))?;
Ok(()) Ok(())
} }
@ -315,13 +310,13 @@ async fn test_write_auto_format_fails_still_writes() -> anyhow::Result<()> {
let mut app = helpers::AppBuilder::new() let mut app = helpers::AppBuilder::new()
.with_file(file.path(), None) .with_file(file.path(), None)
.with_input_text("#[l|]#et foo = 0;\n") .with_input_text("#[l|]#et foo = 0;\n")
.with_lang_config(helpers::test_syntax_conf(Some(lang_conf.into()))) .with_lang_loader(helpers::test_syntax_loader(Some(lang_conf.into())))
.build()?; .build()?;
test_key_sequences(&mut app, vec![(Some(":w<ret>"), None)], false).await?; test_key_sequences(&mut app, vec![(Some(":w<ret>"), None)], false).await?;
// file still saves // file still saves
helpers::assert_file_has_content(file.as_file_mut(), "let foo = 0;\n")?; helpers::assert_file_has_content(&mut file, "let foo = 0;\n")?;
Ok(()) Ok(())
} }
@ -360,13 +355,13 @@ async fn test_write_new_path() -> anyhow::Result<()> {
.await?; .await?;
helpers::assert_file_has_content( helpers::assert_file_has_content(
file1.as_file_mut(), &mut file1,
&helpers::platform_line("i can eat glass, it will not hurt me\n"), &LineFeedHandling::Native.apply("i can eat glass, it will not hurt me\n"),
)?; )?;
helpers::assert_file_has_content( helpers::assert_file_has_content(
file2.as_file_mut(), &mut file2,
&helpers::platform_line("i can eat glass, it will not hurt me\n"), &LineFeedHandling::Native.apply("i can eat glass, it will not hurt me\n"),
)?; )?;
Ok(()) Ok(())
@ -436,8 +431,8 @@ async fn test_write_insert_final_newline_added_if_missing() -> anyhow::Result<()
test_key_sequence(&mut app, Some(":w<ret>"), None, false).await?; test_key_sequence(&mut app, Some(":w<ret>"), None, false).await?;
helpers::assert_file_has_content( helpers::assert_file_has_content(
file.as_file_mut(), &mut file,
&helpers::platform_line("have you tried chamomile tea?\n"), &LineFeedHandling::Native.apply("have you tried chamomile tea?\n"),
)?; )?;
Ok(()) Ok(())
@ -448,14 +443,14 @@ async fn test_write_insert_final_newline_unchanged_if_not_missing() -> anyhow::R
let mut file = tempfile::NamedTempFile::new()?; let mut file = tempfile::NamedTempFile::new()?;
let mut app = helpers::AppBuilder::new() let mut app = helpers::AppBuilder::new()
.with_file(file.path(), None) .with_file(file.path(), None)
.with_input_text(&helpers::platform_line("#[t|]#en minutes, please\n")) .with_input_text(LineFeedHandling::Native.apply("#[t|]#en minutes, please\n"))
.build()?; .build()?;
test_key_sequence(&mut app, Some(":w<ret>"), None, false).await?; test_key_sequence(&mut app, Some(":w<ret>"), None, false).await?;
helpers::assert_file_has_content( helpers::assert_file_has_content(
file.as_file_mut(), &mut file,
&helpers::platform_line("ten minutes, please\n"), &LineFeedHandling::Native.apply("ten minutes, please\n"),
)?; )?;
Ok(()) Ok(())
@ -478,10 +473,8 @@ async fn test_write_insert_final_newline_unchanged_if_missing_and_false() -> any
test_key_sequence(&mut app, Some(":w<ret>"), None, false).await?; test_key_sequence(&mut app, Some(":w<ret>"), None, false).await?;
helpers::assert_file_has_content( reload_file(&mut file).unwrap();
file.as_file_mut(), helpers::assert_file_has_content(&mut file, "the quiet rain continued through the night")?;
"the quiet rain continued through the night",
)?;
Ok(()) Ok(())
} }
@ -507,13 +500,13 @@ async fn test_write_all_insert_final_newline_add_if_missing_and_modified() -> an
.await?; .await?;
helpers::assert_file_has_content( helpers::assert_file_has_content(
file1.as_file_mut(), &mut file1,
&helpers::platform_line("we don't serve time travelers here\n"), &LineFeedHandling::Native.apply("we don't serve time travelers here\n"),
)?; )?;
helpers::assert_file_has_content( helpers::assert_file_has_content(
file2.as_file_mut(), &mut file2,
&helpers::platform_line("a time traveler walks into a bar\n"), &LineFeedHandling::Native.apply("a time traveler walks into a bar\n"),
)?; )?;
Ok(()) Ok(())
@ -531,7 +524,82 @@ async fn test_write_all_insert_final_newline_do_not_add_if_unmodified() -> anyho
test_key_sequence(&mut app, Some(":wa<ret>"), None, false).await?; test_key_sequence(&mut app, Some(":wa<ret>"), None, false).await?;
helpers::assert_file_has_content(file.as_file_mut(), "i lost on Jeopardy!")?; helpers::assert_file_has_content(&mut file, "i lost on Jeopardy!")?;
Ok(())
}
#[tokio::test(flavor = "multi_thread")]
async fn test_symlink_write() -> anyhow::Result<()> {
#[cfg(unix)]
use std::os::unix::fs::symlink;
#[cfg(not(unix))]
use std::os::windows::fs::symlink_file as symlink;
let dir = tempfile::tempdir()?;
let mut file = tempfile::NamedTempFile::new_in(&dir)?;
let symlink_path = dir.path().join("linked");
symlink(file.path(), &symlink_path)?;
let mut app = helpers::AppBuilder::new()
.with_file(&symlink_path, None)
.build()?;
test_key_sequence(
&mut app,
Some("ithe gostak distims the doshes<ret><esc>:w<ret>"),
None,
false,
)
.await?;
reload_file(&mut file).unwrap();
let mut file_content = String::new();
file.as_file_mut().read_to_string(&mut file_content)?;
assert_eq!(
LineFeedHandling::Native.apply("the gostak distims the doshes"),
file_content
);
assert!(symlink_path.is_symlink());
Ok(())
}
#[tokio::test(flavor = "multi_thread")]
async fn test_symlink_write_fail() -> anyhow::Result<()> {
#[cfg(unix)]
use std::os::unix::fs::symlink;
#[cfg(not(unix))]
use std::os::windows::fs::symlink_file as symlink;
let dir = tempfile::tempdir()?;
let file = helpers::new_readonly_tempfile_in_dir(&dir)?;
let symlink_path = dir.path().join("linked");
symlink(file.path(), &symlink_path)?;
let mut app = helpers::AppBuilder::new()
.with_file(&symlink_path, None)
.build()?;
test_key_sequence(
&mut app,
Some("ihello<esc>:wq<ret>"),
Some(&|app| {
let mut docs: Vec<_> = app.editor.documents().collect();
assert_eq!(1, docs.len());
let doc = docs.pop().unwrap();
assert_eq!(Some(&path::normalize(&symlink_path)), doc.path());
assert_eq!(&Severity::Error, app.editor.get_status().unwrap().1);
}),
false,
)
.await?;
assert!(symlink_path.is_symlink());
Ok(()) Ok(())
} }
@ -557,7 +625,7 @@ async fn edit_file_with_content(file_content: &[u8]) -> anyhow::Result<()> {
) )
.await?; .await?;
file.rewind()?; reload_file(&mut file).unwrap();
let mut new_file_content: Vec<u8> = Vec::new(); let mut new_file_content: Vec<u8> = Vec::new();
file.read_to_end(&mut new_file_content)?; file.read_to_end(&mut new_file_content)?;

@ -1,5 +1,4 @@
use std::{ use std::{
fs::File,
io::{Read, Write}, io::{Read, Write},
mem::replace, mem::replace,
path::PathBuf, path::PathBuf,
@ -14,6 +13,46 @@ use helix_view::{current_ref, doc, editor::LspConfig, input::parse_macro, Editor
use tempfile::NamedTempFile; use tempfile::NamedTempFile;
use tokio_stream::wrappers::UnboundedReceiverStream; use tokio_stream::wrappers::UnboundedReceiverStream;
/// Specify how to set up the input text with line feeds
#[derive(Clone, Debug)]
pub enum LineFeedHandling {
/// Replaces all LF chars with the system's appropriate line feed character,
/// and if one doesn't exist already, appends the system's appropriate line
/// ending to the end of a string.
Native,
/// Do not modify the input text in any way. What you give is what you test.
AsIs,
}
impl LineFeedHandling {
/// Apply the line feed handling to the input string, yielding a set of
/// resulting texts with the appropriate line feed substitutions.
pub fn apply(&self, text: &str) -> String {
let line_end = match self {
LineFeedHandling::Native => helix_core::NATIVE_LINE_ENDING,
LineFeedHandling::AsIs => return text.into(),
}
.as_str();
// we can assume that the source files in this code base will always
// be LF, so indoc strings will always insert LF
let mut output = text.replace('\n', line_end);
if !output.ends_with(line_end) {
output.push_str(line_end);
}
output
}
}
impl Default for LineFeedHandling {
fn default() -> Self {
Self::Native
}
}
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct TestCase { pub struct TestCase {
pub in_text: String, pub in_text: String,
@ -21,6 +60,8 @@ pub struct TestCase {
pub in_keys: String, pub in_keys: String,
pub out_text: String, pub out_text: String,
pub out_selection: Selection, pub out_selection: Selection,
pub line_feed_handling: LineFeedHandling,
} }
impl<S, R, V> From<(S, R, V)> for TestCase impl<S, R, V> From<(S, R, V)> for TestCase
@ -30,8 +71,19 @@ where
V: Into<String>, V: Into<String>,
{ {
fn from((input, keys, output): (S, R, V)) -> Self { fn from((input, keys, output): (S, R, V)) -> Self {
let (in_text, in_selection) = test::print(&input.into()); TestCase::from((input, keys, output, LineFeedHandling::default()))
let (out_text, out_selection) = test::print(&output.into()); }
}
impl<S, R, V> From<(S, R, V, LineFeedHandling)> for TestCase
where
S: Into<String>,
R: Into<String>,
V: Into<String>,
{
fn from((input, keys, output, line_feed_handling): (S, R, V, LineFeedHandling)) -> Self {
let (in_text, in_selection) = test::print(&line_feed_handling.apply(&input.into()));
let (out_text, out_selection) = test::print(&line_feed_handling.apply(&output.into()));
TestCase { TestCase {
in_text, in_text,
@ -39,6 +91,7 @@ where
in_keys: keys.into(), in_keys: keys.into(),
out_text, out_text,
out_selection, out_selection,
line_feed_handling,
} }
} }
} }
@ -137,9 +190,10 @@ pub async fn test_key_sequence_with_input_text<T: Into<TestCase>>(
should_exit: bool, should_exit: bool,
) -> anyhow::Result<()> { ) -> anyhow::Result<()> {
let test_case = test_case.into(); let test_case = test_case.into();
let mut app = match app { let mut app = match app {
Some(app) => app, Some(app) => app,
None => Application::new(Args::default(), test_config(), test_syntax_conf(None))?, None => Application::new(Args::default(), test_config(), test_syntax_loader(None))?,
}; };
let (view, doc) = helix_view::current!(app.editor); let (view, doc) = helix_view::current!(app.editor);
@ -162,9 +216,9 @@ pub async fn test_key_sequence_with_input_text<T: Into<TestCase>>(
.await .await
} }
/// Generates language configs that merge in overrides, like a user language /// Generates language config loader that merge in overrides, like a user language
/// config. The argument string must be a raw TOML document. /// config. The argument string must be a raw TOML document.
pub fn test_syntax_conf(overrides: Option<String>) -> helix_core::syntax::Configuration { pub fn test_syntax_loader(overrides: Option<String>) -> helix_core::syntax::Loader {
let mut lang = helix_loader::config::default_lang_config(); let mut lang = helix_loader::config::default_lang_config();
if let Some(overrides) = overrides { if let Some(overrides) = overrides {
@ -172,7 +226,7 @@ pub fn test_syntax_conf(overrides: Option<String>) -> helix_core::syntax::Config
lang = helix_loader::merge_toml_values(lang, override_toml, 3); lang = helix_loader::merge_toml_values(lang, override_toml, 3);
} }
lang.try_into().unwrap() helix_core::syntax::Loader::new(lang.try_into().unwrap()).unwrap()
} }
/// Use this for very simple test cases where there is one input /// Use this for very simple test cases where there is one input
@ -240,23 +294,6 @@ pub fn test_editor_config() -> helix_view::editor::Config {
} }
} }
/// Replaces all LF chars with the system's appropriate line feed
/// character, and if one doesn't exist already, appends the system's
/// appropriate line ending to the end of a string.
pub fn platform_line(input: &str) -> String {
let line_end = helix_core::NATIVE_LINE_ENDING.as_str();
// we can assume that the source files in this code base will always
// be LF, so indoc strings will always insert LF
let mut output = input.replace('\n', line_end);
if !output.ends_with(line_end) {
output.push_str(line_end);
}
output
}
/// Creates a new temporary file that is set to read only. Useful for /// Creates a new temporary file that is set to read only. Useful for
/// testing write failures. /// testing write failures.
pub fn new_readonly_tempfile() -> anyhow::Result<NamedTempFile> { pub fn new_readonly_tempfile() -> anyhow::Result<NamedTempFile> {
@ -268,10 +305,22 @@ pub fn new_readonly_tempfile() -> anyhow::Result<NamedTempFile> {
Ok(file) Ok(file)
} }
/// Creates a new temporary file in the directory that is set to read only. Useful for
/// testing write failures.
pub fn new_readonly_tempfile_in_dir(
dir: impl AsRef<std::path::Path>,
) -> anyhow::Result<NamedTempFile> {
let mut file = tempfile::NamedTempFile::new_in(dir)?;
let metadata = file.as_file().metadata()?;
let mut perms = metadata.permissions();
perms.set_readonly(true);
file.as_file_mut().set_permissions(perms)?;
Ok(file)
}
pub struct AppBuilder { pub struct AppBuilder {
args: Args, args: Args,
config: Config, config: Config,
syn_conf: helix_core::syntax::Configuration, syn_loader: helix_core::syntax::Loader,
input: Option<(String, Selection)>, input: Option<(String, Selection)>,
} }
@ -280,7 +329,7 @@ impl Default for AppBuilder {
Self { Self {
args: Args::default(), args: Args::default(),
config: test_config(), config: test_config(),
syn_conf: test_syntax_conf(None), syn_loader: test_syntax_loader(None),
input: None, input: None,
} }
} }
@ -314,8 +363,8 @@ impl AppBuilder {
self self
} }
pub fn with_lang_config(mut self, syn_conf: helix_core::syntax::Configuration) -> Self { pub fn with_lang_loader(mut self, syn_loader: helix_core::syntax::Loader) -> Self {
self.syn_conf = syn_conf; self.syn_loader = syn_loader;
self self
} }
@ -328,7 +377,7 @@ impl AppBuilder {
bail!("Having the directory {path:?} in args.files[0] is not yet supported for integration tests"); bail!("Having the directory {path:?} in args.files[0] is not yet supported for integration tests");
} }
let mut app = Application::new(self.args, self.config, self.syn_conf)?; let mut app = Application::new(self.args, self.config, self.syn_loader)?;
if let Some((text, selection)) = self.input { if let Some((text, selection)) = self.input {
let (view, doc) = helix_view::current!(app.editor); let (view, doc) = helix_view::current!(app.editor);
@ -352,9 +401,8 @@ pub async fn run_event_loop_until_idle(app: &mut Application) {
app.event_loop_until_idle(&mut rx_stream).await; app.event_loop_until_idle(&mut rx_stream).await;
} }
pub fn assert_file_has_content(file: &mut File, content: &str) -> anyhow::Result<()> { pub fn assert_file_has_content(file: &mut NamedTempFile, content: &str) -> anyhow::Result<()> {
file.flush()?; reload_file(file)?;
file.sync_all()?;
let mut file_content = String::new(); let mut file_content = String::new();
file.read_to_string(&mut file_content)?; file.read_to_string(&mut file_content)?;
@ -368,3 +416,13 @@ pub fn assert_status_not_error(editor: &Editor) {
assert_ne!(&Severity::Error, sev); assert_ne!(&Severity::Error, sev);
} }
} }
pub fn reload_file(file: &mut NamedTempFile) -> anyhow::Result<()> {
let path = file.path();
let f = std::fs::OpenOptions::new()
.write(true)
.read(true)
.open(&path)?;
*file.as_file_mut() = f;
Ok(())
}

@ -6,30 +6,30 @@ async fn auto_indent() -> anyhow::Result<()> {
let enter_tests = [ let enter_tests = [
( (
helpers::platform_line(indoc! {r##" indoc! {r##"
type Test struct {#[}|]# type Test struct {#[}|]#
"##}), "##},
"i<ret>", "i<ret>",
helpers::platform_line(indoc! {"\ indoc! {"\
type Test struct { type Test struct {
\t#[|\n]# \t#[|\n]#
} }
"}), "},
), ),
( (
helpers::platform_line(indoc! {"\ indoc! {"\
func main() { func main() {
\tswitch nil {#[}|]# \tswitch nil {#[}|]#
} }
"}), "},
"i<ret>", "i<ret>",
helpers::platform_line(indoc! {"\ indoc! {"\
func main() { func main() {
\tswitch nil { \tswitch nil {
\t\t#[|\n]# \t\t#[|\n]#
\t} \t}
} }
"}), "},
), ),
]; ];

@ -6,7 +6,7 @@ async fn auto_indent() -> anyhow::Result<()> {
let below_tests = [ let below_tests = [
( (
helpers::platform_line(indoc! {r##" indoc! {r##"
#[t|]#op: #[t|]#op:
baz: foo baz: foo
bazi: bazi:
@ -17,9 +17,9 @@ async fn auto_indent() -> anyhow::Result<()> {
- 2 - 2
bax: foox bax: foox
fook: fook:
"##}), "##},
"o", "o",
helpers::platform_line(indoc! {"\ indoc! {"\
top: top:
#[\n|]# #[\n|]#
baz: foo baz: foo
@ -31,10 +31,10 @@ async fn auto_indent() -> anyhow::Result<()> {
- 2 - 2
bax: foox bax: foox
fook: fook:
"}), "},
), ),
( (
helpers::platform_line(indoc! {r##" indoc! {r##"
top: top:
b#[a|]#z: foo b#[a|]#z: foo
bazi: bazi:
@ -45,9 +45,9 @@ async fn auto_indent() -> anyhow::Result<()> {
- 2 - 2
bax: foox bax: foox
fook: fook:
"##}), "##},
"o", "o",
helpers::platform_line(indoc! {"\ indoc! {"\
top: top:
baz: foo baz: foo
#[\n|]# #[\n|]#
@ -59,10 +59,10 @@ async fn auto_indent() -> anyhow::Result<()> {
- 2 - 2
bax: foox bax: foox
fook: fook:
"}), "},
), ),
( (
helpers::platform_line(indoc! {r##" indoc! {r##"
top: top:
baz: foo baz: foo
bazi#[:|]# bazi#[:|]#
@ -73,9 +73,9 @@ async fn auto_indent() -> anyhow::Result<()> {
- 2 - 2
bax: foox bax: foox
fook: fook:
"##}), "##},
"o", "o",
helpers::platform_line(indoc! {"\ indoc! {"\
top: top:
baz: foo baz: foo
bazi: bazi:
@ -87,10 +87,10 @@ async fn auto_indent() -> anyhow::Result<()> {
- 2 - 2
bax: foox bax: foox
fook: fook:
"}), "},
), ),
( (
helpers::platform_line(indoc! {r##" indoc! {r##"
top: top:
baz: foo baz: foo
bazi: bazi:
@ -101,9 +101,9 @@ async fn auto_indent() -> anyhow::Result<()> {
- 2 - 2
bax: foox bax: foox
fook: fook:
"##}), "##},
"o", "o",
helpers::platform_line(indoc! {"\ indoc! {"\
top: top:
baz: foo baz: foo
bazi: bazi:
@ -115,10 +115,10 @@ async fn auto_indent() -> anyhow::Result<()> {
- 2 - 2
bax: foox bax: foox
fook: fook:
"}), "},
), ),
( (
helpers::platform_line(indoc! {r##" indoc! {r##"
top: top:
baz: foo baz: foo
bazi: bazi:
@ -129,9 +129,9 @@ async fn auto_indent() -> anyhow::Result<()> {
- 2 - 2
bax: foox bax: foox
fook: fook:
"##}), "##},
"o", "o",
helpers::platform_line(indoc! {"\ indoc! {"\
top: top:
baz: foo baz: foo
bazi: bazi:
@ -143,10 +143,10 @@ async fn auto_indent() -> anyhow::Result<()> {
- 2 - 2
bax: foox bax: foox
fook: fook:
"}), "},
), ),
( (
helpers::platform_line(indoc! {"\ indoc! {"\
top: top:
baz: foo baz: foo
bazi: bazi:
@ -157,9 +157,9 @@ async fn auto_indent() -> anyhow::Result<()> {
- 2 - 2
bax: foox bax: foox
fook: fook:
"}), "},
"o", "o",
helpers::platform_line(indoc! {"\ indoc! {"\
top: top:
baz: foo baz: foo
bazi: bazi:
@ -171,10 +171,10 @@ async fn auto_indent() -> anyhow::Result<()> {
- 2 - 2
bax: foox bax: foox
fook: fook:
"}), "},
), ),
( (
helpers::platform_line(indoc! {"\ indoc! {"\
top: top:
baz: foo baz: foo
bazi: bazi:
@ -185,9 +185,9 @@ async fn auto_indent() -> anyhow::Result<()> {
- 2 - 2
bax: foox bax: foox
fook: fook:
"}), "},
"o", "o",
helpers::platform_line(indoc! {"\ indoc! {"\
top: top:
baz: foo baz: foo
bazi: bazi:
@ -199,10 +199,10 @@ async fn auto_indent() -> anyhow::Result<()> {
- 2 - 2
bax: foox bax: foox
fook: fook:
"}), "},
), ),
( (
helpers::platform_line(indoc! {"\ indoc! {"\
top: top:
baz: foo baz: foo
bazi: bazi:
@ -213,9 +213,9 @@ async fn auto_indent() -> anyhow::Result<()> {
- 2 - 2
bax: foox bax: foox
fook:#[\n|]# fook:#[\n|]#
"}), "},
"o", "o",
helpers::platform_line(indoc! {"\ indoc! {"\
top: top:
baz: foo baz: foo
bazi: bazi:
@ -227,10 +227,10 @@ async fn auto_indent() -> anyhow::Result<()> {
bax: foox bax: foox
fook: fook:
#[\n|]# #[\n|]#
"}), "},
), ),
( (
helpers::platform_line(indoc! {"\ indoc! {"\
top: top:
baz: foo baz: foo
bax: | bax: |
@ -239,9 +239,9 @@ async fn auto_indent() -> anyhow::Result<()> {
line line
string#[\n|]# string#[\n|]#
fook: fook:
"}), "},
"o", "o",
helpers::platform_line(indoc! {"\ indoc! {"\
top: top:
baz: foo baz: foo
bax: | bax: |
@ -251,10 +251,10 @@ async fn auto_indent() -> anyhow::Result<()> {
string string
#[\n|]# #[\n|]#
fook: fook:
"}), "},
), ),
( (
helpers::platform_line(indoc! {"\ indoc! {"\
top: top:
baz: foo baz: foo
bax: > bax: >
@ -263,9 +263,9 @@ async fn auto_indent() -> anyhow::Result<()> {
line#[\n|]# line#[\n|]#
string string
fook: fook:
"}), "},
"o", "o",
helpers::platform_line(indoc! {"\ indoc! {"\
top: top:
baz: foo baz: foo
bax: > bax: >
@ -275,74 +275,74 @@ async fn auto_indent() -> anyhow::Result<()> {
#[\n|]# #[\n|]#
string string
fook: fook:
"}), "},
), ),
( (
helpers::platform_line(indoc! {"\ indoc! {"\
top: top:
baz: foo baz: foo
bax: >#[\n|]# bax: >#[\n|]#
fook: fook:
"}), "},
"o", "o",
helpers::platform_line(indoc! {"\ indoc! {"\
top: top:
baz: foo baz: foo
bax: > bax: >
#[\n|]# #[\n|]#
fook: fook:
"}), "},
), ),
( (
helpers::platform_line(indoc! {"\ indoc! {"\
- top:#[\n|]# - top:#[\n|]#
baz: foo baz: foo
bax: foox bax: foox
fook: fook:
"}), "},
"o", "o",
helpers::platform_line(indoc! {"\ indoc! {"\
- top: - top:
#[\n|]# #[\n|]#
baz: foo baz: foo
bax: foox bax: foox
fook: fook:
"}), "},
), ),
( (
helpers::platform_line(indoc! {"\ indoc! {"\
- top: - top:
baz: foo#[\n|]# baz: foo#[\n|]#
bax: foox bax: foox
fook: fook:
"}), "},
"o", "o",
helpers::platform_line(indoc! {"\ indoc! {"\
- top: - top:
baz: foo baz: foo
#[\n|]# #[\n|]#
bax: foox bax: foox
fook: fook:
"}), "},
), ),
( (
helpers::platform_line(indoc! {"\ indoc! {"\
- top: - top:
baz: foo baz: foo
bax: foox#[\n|]# bax: foox#[\n|]#
fook: fook:
"}), "},
"o", "o",
helpers::platform_line(indoc! {"\ indoc! {"\
- top: - top:
baz: foo baz: foo
bax: foox bax: foox
#[\n|]# #[\n|]#
fook: fook:
"}), "},
), ),
( (
helpers::platform_line(indoc! {"\ indoc! {"\
top: top:
baz: baz:
- one: two#[\n|]# - one: two#[\n|]#
@ -350,9 +350,9 @@ async fn auto_indent() -> anyhow::Result<()> {
- top: - top:
baz: foo baz: foo
bax: foox bax: foox
"}), "},
"o", "o",
helpers::platform_line(indoc! {"\ indoc! {"\
top: top:
baz: baz:
- one: two - one: two
@ -361,42 +361,42 @@ async fn auto_indent() -> anyhow::Result<()> {
- top: - top:
baz: foo baz: foo
bax: foox bax: foox
"}), "},
), ),
// yaml map without a key // yaml map without a key
( (
helpers::platform_line(indoc! {"\ indoc! {"\
top:#[\n|]# top:#[\n|]#
"}), "},
"o", "o",
helpers::platform_line(indoc! {"\ indoc! {"\
top: top:
#[\n|]# #[\n|]#
"}), "},
), ),
( (
helpers::platform_line(indoc! {"\ indoc! {"\
top#[:|]# top#[:|]#
bottom: withvalue bottom: withvalue
"}), "},
"o", "o",
helpers::platform_line(indoc! {"\ indoc! {"\
top: top:
#[\n|]# #[\n|]#
bottom: withvalue bottom: withvalue
"}), "},
), ),
( (
helpers::platform_line(indoc! {"\ indoc! {"\
bottom: withvalue bottom: withvalue
top#[:|]# top#[:|]#
"}), "},
"o", "o",
helpers::platform_line(indoc! {"\ indoc! {"\
bottom: withvalue bottom: withvalue
top: top:
#[\n|]# #[\n|]#
"}), "},
), ),
]; ];
@ -406,7 +406,7 @@ async fn auto_indent() -> anyhow::Result<()> {
let above_tests = [ let above_tests = [
( (
helpers::platform_line(indoc! {r##" indoc! {r##"
#[t|]#op: #[t|]#op:
baz: foo baz: foo
bazi: bazi:
@ -417,9 +417,9 @@ async fn auto_indent() -> anyhow::Result<()> {
- 2 - 2
bax: foox bax: foox
fook: fook:
"##}), "##},
"O", "O",
helpers::platform_line(indoc! {"\ indoc! {"\
#[\n|]# #[\n|]#
top: top:
baz: foo baz: foo
@ -431,10 +431,10 @@ async fn auto_indent() -> anyhow::Result<()> {
- 2 - 2
bax: foox bax: foox
fook: fook:
"}), "},
), ),
( (
helpers::platform_line(indoc! {r##" indoc! {r##"
top: top:
b#[a|]#z: foo b#[a|]#z: foo
bazi: bazi:
@ -445,9 +445,9 @@ async fn auto_indent() -> anyhow::Result<()> {
- 2 - 2
bax: foox bax: foox
fook: fook:
"##}), "##},
"O", "O",
helpers::platform_line(indoc! {"\ indoc! {"\
top: top:
#[\n|]# #[\n|]#
baz: foo baz: foo
@ -459,10 +459,10 @@ async fn auto_indent() -> anyhow::Result<()> {
- 2 - 2
bax: foox bax: foox
fook: fook:
"}), "},
), ),
( (
helpers::platform_line(indoc! {r##" indoc! {r##"
top: top:
baz: foo baz: foo
bazi#[:|]# bazi#[:|]#
@ -473,9 +473,9 @@ async fn auto_indent() -> anyhow::Result<()> {
- 2 - 2
bax: foox bax: foox
fook: fook:
"##}), "##},
"O", "O",
helpers::platform_line(indoc! {"\ indoc! {"\
top: top:
baz: foo baz: foo
#[\n|]# #[\n|]#
@ -487,10 +487,10 @@ async fn auto_indent() -> anyhow::Result<()> {
- 2 - 2
bax: foox bax: foox
fook: fook:
"}), "},
), ),
( (
helpers::platform_line(indoc! {r##" indoc! {r##"
top: top:
baz: foo baz: foo
bazi: bazi:
@ -501,9 +501,9 @@ async fn auto_indent() -> anyhow::Result<()> {
- 2 - 2
bax: foox bax: foox
fook: fook:
"##}), "##},
"O", "O",
helpers::platform_line(indoc! {"\ indoc! {"\
top: top:
baz: foo baz: foo
bazi: bazi:
@ -515,10 +515,10 @@ async fn auto_indent() -> anyhow::Result<()> {
- 2 - 2
bax: foox bax: foox
fook: fook:
"}), "},
), ),
( (
helpers::platform_line(indoc! {r##" indoc! {r##"
top: top:
baz: foo baz: foo
bazi: bazi:
@ -529,9 +529,9 @@ async fn auto_indent() -> anyhow::Result<()> {
- 2 - 2
bax: foox bax: foox
fook: fook:
"##}), "##},
"O", "O",
helpers::platform_line(indoc! {"\ indoc! {"\
top: top:
baz: foo baz: foo
bazi: bazi:
@ -543,10 +543,10 @@ async fn auto_indent() -> anyhow::Result<()> {
- 2 - 2
bax: foox bax: foox
fook: fook:
"}), "},
), ),
( (
helpers::platform_line(indoc! {"\ indoc! {"\
top: top:
baz: foo baz: foo
bazi: bazi:
@ -557,9 +557,9 @@ async fn auto_indent() -> anyhow::Result<()> {
- 2 - 2
bax: foox bax: foox
fook: fook:
"}), "},
"O", "O",
helpers::platform_line(indoc! {"\ indoc! {"\
top: top:
baz: foo baz: foo
bazi: bazi:
@ -571,10 +571,10 @@ async fn auto_indent() -> anyhow::Result<()> {
- 2 - 2
bax: foox bax: foox
fook: fook:
"}), "},
), ),
( (
helpers::platform_line(indoc! {"\ indoc! {"\
top: top:
baz: foo baz: foo
bazi: bazi:
@ -585,9 +585,9 @@ async fn auto_indent() -> anyhow::Result<()> {
- 2 - 2
bax: foox bax: foox
fook: fook:
"}), "},
"O", "O",
helpers::platform_line(indoc! {"\ indoc! {"\
top: top:
baz: foo baz: foo
bazi: bazi:
@ -599,10 +599,10 @@ async fn auto_indent() -> anyhow::Result<()> {
- 2 - 2
bax: foox bax: foox
fook: fook:
"}), "},
), ),
( (
helpers::platform_line(indoc! {"\ indoc! {"\
top: top:
baz: foo baz: foo
bazi: bazi:
@ -613,9 +613,9 @@ async fn auto_indent() -> anyhow::Result<()> {
- 2 - 2
bax: foox bax: foox
fook:#[\n|]# fook:#[\n|]#
"}), "},
"O", "O",
helpers::platform_line(indoc! {"\ indoc! {"\
top: top:
baz: foo baz: foo
bazi: bazi:
@ -627,10 +627,10 @@ async fn auto_indent() -> anyhow::Result<()> {
bax: foox bax: foox
#[\n|]# #[\n|]#
fook: fook:
"}), "},
), ),
( (
helpers::platform_line(indoc! {"\ indoc! {"\
top: top:
baz: foo baz: foo
bax: | bax: |
@ -639,9 +639,9 @@ async fn auto_indent() -> anyhow::Result<()> {
line line
string#[\n|]# string#[\n|]#
fook: fook:
"}), "},
"O", "O",
helpers::platform_line(indoc! {"\ indoc! {"\
top: top:
baz: foo baz: foo
bax: | bax: |
@ -651,10 +651,10 @@ async fn auto_indent() -> anyhow::Result<()> {
#[\n|]# #[\n|]#
string string
fook: fook:
"}), "},
), ),
( (
helpers::platform_line(indoc! {"\ indoc! {"\
top: top:
baz: foo baz: foo
bax: > bax: >
@ -663,9 +663,9 @@ async fn auto_indent() -> anyhow::Result<()> {
line line
string string
fook: fook:
"}), "},
"O", "O",
helpers::platform_line(indoc! {"\ indoc! {"\
top: top:
baz: foo baz: foo
bax: > bax: >
@ -675,58 +675,58 @@ async fn auto_indent() -> anyhow::Result<()> {
line line
string string
fook: fook:
"}), "},
), ),
( (
helpers::platform_line(indoc! {"\ indoc! {"\
top: top:
baz: foo baz: foo
bax: > bax: >
fook:#[\n|]# fook:#[\n|]#
"}), "},
"O", "O",
helpers::platform_line(indoc! {"\ indoc! {"\
top: top:
baz: foo baz: foo
bax: > bax: >
#[\n|]# #[\n|]#
fook: fook:
"}), "},
), ),
( (
helpers::platform_line(indoc! {"\ indoc! {"\
- top: - top:
baz: foo#[\n|]# baz: foo#[\n|]#
bax: foox bax: foox
fook: fook:
"}), "},
"O", "O",
helpers::platform_line(indoc! {"\ indoc! {"\
- top: - top:
#[\n|]# #[\n|]#
baz: foo baz: foo
bax: foox bax: foox
fook: fook:
"}), "},
), ),
( (
helpers::platform_line(indoc! {"\ indoc! {"\
- top: - top:
baz: foo baz: foo
bax: foox bax: foox
fook:#[\n|]# fook:#[\n|]#
"}), "},
"O", "O",
helpers::platform_line(indoc! {"\ indoc! {"\
- top: - top:
baz: foo baz: foo
bax: foox bax: foox
#[\n|]# #[\n|]#
fook: fook:
"}), "},
), ),
( (
helpers::platform_line(indoc! {"\ indoc! {"\
top: top:
baz: baz:
- one: two#[\n|]# - one: two#[\n|]#
@ -734,9 +734,9 @@ async fn auto_indent() -> anyhow::Result<()> {
- top: - top:
baz: foo baz: foo
bax: foox bax: foox
"}), "},
"O", "O",
helpers::platform_line(indoc! {"\ indoc! {"\
top: top:
baz: baz:
#[\n|]# #[\n|]#
@ -745,42 +745,42 @@ async fn auto_indent() -> anyhow::Result<()> {
- top: - top:
baz: foo baz: foo
bax: foox bax: foox
"}), "},
), ),
// yaml map without a key // yaml map without a key
( (
helpers::platform_line(indoc! {"\ indoc! {"\
top:#[\n|]# top:#[\n|]#
"}), "},
"O", "O",
helpers::platform_line(indoc! {"\ indoc! {"\
#[\n|]# #[\n|]#
top: top:
"}), "},
), ),
( (
helpers::platform_line(indoc! {"\ indoc! {"\
bottom: withvalue bottom: withvalue
top#[:|]# top#[:|]#
"}), "},
"O", "O",
helpers::platform_line(indoc! {"\ indoc! {"\
bottom: withvalue bottom: withvalue
#[\n|]# #[\n|]#
top: top:
"}), "},
), ),
( (
helpers::platform_line(indoc! {"\ indoc! {"\
top: top:
bottom:#[ |]#withvalue bottom:#[ |]#withvalue
"}), "},
"O", "O",
helpers::platform_line(indoc! {"\ indoc! {"\
top: top:
#[\n|]# #[\n|]#
bottom: withvalue bottom: withvalue
"}), "},
), ),
]; ];
@ -790,24 +790,24 @@ async fn auto_indent() -> anyhow::Result<()> {
let enter_tests = [ let enter_tests = [
( (
helpers::platform_line(indoc! {r##" indoc! {r##"
foo: #[b|]#ar foo: #[b|]#ar
"##}), "##},
"i<ret>", "i<ret>",
helpers::platform_line(indoc! {"\ indoc! {"\
foo: foo:
#[|b]#ar #[|b]#ar
"}), "},
), ),
( (
helpers::platform_line(indoc! {"\ indoc! {"\
foo:#[\n|]# foo:#[\n|]#
"}), "},
"i<ret>", "i<ret>",
helpers::platform_line(indoc! {"\ indoc! {"\
foo: foo:
#[|\n]# #[|\n]#
"}), "},
), ),
]; ];

@ -8,6 +8,7 @@ async fn insert_mode_cursor_position() -> anyhow::Result<()> {
in_keys: "i".into(), in_keys: "i".into(),
out_text: String::new(), out_text: String::new(),
out_selection: Selection::single(0, 0), out_selection: Selection::single(0, 0),
line_feed_handling: LineFeedHandling::AsIs,
}) })
.await?; .await?;
@ -106,6 +107,14 @@ async fn surround_by_character() -> anyhow::Result<()> {
)) ))
.await?; .await?;
// Selection direction is preserved
test((
"(so [many {go#[|od]#} text] here)",
"mi{",
"(so [many {#[|good]#} text] here)",
))
.await?;
Ok(()) Ok(())
} }
@ -365,6 +374,41 @@ async fn surround_around_pair() -> anyhow::Result<()> {
Ok(()) Ok(())
} }
#[tokio::test(flavor = "multi_thread")]
async fn match_around_closest_ts() -> anyhow::Result<()> {
test_with_config(
AppBuilder::new().with_file("foo.rs", None),
(
r#"fn main() {todo!{"f#[|oo]#)"};}"#,
"mam",
r#"fn main() {todo!{#[|"foo)"]#};}"#,
),
)
.await?;
test_with_config(
AppBuilder::new().with_file("foo.rs", None),
(
r##"fn main() { let _ = ("#[|1]#23", "#(|1)#23"); } "##,
"3mam",
r##"fn main() #[|{ let _ = ("123", "123"); }]# "##,
),
)
.await?;
test_with_config(
AppBuilder::new().with_file("foo.rs", None),
(
r##" fn main() { let _ = ("12#[|3", "12]#3"); } "##,
"1mam",
r##" fn main() { let _ = #[|("123", "123")]#; } "##,
),
)
.await?;
Ok(())
}
/// Ensure the very initial cursor in an opened file is the width of /// Ensure the very initial cursor in an opened file is the width of
/// the first grapheme /// the first grapheme
#[tokio::test(flavor = "multi_thread")] #[tokio::test(flavor = "multi_thread")]
@ -392,20 +436,10 @@ async fn cursor_position_newly_opened_file() -> anyhow::Result<()> {
#[tokio::test(flavor = "multi_thread")] #[tokio::test(flavor = "multi_thread")]
async fn cursor_position_append_eof() -> anyhow::Result<()> { async fn cursor_position_append_eof() -> anyhow::Result<()> {
// Selection is forwards // Selection is forwards
test(( test(("#[foo|]#", "abar<esc>", "#[foobar|]#\n")).await?;
"#[foo|]#",
"abar<esc>",
helpers::platform_line("#[foobar|]#\n"),
))
.await?;
// Selection is backwards // Selection is backwards
test(( test(("#[|foo]#", "abar<esc>", "#[foobar|]#\n")).await?;
"#[|foo]#",
"abar<esc>",
helpers::platform_line("#[foobar|]#\n"),
))
.await?;
Ok(()) Ok(())
} }
@ -415,19 +449,19 @@ async fn select_mode_tree_sitter_next_function_is_union_of_objects() -> anyhow::
test_with_config( test_with_config(
AppBuilder::new().with_file("foo.rs", None), AppBuilder::new().with_file("foo.rs", None),
( (
helpers::platform_line(indoc! {"\ indoc! {"\
#[/|]#// Increments #[/|]#// Increments
fn inc(x: usize) -> usize { x + 1 } fn inc(x: usize) -> usize { x + 1 }
/// Decrements /// Decrements
fn dec(x: usize) -> usize { x - 1 } fn dec(x: usize) -> usize { x - 1 }
"}), "},
"]fv]f", "]fv]f",
helpers::platform_line(indoc! {"\ indoc! {"\
/// Increments /// Increments
#[fn inc(x: usize) -> usize { x + 1 } #[fn inc(x: usize) -> usize { x + 1 }
/// Decrements /// Decrements
fn dec(x: usize) -> usize { x - 1 }|]# fn dec(x: usize) -> usize { x - 1 }|]#
"}), "},
), ),
) )
.await?; .await?;
@ -440,19 +474,19 @@ async fn select_mode_tree_sitter_prev_function_unselects_object() -> anyhow::Res
test_with_config( test_with_config(
AppBuilder::new().with_file("foo.rs", None), AppBuilder::new().with_file("foo.rs", None),
( (
helpers::platform_line(indoc! {"\ indoc! {"\
/// Increments /// Increments
#[fn inc(x: usize) -> usize { x + 1 } #[fn inc(x: usize) -> usize { x + 1 }
/// Decrements /// Decrements
fn dec(x: usize) -> usize { x - 1 }|]# fn dec(x: usize) -> usize { x - 1 }|]#
"}), "},
"v[f", "v[f",
helpers::platform_line(indoc! {"\ indoc! {"\
/// Increments /// Increments
#[fn inc(x: usize) -> usize { x + 1 }|]# #[fn inc(x: usize) -> usize { x + 1 }|]#
/// Decrements /// Decrements
fn dec(x: usize) -> usize { x - 1 } fn dec(x: usize) -> usize { x - 1 }
"}), "},
), ),
) )
.await?; .await?;
@ -466,23 +500,23 @@ async fn select_mode_tree_sitter_prev_function_goes_backwards_to_object() -> any
test_with_config( test_with_config(
AppBuilder::new().with_file("foo.rs", None), AppBuilder::new().with_file("foo.rs", None),
( (
helpers::platform_line(indoc! {"\ indoc! {"\
/// Increments /// Increments
fn inc(x: usize) -> usize { x + 1 } fn inc(x: usize) -> usize { x + 1 }
/// Decrements /// Decrements
fn dec(x: usize) -> usize { x - 1 } fn dec(x: usize) -> usize { x - 1 }
/// Identity /// Identity
#[fn ident(x: usize) -> usize { x }|]# #[fn ident(x: usize) -> usize { x }|]#
"}), "},
"v[f", "v[f",
helpers::platform_line(indoc! {"\ indoc! {"\
/// Increments /// Increments
fn inc(x: usize) -> usize { x + 1 } fn inc(x: usize) -> usize { x + 1 }
/// Decrements /// Decrements
#[|fn dec(x: usize) -> usize { x - 1 } #[|fn dec(x: usize) -> usize { x - 1 }
/// Identity /// Identity
]#fn ident(x: usize) -> usize { x } ]#fn ident(x: usize) -> usize { x }
"}), "},
), ),
) )
.await?; .await?;
@ -490,23 +524,23 @@ async fn select_mode_tree_sitter_prev_function_goes_backwards_to_object() -> any
test_with_config( test_with_config(
AppBuilder::new().with_file("foo.rs", None), AppBuilder::new().with_file("foo.rs", None),
( (
helpers::platform_line(indoc! {"\ indoc! {"\
/// Increments /// Increments
fn inc(x: usize) -> usize { x + 1 } fn inc(x: usize) -> usize { x + 1 }
/// Decrements /// Decrements
fn dec(x: usize) -> usize { x - 1 } fn dec(x: usize) -> usize { x - 1 }
/// Identity /// Identity
#[fn ident(x: usize) -> usize { x }|]# #[fn ident(x: usize) -> usize { x }|]#
"}), "},
"v[f[f", "v[f[f",
helpers::platform_line(indoc! {"\ indoc! {"\
/// Increments /// Increments
#[|fn inc(x: usize) -> usize { x + 1 } #[|fn inc(x: usize) -> usize { x + 1 }
/// Decrements /// Decrements
fn dec(x: usize) -> usize { x - 1 } fn dec(x: usize) -> usize { x - 1 }
/// Identity /// Identity
]#fn ident(x: usize) -> usize { x } ]#fn ident(x: usize) -> usize { x }
"}), "},
), ),
) )
.await?; .await?;
@ -517,38 +551,178 @@ async fn select_mode_tree_sitter_prev_function_goes_backwards_to_object() -> any
#[tokio::test(flavor = "multi_thread")] #[tokio::test(flavor = "multi_thread")]
async fn find_char_line_ending() -> anyhow::Result<()> { async fn find_char_line_ending() -> anyhow::Result<()> {
test(( test((
helpers::platform_line(indoc! { indoc! {
"\ "\
one one
#[|t]#wo #[|t]#wo
three" three"
}), },
"T<ret>gll2f<ret>", "T<ret>gll2f<ret>",
helpers::platform_line(indoc! { indoc! {
"\ "\
one one
two#[ two#[
|]#three" |]#three"
}), },
)) ))
.await?; .await?;
test(( test((
helpers::platform_line(indoc! { indoc! {
"\ "\
#[|o]#ne #[|o]#ne
two two
three" three"
}), },
"f<ret>2t<ret>ghT<ret>F<ret>", "f<ret>2t<ret>ghT<ret>F<ret>",
helpers::platform_line(indoc! { indoc! {
"\ "\
one#[| one#[|
t]#wo t]#wo
three" three"
}), },
))
.await?;
Ok(())
}
#[tokio::test(flavor = "multi_thread")]
async fn test_surround_replace() -> anyhow::Result<()> {
test((
indoc! {"\
(#[|a]#)
"},
"mrm{",
indoc! {"\
{#[|a]#}
"},
))
.await?;
test((
indoc! {"\
(#[a|]#)
"},
"mrm{",
indoc! {"\
{#[a|]#}
"},
))
.await?;
test((
indoc! {"\
{{
#(}|)#
#[}|]#
"},
"mrm)",
indoc! {"\
((
#()|)#
#[)|]#
"},
))
.await?;
Ok(())
}
#[tokio::test(flavor = "multi_thread")]
async fn test_surround_delete() -> anyhow::Result<()> {
test((
indoc! {"\
(#[|a]#)
"},
"mdm",
indoc! {"\
#[|a]#
"},
)) ))
.await?; .await?;
test((
indoc! {"\
(#[a|]#)
"},
"mdm",
indoc! {"\
#[a|]#
"},
))
.await?;
test((
indoc! {"\
{{
#(}|)#
#[}|]#
"},
"mdm",
"\n\n#(\n|)##[\n|]#",
))
.await?;
Ok(())
}
#[tokio::test(flavor = "multi_thread")]
async fn tree_sitter_motions_work_across_injections() -> anyhow::Result<()> {
test_with_config(
AppBuilder::new().with_file("foo.html", None),
(
"<script>let #[|x]# = 1;</script>",
"<A-o>",
"<script>let #[|x = 1]#;</script>",
),
)
.await?;
// When the full injected layer is selected, expand_selection jumps to
// a more shallow layer.
test_with_config(
AppBuilder::new().with_file("foo.html", None),
(
"<script>#[|let x = 1;]#</script>",
"<A-o>",
"#[|<script>let x = 1;</script>]#",
),
)
.await?;
test_with_config(
AppBuilder::new().with_file("foo.html", None),
(
"<script>let #[|x = 1]#;</script>",
"<A-i>",
"<script>let #[|x]# = 1;</script>",
),
)
.await?;
test_with_config(
AppBuilder::new().with_file("foo.html", None),
(
"<script>let #[|x]# = 1;</script>",
"<A-n>",
"<script>let x #[=|]# 1;</script>",
),
)
.await?;
test_with_config(
AppBuilder::new().with_file("foo.html", None),
(
"<script>let #[|x]# = 1;</script>",
"<A-p>",
"<script>#[|let]# x = 1;</script>",
),
)
.await?;
Ok(()) Ok(())
} }

@ -62,9 +62,9 @@ async fn test_split_write_quit_all() -> anyhow::Result<()> {
) )
.await?; .await?;
helpers::assert_file_has_content(file1.as_file_mut(), &platform_line("hello1"))?; helpers::assert_file_has_content(&mut file1, &LineFeedHandling::Native.apply("hello1"))?;
helpers::assert_file_has_content(file2.as_file_mut(), &platform_line("hello2"))?; helpers::assert_file_has_content(&mut file2, &LineFeedHandling::Native.apply("hello2"))?;
helpers::assert_file_has_content(file3.as_file_mut(), &platform_line("hello3"))?; helpers::assert_file_has_content(&mut file3, &LineFeedHandling::Native.apply("hello3"))?;
Ok(()) Ok(())
} }
@ -91,7 +91,7 @@ async fn test_split_write_quit_same_file() -> anyhow::Result<()> {
let doc = docs.pop().unwrap(); let doc = docs.pop().unwrap();
assert_eq!( assert_eq!(
helpers::platform_line("hello\ngoodbye"), LineFeedHandling::Native.apply("hello\ngoodbye"),
doc.text().to_string() doc.text().to_string()
); );
@ -110,7 +110,7 @@ async fn test_split_write_quit_same_file() -> anyhow::Result<()> {
let doc = docs.pop().unwrap(); let doc = docs.pop().unwrap();
assert_eq!( assert_eq!(
helpers::platform_line("hello\ngoodbye"), LineFeedHandling::Native.apply("hello\ngoodbye"),
doc.text().to_string() doc.text().to_string()
); );
@ -122,10 +122,7 @@ async fn test_split_write_quit_same_file() -> anyhow::Result<()> {
) )
.await?; .await?;
helpers::assert_file_has_content( helpers::assert_file_has_content(&mut file, &LineFeedHandling::Native.apply("hello\ngoodbye"))?;
file.as_file_mut(),
&helpers::platform_line("hello\ngoodbye"),
)?;
Ok(()) Ok(())
} }
@ -151,7 +148,13 @@ async fn test_changes_in_splits_apply_to_all_views() -> anyhow::Result<()> {
// //
// This panicked in the past because the jumplist entry on line 2 of window 2 // This panicked in the past because the jumplist entry on line 2 of window 2
// was not updated and after the `kd` step, pointed outside of the document. // was not updated and after the `kd` step, pointed outside of the document.
test(("#[|]#", "<C-w>v[<space><C-s><C-w>wkd<C-w>qd", "#[|]#")).await?; test((
"#[|]#",
"<C-w>v[<space><C-s><C-w>wkd<C-w>qd",
"#[|]#",
LineFeedHandling::AsIs,
))
.await?;
// Transactions are applied to the views for windows lazily when they are focused. // Transactions are applied to the views for windows lazily when they are focused.
// This case panics if the transactions and inversions are not applied in the // This case panics if the transactions and inversions are not applied in the
@ -160,6 +163,7 @@ async fn test_changes_in_splits_apply_to_all_views() -> anyhow::Result<()> {
"#[|]#", "#[|]#",
"[<space>[<space>[<space><C-w>vuuu<C-w>wUUU<C-w>quuu", "[<space>[<space>[<space><C-w>vuuu<C-w>wUUU<C-w>quuu",
"#[|]#", "#[|]#",
LineFeedHandling::AsIs,
)) ))
.await?; .await?;
@ -185,6 +189,7 @@ async fn test_changes_in_splits_apply_to_all_views() -> anyhow::Result<()> {
"#[|]#", "#[|]#",
"3[<space><C-w>v<C-s><C-w>wuu3[<space><C-w>q%d", "3[<space><C-w>v<C-s><C-w>wuu3[<space><C-w>q%d",
"#[|]#", "#[|]#",
LineFeedHandling::AsIs,
)) ))
.await?; .await?;

@ -19,9 +19,9 @@ steel = ["dep:steel-core", "helix-view/steel", "helix-core/steel"]
helix-view = { path = "../helix-view", features = ["term"] } helix-view = { path = "../helix-view", features = ["term"] }
helix-core = { path = "../helix-core" } helix-core = { path = "../helix-core" }
bitflags = "2.4" bitflags = "2.5"
cassowary = "0.3" cassowary = "0.3"
unicode-segmentation = "1.10" unicode-segmentation = "1.11"
crossterm = { version = "0.27", optional = true } crossterm = { version = "0.27", optional = true }
termini = "1.0" termini = "1.0"
serde = { version = "1", "optional" = true, features = ["derive"]} serde = { version = "1", "optional" = true, features = ["derive"]}

@ -52,10 +52,14 @@ impl Default for Capabilities {
impl Capabilities { impl Capabilities {
/// Detect capabilities from the terminfo database located based /// Detect capabilities from the terminfo database located based
/// on the $TERM environment variable. If detection fails, returns /// on the $TERM environment variable. If detection fails, returns
/// a default value where no capability is supported. /// a default value where no capability is supported, or just undercurl
/// if config.undercurl is set.
pub fn from_env_or_default(config: &EditorConfig) -> Self { pub fn from_env_or_default(config: &EditorConfig) -> Self {
match termini::TermInfo::from_env() { match termini::TermInfo::from_env() {
Err(_) => Capabilities::default(), Err(_) => Capabilities {
has_extended_underlines: config.undercurl,
..Capabilities::default()
},
Ok(t) => Capabilities { Ok(t) => Capabilities {
// Smulx, VTE: https://unix.stackexchange.com/a/696253/246284 // Smulx, VTE: https://unix.stackexchange.com/a/696253/246284
// Su (used by kitty): https://sw.kovidgoyal.net/kitty/underlines // Su (used by kitty): https://sw.kovidgoyal.net/kitty/underlines
@ -87,6 +91,10 @@ where
W: Write, W: Write,
{ {
pub fn new(buffer: W, config: &EditorConfig) -> CrosstermBackend<W> { pub fn new(buffer: W, config: &EditorConfig) -> CrosstermBackend<W> {
// helix is not usable without colors, but crossterm will disable
// them by default if NO_COLOR is set in the environment. Override
// this behaviour.
crossterm::style::force_color_output(true);
CrosstermBackend { CrosstermBackend {
buffer, buffer,
capabilities: Capabilities::from_env_or_default(config), capabilities: Capabilities::from_env_or_default(config),

@ -28,15 +28,15 @@ fn get_line_offset(line_width: u16, text_area_width: u16, alignment: Alignment)
/// # use helix_tui::widgets::{Block, Borders, Paragraph, Wrap}; /// # use helix_tui::widgets::{Block, Borders, Paragraph, Wrap};
/// # use helix_tui::layout::{Alignment}; /// # use helix_tui::layout::{Alignment};
/// # use helix_view::graphics::{Style, Color, Modifier}; /// # use helix_view::graphics::{Style, Color, Modifier};
/// let text = vec![ /// let text = Text::from(vec![
/// Spans::from(vec![ /// Spans::from(vec![
/// Span::raw("First"), /// Span::raw("First"),
/// Span::styled("line",Style::default().add_modifier(Modifier::ITALIC)), /// Span::styled("line",Style::default().add_modifier(Modifier::ITALIC)),
/// Span::raw("."), /// Span::raw("."),
/// ]), /// ]),
/// Spans::from(Span::styled("Second line", Style::default().fg(Color::Red))), /// Spans::from(Span::styled("Second line", Style::default().fg(Color::Red))),
/// ]; /// ]);
/// Paragraph::new(text) /// Paragraph::new(&text)
/// .block(Block::default().title("Paragraph").borders(Borders::ALL)) /// .block(Block::default().title("Paragraph").borders(Borders::ALL))
/// .style(Style::default().fg(Color::White).bg(Color::Black)) /// .style(Style::default().fg(Color::White).bg(Color::Black))
/// .alignment(Alignment::Center) /// .alignment(Alignment::Center)
@ -51,7 +51,7 @@ pub struct Paragraph<'a> {
/// How to wrap the text /// How to wrap the text
wrap: Option<Wrap>, wrap: Option<Wrap>,
/// The text to display /// The text to display
text: Text<'a>, text: &'a Text<'a>,
/// Scroll /// Scroll
scroll: (u16, u16), scroll: (u16, u16),
/// Alignment of the text /// Alignment of the text
@ -70,7 +70,7 @@ pub struct Paragraph<'a> {
/// - Here is another point that is long enough to wrap"#); /// - Here is another point that is long enough to wrap"#);
/// ///
/// // With leading spaces trimmed (window width of 30 chars): /// // With leading spaces trimmed (window width of 30 chars):
/// Paragraph::new(bullet_points.clone()).wrap(Wrap { trim: true }); /// Paragraph::new(&bullet_points).wrap(Wrap { trim: true });
/// // Some indented points: /// // Some indented points:
/// // - First thing goes here and is /// // - First thing goes here and is
/// // long so that it wraps /// // long so that it wraps
@ -78,7 +78,7 @@ pub struct Paragraph<'a> {
/// // is long enough to wrap /// // is long enough to wrap
/// ///
/// // But without trimming, indentation is preserved: /// // But without trimming, indentation is preserved:
/// Paragraph::new(bullet_points).wrap(Wrap { trim: false }); /// Paragraph::new(&bullet_points).wrap(Wrap { trim: false });
/// // Some indented points: /// // Some indented points:
/// // - First thing goes here /// // - First thing goes here
/// // and is long so that it wraps /// // and is long so that it wraps
@ -92,15 +92,12 @@ pub struct Wrap {
} }
impl<'a> Paragraph<'a> { impl<'a> Paragraph<'a> {
pub fn new<T>(text: T) -> Paragraph<'a> pub fn new(text: &'a Text) -> Paragraph<'a> {
where
T: Into<Text<'a>>,
{
Paragraph { Paragraph {
block: None, block: None,
style: Default::default(), style: Default::default(),
wrap: None, wrap: None,
text: text.into(), text,
scroll: (0, 0), scroll: (0, 0),
alignment: Alignment::Left, alignment: Alignment::Left,
} }

@ -4,6 +4,7 @@ use helix_core::unicode::width::UnicodeWidthStr;
use unicode_segmentation::UnicodeSegmentation; use unicode_segmentation::UnicodeSegmentation;
const NBSP: &str = "\u{00a0}"; const NBSP: &str = "\u{00a0}";
const NNBSP: &str = "\u{202f}";
/// A state machine to pack styled symbols into lines. /// A state machine to pack styled symbols into lines.
/// Cannot implement it as Iterator since it yields slices of the internal buffer (need streaming /// Cannot implement it as Iterator since it yields slices of the internal buffer (need streaming
@ -58,7 +59,8 @@ impl<'a, 'b> LineComposer<'a> for WordWrapper<'a, 'b> {
let mut symbols_exhausted = true; let mut symbols_exhausted = true;
for StyledGrapheme { symbol, style } in &mut self.symbols { for StyledGrapheme { symbol, style } in &mut self.symbols {
symbols_exhausted = false; symbols_exhausted = false;
let symbol_whitespace = symbol.chars().all(&char::is_whitespace) && symbol != NBSP; let symbol_whitespace =
symbol.chars().all(&char::is_whitespace) && symbol != NBSP && symbol != NNBSP;
// Ignore characters wider that the total max width. // Ignore characters wider that the total max width.
if symbol.width() as u16 > self.max_line_width if symbol.width() as u16 > self.max_line_width
@ -496,6 +498,20 @@ mod test {
assert_eq!(word_wrapper_space, vec!["AAAAAAAAAAAAAAA AAAA", "AAA",]); assert_eq!(word_wrapper_space, vec!["AAAAAAAAAAAAAAA AAAA", "AAA",]);
} }
#[test]
fn line_composer_word_wrapper_nnbsp() {
let width = 20;
let text = "AAAAAAAAAAAAAAA AAAA\u{202f}AAA";
let (word_wrapper, _) = run_composer(Composer::WordWrapper { trim: true }, text, width);
assert_eq!(word_wrapper, vec!["AAAAAAAAAAAAAAA", "AAAA\u{202f}AAA",]);
// Ensure that if the character was a regular space, it would be wrapped differently.
let text_space = text.replace('\u{202f}', " ");
let (word_wrapper_space, _) =
run_composer(Composer::WordWrapper { trim: true }, &text_space, width);
assert_eq!(word_wrapper_space, vec!["AAAAAAAAAAAAAAA AAAA", "AAA",]);
}
#[test] #[test]
fn line_composer_word_wrapper_preserve_indentation() { fn line_composer_word_wrapper_preserve_indentation() {
let width = 20; let width = 20;

@ -17,14 +17,16 @@ fn terminal_buffer_size_should_not_be_limited() {
// let backend = TestBackend::new(10, 10); // let backend = TestBackend::new(10, 10);
// let mut terminal = Terminal::new(backend)?; // let mut terminal = Terminal::new(backend)?;
// let frame = terminal.draw(|f| { // let frame = terminal.draw(|f| {
// let paragraph = Paragraph::new("Test"); // let text = Text::from("Test");
// let paragraph = Paragraph::new(&text);
// f.render_widget(paragraph, f.size()); // f.render_widget(paragraph, f.size());
// })?; // })?;
// assert_eq!(frame.buffer.get(0, 0).symbol, "T"); // assert_eq!(frame.buffer.get(0, 0).symbol, "T");
// assert_eq!(frame.area, Rect::new(0, 0, 10, 10)); // assert_eq!(frame.area, Rect::new(0, 0, 10, 10));
// terminal.backend_mut().resize(8, 8); // terminal.backend_mut().resize(8, 8);
// let frame = terminal.draw(|f| { // let frame = terminal.draw(|f| {
// let paragraph = Paragraph::new("test"); // let text = Text::from("test");
// let paragraph = Paragraph::new(&text);
// f.render_widget(paragraph, f.size()); // f.render_widget(paragraph, f.size());
// })?; // })?;
// assert_eq!(frame.buffer.get(0, 0).symbol, "t"); // assert_eq!(frame.buffer.get(0, 0).symbol, "t");

@ -21,8 +21,8 @@
// terminal // terminal
// .draw(|f| { // .draw(|f| {
// let size = f.size(); // let size = f.size();
// let text = vec![Spans::from(SAMPLE_STRING)]; // let text = Text::from(SAMPLE_STRING);
// let paragraph = Paragraph::new(text) // let paragraph = Paragraph::new(&text)
// .block(Block::default().borders(Borders::ALL)) // .block(Block::default().borders(Borders::ALL))
// .alignment(alignment) // .alignment(alignment)
// .wrap(Wrap { trim: true }); // .wrap(Wrap { trim: true });
@ -88,8 +88,8 @@
// terminal // terminal
// .draw(|f| { // .draw(|f| {
// let size = f.size(); // let size = f.size();
// let text = vec![Spans::from(s)]; // let text = Text::from(s);
// let paragraph = Paragraph::new(text) // let paragraph = Paragraph::new(&text)
// .block(Block::default().borders(Borders::ALL)) // .block(Block::default().borders(Borders::ALL))
// .wrap(Wrap { trim: true }); // .wrap(Wrap { trim: true });
// f.render_widget(paragraph, size); // f.render_widget(paragraph, size);
@ -120,8 +120,8 @@
// terminal // terminal
// .draw(|f| { // .draw(|f| {
// let size = f.size(); // let size = f.size();
// let text = vec![Spans::from(s)]; // let text = Text::from(s);
// let paragraph = Paragraph::new(text) // let paragraph = Paragraph::new(&text)
// .block(Block::default().borders(Borders::ALL)) // .block(Block::default().borders(Borders::ALL))
// .wrap(Wrap { trim: true }); // .wrap(Wrap { trim: true });
// f.render_widget(paragraph, size); // f.render_widget(paragraph, size);
@ -155,8 +155,8 @@
// terminal // terminal
// .draw(|f| { // .draw(|f| {
// let size = f.size(); // let size = f.size();
// let text = Text::from(line);
// let paragraph = Paragraph::new(line).block(Block::default().borders(Borders::ALL)); // let paragraph = Paragraph::new(&text).block(Block::default().borders(Borders::ALL));
// f.render_widget(paragraph, size); // f.render_widget(paragraph, size);
// }) // })
// .unwrap(); // .unwrap();
@ -174,7 +174,7 @@
// let text = Text::from( // let text = Text::from(
// "段落现在可以水平滚动了!\nParagraph can scroll horizontally!\nShort line", // "段落现在可以水平滚动了!\nParagraph can scroll horizontally!\nShort line",
// ); // );
// let paragraph = Paragraph::new(text) // let paragraph = Paragraph::new(&text)
// .block(Block::default().borders(Borders::ALL)) // .block(Block::default().borders(Borders::ALL))
// .alignment(alignment) // .alignment(alignment)
// .scroll(scroll); // .scroll(scroll);

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

Loading…
Cancel
Save