mirror of https://github.com/helix-editor/helix
Merge remote-tracking branch 'origin/master' into goto_next_reference
commit
26a2ef8bc5
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,158 @@
|
|||||||
|
## Building from source
|
||||||
|
|
||||||
|
- [Configuring Helix's runtime files](#configuring-helixs-runtime-files)
|
||||||
|
- [Linux and macOS](#linux-and-macos)
|
||||||
|
- [Windows](#windows)
|
||||||
|
- [Multiple runtime directories](#multiple-runtime-directories)
|
||||||
|
- [Note to packagers](#note-to-packagers)
|
||||||
|
- [Validating the installation](#validating-the-installation)
|
||||||
|
- [Configure the desktop shortcut](#configure-the-desktop-shortcut)
|
||||||
|
|
||||||
|
Requirements:
|
||||||
|
|
||||||
|
Clone the Helix GitHub repository into a directory of your choice. The
|
||||||
|
examples in this documentation assume installation into either `~/src/` on
|
||||||
|
Linux and macOS, or `%userprofile%\src\` on Windows.
|
||||||
|
|
||||||
|
- The [Rust toolchain](https://www.rust-lang.org/tools/install)
|
||||||
|
- The [Git version control system](https://git-scm.com/)
|
||||||
|
- A C++14 compatible compiler to build the tree-sitter grammars, for example GCC or Clang
|
||||||
|
|
||||||
|
If you are using the `musl-libc` standard library instead of `glibc` the following environment variable must be set during the build to ensure tree-sitter grammars can be loaded correctly:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
RUSTFLAGS="-C target-feature=-crt-static"
|
||||||
|
```
|
||||||
|
|
||||||
|
1. Clone the repository:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
git clone https://github.com/helix-editor/helix
|
||||||
|
cd helix
|
||||||
|
```
|
||||||
|
|
||||||
|
2. Compile from source:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
cargo install --path helix-term --locked
|
||||||
|
```
|
||||||
|
|
||||||
|
This command will create the `hx` executable and construct the tree-sitter
|
||||||
|
grammars in the local `runtime` folder.
|
||||||
|
|
||||||
|
> 💡 If you do not want to fetch or build grammars, set an environment variable `HELIX_DISABLE_AUTO_GRAMMAR_BUILD`
|
||||||
|
|
||||||
|
> 💡 Tree-sitter grammars can be fetched and compiled if not pre-packaged. Fetch
|
||||||
|
> grammars with `hx --grammar fetch` and compile them with
|
||||||
|
> `hx --grammar build`. This will install them in
|
||||||
|
> the `runtime` directory within the user's helix config directory (more
|
||||||
|
> [details below](#multiple-runtime-directories)).
|
||||||
|
|
||||||
|
### Configuring Helix's runtime files
|
||||||
|
|
||||||
|
#### Linux and macOS
|
||||||
|
|
||||||
|
The **runtime** directory is one below the Helix source, so either export a
|
||||||
|
`HELIX_RUNTIME` environment variable to point to that directory and add it to
|
||||||
|
your `~/.bashrc` or equivalent:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
export HELIX_RUNTIME=~/src/helix/runtime
|
||||||
|
```
|
||||||
|
|
||||||
|
Or, create a symbolic link:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
ln -Ts $PWD/runtime ~/.config/helix/runtime
|
||||||
|
```
|
||||||
|
|
||||||
|
If the above command fails to create a symbolic link because the file exists either move `~/.config/helix/runtime` to a new location or delete it, then run the symlink command above again.
|
||||||
|
|
||||||
|
#### Windows
|
||||||
|
|
||||||
|
Either set the `HELIX_RUNTIME` environment variable to point to the runtime files using the Windows setting (search for
|
||||||
|
`Edit environment variables for your account`) or use the `setx` command in
|
||||||
|
Cmd:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
setx HELIX_RUNTIME "%userprofile%\source\repos\helix\runtime"
|
||||||
|
```
|
||||||
|
|
||||||
|
> 💡 `%userprofile%` resolves to your user directory like
|
||||||
|
> `C:\Users\Your-Name\` for example.
|
||||||
|
|
||||||
|
Or, create a symlink in `%appdata%\helix\` that links to the source code directory:
|
||||||
|
|
||||||
|
| Method | Command |
|
||||||
|
| ---------- | -------------------------------------------------------------------------------------- |
|
||||||
|
| PowerShell | `New-Item -ItemType Junction -Target "runtime" -Path "$Env:AppData\helix\runtime"` |
|
||||||
|
| Cmd | `cd %appdata%\helix` <br/> `mklink /D runtime "%userprofile%\src\helix\runtime"` |
|
||||||
|
|
||||||
|
> 💡 On Windows, creating a symbolic link may require running PowerShell or
|
||||||
|
> Cmd as an administrator.
|
||||||
|
|
||||||
|
#### Multiple runtime directories
|
||||||
|
|
||||||
|
When Helix finds multiple runtime directories it will search through them for files in the
|
||||||
|
following order:
|
||||||
|
|
||||||
|
1. `runtime/` sibling directory to `$CARGO_MANIFEST_DIR` directory (this is intended for
|
||||||
|
developing and testing helix only).
|
||||||
|
2. `runtime/` subdirectory of OS-dependent helix user config directory.
|
||||||
|
3. `$HELIX_RUNTIME`
|
||||||
|
4. Distribution-specific fallback directory (set at compile time—not run time—
|
||||||
|
with the `HELIX_DEFAULT_RUNTIME` environment variable)
|
||||||
|
5. `runtime/` subdirectory of path to Helix executable.
|
||||||
|
|
||||||
|
This order also sets the priority for selecting which file will be used if multiple runtime
|
||||||
|
directories have files with the same name.
|
||||||
|
|
||||||
|
#### Note to packagers
|
||||||
|
|
||||||
|
If you are making a package of Helix for end users, to provide a good out of
|
||||||
|
the box experience, you should set the `HELIX_DEFAULT_RUNTIME` environment
|
||||||
|
variable at build time (before invoking `cargo build`) to a directory which
|
||||||
|
will store the final runtime files after installation. For example, say you want
|
||||||
|
to package the runtime into `/usr/lib/helix/runtime`. The rough steps a build
|
||||||
|
script could follow are:
|
||||||
|
|
||||||
|
1. `export HELIX_DEFAULT_RUNTIME=/usr/lib/helix/runtime`
|
||||||
|
1. `cargo build --profile opt --locked --path helix-term`
|
||||||
|
1. `cp -r runtime $BUILD_DIR/usr/lib/helix/`
|
||||||
|
1. `cp target/opt/hx $BUILD_DIR/usr/bin/hx`
|
||||||
|
|
||||||
|
This way the resulting `hx` binary will always look for its runtime directory in
|
||||||
|
`/usr/lib/helix/runtime` if the user has no custom runtime in `~/.config/helix`
|
||||||
|
or `HELIX_RUNTIME`.
|
||||||
|
|
||||||
|
### Validating the installation
|
||||||
|
|
||||||
|
To make sure everything is set up as expected you should run the Helix health
|
||||||
|
check:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
hx --health
|
||||||
|
```
|
||||||
|
|
||||||
|
For more information on the health check results refer to
|
||||||
|
[Health check](https://github.com/helix-editor/helix/wiki/Healthcheck).
|
||||||
|
|
||||||
|
### Configure the desktop shortcut
|
||||||
|
|
||||||
|
If your desktop environment supports the
|
||||||
|
[XDG desktop menu](https://specifications.freedesktop.org/menu-spec/menu-spec-latest.html)
|
||||||
|
you can configure Helix to show up in the application menu by copying the
|
||||||
|
provided `.desktop` and icon files to their correct folders:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
cp contrib/Helix.desktop ~/.local/share/applications
|
||||||
|
cp contrib/helix.png ~/.icons # or ~/.local/share/icons
|
||||||
|
```
|
||||||
|
|
||||||
|
To use another terminal than the system default, you can modify the `.desktop`
|
||||||
|
file. For example, to use `kitty`:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
sed -i "s|Exec=hx %F|Exec=kitty hx %F|g" ~/.local/share/applications/Helix.desktop
|
||||||
|
sed -i "s|Terminal=true|Terminal=false|g" ~/.local/share/applications/Helix.desktop
|
||||||
|
```
|
@ -0,0 +1,395 @@
|
|||||||
|
## Editor
|
||||||
|
|
||||||
|
- [`[editor]` Section](#editor-section)
|
||||||
|
- [`[editor.statusline]` Section](#editorstatusline-section)
|
||||||
|
- [`[editor.lsp]` Section](#editorlsp-section)
|
||||||
|
- [`[editor.cursor-shape]` Section](#editorcursor-shape-section)
|
||||||
|
- [`[editor.file-picker]` Section](#editorfile-picker-section)
|
||||||
|
- [`[editor.auto-pairs]` Section](#editorauto-pairs-section)
|
||||||
|
- [`[editor.search]` Section](#editorsearch-section)
|
||||||
|
- [`[editor.whitespace]` Section](#editorwhitespace-section)
|
||||||
|
- [`[editor.indent-guides]` Section](#editorindent-guides-section)
|
||||||
|
- [`[editor.gutters]` Section](#editorgutters-section)
|
||||||
|
- [`[editor.gutters.line-numbers]` Section](#editorguttersline-numbers-section)
|
||||||
|
- [`[editor.gutters.diagnostics]` Section](#editorguttersdiagnostics-section)
|
||||||
|
- [`[editor.gutters.diff]` Section](#editorguttersdiff-section)
|
||||||
|
- [`[editor.gutters.spacer]` Section](#editorguttersspacer-section)
|
||||||
|
- [`[editor.soft-wrap]` Section](#editorsoft-wrap-section)
|
||||||
|
- [`[editor.smart-tab]` Section](#editorsmart-tab-section)
|
||||||
|
|
||||||
|
### `[editor]` Section
|
||||||
|
|
||||||
|
| Key | Description | Default |
|
||||||
|
|--|--|---------|
|
||||||
|
| `scrolloff` | Number of lines of padding around the edge of the screen when scrolling | `5` |
|
||||||
|
| `mouse` | Enable mouse mode | `true` |
|
||||||
|
| `middle-click-paste` | Middle click paste support | `true` |
|
||||||
|
| `scroll-lines` | Number of lines to scroll per scroll wheel step | `3` |
|
||||||
|
| `shell` | Shell to use when running external commands | Unix: `["sh", "-c"]`<br/>Windows: `["cmd", "/C"]` |
|
||||||
|
| `line-number` | Line number display: `absolute` simply shows each line's number, while `relative` shows the distance from the current line. When unfocused or in insert mode, `relative` will still show absolute line numbers | `absolute` |
|
||||||
|
| `cursorline` | Highlight all lines with a cursor | `false` |
|
||||||
|
| `cursorcolumn` | Highlight all columns with a cursor | `false` |
|
||||||
|
| `gutters` | Gutters to display: Available are `diagnostics` and `diff` and `line-numbers` and `spacer`, note that `diagnostics` also includes other features like breakpoints, 1-width padding will be inserted if gutters is non-empty | `["diagnostics", "spacer", "line-numbers", "spacer", "diff"]` |
|
||||||
|
| `auto-completion` | Enable automatic pop up of auto-completion | `true` |
|
||||||
|
| `auto-format` | Enable automatic formatting on save | `true` |
|
||||||
|
| `idle-timeout` | Time in milliseconds since last keypress before idle timers trigger. | `250` |
|
||||||
|
| `completion-timeout` | Time in milliseconds after typing a word character before completions are shown, set to 5 for instant. | `250` |
|
||||||
|
| `preview-completion-insert` | Whether to apply completion item instantly when selected | `true` |
|
||||||
|
| `completion-trigger-len` | The min-length of word under cursor to trigger autocompletion | `2` |
|
||||||
|
| `completion-replace` | Set to `true` to make completions always replace the entire word and not just the part before the cursor | `false` |
|
||||||
|
| `auto-info` | Whether to display info boxes | `true` |
|
||||||
|
| `true-color` | Set to `true` to override automatic detection of terminal truecolor support in the event of a false negative | `false` |
|
||||||
|
| `undercurl` | Set to `true` to override automatic detection of terminal undercurl support in the event of a false negative | `false` |
|
||||||
|
| `rulers` | List of column positions at which to display the rulers. Can be overridden by language specific `rulers` in `languages.toml` file | `[]` |
|
||||||
|
| `bufferline` | Renders a line at the top of the editor displaying open buffers. Can be `always`, `never` or `multiple` (only shown if more than one buffer is in use) | `never` |
|
||||||
|
| `color-modes` | Whether to color the mode indicator with different colors depending on the mode itself | `false` |
|
||||||
|
| `text-width` | Maximum line length. Used for the `:reflow` command and soft-wrapping if `soft-wrap.wrap-at-text-width` is set | `80` |
|
||||||
|
| `workspace-lsp-roots` | Directories relative to the workspace root that are treated as LSP roots. Should only be set in `.helix/config.toml` | `[]` |
|
||||||
|
| `default-line-ending` | The line ending to use for new documents. Can be `native`, `lf`, `crlf`, `ff`, `cr` or `nel`. `native` uses the platform's native line ending (`crlf` on Windows, otherwise `lf`). | `native` |
|
||||||
|
| `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` |
|
||||||
|
| `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
|
||||||
|
|
||||||
|
Allows configuring the statusline at the bottom of the editor.
|
||||||
|
|
||||||
|
The configuration distinguishes between three areas of the status line:
|
||||||
|
|
||||||
|
`[ ... ... LEFT ... ... | ... ... ... CENTER ... ... ... | ... ... RIGHT ... ... ]`
|
||||||
|
|
||||||
|
Statusline elements can be defined as follows:
|
||||||
|
|
||||||
|
```toml
|
||||||
|
[editor.statusline]
|
||||||
|
left = ["mode", "spinner"]
|
||||||
|
center = ["file-name"]
|
||||||
|
right = ["diagnostics", "selections", "position", "file-encoding", "file-line-ending", "file-type"]
|
||||||
|
separator = "│"
|
||||||
|
mode.normal = "NORMAL"
|
||||||
|
mode.insert = "INSERT"
|
||||||
|
mode.select = "SELECT"
|
||||||
|
```
|
||||||
|
The `[editor.statusline]` key takes the following sub-keys:
|
||||||
|
|
||||||
|
| Key | Description | Default |
|
||||||
|
| --- | --- | --- |
|
||||||
|
| `left` | A list of elements aligned to the left of the statusline | `["mode", "spinner", "file-name", "read-only-indicator", "file-modification-indicator"]` |
|
||||||
|
| `center` | A list of elements aligned to the middle of the statusline | `[]` |
|
||||||
|
| `right` | A list of elements aligned to the right of the statusline | `["diagnostics", "selections", "register", "position", "file-encoding"]` |
|
||||||
|
| `separator` | The character used to separate elements in the statusline | `"│"` |
|
||||||
|
| `mode.normal` | The text shown in the `mode` element for normal mode | `"NOR"` |
|
||||||
|
| `mode.insert` | The text shown in the `mode` element for insert mode | `"INS"` |
|
||||||
|
| `mode.select` | The text shown in the `mode` element for select mode | `"SEL"` |
|
||||||
|
|
||||||
|
The following statusline elements can be configured:
|
||||||
|
|
||||||
|
| Key | Description |
|
||||||
|
| ------ | ----------- |
|
||||||
|
| `mode` | The current editor mode (`mode.normal`/`mode.insert`/`mode.select`) |
|
||||||
|
| `spinner` | A progress spinner indicating LSP activity |
|
||||||
|
| `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-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-line-ending` | The file line endings (CRLF or LF) |
|
||||||
|
| `read-only-indicator` | An indicator that shows `[readonly]` when a file cannot be written |
|
||||||
|
| `total-line-numbers` | The total line numbers of the opened file |
|
||||||
|
| `file-type` | The type of the opened file |
|
||||||
|
| `diagnostics` | The number of warnings and/or errors |
|
||||||
|
| `workspace-diagnostics` | The number of warnings and/or errors on workspace |
|
||||||
|
| `selections` | The number of active selections |
|
||||||
|
| `primary-selection-length` | The number of characters currently in primary selection |
|
||||||
|
| `position` | The cursor position |
|
||||||
|
| `position-percentage` | The cursor position as a percentage of the total number of lines |
|
||||||
|
| `separator` | The string defined in `editor.statusline.separator` (defaults to `"│"`) |
|
||||||
|
| `spacer` | Inserts a space between elements (multiple/contiguous spacers may be specified) |
|
||||||
|
| `version-control` | The current branch name or detached commit hash of the opened workspace |
|
||||||
|
| `register` | The current selected register |
|
||||||
|
|
||||||
|
### `[editor.lsp]` Section
|
||||||
|
|
||||||
|
| Key | Description | Default |
|
||||||
|
| --- | ----------- | ------- |
|
||||||
|
| `enable` | Enables LSP integration. Setting to false will completely disable language servers regardless of language settings.| `true` |
|
||||||
|
| `display-messages` | Display LSP progress messages below statusline[^1] | `false` |
|
||||||
|
| `auto-signature-help` | Enable automatic popup of signature help (parameter hints) | `true` |
|
||||||
|
| `display-inlay-hints` | Display inlay hints[^2] | `false` |
|
||||||
|
| `display-signature-help-docs` | Display docs under signature help popup | `true` |
|
||||||
|
| `snippets` | Enables snippet completions. Requires a server restart (`:lsp-restart`) to take effect after `:config-reload`/`:set`. | `true` |
|
||||||
|
| `goto-reference-include-declaration` | Include declaration in the goto references popup. | `true` |
|
||||||
|
|
||||||
|
[^1]: By default, a progress spinner is shown in the statusline beside the file path.
|
||||||
|
|
||||||
|
[^2]: You may also have to activate them in the LSP config for them to appear, not just in Helix. Inlay hints in Helix are still being improved on and may be a little bit laggy/janky under some circumstances. Please report any bugs you see so we can fix them!
|
||||||
|
|
||||||
|
### `[editor.cursor-shape]` Section
|
||||||
|
|
||||||
|
Defines the shape of cursor in each mode.
|
||||||
|
Valid values for these options are `block`, `bar`, `underline`, or `hidden`.
|
||||||
|
|
||||||
|
> 💡 Due to limitations of the terminal environment, only the primary cursor can
|
||||||
|
> change shape.
|
||||||
|
|
||||||
|
| Key | Description | Default |
|
||||||
|
| --- | ----------- | ------- |
|
||||||
|
| `normal` | Cursor shape in [normal mode][normal mode] | `block` |
|
||||||
|
| `insert` | Cursor shape in [insert mode][insert mode] | `block` |
|
||||||
|
| `select` | Cursor shape in [select mode][select mode] | `block` |
|
||||||
|
|
||||||
|
[normal mode]: ./keymap.md#normal-mode
|
||||||
|
[insert mode]: ./keymap.md#insert-mode
|
||||||
|
[select mode]: ./keymap.md#select--extend-mode
|
||||||
|
|
||||||
|
### `[editor.file-picker]` Section
|
||||||
|
|
||||||
|
Set options for file picker and global search. Ignoring a file means it is
|
||||||
|
not visible in the Helix file picker and global search.
|
||||||
|
|
||||||
|
All git related options are only enabled in a git repository.
|
||||||
|
|
||||||
|
| Key | Description | Default |
|
||||||
|
|--|--|---------|
|
||||||
|
|`hidden` | Enables ignoring hidden files | `true`
|
||||||
|
|`follow-symlinks` | Follow symlinks instead of ignoring them | `true`
|
||||||
|
|`deduplicate-links` | Ignore symlinks that point at files already shown in the picker | `true`
|
||||||
|
|`parents` | Enables reading ignore files from parent directories | `true`
|
||||||
|
|`ignore` | Enables reading `.ignore` 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-exclude` | Enables reading `.git/info/exclude` files | `true`
|
||||||
|
|`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.
|
||||||
|
|
||||||
|
Additionally, you can use Helix-specific ignore files by creating a local `.helix/ignore` file in the current workspace or a global `ignore` file located in your Helix config directory:
|
||||||
|
- Linux and Mac: `~/.config/helix/ignore`
|
||||||
|
- Windows: `%AppData%\helix\ignore`
|
||||||
|
|
||||||
|
Example:
|
||||||
|
|
||||||
|
```ini
|
||||||
|
# unignore in file picker and global search
|
||||||
|
!.github/
|
||||||
|
!.gitignore
|
||||||
|
!.gitattributes
|
||||||
|
```
|
||||||
|
|
||||||
|
### `[editor.auto-pairs]` Section
|
||||||
|
|
||||||
|
Enables automatic insertion of pairs to parentheses, brackets, etc. Can be a
|
||||||
|
simple boolean value, or a specific mapping of pairs of single characters.
|
||||||
|
|
||||||
|
To disable auto-pairs altogether, set `auto-pairs` to `false`:
|
||||||
|
|
||||||
|
```toml
|
||||||
|
[editor]
|
||||||
|
auto-pairs = false # defaults to `true`
|
||||||
|
```
|
||||||
|
|
||||||
|
The default pairs are <code>(){}[]''""``</code>, but these can be customized by
|
||||||
|
setting `auto-pairs` to a TOML table:
|
||||||
|
|
||||||
|
```toml
|
||||||
|
[editor.auto-pairs]
|
||||||
|
'(' = ')'
|
||||||
|
'{' = '}'
|
||||||
|
'[' = ']'
|
||||||
|
'"' = '"'
|
||||||
|
'`' = '`'
|
||||||
|
'<' = '>'
|
||||||
|
```
|
||||||
|
|
||||||
|
Additionally, this setting can be used in a language config. Unless
|
||||||
|
the editor setting is `false`, this will override the editor config in
|
||||||
|
documents with this language.
|
||||||
|
|
||||||
|
Example `languages.toml` that adds `<>` and removes `''`
|
||||||
|
|
||||||
|
```toml
|
||||||
|
[[language]]
|
||||||
|
name = "rust"
|
||||||
|
|
||||||
|
[language.auto-pairs]
|
||||||
|
'(' = ')'
|
||||||
|
'{' = '}'
|
||||||
|
'[' = ']'
|
||||||
|
'"' = '"'
|
||||||
|
'`' = '`'
|
||||||
|
'<' = '>'
|
||||||
|
```
|
||||||
|
|
||||||
|
### `[editor.auto-save]` Section
|
||||||
|
|
||||||
|
Control auto save behavior.
|
||||||
|
|
||||||
|
| Key | Description | Default |
|
||||||
|
|--|--|---------|
|
||||||
|
| `focus-lost` | Enable automatic saving on the focus moving away from Helix. Requires [focus event support](https://github.com/helix-editor/helix/wiki/Terminal-Support) from your terminal | `false` |
|
||||||
|
| `after-delay.enable` | Enable automatic saving after `auto-save.after-delay.timeout` milliseconds have passed since last edit. | `false` |
|
||||||
|
| `after-delay.timeout` | Time in milliseconds since last edit before auto save timer triggers. | `3000` |
|
||||||
|
|
||||||
|
### `[editor.search]` Section
|
||||||
|
|
||||||
|
Search specific options.
|
||||||
|
|
||||||
|
| Key | Description | Default |
|
||||||
|
|--|--|---------|
|
||||||
|
| `smart-case` | Enable smart case regex searching (case-insensitive unless pattern contains upper case characters) | `true` |
|
||||||
|
| `wrap-around`| Whether the search should wrap after depleting the matches | `true` |
|
||||||
|
|
||||||
|
### `[editor.whitespace]` Section
|
||||||
|
|
||||||
|
Options for rendering whitespace with visible characters. Use `:set whitespace.render all` to temporarily enable visible whitespace.
|
||||||
|
|
||||||
|
| Key | Description | Default |
|
||||||
|
|-----|-------------|---------|
|
||||||
|
| `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`, `nnbsp`, `newline` or `tabpad` | See example below |
|
||||||
|
|
||||||
|
Example
|
||||||
|
|
||||||
|
```toml
|
||||||
|
[editor.whitespace]
|
||||||
|
render = "all"
|
||||||
|
# or control each character
|
||||||
|
[editor.whitespace.render]
|
||||||
|
space = "all"
|
||||||
|
tab = "all"
|
||||||
|
nbsp = "none"
|
||||||
|
nnbsp = "none"
|
||||||
|
newline = "none"
|
||||||
|
|
||||||
|
[editor.whitespace.characters]
|
||||||
|
space = "·"
|
||||||
|
nbsp = "⍽"
|
||||||
|
nnbsp = "␣"
|
||||||
|
tab = "→"
|
||||||
|
newline = "⏎"
|
||||||
|
tabpad = "·" # Tabs will look like "→···" (depending on tab width)
|
||||||
|
```
|
||||||
|
|
||||||
|
### `[editor.indent-guides]` Section
|
||||||
|
|
||||||
|
Options for rendering vertical indent guides.
|
||||||
|
|
||||||
|
| Key | Description | Default |
|
||||||
|
| --- | --- | --- |
|
||||||
|
| `render` | Whether to render indent guides | `false` |
|
||||||
|
| `character` | Literal character to use for rendering the indent guide | `│` |
|
||||||
|
| `skip-levels` | Number of indent levels to skip | `0` |
|
||||||
|
|
||||||
|
Example:
|
||||||
|
|
||||||
|
```toml
|
||||||
|
[editor.indent-guides]
|
||||||
|
render = true
|
||||||
|
character = "╎" # Some characters that work well: "▏", "┆", "┊", "⸽"
|
||||||
|
skip-levels = 1
|
||||||
|
```
|
||||||
|
|
||||||
|
### `[editor.gutters]` Section
|
||||||
|
|
||||||
|
For simplicity, `editor.gutters` accepts an array of gutter types, which will
|
||||||
|
use default settings for all gutter components.
|
||||||
|
|
||||||
|
```toml
|
||||||
|
[editor]
|
||||||
|
gutters = ["diff", "diagnostics", "line-numbers", "spacer"]
|
||||||
|
```
|
||||||
|
|
||||||
|
To customize the behavior of gutters, the `[editor.gutters]` section must
|
||||||
|
be used. This section contains top level settings, as well as settings for
|
||||||
|
specific gutter components as subsections.
|
||||||
|
|
||||||
|
| Key | Description | Default |
|
||||||
|
| --- | --- | --- |
|
||||||
|
| `layout` | A vector of gutters to display | `["diagnostics", "spacer", "line-numbers", "spacer", "diff"]` |
|
||||||
|
|
||||||
|
Example:
|
||||||
|
|
||||||
|
```toml
|
||||||
|
[editor.gutters]
|
||||||
|
layout = ["diff", "diagnostics", "line-numbers", "spacer"]
|
||||||
|
```
|
||||||
|
|
||||||
|
#### `[editor.gutters.line-numbers]` Section
|
||||||
|
|
||||||
|
Options for the line number gutter
|
||||||
|
|
||||||
|
| Key | Description | Default |
|
||||||
|
| --- | --- | --- |
|
||||||
|
| `min-width` | The minimum number of characters to use | `3` |
|
||||||
|
|
||||||
|
Example:
|
||||||
|
|
||||||
|
```toml
|
||||||
|
[editor.gutters.line-numbers]
|
||||||
|
min-width = 1
|
||||||
|
```
|
||||||
|
|
||||||
|
#### `[editor.gutters.diagnostics]` Section
|
||||||
|
|
||||||
|
Currently unused
|
||||||
|
|
||||||
|
#### `[editor.gutters.diff]` Section
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
Currently unused
|
||||||
|
|
||||||
|
### `[editor.soft-wrap]` Section
|
||||||
|
|
||||||
|
Options for soft wrapping lines that exceed the view width:
|
||||||
|
|
||||||
|
| Key | Description | Default |
|
||||||
|
| --- | --- | --- |
|
||||||
|
| `enable` | Whether soft wrapping is enabled. | `false` |
|
||||||
|
| `max-wrap` | Maximum free space left at the end of the line. | `20` |
|
||||||
|
| `max-indent-retain` | Maximum indentation to carry over when soft wrapping a line. | `40` |
|
||||||
|
| `wrap-indicator` | Text inserted before soft wrapped lines, highlighted with `ui.virtual.wrap` | `↪ ` |
|
||||||
|
| `wrap-at-text-width` | Soft wrap at `text-width` instead of using the full viewport size. | `false` |
|
||||||
|
|
||||||
|
Example:
|
||||||
|
|
||||||
|
```toml
|
||||||
|
[editor.soft-wrap]
|
||||||
|
enable = true
|
||||||
|
max-wrap = 25 # increase value to reduce forced mid-word wrapping
|
||||||
|
max-indent-retain = 0
|
||||||
|
wrap-indicator = "" # set wrap-indicator to "" to hide it
|
||||||
|
```
|
||||||
|
|
||||||
|
### `[editor.smart-tab]` Section
|
||||||
|
|
||||||
|
Options for navigating and editing using tab key.
|
||||||
|
|
||||||
|
| 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` |
|
||||||
|
| `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"
|
||||||
|
```
|
@ -0,0 +1,150 @@
|
|||||||
|
## Package managers
|
||||||
|
|
||||||
|
- [Linux](#linux)
|
||||||
|
- [Ubuntu](#ubuntu)
|
||||||
|
- [Fedora/RHEL](#fedorarhel)
|
||||||
|
- [Arch Linux extra](#arch-linux-extra)
|
||||||
|
- [NixOS](#nixos)
|
||||||
|
- [Flatpak](#flatpak)
|
||||||
|
- [Snap](#snap)
|
||||||
|
- [AppImage](#appimage)
|
||||||
|
- [macOS](#macos)
|
||||||
|
- [Homebrew Core](#homebrew-core)
|
||||||
|
- [MacPorts](#macports)
|
||||||
|
- [Windows](#windows)
|
||||||
|
- [Winget](#winget)
|
||||||
|
- [Scoop](#scoop)
|
||||||
|
- [Chocolatey](#chocolatey)
|
||||||
|
- [MSYS2](#msys2)
|
||||||
|
|
||||||
|
[![Packaging status](https://repology.org/badge/vertical-allrepos/helix.svg)](https://repology.org/project/helix/versions)
|
||||||
|
|
||||||
|
## Linux
|
||||||
|
|
||||||
|
The following third party repositories are available:
|
||||||
|
|
||||||
|
### Ubuntu
|
||||||
|
|
||||||
|
Add the `PPA` for Helix:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
sudo add-apt-repository ppa:maveonair/helix-editor
|
||||||
|
sudo apt update
|
||||||
|
sudo apt install helix
|
||||||
|
```
|
||||||
|
|
||||||
|
### Fedora/RHEL
|
||||||
|
|
||||||
|
```sh
|
||||||
|
sudo dnf install helix
|
||||||
|
```
|
||||||
|
|
||||||
|
### Arch Linux extra
|
||||||
|
|
||||||
|
Releases are available in the `extra` repository:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
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
|
||||||
|
in the AUR, which builds the master branch.
|
||||||
|
|
||||||
|
### NixOS
|
||||||
|
|
||||||
|
Helix is available in [nixpkgs](https://github.com/nixos/nixpkgs) through the `helix` attribute,
|
||||||
|
the unstable channel usually carries the latest release.
|
||||||
|
|
||||||
|
Helix is also available as a [flake](https://wiki.nixos.org/wiki/Flakes) in the project
|
||||||
|
root. Use `nix develop` to spin up a reproducible development shell. Outputs are
|
||||||
|
cached for each push to master using [Cachix](https://www.cachix.org/). The
|
||||||
|
flake is configured to automatically make use of this cache assuming the user
|
||||||
|
accepts the new settings on first use.
|
||||||
|
|
||||||
|
If you are using a version of Nix without flakes enabled,
|
||||||
|
[install Cachix CLI](https://docs.cachix.org/installation) and use
|
||||||
|
`cachix use helix` to configure Nix to use cached outputs when possible.
|
||||||
|
|
||||||
|
### Flatpak
|
||||||
|
|
||||||
|
Helix is available on [Flathub](https://flathub.org/en-GB/apps/com.helix_editor.Helix):
|
||||||
|
|
||||||
|
```sh
|
||||||
|
flatpak install flathub com.helix_editor.Helix
|
||||||
|
flatpak run com.helix_editor.Helix
|
||||||
|
```
|
||||||
|
|
||||||
|
### Snap
|
||||||
|
|
||||||
|
Helix is available on [Snapcraft](https://snapcraft.io/helix) and can be installed with:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
snap install --classic helix
|
||||||
|
```
|
||||||
|
|
||||||
|
This will install Helix as both `/snap/bin/helix` and `/snap/bin/hx`, so make sure `/snap/bin` is in your `PATH`.
|
||||||
|
|
||||||
|
### AppImage
|
||||||
|
|
||||||
|
Install Helix using the Linux [AppImage](https://appimage.org/) format.
|
||||||
|
Download the official Helix AppImage from the [latest releases](https://github.com/helix-editor/helix/releases/latest) page.
|
||||||
|
|
||||||
|
```sh
|
||||||
|
chmod +x helix-*.AppImage # change permission for executable mode
|
||||||
|
./helix-*.AppImage # run helix
|
||||||
|
```
|
||||||
|
|
||||||
|
## macOS
|
||||||
|
|
||||||
|
### Homebrew Core
|
||||||
|
|
||||||
|
```sh
|
||||||
|
brew install helix
|
||||||
|
```
|
||||||
|
|
||||||
|
### MacPorts
|
||||||
|
|
||||||
|
```sh
|
||||||
|
port install helix
|
||||||
|
```
|
||||||
|
|
||||||
|
## Windows
|
||||||
|
|
||||||
|
Install on Windows using [Winget](https://learn.microsoft.com/en-us/windows/package-manager/winget/), [Scoop](https://scoop.sh/), [Chocolatey](https://chocolatey.org/)
|
||||||
|
or [MSYS2](https://msys2.org/).
|
||||||
|
|
||||||
|
### Winget
|
||||||
|
Windows Package Manager winget command-line tool is by default available on Windows 11 and modern versions of Windows 10 as a part of the App Installer.
|
||||||
|
You can get [App Installer from the Microsoft Store](https://www.microsoft.com/p/app-installer/9nblggh4nns1#activetab=pivot:overviewtab). If it's already installed, make sure it is updated with the latest version.
|
||||||
|
|
||||||
|
```sh
|
||||||
|
winget install Helix.Helix
|
||||||
|
```
|
||||||
|
|
||||||
|
### Scoop
|
||||||
|
|
||||||
|
```sh
|
||||||
|
scoop install helix
|
||||||
|
```
|
||||||
|
|
||||||
|
### Chocolatey
|
||||||
|
|
||||||
|
```sh
|
||||||
|
choco install helix
|
||||||
|
```
|
||||||
|
|
||||||
|
### MSYS2
|
||||||
|
|
||||||
|
For 64-bit Windows 8.1 or above:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
pacman -S mingw-w64-ucrt-x86_64-helix
|
||||||
|
```
|
@ -0,0 +1,54 @@
|
|||||||
|
## Registers
|
||||||
|
|
||||||
|
- [User-defined registers](#user-defined-registers)
|
||||||
|
- [Default registers](#default-registers)
|
||||||
|
- [Special registers](#special-registers)
|
||||||
|
|
||||||
|
In Helix, registers are storage locations for text and other data, such as the
|
||||||
|
result of a search. Registers can be used to cut, copy, and paste text, similar
|
||||||
|
to the clipboard in other text editors. Usage is similar to Vim, with `"` being
|
||||||
|
used to select a register.
|
||||||
|
|
||||||
|
### User-defined registers
|
||||||
|
|
||||||
|
Helix allows you to create your own named registers for storing text, for
|
||||||
|
example:
|
||||||
|
|
||||||
|
- `"ay` - Yank the current selection to register `a`.
|
||||||
|
- `"op` - Paste the text in register `o` after the selection.
|
||||||
|
|
||||||
|
If a register is selected before invoking a change or delete command, the selection will be stored in the register and the action will be carried out:
|
||||||
|
|
||||||
|
- `"hc` - Store the selection in register `h` and then change it (delete and enter insert mode).
|
||||||
|
- `"md` - Store the selection in register `m` and delete it.
|
||||||
|
|
||||||
|
### Default registers
|
||||||
|
|
||||||
|
Commands that use registers, like yank (`y`), use a default register if none is specified.
|
||||||
|
These registers are used as defaults:
|
||||||
|
|
||||||
|
| Register character | Contains |
|
||||||
|
| --- | --- |
|
||||||
|
| `/` | Last search |
|
||||||
|
| `:` | Last executed command |
|
||||||
|
| `"` | Last yanked text |
|
||||||
|
| `@` | Last recorded macro |
|
||||||
|
|
||||||
|
### Special registers
|
||||||
|
|
||||||
|
Some registers have special behavior when read from and written to.
|
||||||
|
|
||||||
|
| Register character | When read | When written |
|
||||||
|
| --- | --- | --- |
|
||||||
|
| `_` | No values are returned | All values are discarded |
|
||||||
|
| `#` | Selection indices (first selection is `1`, second is `2`, etc.) | This register is not writable |
|
||||||
|
| `.` | Contents of the current selections | This register is not writable |
|
||||||
|
| `%` | Name of the current file | This register is not writable |
|
||||||
|
| `+` | Reads from the system clipboard | Joins and yanks to the system clipboard |
|
||||||
|
| `*` | Reads from the primary clipboard | Joins and yanks to the primary clipboard |
|
||||||
|
|
||||||
|
When yanking multiple selections to the clipboard registers, the selections
|
||||||
|
are joined with newlines. Pasting from these registers will paste multiple
|
||||||
|
selections if the clipboard was last yanked to by the Helix session. Otherwise
|
||||||
|
the clipboard contents are pasted as one selection.
|
||||||
|
|
@ -0,0 +1,24 @@
|
|||||||
|
## Surround
|
||||||
|
|
||||||
|
Helix includes built-in functionality similar to [vim-surround](https://github.com/tpope/vim-surround).
|
||||||
|
The keymappings have been inspired from [vim-sandwich](https://github.com/machakann/vim-sandwich):
|
||||||
|
|
||||||
|
![Surround demo](https://user-images.githubusercontent.com/23398472/122865801-97073180-d344-11eb-8142-8f43809982c6.gif)
|
||||||
|
|
||||||
|
| Key Sequence | Action |
|
||||||
|
| --------------------------------- | --------------------------------------- |
|
||||||
|
| `ms<char>` (after selecting text) | Add surround characters to selection |
|
||||||
|
| `mr<char_to_replace><new_char>` | Replace the closest surround characters |
|
||||||
|
| `md<char_to_delete>` | Delete the closest surround characters |
|
||||||
|
|
||||||
|
You can use counts to act on outer pairs.
|
||||||
|
|
||||||
|
Surround can also act on multiple selections. For example, to change every occurrence of `(use)` to `[use]`:
|
||||||
|
|
||||||
|
1. `%` to select the whole file
|
||||||
|
2. `s` to split the selections on a search term
|
||||||
|
3. Input `use` and hit Enter
|
||||||
|
4. `mr([` to replace the parentheses with square brackets
|
||||||
|
|
||||||
|
Multiple characters are currently not supported, but planned for future release.
|
||||||
|
|
@ -0,0 +1,66 @@
|
|||||||
|
## Moving the selection with syntax-aware motions
|
||||||
|
|
||||||
|
`Alt-p`, `Alt-o`, `Alt-i`, and `Alt-n` (or `Alt` and arrow keys) allow you to move the
|
||||||
|
selection according to its location in the syntax tree. For example, many languages have the
|
||||||
|
following syntax for function calls:
|
||||||
|
|
||||||
|
```js
|
||||||
|
func(arg1, arg2, arg3);
|
||||||
|
```
|
||||||
|
|
||||||
|
A function call might be parsed by tree-sitter into a tree like the following.
|
||||||
|
|
||||||
|
```tsq
|
||||||
|
(call
|
||||||
|
function: (identifier) ; func
|
||||||
|
arguments:
|
||||||
|
(arguments ; (arg1, arg2, arg3)
|
||||||
|
(identifier) ; arg1
|
||||||
|
(identifier) ; arg2
|
||||||
|
(identifier))) ; arg3
|
||||||
|
```
|
||||||
|
|
||||||
|
Use `:tree-sitter-subtree` to view the syntax tree of the primary selection. In
|
||||||
|
a more intuitive tree format:
|
||||||
|
|
||||||
|
```
|
||||||
|
┌────┐
|
||||||
|
│call│
|
||||||
|
┌─────┴────┴─────┐
|
||||||
|
│ │
|
||||||
|
┌─────▼────┐ ┌────▼────┐
|
||||||
|
│identifier│ │arguments│
|
||||||
|
│ "func" │ ┌────┴───┬─────┴───┐
|
||||||
|
└──────────┘ │ │ │
|
||||||
|
│ │ │
|
||||||
|
┌─────────▼┐ ┌────▼─────┐ ┌▼─────────┐
|
||||||
|
│identifier│ │identifier│ │identifier│
|
||||||
|
│ "arg1" │ │ "arg2" │ │ "arg3" │
|
||||||
|
└──────────┘ └──────────┘ └──────────┘
|
||||||
|
```
|
||||||
|
|
||||||
|
If you have a selection that wraps `arg1` (see the tree above), and you use
|
||||||
|
`Alt-n`, it will select the next sibling in the syntax tree: `arg2`.
|
||||||
|
|
||||||
|
```js
|
||||||
|
// before
|
||||||
|
func([arg1], arg2, arg3)
|
||||||
|
// after
|
||||||
|
func(arg1, [arg2], arg3);
|
||||||
|
```
|
||||||
|
|
||||||
|
Similarly, `Alt-o` will expand the selection to the parent node, in this case, the
|
||||||
|
arguments node.
|
||||||
|
|
||||||
|
```js
|
||||||
|
func[(arg1, arg2, arg3)];
|
||||||
|
```
|
||||||
|
|
||||||
|
There is also some nuanced behavior that prevents you from getting stuck on a
|
||||||
|
node with no sibling. When using `Alt-p` with a selection on `arg1`, the previous
|
||||||
|
child node will be selected. In the event that `arg1` does not have a previous
|
||||||
|
sibling, the selection will move up the syntax tree and select the previous
|
||||||
|
element. As a result, using `Alt-p` with a selection on `arg1` will move the
|
||||||
|
selection to the "func" `identifier`.
|
||||||
|
|
||||||
|
[lang-support]: ./lang-support.md
|
@ -0,0 +1,47 @@
|
|||||||
|
## Selecting and manipulating text with textobjects
|
||||||
|
|
||||||
|
In Helix, textobjects are a way to select, manipulate and operate on a piece of
|
||||||
|
text in a structured way. They allow you to refer to blocks of text based on
|
||||||
|
their structure or purpose, such as a word, sentence, paragraph, or even a
|
||||||
|
function or block of code.
|
||||||
|
|
||||||
|
![Textobject demo](https://user-images.githubusercontent.com/23398472/124231131-81a4bb00-db2d-11eb-9d10-8e577ca7b177.gif)
|
||||||
|
![Textobject tree-sitter demo](https://user-images.githubusercontent.com/23398472/132537398-2a2e0a54-582b-44ab-a77f-eb818942203d.gif)
|
||||||
|
|
||||||
|
- `ma` - Select around the object (`va` in Vim, `<alt-a>` in Kakoune)
|
||||||
|
- `mi` - Select inside the object (`vi` in Vim, `<alt-i>` in Kakoune)
|
||||||
|
|
||||||
|
| Key after `mi` or `ma` | Textobject selected |
|
||||||
|
| --- | --- |
|
||||||
|
| `w` | Word |
|
||||||
|
| `W` | WORD |
|
||||||
|
| `p` | Paragraph |
|
||||||
|
| `(`, `[`, `'`, etc. | Specified surround pairs |
|
||||||
|
| `m` | The closest surround pair |
|
||||||
|
| `f` | Function |
|
||||||
|
| `t` | Type (or Class) |
|
||||||
|
| `a` | Argument/parameter |
|
||||||
|
| `c` | Comment |
|
||||||
|
| `T` | Test |
|
||||||
|
| `g` | Change |
|
||||||
|
|
||||||
|
> 💡 `f`, `t`, etc. need a tree-sitter grammar active for the current
|
||||||
|
document and a special tree-sitter query file to work properly. [Only
|
||||||
|
some grammars](./lang-support.md) currently have the query file implemented.
|
||||||
|
Contributions are welcome!
|
||||||
|
|
||||||
|
## Navigating using tree-sitter textobjects
|
||||||
|
|
||||||
|
Navigating between functions, classes, parameters, and other elements is
|
||||||
|
possible using tree-sitter and textobject queries. For
|
||||||
|
example to move to the next function use `]f`, to move to previous
|
||||||
|
type use `[t`, and so on.
|
||||||
|
|
||||||
|
![Tree-sitter-nav-demo](https://user-images.githubusercontent.com/23398472/152332550-7dfff043-36a2-4aec-b8f2-77c13eb56d6f.gif)
|
||||||
|
|
||||||
|
For the full reference see the [unimpaired](./keymap.html#unimpaired) section of the key bind
|
||||||
|
documentation.
|
||||||
|
|
||||||
|
> 💡 This feature relies on tree-sitter textobjects
|
||||||
|
> and requires the corresponding query file to work properly.
|
||||||
|
|
@ -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)
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -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())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -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;
|
||||||
|
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,117 @@
|
|||||||
|
use std::{
|
||||||
|
sync::{
|
||||||
|
atomic::{self, AtomicBool},
|
||||||
|
Arc,
|
||||||
|
},
|
||||||
|
time::Duration,
|
||||||
|
};
|
||||||
|
|
||||||
|
use anyhow::Ok;
|
||||||
|
use arc_swap::access::Access;
|
||||||
|
|
||||||
|
use helix_event::{register_hook, send_blocking};
|
||||||
|
use helix_view::{
|
||||||
|
document::Mode,
|
||||||
|
events::DocumentDidChange,
|
||||||
|
handlers::{AutoSaveEvent, Handlers},
|
||||||
|
Editor,
|
||||||
|
};
|
||||||
|
use tokio::time::Instant;
|
||||||
|
|
||||||
|
use crate::{
|
||||||
|
commands, compositor,
|
||||||
|
events::OnModeSwitch,
|
||||||
|
job::{self, Jobs},
|
||||||
|
};
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub(super) struct AutoSaveHandler {
|
||||||
|
save_pending: Arc<AtomicBool>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl AutoSaveHandler {
|
||||||
|
pub fn new() -> AutoSaveHandler {
|
||||||
|
AutoSaveHandler {
|
||||||
|
save_pending: Default::default(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl helix_event::AsyncHook for AutoSaveHandler {
|
||||||
|
type Event = AutoSaveEvent;
|
||||||
|
|
||||||
|
fn handle_event(
|
||||||
|
&mut self,
|
||||||
|
event: Self::Event,
|
||||||
|
existing_debounce: Option<tokio::time::Instant>,
|
||||||
|
) -> Option<Instant> {
|
||||||
|
match event {
|
||||||
|
Self::Event::DocumentChanged { save_after } => {
|
||||||
|
Some(Instant::now() + Duration::from_millis(save_after))
|
||||||
|
}
|
||||||
|
Self::Event::LeftInsertMode => {
|
||||||
|
if existing_debounce.is_some() {
|
||||||
|
// If the change happened more recently than the debounce, let the
|
||||||
|
// debounce run down before saving.
|
||||||
|
existing_debounce
|
||||||
|
} else {
|
||||||
|
// Otherwise if there is a save pending, save immediately.
|
||||||
|
if self.save_pending.load(atomic::Ordering::Relaxed) {
|
||||||
|
self.finish_debounce();
|
||||||
|
}
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn finish_debounce(&mut self) {
|
||||||
|
let save_pending = self.save_pending.clone();
|
||||||
|
job::dispatch_blocking(move |editor, _| {
|
||||||
|
if editor.mode() == Mode::Insert {
|
||||||
|
// Avoid saving while in insert mode since this mixes up
|
||||||
|
// the modification indicator and prevents future saves.
|
||||||
|
save_pending.store(true, atomic::Ordering::Relaxed);
|
||||||
|
} else {
|
||||||
|
request_auto_save(editor);
|
||||||
|
save_pending.store(false, atomic::Ordering::Relaxed);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn request_auto_save(editor: &mut Editor) {
|
||||||
|
let context = &mut compositor::Context {
|
||||||
|
editor,
|
||||||
|
scroll: Some(0),
|
||||||
|
jobs: &mut Jobs::new(),
|
||||||
|
};
|
||||||
|
|
||||||
|
if let Err(e) = commands::typed::write_all_impl(context, false, false) {
|
||||||
|
context.editor.set_error(format!("{}", e));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(super) fn register_hooks(handlers: &Handlers) {
|
||||||
|
let tx = handlers.auto_save.clone();
|
||||||
|
register_hook!(move |event: &mut DocumentDidChange<'_>| {
|
||||||
|
let config = event.doc.config.load();
|
||||||
|
if config.auto_save.after_delay.enable {
|
||||||
|
send_blocking(
|
||||||
|
&tx,
|
||||||
|
AutoSaveEvent::DocumentChanged {
|
||||||
|
save_after: config.auto_save.after_delay.timeout,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
});
|
||||||
|
|
||||||
|
let tx = handlers.auto_save.clone();
|
||||||
|
register_hook!(move |event: &mut OnModeSwitch<'_, '_>| {
|
||||||
|
if event.old_mode == Mode::Insert {
|
||||||
|
send_blocking(&tx, AutoSaveEvent::LeftInsertMode)
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
});
|
||||||
|
}
|
@ -0,0 +1,175 @@
|
|||||||
|
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;
|
||||||
|
}
|
||||||
|
// We consider an item to be fully resolved if it has non-empty, none-`None` details,
|
||||||
|
// docs and additional text-edits. Ideally we could use `is_some` instead of this
|
||||||
|
// check but some language servers send values like `Some([])` for additional text
|
||||||
|
// edits although the items need to be resolved. This is probably a consequence of
|
||||||
|
// how `null` works in the JavaScript world.
|
||||||
|
let is_resolved = item
|
||||||
|
.item
|
||||||
|
.documentation
|
||||||
|
.as_ref()
|
||||||
|
.is_some_and(|docs| match docs {
|
||||||
|
lsp::Documentation::String(text) => !text.is_empty(),
|
||||||
|
lsp::Documentation::MarkupContent(markup) => !markup.value.is_empty(),
|
||||||
|
})
|
||||||
|
&& item
|
||||||
|
.item
|
||||||
|
.detail
|
||||||
|
.as_ref()
|
||||||
|
.is_some_and(|detail| !detail.is_empty())
|
||||||
|
&& item
|
||||||
|
.item
|
||||||
|
.additional_text_edits
|
||||||
|
.as_ref()
|
||||||
|
.is_some_and(|edits| !edits.is_empty());
|
||||||
|
if is_resolved {
|
||||||
|
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
|
||||||
|
}
|
||||||
|
}
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue