From e2878a6e21914541d546bf98c961f2d05e198e9d Mon Sep 17 00:00:00 2001 From: Joe Date: Mon, 20 Jun 2022 10:21:21 -0400 Subject: [PATCH 01/11] Add noctis bordo theme (#2830) --- runtime/themes/noctis_bordo.toml | 80 ++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 runtime/themes/noctis_bordo.toml diff --git a/runtime/themes/noctis_bordo.toml b/runtime/themes/noctis_bordo.toml new file mode 100644 index 000000000..3487d7840 --- /dev/null +++ b/runtime/themes/noctis_bordo.toml @@ -0,0 +1,80 @@ +# Author: Joseph Harrison-Lim + +"attributes" = { fg = "#7060eb", modifiers = ["bold"] } +"comment" = { fg = "base03", modifiers = ["italic"] } +"comment.block.documentation" = { fg = "base06", modifiers = ["italic"] } +"constant" = "base09" +"constant.character.escape" = "base0C" +"constant.numeric" = "#7060eb" +"constructor" = "base0D" +"function" = "base0D" +"keyword" = "base0E" +"keyword.control" = { fg = "base0E", modifiers = ["bold"] } +"keyword.directive" = "white" +"keyword.import" = { fg = "#df769b" } +"keyword.operator" = { fg = "base0E", modifiers = ["italic"] } +"label" = "base0E" +"namespace" = "base0E" +"operator" = "base05" +"string" = "base0B" +"type" = "base10" +"variable" = "base08" +"variable.other.member" = "base08" +"special" = "base0D" + +"ui.background" = { bg = "base00" } +"ui.virtual" = "base03" +"ui.menu" = { fg = "base05", bg = "base01" } +"ui.menu.selected" = { fg = "base0B", bg = "base01" } +"ui.popup" = { bg = "base01" } +"ui.window" = { bg = "base01" } +"ui.linenr" = { fg = "#715b63", bg = "base01" } +"ui.linenr.selected" = { fg = "base02", bg = "base01", modifiers = ["bold"] } +"ui.selection" = { fg = "base05", bg = "base02" } +"ui.statusline" = { fg = "base02", bg = "base01" } +"ui.cursor" = { fg = "base04", modifiers = ["reversed"] } +"ui.cursor.primary" = { fg = "base05", modifiers = ["reversed"] } +"ui.text" = "base05" +"ui.text.focus" = "base05" +"ui.cursor.match" = { fg = "base0A", modifiers = ["underlined"] } +"ui.help" = { fg = "base06", bg = "base01" } + +"markup.bold" = { fg = "base0A", modifiers = ["bold"] } +"markup.heading" = "base0D" +"markup.italic" = { fg = "base0E", modifiers = ["italic"] } +"markup.link.text" = "base08" +"markup.link.url" = { fg = "base09", modifiers = ["underlined"] } +"markup.list" = "base08" +"markup.quote" = "base0C" +"markup.raw" = "base0B" + +"diff.delta" = "base09" +"diff.plus" = "base0B" +"diff.minus" = "base08" + +"diagnostic" = { modifiers = ["underlined"] } +"ui.gutter" = { bg = "base01" } +"info" = "base0D" +"hint" = "base03" +"debug" = "base03" +"warning" = "base09" +"error" = "base08" + +[palette] +base00 = "#322a2d" # Default Background +base01 = "#2c2528" # Lighter Background (Used for status bars, line number and folding marks) +base02 = "#997582" # Selection Background +base03 = "#585858" # Comments, Invisibles, Line Highlighting +base04 = "#322a2d" # Dark Foreground (Used for status bars) +base05 = "#cbbec2" # Default Foreground, Caret, Delimiters, Operators +base06 = "#e8e8e8" # Light Foreground (Not often used) +base07 = "#f8f8f8" # Light Background (Not often used) +base08 = "#e4b781" # Variables, XML Tags, Markup Link Text, Markup Lists, Diff Deleted +base09 = "#d5971a" # Integers, Boolean, Constants, XML Attributes, Markup Link Url +base0A = "#df769b" # Classes, Markup Bold, Search Text Background +base0B = "#49e9a6" # Strings, Inherited Class, Markup Code, Diff Inserted +base0C = "#16b673" # Support, Regular Expressions, Escape Characters, Markup Quotes +base0D = "#16a3b6" # Functions, Methods, Attribute IDs, Headings +base0E = "#ba8baf" # Keywords, Storage, Selector, Markup Italic, Diff Changed +base0F = "#d67e5c" # Deprecated, Opening/Closing Embedded Language Tags, e.g. +base10 = "#b0b0ff" # Types From 55f4f6951571d44862d5f1bc3ad7094953b788b6 Mon Sep 17 00:00:00 2001 From: lazytanuki <43273245+lazytanuki@users.noreply.github.com> Date: Mon, 20 Jun 2022 17:07:32 +0200 Subject: [PATCH 02/11] fix: do not color health summary when stdout is piped (#2836) * fix: do not color health summary when stdout is piped * fix: use crossterm instead of is-terminal --- helix-term/src/health.rs | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/helix-term/src/health.rs b/helix-term/src/health.rs index bd74f4787..f64e121d5 100644 --- a/helix-term/src/health.rs +++ b/helix-term/src/health.rs @@ -1,4 +1,7 @@ -use crossterm::style::{Color, Print, Stylize}; +use crossterm::{ + style::{Color, Print, Stylize}, + tty::IsTty, +}; use helix_core::config::{default_syntax_loader, user_syntax_loader}; use helix_loader::grammar::load_runtime_file; use std::io::Write; @@ -106,17 +109,19 @@ pub fn languages_all() -> std::io::Result<()> { let terminal_cols = crossterm::terminal::size().map(|(c, _)| c).unwrap_or(80); let column_width = terminal_cols as usize / headings.len(); + let is_terminal = std::io::stdout().is_tty(); let column = |item: &str, color: Color| { - let data = format!( + let mut data = format!( "{:width$}", item.get(..column_width - 2) .map(|s| format!("{}…", s)) .unwrap_or_else(|| item.to_string()), width = column_width, - ) - .stylize() - .with(color); + ); + if is_terminal { + data = data.stylize().with(color).to_string(); + } // We can't directly use println!() because of // https://github.com/crossterm-rs/crossterm/issues/589 From cad4e03a00d6431ccde76a080de2a5b328d54d9d Mon Sep 17 00:00:00 2001 From: farwyler <1705805+farwyler@users.noreply.github.com> Date: Mon, 20 Jun 2022 17:55:51 +0200 Subject: [PATCH 03/11] adds missing tree-sitter-comment injection for js/ts (#2763) --- runtime/queries/javascript/injections.scm | 12 ++++++++++-- runtime/queries/jsdoc/injections.scm | 5 +++++ 2 files changed, 15 insertions(+), 2 deletions(-) create mode 100644 runtime/queries/jsdoc/injections.scm diff --git a/runtime/queries/javascript/injections.scm b/runtime/queries/javascript/injections.scm index e84291115..af3aef105 100644 --- a/runtime/queries/javascript/injections.scm +++ b/runtime/queries/javascript/injections.scm @@ -22,7 +22,15 @@ ((regex_pattern) @injection.content (#set! injection.language "regex")) - ; Parse JSDoc annotations in comments +; Parse JSDoc annotations in multiline comments ((comment) @injection.content - (#set! injection.language "jsdoc")) + (#set! injection.language "jsdoc") + (#match? @injection.content "^/\\*+")) + +; Parse general tags in single line comments + +((comment) @injection.content + (#set! injection.language "comment") + (#match? @injection.content "^//")) + diff --git a/runtime/queries/jsdoc/injections.scm b/runtime/queries/jsdoc/injections.scm new file mode 100644 index 000000000..877b671d6 --- /dev/null +++ b/runtime/queries/jsdoc/injections.scm @@ -0,0 +1,5 @@ +; Parse general comment tags + +((document) @injection.content + (#set! injection.include-children) + (#set! injection.language "comment")) \ No newline at end of file From 8c64c3dfa3be911344ae0acaeee8018ffccde643 Mon Sep 17 00:00:00 2001 From: Mathis Brossier Date: Mon, 20 Jun 2022 20:41:34 +0200 Subject: [PATCH 04/11] mouse selection now uses character indexing (#2839) --- helix-term/src/ui/editor.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/helix-term/src/ui/editor.rs b/helix-term/src/ui/editor.rs index f074d9f1c..192fa1804 100644 --- a/helix-term/src/ui/editor.rs +++ b/helix-term/src/ui/editor.rs @@ -1046,7 +1046,7 @@ impl EditorView { let mut selection = doc.selection(view.id).clone(); let primary = selection.primary_mut(); - *primary = Range::new(primary.anchor, pos); + *primary = primary.put_cursor(doc.text().slice(..), pos, true); doc.set_selection(view.id, selection); EventResult::Consumed(None) } From 0ad10ce6f7159bc857eef1445a6c5cc28ae6a249 Mon Sep 17 00:00:00 2001 From: Michael Davis Date: Mon, 20 Jun 2022 18:15:50 -0500 Subject: [PATCH 05/11] rewrite language configuration docs (#2838) This change moves the configuration tables from the Adding Languages guide into the overall Languages section. It also adds more detailed documentation on the `language-server` configuration key and fixes a typo in the "mylang" example (the scope was `scope.mylang` instead of `source.mylang`). --- book/src/guides/adding_languages.md | 67 ++---------------- book/src/languages.md | 106 +++++++++++++++++++++++----- 2 files changed, 97 insertions(+), 76 deletions(-) diff --git a/book/src/guides/adding_languages.md b/book/src/guides/adding_languages.md index 0cd6c27bb..5be7a2644 100644 --- a/book/src/guides/adding_languages.md +++ b/book/src/guides/adding_languages.md @@ -2,40 +2,8 @@ ## Language configuration -To add a new language, you need to add a `language` entry to the -[`languages.toml`][languages.toml] found in the root of the repository; -this `languages.toml` file is included at compilation time, and is -distinct from the `languages.toml` file in the user's [configuration -directory](../configuration.md). - -```toml -[[language]] -name = "mylang" -scope = "scope.mylang" -injection-regex = "^mylang$" -file-types = ["mylang", "myl"] -comment-token = "#" -indent = { tab-width = 2, unit = " " } -language-server = { command = "mylang-lsp", args = ["--stdio"] } -``` - -These are the available keys and descriptions for the file. - -| Key | Description | -| ---- | ----------- | -| `name` | The name of the language | -| `scope` | A string like `source.js` that identifies the language. Currently, we strive to match the scope names used by popular TextMate grammars and by the Linguist library. Usually `source.` or `text.` in case of markup languages | -| `injection-regex` | regex pattern that will be tested against a language name in order to determine whether this language should be used for a potential [language injection][treesitter-language-injection] site. | -| `file-types` | The filetypes of the language, for example `["yml", "yaml"]`. Extensions and full file names are supported. | -| `shebangs` | The interpreters from the shebang line, for example `["sh", "bash"]` | -| `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 | -| `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 | -| `indent` | The indent to use. Has sub keys `tab-width` and `unit` | -| `language-server` | The Language Server to run. Has sub keys `command` and `args` | -| `config` | Language Server configuration | -| `grammar` | The tree-sitter grammar to use (defaults to the value of `name`) | +To add a new language, you need to add a `[[language]]` entry to the +`languages.toml` (see the [language configuration section]). When adding a new language or Language Server configuration for an existing language, run `cargo xtask docgen` to add the new configuration to the @@ -45,32 +13,12 @@ When adding a Language Server configuration, be sure to update the ## Grammar configuration -If a tree-sitter grammar is available for the language, add a new `grammar` +If a tree-sitter grammar is available for the language, add a new `[[grammar]]` entry to `languages.toml`. -```toml -[[grammar]] -name = "mylang" -source = { git = "https://github.com/example/mylang", rev = "a250c4582510ff34767ec3b7dcdd3c24e8c8aa68" } -``` - -Grammar configuration takes these keys: - -| Key | Description | -| --- | ----------- | -| `name` | The name of the tree-sitter grammar | -| `source` | The method of fetching the grammar - a table with a schema defined below | - -Where `source` is a table with either these keys when using a grammar from a -git repository: - -| Key | Description | -| --- | ----------- | -| `git` | A git remote URL from which the grammar should be cloned | -| `rev` | The revision (commit hash or tag) which should be fetched | -| `subpath` | A path within the grammar directory which should be built. Some grammar repositories host multiple grammars (for example `tree-sitter-typescript` and `tree-sitter-ocaml`) in subdirectories. This key is used to point `hx --grammar build` to the correct path for compilation. When omitted, the root of repository is used | - -Or a `path` key with an absolute path to a locally available grammar directory. +You may use the `source.path` key rather than `source.git` with an absolute path +to a locally available grammar for testing, but switch to `source.git` before +submitting a pull request. ## Queries @@ -91,8 +39,7 @@ the last matching query supersedes the ones before it. See - If a parser is segfaulting or you want to remove the parser, make sure to remove the compiled parser in `runtime/grammar/.so` -[treesitter-language-injection]: https://tree-sitter.github.io/tree-sitter/syntax-highlighting#language-injection -[languages.toml]: https://github.com/helix-editor/helix/blob/master/languages.toml +[language configuration section]: ../languages.md [neovim-query-precedence]: https://github.com/helix-editor/helix/pull/1170#issuecomment-997294090 [install-lsp-wiki]: https://github.com/helix-editor/helix/wiki/How-to-install-the-default-language-servers [lang-support]: ../lang-support.md diff --git a/book/src/languages.md b/book/src/languages.md index 8c27785e2..a9d5bea83 100644 --- a/book/src/languages.md +++ b/book/src/languages.md @@ -1,10 +1,17 @@ # Languages -Language-specific settings and settings for particular language servers can be configured in a `languages.toml` file placed in your [configuration directory](./configuration.md). Helix actually uses two `languages.toml` files, the [first one](https://github.com/helix-editor/helix/blob/master/languages.toml) is in the main helix repository; it contains the default settings for each language and is included in the helix binary at compile time. Users who want to see the available settings and options can either reference the helix repo's `languages.toml` file, or consult the table in the [adding languages](./guides/adding_languages.md) section. +Language-specific settings and settings for language servers are configured +in `languages.toml` files. -A local `languages.toml` can be created within a `.helix` directory. Its settings will be merged with both the global and default configs. +## `languages.toml` files -Changes made to the `languages.toml` file in a user's [configuration directory](./configuration.md) are merged with helix's defaults on start-up, such that a user's settings will take precedence over defaults in the event of a collision. For example, the default `languages.toml` sets rust's `auto-format` to `true`. If a user wants to disable auto-format, they can change the `languages.toml` in their [configuration directory](./configuration.md) to make the rust entry read like the example below; the new key/value pair `auto-format = false` will override the default when the two sets of settings are merged on start-up: +There are three possible `languages.toml` files. The first is compiled into +Helix and lives in the [Helix repository](https://github.com/helix-editor/helix/blob/master/languages.toml). +This provides the default configurations for languages and language servers. + +You may define a `languages.toml` in your [configuration directory](./configuration.md) +which overrides values from the built-in language configuration. For example +to disable auto-LSP-formatting in Rust: ```toml # in /helix/languages.toml @@ -14,9 +21,60 @@ name = "rust" auto-format = false ``` -## LSP formatting options +Language configuration may also be overridden local to a project by creating +a `languages.toml` file under a `.helix` directory. Its settings will be merged +with the language configuration in the configuration directory and the built-in +configuration. + +## Language configuration -Use `format` field to pass extra formatting options to [Document Formatting Requests](https://github.com/microsoft/language-server-protocol/blob/gh-pages/_specifications/specification-3-16.md#document-formatting-request--leftwards_arrow_with_hook). +Each language is configured by adding a `[[language]]` section to a +`languages.toml` file. For example: + +```toml +[[language]] +name = "mylang" +scope = "source.mylang" +injection-regex = "^mylang$" +file-types = ["mylang", "myl"] +comment-token = "#" +indent = { tab-width = 2, unit = " " } +language-server = { command = "mylang-lsp", args = ["--stdio"] } +``` + +These configuration keys are available: + +| Key | Description | +| ---- | ----------- | +| `name` | The name of the language | +| `scope` | A string like `source.js` that identifies the language. Currently, we strive to match the scope names used by popular TextMate grammars and by the Linguist library. Usually `source.` or `text.` in case of markup languages | +| `injection-regex` | regex pattern that will be tested against a language name in order to determine whether this language should be used for a potential [language injection][treesitter-language-injection] site. | +| `file-types` | The filetypes of the language, for example `["yml", "yaml"]`. Extensions and full file names are supported. | +| `shebangs` | The interpreters from the shebang line, for example `["sh", "bash"]` | +| `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 | +| `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 | +| `indent` | The indent to use. Has sub keys `tab-width` and `unit` | +| `language-server` | The Language Server to run. See the Language Server configuration section below. | +| `config` | Language Server configuration | +| `grammar` | The tree-sitter grammar to use (defaults to the value of `name`) | + +### Language Server configuration + +The `language-server` field takes the following keys: + +| Key | Description | +| --- | ----------- | +| `command` | The name of the language server binary to execute. Binaries must be in `$PATH` | +| `args` | A list of arguments to pass to the language server binary | +| `timeout` | The maximum time a request to the language server may take, in seconds. Defaults to `20` | +| `language-id` | The language name to pass to the language server. Some language servers support multiple languages and use this field to determine which one is being served in a buffer | + +The top-level `config` field is used to configure the LSP initialization options. A `format` +sub-table within `config` can be used to pass extra formatting options to +[Document Formatting Requests](https://github.com/microsoft/language-server-protocol/blob/gh-pages/_specifications/specification-3-16.md#document-formatting-request--leftwards_arrow_with_hook). +For example with typescript: ```toml [[language]] @@ -26,23 +84,37 @@ auto-format = true config = { format = { "semicolons" = "insert", "insertSpaceBeforeFunctionParenthesis" = true } } ``` -## Tree-sitter grammars +## Tree-sitter grammar configuration -Tree-sitter grammars can also be configured in `languages.toml`: +The source for a language's tree-sitter grammar is specified in a `[[grammar]]` +section in `languages.toml`. For example: ```toml -# in /helix/languages.toml - [[grammar]] -name = "rust" -source = { git = "https://github.com/tree-sitter/tree-sitter-rust", rev = "a250c4582510ff34767ec3b7dcdd3c24e8c8aa68" } - -[[grammar]] -name = "c" -source = { path = "/path/to/tree-sitter-c" } +name = "mylang" +source = { git = "https://github.com/example/mylang", rev = "a250c4582510ff34767ec3b7dcdd3c24e8c8aa68" } ``` -You may use a top-level `use-grammars` key to control which grammars are fetched and built. +Grammar configuration takes these keys: + +| Key | Description | +| --- | ----------- | +| `name` | The name of the tree-sitter grammar | +| `source` | The method of fetching the grammar - a table with a schema defined below | + +Where `source` is a table with either these keys when using a grammar from a +git repository: + +| Key | Description | +| --- | ----------- | +| `git` | A git remote URL from which the grammar should be cloned | +| `rev` | The revision (commit hash or tag) which should be fetched | +| `subpath` | A path within the grammar directory which should be built. Some grammar repositories host multiple grammars (for example `tree-sitter-typescript` and `tree-sitter-ocaml`) in subdirectories. This key is used to point `hx --grammar build` to the correct path for compilation. When omitted, the root of repository is used | + +### Choosing grammars + +You may use a top-level `use-grammars` key to control which grammars are +fetched and built when using `hx --grammar fetch` and `hx --grammar build`. ```toml # Note: this key must come **before** the [[language]] and [[grammar]] sections @@ -52,3 +124,5 @@ use-grammars = { except = [ "yaml", "json" ] } ``` When omitted, all grammars are fetched and built. + +[treesitter-language-injection]: https://tree-sitter.github.io/tree-sitter/syntax-highlighting#language-injection From 009f8c4d3bdf56c9c5b0ba9489864d4d242a6e01 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Tue, 21 Jun 2022 08:08:45 +0530 Subject: [PATCH 06/11] build(deps): bump anyhow from 1.0.57 to 1.0.58 (#2843) Bumps [anyhow](https://github.com/dtolnay/anyhow) from 1.0.57 to 1.0.58. - [Release notes](https://github.com/dtolnay/anyhow/releases) - [Commits](https://github.com/dtolnay/anyhow/compare/1.0.57...1.0.58) --- updated-dependencies: - dependency-name: anyhow dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- Cargo.lock | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ddf267906..6257c9ea1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -13,9 +13,9 @@ dependencies = [ [[package]] name = "anyhow" -version = "1.0.57" +version = "1.0.58" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "08f9b8508dccb7687a1d6c4ce66b2b0ecef467c94667de27d8d7fe1f8d2a9cdc" +checksum = "bb07d2053ccdbe10e2af2995a2f116c1330396493dc1269f6a91d0ae82e19704" [[package]] name = "arc-swap" From 43027d91046f79d6dc495b351cdcbfd3819cd9e1 Mon Sep 17 00:00:00 2001 From: A-Walrus <58790821+A-Walrus@users.noreply.github.com> Date: Tue, 21 Jun 2022 05:39:43 +0300 Subject: [PATCH 07/11] Display highest severity diagnostic in gutter (#2835) * Display highest severity diagnostic in gutter * Improve gutter diagnostic performance Very slight improvement (doesn't really make a difference), iterates over the diagnostics of the line once instead of twice. * Add comment justifying unwrap --- helix-view/src/gutter.rs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/helix-view/src/gutter.rs b/helix-view/src/gutter.rs index eb6796bec..05fec7589 100644 --- a/helix-view/src/gutter.rs +++ b/helix-view/src/gutter.rs @@ -26,7 +26,18 @@ pub fn diagnostic<'doc>( Box::new(move |line: usize, _selected: bool, out: &mut String| { use helix_core::diagnostic::Severity; if let Ok(index) = diagnostics.binary_search_by_key(&line, |d| d.line) { - let diagnostic = &diagnostics[index]; + let after = diagnostics[index..].iter().take_while(|d| d.line == line); + + let before = diagnostics[..index] + .iter() + .rev() + .take_while(|d| d.line == line); + + let diagnostics_on_line = after.chain(before); + + // This unwrap is safe because the iterator cannot be empty as it contains at least the item found by the binary search. + let diagnostic = diagnostics_on_line.max_by_key(|d| d.severity).unwrap(); + write!(out, "●").unwrap(); return Some(match diagnostic.severity { Some(Severity::Error) => error, From 67f6c85792dbdbe0ff3f9328874c7ab23ff5569b Mon Sep 17 00:00:00 2001 From: "Connor Lay (Clay)" Date: Sat, 18 Jun 2022 14:24:01 -0700 Subject: [PATCH 08/11] text-objects: add test capture & elixir queries --- book/src/guides/textobject.md | 2 ++ book/src/keymap.md | 2 ++ book/src/usage.md | 1 + helix-term/src/commands.rs | 12 ++++++++++++ helix-term/src/keymap/default.rs | 2 ++ runtime/queries/elixir/textobjects.scm | 8 +++++++- 6 files changed, 26 insertions(+), 1 deletion(-) diff --git a/book/src/guides/textobject.md b/book/src/guides/textobject.md index cccd4bbf0..8a2173547 100644 --- a/book/src/guides/textobject.md +++ b/book/src/guides/textobject.md @@ -20,6 +20,8 @@ The following [captures][tree-sitter-captures] are recognized: | `function.around` | | `class.inside` | | `class.around` | +| `test.inside` | +| `test.around` | | `parameter.inside` | | `comment.inside` | | `comment.around` | diff --git a/book/src/keymap.md b/book/src/keymap.md index fef76efb6..7efbdd237 100644 --- a/book/src/keymap.md +++ b/book/src/keymap.md @@ -282,6 +282,8 @@ Mappings in the style of [vim-unimpaired](https://github.com/tpope/vim-unimpaire | `[a` | Go to previous argument/parameter (**TS**) | `goto_prev_parameter` | | `]o` | Go to next comment (**TS**) | `goto_next_comment` | | `[o` | Go to previous comment (**TS**) | `goto_prev_comment` | +| `]t` | Go to next test (**TS**) | `goto_next_test` | +| `]t` | Go to previous test (**TS**) | `goto_prev_test` | | `]p` | Go to next paragraph | `goto_next_paragraph` | | `[p` | Go to previous paragraph | `goto_prev_paragraph` | | `[space` | Add newline above | `add_newline_above` | diff --git a/book/src/usage.md b/book/src/usage.md index ad21a94c9..ba631b623 100644 --- a/book/src/usage.md +++ b/book/src/usage.md @@ -143,6 +143,7 @@ Currently supported: `word`, `surround`, `function`, `class`, `parameter`. | `c` | Class | | `a` | Argument/parameter | | `o` | Comment | +| `t` | Test | > NOTE: `f`, `c`, etc need a tree-sitter grammar active for the current document and a special tree-sitter query file to work properly. [Only diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index c9c8e6a98..046351a36 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -395,6 +395,8 @@ impl MappableCommand { goto_prev_parameter, "Goto previous parameter", goto_next_comment, "Goto next comment", goto_prev_comment, "Goto previous comment", + goto_next_test, "Goto next test", + goto_prev_test, "Goto previous test", goto_next_paragraph, "Goto next paragraph", goto_prev_paragraph, "Goto previous paragraph", dap_launch, "Launch debug target", @@ -4098,6 +4100,14 @@ fn goto_prev_comment(cx: &mut Context) { goto_ts_object_impl(cx, "comment", Direction::Backward) } +fn goto_next_test(cx: &mut Context) { + goto_ts_object_impl(cx, "test", Direction::Forward) +} + +fn goto_prev_test(cx: &mut Context) { + goto_ts_object_impl(cx, "test", Direction::Backward) +} + fn select_textobject_around(cx: &mut Context) { select_textobject(cx, textobject::TextObject::Around); } @@ -4141,6 +4151,7 @@ fn select_textobject(cx: &mut Context, objtype: textobject::TextObject) { 'f' => textobject_treesitter("function", range), 'a' => textobject_treesitter("parameter", range), 'o' => textobject_treesitter("comment", range), + 't' => textobject_treesitter("test", range), 'p' => textobject::textobject_paragraph(text, range, objtype, count), 'm' => textobject::textobject_surround_closest(text, range, objtype, count), // TODO: cancel new ranges if inconsistent surround matches across lines @@ -4170,6 +4181,7 @@ fn select_textobject(cx: &mut Context, objtype: textobject::TextObject) { ("f", "Function (tree-sitter)"), ("a", "Argument/parameter (tree-sitter)"), ("o", "Comment (tree-sitter)"), + ("t", "Test (tree-sitter)"), ("m", "Matching delimiter under cursor"), (" ", "... or any character acting as a pair"), ]; diff --git a/helix-term/src/keymap/default.rs b/helix-term/src/keymap/default.rs index 0f0b09dd5..c36951172 100644 --- a/helix-term/src/keymap/default.rs +++ b/helix-term/src/keymap/default.rs @@ -104,6 +104,7 @@ pub fn default() -> HashMap { "c" => goto_prev_class, "a" => goto_prev_parameter, "o" => goto_prev_comment, + "t" => goto_prev_test, "p" => goto_prev_paragraph, "space" => add_newline_above, }, @@ -114,6 +115,7 @@ pub fn default() -> HashMap { "c" => goto_next_class, "a" => goto_next_parameter, "o" => goto_next_comment, + "t" => goto_next_test, "p" => goto_next_paragraph, "space" => add_newline_below, }, diff --git a/runtime/queries/elixir/textobjects.scm b/runtime/queries/elixir/textobjects.scm index 52a6f66de..227a52f4e 100644 --- a/runtime/queries/elixir/textobjects.scm +++ b/runtime/queries/elixir/textobjects.scm @@ -16,7 +16,7 @@ (pair value: (_) @function.inside))?)? (do_block (_)* @function.inside)?) - (#match? @_keyword "^(def|defdelegate|defguard|defguardp|defmacro|defmacrop|defn|defnp|defp|test|describe|setup)$")) @function.around + (#match? @_keyword "^(def|defdelegate|defguard|defguardp|defmacro|defmacrop|defn|defnp|defp)$")) @function.around (anonymous_function (stab_clause right: (body) @function.inside)) @function.around @@ -25,3 +25,9 @@ target: (identifier) @_keyword (do_block (_)* @class.inside)) (#match? @_keyword "^(defmodule|defprotocol|defimpl)$")) @class.around + +((call + target: (identifier) @_keyword + (arguments ((string) . (_)?)) + (do_block (_)* @test.inside)?) + (#match? @_keyword "^(test|describe)$")) @test.around From 9f676dab57e6421dd4c33a7ccf16e12cfb9b62cf Mon Sep 17 00:00:00 2001 From: Michael Davis Date: Sun, 19 Jun 2022 21:36:33 -0500 Subject: [PATCH 09/11] add test textobjects queries for erlang,gleam,go,python,rust --- runtime/queries/erlang/textobjects.scm | 8 ++++++++ runtime/queries/gleam/textobjects.scm | 5 +++++ runtime/queries/go/textobjects.scm | 5 +++++ runtime/queries/python/textobjects.scm | 5 +++++ runtime/queries/rust/textobjects.scm | 14 ++++++++++++++ 5 files changed, 37 insertions(+) diff --git a/runtime/queries/erlang/textobjects.scm b/runtime/queries/erlang/textobjects.scm index c46b5c6f6..013629f88 100644 --- a/runtime/queries/erlang/textobjects.scm +++ b/runtime/queries/erlang/textobjects.scm @@ -6,3 +6,11 @@ (stab_clause body: (_) @function.inside)) @function.around (comment (comment_content) @comment.inside) @comment.around + +; EUnit test names. +; (CommonTest cases are not recognizable by syntax alone.) +((function_clause + name: (atom) @_name + pattern: (arguments (_)? @parameter.inside) + body: (_) @test.inside) @test.around + (#match? @_name "_test$")) diff --git a/runtime/queries/gleam/textobjects.scm b/runtime/queries/gleam/textobjects.scm index b382f4bd0..19cd0dcf9 100644 --- a/runtime/queries/gleam/textobjects.scm +++ b/runtime/queries/gleam/textobjects.scm @@ -4,3 +4,8 @@ (anonymous_function body: (function_body) @function.inside) @function.around + +((function + name: (identifier) @_name + body: (function_body) @test.inside) @test.around + (#match? @_name "_test$")) diff --git a/runtime/queries/go/textobjects.scm b/runtime/queries/go/textobjects.scm index a48ccce18..df1b0866f 100644 --- a/runtime/queries/go/textobjects.scm +++ b/runtime/queries/go/textobjects.scm @@ -26,3 +26,8 @@ (comment) @comment.inside (comment)+ @comment.around + +((function_declaration + name: (identifier) @_name + body: (block)? @test.inside) @test.around + (#match? @_name "^Test")) diff --git a/runtime/queries/python/textobjects.scm b/runtime/queries/python/textobjects.scm index 5c6a61d17..966e47446 100644 --- a/runtime/queries/python/textobjects.scm +++ b/runtime/queries/python/textobjects.scm @@ -16,3 +16,8 @@ (comment) @comment.inside (comment)+ @comment.around + +((function_definition + name: (identifier) @_name + body: (block)? @test.inside) @test.around + (#match? @_name "^test_")) diff --git a/runtime/queries/rust/textobjects.scm b/runtime/queries/rust/textobjects.scm index ba86050b5..94c8c9f8b 100644 --- a/runtime/queries/rust/textobjects.scm +++ b/runtime/queries/rust/textobjects.scm @@ -77,3 +77,17 @@ (line_comment)+ @comment.around (block_comment) @comment.around + +(; #[test] + (attribute_item + (meta_item + (identifier) @_test_attribute)) + ; allow other attributes like #[should_panic] and comments + [ + (attribute_item) + (line_comment) + ]* + ; the test function + (function_item + body: (_) @test.inside) @test.around + (#eq? @_test_attribute "test")) From fa4934cff9aa5b86b907e218313a7b370962ae67 Mon Sep 17 00:00:00 2001 From: Mathspy Date: Tue, 21 Jun 2022 12:35:25 -0400 Subject: [PATCH 10/11] Default rulers color to red (#2669) * Default rulers color to red Currently if the theme a user is using doesn't have `ui.virtual.rulers` set and they set up a ruler it just fails silently making it really hard to figure out what went wrong. Did they set incorrectly set the ruler? Are they using an outdated version of Helix that doesn't support rulers? This happened to me today, I even switched to the default theme with the assumption that maybe my theme just doesn't have the rulers setup properly and it still didn't work. Not sure if this is a good idea or not, feel free to suggest better alternatives! * Use builtin Style methods instead of Bevy style defaults Co-authored-by: Michael Davis * Only default the style if there's no ui or ui.virtual * Update themes style from ui.virtual to ui.virtual.whitespace * Revert ui.virtual change in onelight theme * Prefer unwrap_or_else Co-authored-by: Michael Davis --- helix-term/src/ui/editor.rs | 6 ++++-- runtime/themes/base16_default_dark.toml | 2 +- runtime/themes/base16_default_light.toml | 2 +- runtime/themes/base16_terminal.toml | 2 +- runtime/themes/bogster.toml | 2 +- runtime/themes/boo_berry.toml | 2 +- runtime/themes/catppuccin.toml | 2 +- runtime/themes/dark_plus.toml | 2 +- runtime/themes/dracula.toml | 2 +- runtime/themes/everforest_dark.toml | 2 +- runtime/themes/everforest_light.toml | 2 +- runtime/themes/gruvbox.toml | 2 +- runtime/themes/gruvbox_light.toml | 2 +- runtime/themes/ingrid.toml | 2 +- runtime/themes/monokai.toml | 2 +- runtime/themes/monokai_pro.toml | 2 +- runtime/themes/monokai_pro_machine.toml | 2 +- runtime/themes/monokai_pro_octagon.toml | 2 +- runtime/themes/monokai_pro_ristretto.toml | 2 +- runtime/themes/monokai_pro_spectrum.toml | 2 +- runtime/themes/nord.toml | 2 +- runtime/themes/rose_pine.toml | 2 +- runtime/themes/rose_pine_dawn.toml | 2 +- runtime/themes/serika-dark.toml | 2 +- runtime/themes/serika-light.toml | 2 +- runtime/themes/snazzy.toml | 2 +- runtime/themes/solarized_dark.toml | 2 +- runtime/themes/solarized_light.toml | 2 +- runtime/themes/spacebones_light.toml | 2 +- 29 files changed, 32 insertions(+), 30 deletions(-) diff --git a/helix-term/src/ui/editor.rs b/helix-term/src/ui/editor.rs index 192fa1804..a8027d1b8 100644 --- a/helix-term/src/ui/editor.rs +++ b/helix-term/src/ui/editor.rs @@ -20,7 +20,7 @@ use helix_core::{ use helix_view::{ document::{Mode, SCRATCH_BUFFER_NAME}, editor::{CompleteAction, CursorShapeConfig}, - graphics::{CursorKind, Modifier, Rect, Style}, + graphics::{Color, CursorKind, Modifier, Rect, Style}, input::KeyEvent, keyboard::{KeyCode, KeyModifiers}, Document, Editor, Theme, View, @@ -170,7 +170,9 @@ impl EditorView { theme: &Theme, ) { let editor_rulers = &editor.config().rulers; - let ruler_theme = theme.get("ui.virtual.ruler"); + let ruler_theme = theme + .try_get("ui.virtual.ruler") + .unwrap_or_else(|| Style::default().bg(Color::Red)); let rulers = doc .language_config() diff --git a/runtime/themes/base16_default_dark.toml b/runtime/themes/base16_default_dark.toml index 7516e492e..33252330e 100644 --- a/runtime/themes/base16_default_dark.toml +++ b/runtime/themes/base16_default_dark.toml @@ -1,7 +1,7 @@ # Author: RayGervais "ui.background" = { bg = "base00" } -"ui.virtual" = "base03" +"ui.virtual.whitespace" = "base03" "ui.menu" = { fg = "base05", bg = "base01" } "ui.menu.selected" = { fg = "base01", bg = "base04" } "ui.linenr" = { fg = "base03", bg = "base01" } diff --git a/runtime/themes/base16_default_light.toml b/runtime/themes/base16_default_light.toml index 368474591..bc2b8e671 100644 --- a/runtime/themes/base16_default_light.toml +++ b/runtime/themes/base16_default_light.toml @@ -12,7 +12,7 @@ "ui.statusline" = { fg = "base04", bg = "base01" } "ui.cursor" = { fg = "base04", modifiers = ["reversed"] } "ui.cursor.primary" = { fg = "base05", modifiers = ["reversed"] } -"ui.virtual" = "base03" +"ui.virtual.whitespace" = "base03" "ui.text" = "base05" "operator" = "base05" "ui.text.focus" = "base05" diff --git a/runtime/themes/base16_terminal.toml b/runtime/themes/base16_terminal.toml index 928488168..3a1d48457 100644 --- a/runtime/themes/base16_terminal.toml +++ b/runtime/themes/base16_terminal.toml @@ -13,7 +13,7 @@ "ui.help" = { fg = "white", bg = "black" } "ui.cursor" = { fg = "light-gray", modifiers = ["reversed"] } "ui.cursor.primary" = { fg = "light-gray", modifiers = ["reversed"] } -"ui.virtual" = "light-gray" +"ui.virtual.whitespace" = "light-gray" "variable" = "light-red" "constant.numeric" = "yellow" "constant" = "yellow" diff --git a/runtime/themes/bogster.toml b/runtime/themes/bogster.toml index df3a7f315..76e24648b 100644 --- a/runtime/themes/bogster.toml +++ b/runtime/themes/bogster.toml @@ -53,7 +53,7 @@ "ui.text" = { fg = "#e5ded6" } "ui.text.focus" = { fg = "#e5ded6", modifiers= ["bold"] } -"ui.virtual" = "#627d9d" +"ui.virtual.whitespace" = "#627d9d" "ui.selection" = { bg = "#313f4e" } # "ui.cursor.match" # TODO might want to override this because dimmed is not widely supported diff --git a/runtime/themes/boo_berry.toml b/runtime/themes/boo_berry.toml index 5cd253974..a79b75c59 100644 --- a/runtime/themes/boo_berry.toml +++ b/runtime/themes/boo_berry.toml @@ -44,7 +44,7 @@ "ui.menu" = { fg = "lilac", bg = "berry_saturated" } "ui.menu.selected" = { fg = "mint", bg = "berry_saturated" } "ui.selection" = { bg = "berry_saturated" } -"ui.virtual" = { fg = "berry_desaturated" } +"ui.virtual.whitespace" = { fg = "berry_desaturated" } "diff.plus" = { fg = "mint" } "diff.delta" = { fg = "gold" } diff --git a/runtime/themes/catppuccin.toml b/runtime/themes/catppuccin.toml index da9be4fbd..eaa4ba517 100644 --- a/runtime/themes/catppuccin.toml +++ b/runtime/themes/catppuccin.toml @@ -48,7 +48,7 @@ label = "peach" "ui.text" = { fg = "pink" } "ui.text.focus" = { fg = "white" } -"ui.virtual" = { fg = "gray_0" } +"ui.virtual.whitespace" = { fg = "gray_0" } "ui.selection" = { bg = "#540099" } "ui.selection.primary" = { bg = "#540099" } diff --git a/runtime/themes/dark_plus.toml b/runtime/themes/dark_plus.toml index c785dd388..957ca61d4 100644 --- a/runtime/themes/dark_plus.toml +++ b/runtime/themes/dark_plus.toml @@ -78,7 +78,7 @@ "ui.text" = { fg = "text" } "ui.text.focus" = { fg = "white" } -"ui.virtual" = { fg = "dark_gray" } +"ui.virtual.whitespace" = { fg = "dark_gray" } "ui.virtual.ruler" = { bg = "borders" } "warning" = { fg = "gold2" } diff --git a/runtime/themes/dracula.toml b/runtime/themes/dracula.toml index e32c3117b..72b37d027 100644 --- a/runtime/themes/dracula.toml +++ b/runtime/themes/dracula.toml @@ -36,7 +36,7 @@ "ui.text" = { fg = "foreground" } "ui.text.focus" = { fg = "cyan" } "ui.window" = { fg = "foreground" } -"ui.virtual" = { fg = "comment" } +"ui.virtual.whitespace" = { fg = "comment" } "error" = { fg = "red" } "warning" = { fg = "cyan" } diff --git a/runtime/themes/everforest_dark.toml b/runtime/themes/everforest_dark.toml index 5b6d1b7cf..ef74ea9e8 100644 --- a/runtime/themes/everforest_dark.toml +++ b/runtime/themes/everforest_dark.toml @@ -70,7 +70,7 @@ "ui.menu" = { fg = "fg", bg = "bg2" } "ui.menu.selected" = { fg = "bg0", bg = "green" } "ui.selection" = { bg = "bg3" } -"ui.virtual" = "grey0" +"ui.virtual.whitespace" = "grey0" "hint" = "blue" "info" = "aqua" diff --git a/runtime/themes/everforest_light.toml b/runtime/themes/everforest_light.toml index b03701653..60557ba0c 100644 --- a/runtime/themes/everforest_light.toml +++ b/runtime/themes/everforest_light.toml @@ -70,7 +70,7 @@ "ui.menu" = { fg = "fg", bg = "bg2" } "ui.menu.selected" = { fg = "bg0", bg = "green" } "ui.selection" = { bg = "bg3" } -"ui.virtual" = "grey0" +"ui.virtual.whitespace" = "grey0" "hint" = "blue" "info" = "aqua" diff --git a/runtime/themes/gruvbox.toml b/runtime/themes/gruvbox.toml index 6de35244a..48fb14fa1 100644 --- a/runtime/themes/gruvbox.toml +++ b/runtime/themes/gruvbox.toml @@ -53,7 +53,7 @@ "ui.cursor.match" = { bg = "bg2" } "ui.menu" = { fg = "fg1", bg = "bg2" } "ui.menu.selected" = { fg = "bg2", bg = "blue1", modifiers = ["bold"] } -"ui.virtual" = "bg2" +"ui.virtual.whitespace" = "bg2" "diagnostic" = { modifiers = ["underlined"] } diff --git a/runtime/themes/gruvbox_light.toml b/runtime/themes/gruvbox_light.toml index 2930dff0d..02a32dec4 100644 --- a/runtime/themes/gruvbox_light.toml +++ b/runtime/themes/gruvbox_light.toml @@ -54,7 +54,7 @@ "ui.cursor.match" = { bg = "bg2" } "ui.menu" = { fg = "fg1", bg = "bg2" } "ui.menu.selected" = { fg = "bg2", bg = "blue1", modifiers = ["bold"] } -"ui.virtual" = "bg2" +"ui.virtual.whitespace" = "bg2" "diagnostic" = { modifiers = ["underlined"] } diff --git a/runtime/themes/ingrid.toml b/runtime/themes/ingrid.toml index 79b749b14..587137045 100644 --- a/runtime/themes/ingrid.toml +++ b/runtime/themes/ingrid.toml @@ -53,7 +53,7 @@ "ui.text" = { fg = "#7B91B3" } "ui.text.focus" = { fg = "#250E07", modifiers= ["bold"] } -"ui.virtual" = "#A6B6CE" +"ui.virtual.whitespace" = "#A6B6CE" "ui.selection" = { bg = "#540099" } # "ui.cursor.match" # TODO might want to override this because dimmed is not widely supported diff --git a/runtime/themes/monokai.toml b/runtime/themes/monokai.toml index 3fb1fadc3..5a8906154 100644 --- a/runtime/themes/monokai.toml +++ b/runtime/themes/monokai.toml @@ -32,7 +32,7 @@ "attribute" = { fg = "fn_declaration" } "comment" = { fg = "#88846F" } -"ui.virtual" = "#88846F" +"ui.virtual.whitespace" = "#88846F" "string" = { fg = "#e6db74" } "constant.character" = { fg = "#e6db74" } diff --git a/runtime/themes/monokai_pro.toml b/runtime/themes/monokai_pro.toml index 5580a33c6..7c457d458 100644 --- a/runtime/themes/monokai_pro.toml +++ b/runtime/themes/monokai_pro.toml @@ -6,7 +6,7 @@ "ui.text.focus" = { fg = "yellow", modifiers= ["bold"] } "ui.menu" = { fg = "base8", bg = "base3" } "ui.menu.selected" = { fg = "base2", bg = "yellow" } -"ui.virtual" = "base5" +"ui.virtual.whitespace" = "base5" "info" = "base8" "hint" = "base8" diff --git a/runtime/themes/monokai_pro_machine.toml b/runtime/themes/monokai_pro_machine.toml index abbe5bdca..bfc7031d3 100644 --- a/runtime/themes/monokai_pro_machine.toml +++ b/runtime/themes/monokai_pro_machine.toml @@ -6,7 +6,7 @@ "ui.text.focus" = { fg = "yellow", modifiers= ["bold"] } "ui.menu" = { fg = "base8", bg = "base3" } "ui.menu.selected" = { fg = "base2", bg = "yellow" } -"ui.virtual" = "base5" +"ui.virtual.whitespace" = "base5" "info" = "base8" "hint" = "base8" diff --git a/runtime/themes/monokai_pro_octagon.toml b/runtime/themes/monokai_pro_octagon.toml index b249cfe21..889e76249 100644 --- a/runtime/themes/monokai_pro_octagon.toml +++ b/runtime/themes/monokai_pro_octagon.toml @@ -6,7 +6,7 @@ "ui.text.focus" = { fg = "yellow", modifiers= ["bold"] } "ui.menu" = { fg = "base8", bg = "base3" } "ui.menu.selected" = { fg = "base2", bg = "yellow" } -"ui.virtual" = "base5" +"ui.virtual.whitespace" = "base5" "info" = "base8" "hint" = "base8" diff --git a/runtime/themes/monokai_pro_ristretto.toml b/runtime/themes/monokai_pro_ristretto.toml index cd4cbd8e9..f8ad8422e 100644 --- a/runtime/themes/monokai_pro_ristretto.toml +++ b/runtime/themes/monokai_pro_ristretto.toml @@ -6,7 +6,7 @@ "ui.text.focus" = { fg = "yellow", modifiers= ["bold"] } "ui.menu" = { fg = "base8", bg = "base3" } "ui.menu.selected" = { fg = "base2", bg = "yellow" } -"ui.virtual" = "base5" +"ui.virtual.whitespace" = "base5" "info" = "base8" "hint" = "base8" diff --git a/runtime/themes/monokai_pro_spectrum.toml b/runtime/themes/monokai_pro_spectrum.toml index 4160a15e1..9f5864fcc 100644 --- a/runtime/themes/monokai_pro_spectrum.toml +++ b/runtime/themes/monokai_pro_spectrum.toml @@ -6,7 +6,7 @@ "ui.text.focus" = { fg = "yellow", modifiers= ["bold"] } "ui.menu" = { fg = "base8", bg = "base3" } "ui.menu.selected" = { fg = "base2", bg = "yellow" } -"ui.virtual" = "base5" +"ui.virtual.whitespace" = "base5" "info" = "base8" "hint" = "base8" diff --git a/runtime/themes/nord.toml b/runtime/themes/nord.toml index a61c17157..3b994bb59 100644 --- a/runtime/themes/nord.toml +++ b/runtime/themes/nord.toml @@ -4,7 +4,7 @@ "ui.text.focus" = { fg = "nord8", modifiers= ["bold"] } "ui.menu" = { fg = "nord6", bg = "#232d38" } "ui.menu.selected" = { fg = "nord8", bg = "nord2" } -"ui.virtual" = "gray" +"ui.virtual.whitespace" = "gray" "info" = "nord8" "hint" = "nord8" diff --git a/runtime/themes/rose_pine.toml b/runtime/themes/rose_pine.toml index f05758801..09b1e25c9 100644 --- a/runtime/themes/rose_pine.toml +++ b/runtime/themes/rose_pine.toml @@ -14,7 +14,7 @@ "ui.text" = { fg = "text" } "ui.text.focus" = { fg = "foam", modifiers = ["bold"]} "ui.text.info" = {fg = "pine", modifiers = ["bold"]} -"ui.virtual" = "highlight" +"ui.virtual.whitespace" = "highlight" "operator" = "rose" "variable" = "text" "constant.numeric" = "iris" diff --git a/runtime/themes/rose_pine_dawn.toml b/runtime/themes/rose_pine_dawn.toml index 5ad304e38..9ba0959db 100644 --- a/runtime/themes/rose_pine_dawn.toml +++ b/runtime/themes/rose_pine_dawn.toml @@ -14,7 +14,7 @@ "ui.text" = { fg = "text" } "ui.text.focus" = { fg = "foam", modifiers = ["bold"]} "ui.text.info" = {fg = "pine", modifiers = ["bold"]} -"ui.virtual" = "highlight" +"ui.virtual.whitespace" = "highlight" "operator" = "rose" "variable" = "text" "number" = "iris" diff --git a/runtime/themes/serika-dark.toml b/runtime/themes/serika-dark.toml index 3dd982d19..3b4bc60f5 100644 --- a/runtime/themes/serika-dark.toml +++ b/runtime/themes/serika-dark.toml @@ -50,7 +50,7 @@ "ui.menu" = { fg = "fg", bg = "bg2" } "ui.menu.selected" = { fg = "bg0", bg = "bg_yellow" } "ui.selection" = { bg = "bg3" } -"ui.virtual" = "grey2" +"ui.virtual.whitespace" = "grey2" "hint" = "blue" "info" = "aqua" diff --git a/runtime/themes/serika-light.toml b/runtime/themes/serika-light.toml index 67c8328b7..3b0f8fb47 100644 --- a/runtime/themes/serika-light.toml +++ b/runtime/themes/serika-light.toml @@ -50,7 +50,7 @@ "ui.menu" = { fg = "bg0", bg = "bg3" } "ui.menu.selected" = { fg = "bg0", bg = "bg_yellow" } "ui.selection" = { fg = "bg0", bg = "bg3" } -"ui.virtual" = { fg = "bg2" } +"ui.virtual.whitespace" = { fg = "bg2" } "hint" = "blue" "info" = "aqua" diff --git a/runtime/themes/snazzy.toml b/runtime/themes/snazzy.toml index c0547f33b..da47fd636 100644 --- a/runtime/themes/snazzy.toml +++ b/runtime/themes/snazzy.toml @@ -38,7 +38,7 @@ "ui.text" = { fg = "foreground" } "ui.text.focus" = { fg = "cyan" } "ui.window" = { fg = "foreground" } -"ui.virtual" = { fg = "comment" } +"ui.virtual.whitespace" = { fg = "comment" } "error" = { fg = "red" } "warning" = { fg = "cyan" } diff --git a/runtime/themes/solarized_dark.toml b/runtime/themes/solarized_dark.toml index f15e1fa0d..d8126f6ea 100644 --- a/runtime/themes/solarized_dark.toml +++ b/runtime/themes/solarized_dark.toml @@ -39,7 +39,7 @@ # 背景 "ui.background" = { bg = "base03" } -"ui.virtual" = { fg = "base01" } +"ui.virtual.whitespace" = { fg = "base01" } # 行号栏 "ui.linenr" = { fg = "base0", bg = "base02" } diff --git a/runtime/themes/solarized_light.toml b/runtime/themes/solarized_light.toml index eec4220db..cd1028bda 100644 --- a/runtime/themes/solarized_light.toml +++ b/runtime/themes/solarized_light.toml @@ -40,7 +40,7 @@ # background "ui.background" = { bg = "base03" } -"ui.virtual" = { fg = "base01" } +"ui.virtual.whitespace" = { fg = "base01" } # 行号栏 # line number column diff --git a/runtime/themes/spacebones_light.toml b/runtime/themes/spacebones_light.toml index 80a193751..b088e2d3e 100644 --- a/runtime/themes/spacebones_light.toml +++ b/runtime/themes/spacebones_light.toml @@ -64,7 +64,7 @@ "ui.cursor.match" = { bg = "bg3" } "ui.menu" = { fg = "fg1", bg = "bg2" } "ui.menu.selected" = { fg = "#655370", bg = "#d1dcdf", modifiers = ["bold"] } -"ui.virtual" = "bg2" +"ui.virtual.whitespace" = "bg2" "diagnostic" = { modifiers = ["underlined"] } From 6a3f7f2c399f0b92cef97b0c85ebe976fd7cfcac Mon Sep 17 00:00:00 2001 From: Matthew Toohey Date: Tue, 21 Jun 2022 12:36:36 -0400 Subject: [PATCH 11/11] feat: make `move_vertically` aware of tabs and wide characters (#2620) * feat: make `move_vertically` aware of tabs and wide characters * refactor: replace unnecessary checked_sub with comparison * refactor: leave pos_at_coords unchanged and introduce separate pos_at_visual_coords * style: include comment to explain `pos_at_visual_coords` breaking condition * refactor: use `pos_at_visual_coords` in `text_pos_at_screen_coords` * feat: make `copy_selection_on_line` aware of wide characters --- helix-core/src/lib.rs | 4 +- helix-core/src/movement.rs | 43 ++++++++------- helix-core/src/position.rs | 106 +++++++++++++++++++++++++++++++++++-- helix-term/src/commands.rs | 26 ++++----- helix-view/src/view.rs | 53 +++++-------------- 5 files changed, 153 insertions(+), 79 deletions(-) diff --git a/helix-core/src/lib.rs b/helix-core/src/lib.rs index 627b73bb0..735a62c1b 100644 --- a/helix-core/src/lib.rs +++ b/helix-core/src/lib.rs @@ -63,7 +63,9 @@ pub type Tendril = SmartString; pub use {regex, tree_sitter}; pub use graphemes::RopeGraphemes; -pub use position::{coords_at_pos, pos_at_coords, visual_coords_at_pos, Position}; +pub use position::{ + coords_at_pos, pos_at_coords, pos_at_visual_coords, visual_coords_at_pos, Position, +}; pub use selection::{Range, Selection}; pub use smallvec::{smallvec, SmallVec}; pub use syntax::Syntax; diff --git a/helix-core/src/movement.rs b/helix-core/src/movement.rs index f60b3c83f..2155f77a4 100644 --- a/helix-core/src/movement.rs +++ b/helix-core/src/movement.rs @@ -5,16 +5,15 @@ use tree_sitter::{Node, QueryCursor}; use crate::{ chars::{categorize_char, char_is_line_ending, CharCategory}, - coords_at_pos, graphemes::{ next_grapheme_boundary, nth_next_grapheme_boundary, nth_prev_grapheme_boundary, prev_grapheme_boundary, }, line_ending::rope_is_line_ending, - pos_at_coords, + pos_at_visual_coords, syntax::LanguageConfiguration, textobject::TextObject, - Position, Range, RopeSlice, + visual_coords_at_pos, Position, Range, RopeSlice, }; #[derive(Debug, Copy, Clone, PartialEq, Eq)] @@ -35,6 +34,7 @@ pub fn move_horizontally( dir: Direction, count: usize, behaviour: Movement, + _: usize, ) -> Range { let pos = range.cursor(slice); @@ -54,15 +54,12 @@ pub fn move_vertically( dir: Direction, count: usize, behaviour: Movement, + tab_width: usize, ) -> Range { let pos = range.cursor(slice); // Compute the current position's 2d coordinates. - // TODO: switch this to use `visual_coords_at_pos` rather than - // `coords_at_pos` as this will cause a jerky movement when the visual - // position does not match, like moving from a line with tabs/CJK to - // a line without - let Position { row, col } = coords_at_pos(slice, pos); + let Position { row, col } = visual_coords_at_pos(slice, pos, tab_width); let horiz = range.horiz.unwrap_or(col as u32); // Compute the new position. @@ -71,7 +68,7 @@ pub fn move_vertically( Direction::Backward => row.saturating_sub(count), }; let new_col = col.max(horiz as usize); - let new_pos = pos_at_coords(slice, Position::new(new_row, new_col), true); + let new_pos = pos_at_visual_coords(slice, Position::new(new_row, new_col), tab_width); // Special-case to avoid moving to the end of the last non-empty line. if behaviour == Movement::Extend && slice.line(new_row).len_chars() == 0 { @@ -446,6 +443,8 @@ pub fn goto_treesitter_object( mod test { use ropey::Rope; + use crate::{coords_at_pos, pos_at_coords}; + use super::*; const SINGLE_LINE_SAMPLE: &str = "This is a simple alphabetic line"; @@ -472,7 +471,7 @@ mod test { assert_eq!( coords_at_pos( slice, - move_vertically(slice, range, Direction::Forward, 1, Movement::Move).head + move_vertically(slice, range, Direction::Forward, 1, Movement::Move, 4).head ), (1, 3).into() ); @@ -496,7 +495,7 @@ mod test { ]; for ((direction, amount), coordinates) in moves_and_expected_coordinates { - range = move_horizontally(slice, range, direction, amount, Movement::Move); + range = move_horizontally(slice, range, direction, amount, Movement::Move, 0); assert_eq!(coords_at_pos(slice, range.head), coordinates.into()) } } @@ -522,7 +521,7 @@ mod test { ]; for ((direction, amount), coordinates) in moves_and_expected_coordinates { - range = move_horizontally(slice, range, direction, amount, Movement::Move); + range = move_horizontally(slice, range, direction, amount, Movement::Move, 0); assert_eq!(coords_at_pos(slice, range.head), coordinates.into()); assert_eq!(range.head, range.anchor); } @@ -544,7 +543,7 @@ mod test { ]; for (direction, amount) in moves { - range = move_horizontally(slice, range, direction, amount, Movement::Extend); + range = move_horizontally(slice, range, direction, amount, Movement::Extend, 0); assert_eq!(range.anchor, original_anchor); } } @@ -568,7 +567,7 @@ mod test { ]; for ((direction, amount), coordinates) in moves_and_expected_coordinates { - range = move_vertically(slice, range, direction, amount, Movement::Move); + range = move_vertically(slice, range, direction, amount, Movement::Move, 4); assert_eq!(coords_at_pos(slice, range.head), coordinates.into()); assert_eq!(range.head, range.anchor); } @@ -602,8 +601,8 @@ mod test { for ((axis, direction, amount), coordinates) in moves_and_expected_coordinates { range = match axis { - Axis::H => move_horizontally(slice, range, direction, amount, Movement::Move), - Axis::V => move_vertically(slice, range, direction, amount, Movement::Move), + Axis::H => move_horizontally(slice, range, direction, amount, Movement::Move, 0), + Axis::V => move_vertically(slice, range, direction, amount, Movement::Move, 4), }; assert_eq!(coords_at_pos(slice, range.head), coordinates.into()); assert_eq!(range.head, range.anchor); @@ -627,18 +626,18 @@ mod test { let moves_and_expected_coordinates = [ // Places cursor at the fourth kana. ((Axis::H, Direction::Forward, 4), (0, 4)), - // Descent places cursor at the 4th character. - ((Axis::V, Direction::Forward, 1usize), (1, 4)), - // Moving back 1 character. - ((Axis::H, Direction::Backward, 1usize), (1, 3)), + // Descent places cursor at the 8th character. + ((Axis::V, Direction::Forward, 1usize), (1, 8)), + // Moving back 2 characters. + ((Axis::H, Direction::Backward, 2usize), (1, 6)), // Jumping back up 1 line. ((Axis::V, Direction::Backward, 1usize), (0, 3)), ]; for ((axis, direction, amount), coordinates) in moves_and_expected_coordinates { range = match axis { - Axis::H => move_horizontally(slice, range, direction, amount, Movement::Move), - Axis::V => move_vertically(slice, range, direction, amount, Movement::Move), + Axis::H => move_horizontally(slice, range, direction, amount, Movement::Move, 0), + Axis::V => move_vertically(slice, range, direction, amount, Movement::Move, 4), }; assert_eq!(coords_at_pos(slice, range.head), coordinates.into()); assert_eq!(range.head, range.anchor); diff --git a/helix-core/src/position.rs b/helix-core/src/position.rs index ce37300a4..f456eb988 100644 --- a/helix-core/src/position.rs +++ b/helix-core/src/position.rs @@ -109,9 +109,6 @@ pub fn visual_coords_at_pos(text: RopeSlice, pos: usize, tab_width: usize) -> Po /// with left-side block-cursor positions, as this prevents the the block cursor /// from jumping to the next line. Otherwise you typically want it to be `false`, /// such as when dealing with raw anchor/head positions. -/// -/// TODO: this should be changed to work in terms of visual row/column, not -/// graphemes. pub fn pos_at_coords(text: RopeSlice, coords: Position, limit_before_line_ending: bool) -> usize { let Position { mut row, col } = coords; if limit_before_line_ending { @@ -135,6 +132,43 @@ pub fn pos_at_coords(text: RopeSlice, coords: Position, limit_before_line_ending line_start + col_char_offset } +/// Convert visual (line, column) coordinates to a character index. +/// +/// If the `line` coordinate is beyond the end of the file, the EOF +/// position will be returned. +/// +/// If the `column` coordinate is past the end of the given line, the +/// line-end position (in this case, just before the line ending +/// character) will be returned. +pub fn pos_at_visual_coords(text: RopeSlice, coords: Position, tab_width: usize) -> usize { + let Position { mut row, col } = coords; + row = row.min(text.len_lines() - 1); + let line_start = text.line_to_char(row); + let line_end = line_end_char_index(&text, row); + + let mut col_char_offset = 0; + let mut cols_remaining = col; + for grapheme in RopeGraphemes::new(text.slice(line_start..line_end)) { + let grapheme_width = if grapheme == "\t" { + tab_width - ((col - cols_remaining) % tab_width) + } else { + let grapheme = Cow::from(grapheme); + grapheme_width(&grapheme) + }; + + // If pos is in the middle of a wider grapheme (tab for example) + // return the starting offset. + if grapheme_width > cols_remaining { + break; + } + + cols_remaining -= grapheme_width; + col_char_offset += grapheme.chars().count(); + } + + line_start + col_char_offset +} + #[cfg(test)] mod test { use super::*; @@ -305,4 +339,70 @@ mod test { assert_eq!(pos_at_coords(slice, (0, 10).into(), true), 0); assert_eq!(pos_at_coords(slice, (10, 10).into(), true), 0); } + + #[test] + fn test_pos_at_visual_coords() { + let text = Rope::from("ḧëḷḷö\nẅöṛḷḋ"); + let slice = text.slice(..); + assert_eq!(pos_at_visual_coords(slice, (0, 0).into(), 4), 0); + assert_eq!(pos_at_visual_coords(slice, (0, 5).into(), 4), 5); // position on \n + assert_eq!(pos_at_visual_coords(slice, (0, 6).into(), 4), 5); // position after \n + assert_eq!(pos_at_visual_coords(slice, (1, 0).into(), 4), 6); // position on w + assert_eq!(pos_at_visual_coords(slice, (1, 1).into(), 4), 7); // position on o + assert_eq!(pos_at_visual_coords(slice, (1, 4).into(), 4), 10); // position on d + + // Test with wide characters. + let text = Rope::from("今日はいい\n"); + let slice = text.slice(..); + assert_eq!(pos_at_visual_coords(slice, (0, 0).into(), 4), 0); + assert_eq!(pos_at_visual_coords(slice, (0, 1).into(), 4), 0); + assert_eq!(pos_at_visual_coords(slice, (0, 2).into(), 4), 1); + assert_eq!(pos_at_visual_coords(slice, (0, 3).into(), 4), 1); + assert_eq!(pos_at_visual_coords(slice, (0, 4).into(), 4), 2); + assert_eq!(pos_at_visual_coords(slice, (0, 5).into(), 4), 2); + assert_eq!(pos_at_visual_coords(slice, (0, 6).into(), 4), 3); + assert_eq!(pos_at_visual_coords(slice, (0, 7).into(), 4), 3); + assert_eq!(pos_at_visual_coords(slice, (0, 8).into(), 4), 4); + assert_eq!(pos_at_visual_coords(slice, (0, 9).into(), 4), 4); + // assert_eq!(pos_at_visual_coords(slice, (0, 10).into(), 4, false), 5); + // assert_eq!(pos_at_visual_coords(slice, (0, 10).into(), 4, true), 5); + assert_eq!(pos_at_visual_coords(slice, (1, 0).into(), 4), 6); + + // Test with grapheme clusters. + let text = Rope::from("a̐éö̲\r\n"); + let slice = text.slice(..); + assert_eq!(pos_at_visual_coords(slice, (0, 0).into(), 4), 0); + assert_eq!(pos_at_visual_coords(slice, (0, 1).into(), 4), 2); + assert_eq!(pos_at_visual_coords(slice, (0, 2).into(), 4), 4); + assert_eq!(pos_at_visual_coords(slice, (0, 3).into(), 4), 7); // \r\n is one char here + assert_eq!(pos_at_visual_coords(slice, (0, 4).into(), 4), 7); + assert_eq!(pos_at_visual_coords(slice, (1, 0).into(), 4), 9); + + // Test with wide-character grapheme clusters. + let text = Rope::from("किमपि"); + // 2 - 1 - 2 codepoints + // TODO: delete handling as per https://news.ycombinator.com/item?id=20058454 + let slice = text.slice(..); + assert_eq!(pos_at_visual_coords(slice, (0, 0).into(), 4), 0); + assert_eq!(pos_at_visual_coords(slice, (0, 1).into(), 4), 0); + assert_eq!(pos_at_visual_coords(slice, (0, 2).into(), 4), 2); + assert_eq!(pos_at_visual_coords(slice, (0, 3).into(), 4), 3); + + // Test with tabs. + let text = Rope::from("\tHello\n"); + let slice = text.slice(..); + assert_eq!(pos_at_visual_coords(slice, (0, 0).into(), 4), 0); + assert_eq!(pos_at_visual_coords(slice, (0, 1).into(), 4), 0); + assert_eq!(pos_at_visual_coords(slice, (0, 2).into(), 4), 0); + assert_eq!(pos_at_visual_coords(slice, (0, 3).into(), 4), 0); + assert_eq!(pos_at_visual_coords(slice, (0, 4).into(), 4), 1); + assert_eq!(pos_at_visual_coords(slice, (0, 5).into(), 4), 2); + + // Test out of bounds. + let text = Rope::new(); + let slice = text.slice(..); + assert_eq!(pos_at_visual_coords(slice, (10, 0).into(), 4), 0); + assert_eq!(pos_at_visual_coords(slice, (0, 10).into(), 4), 0); + assert_eq!(pos_at_visual_coords(slice, (10, 10).into(), 4), 0); + } } diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 046351a36..d7937ff59 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -16,14 +16,14 @@ use helix_core::{ line_ending::{get_line_ending_of_str, line_end_char_index, str_is_line_ending}, match_brackets, movement::{self, Direction}, - object, pos_at_coords, + object, pos_at_coords, pos_at_visual_coords, regex::{self, Regex, RegexBuilder}, search::{self, CharMatcher}, selection, shellwords, surround, textobject, tree_sitter::Node, unicode::width::UnicodeWidthChar, - LineEnding, Position, Range, Rope, RopeGraphemes, RopeSlice, Selection, SmallVec, Tendril, - Transaction, + visual_coords_at_pos, LineEnding, Position, Range, Rope, RopeGraphemes, RopeSlice, Selection, + SmallVec, Tendril, Transaction, }; use helix_view::{ clipboard::ClipboardType, @@ -511,7 +511,7 @@ fn no_op(_cx: &mut Context) {} fn move_impl(cx: &mut Context, move_fn: F, dir: Direction, behaviour: Movement) where - F: Fn(RopeSlice, Range, Direction, usize, Movement) -> Range, + F: Fn(RopeSlice, Range, Direction, usize, Movement, usize) -> Range, { let count = cx.count(); let (view, doc) = current!(cx.editor); @@ -520,7 +520,7 @@ where let selection = doc .selection(view.id) .clone() - .transform(|range| move_fn(text, range, dir, count, behaviour)); + .transform(|range| move_fn(text, range, dir, count, behaviour, doc.tab_width())); doc.set_selection(view.id, selection); } @@ -1412,9 +1412,10 @@ fn copy_selection_on_line(cx: &mut Context, direction: Direction) { range.head }; - // TODO: this should use visual offsets / pos_at_screen_coords - let head_pos = coords_at_pos(text, head); - let anchor_pos = coords_at_pos(text, range.anchor); + let tab_width = doc.tab_width(); + + let head_pos = visual_coords_at_pos(text, head, tab_width); + let anchor_pos = visual_coords_at_pos(text, range.anchor, tab_width); let height = std::cmp::max(head_pos.row, anchor_pos.row) - std::cmp::min(head_pos.row, anchor_pos.row) @@ -1444,12 +1445,13 @@ fn copy_selection_on_line(cx: &mut Context, direction: Direction) { break; } - let anchor = pos_at_coords(text, Position::new(anchor_row, anchor_pos.col), true); - let head = pos_at_coords(text, Position::new(head_row, head_pos.col), true); + let anchor = + pos_at_visual_coords(text, Position::new(anchor_row, anchor_pos.col), tab_width); + let head = pos_at_visual_coords(text, Position::new(head_row, head_pos.col), tab_width); // skip lines that are too short - if coords_at_pos(text, anchor).col == anchor_pos.col - && coords_at_pos(text, head).col == head_pos.col + if visual_coords_at_pos(text, anchor, tab_width).col == anchor_pos.col + && visual_coords_at_pos(text, head, tab_width).col == head_pos.col { if is_primary { primary_index = ranges.len(); diff --git a/helix-view/src/view.rs b/helix-view/src/view.rs index a496fe330..bfae12a44 100644 --- a/helix-view/src/view.rs +++ b/helix-view/src/view.rs @@ -1,15 +1,9 @@ -use std::borrow::Cow; - use crate::{ graphics::Rect, gutter::{self, Gutter}, Document, DocumentId, ViewId, }; -use helix_core::{ - graphemes::{grapheme_width, RopeGraphemes}, - line_ending::line_end_char_index, - visual_coords_at_pos, Position, RopeSlice, Selection, -}; +use helix_core::{pos_at_visual_coords, visual_coords_at_pos, Position, RopeSlice, Selection}; use std::fmt; @@ -251,44 +245,21 @@ impl View { return None; } - let line_number = (row - inner.y) as usize + self.offset.row; - - if line_number > text.len_lines() - 1 { + let text_row = (row - inner.y) as usize + self.offset.row; + if text_row > text.len_lines() - 1 { return Some(text.len_chars()); } - let mut pos = text.line_to_char(line_number); - - let current_line = text.line(line_number); - - let target = (column - inner.x) as usize + self.offset.col; - let mut col = 0; - - // TODO: extract this part as pos_at_visual_coords - for grapheme in RopeGraphemes::new(current_line) { - if col >= target { - break; - } - - let width = if grapheme == "\t" { - tab_width - (col % tab_width) - } else { - let grapheme = Cow::from(grapheme); - grapheme_width(&grapheme) - }; - - // If pos is in the middle of a wider grapheme (tab for example) - // return the starting offset. - if col + width > target { - break; - } + let text_col = (column - inner.x) as usize + self.offset.col; - col += width; - // TODO: use byte pos that converts back to char pos? - pos += grapheme.chars().count(); - } - - Some(pos.min(line_end_char_index(&text.slice(..), line_number))) + Some(pos_at_visual_coords( + *text, + Position { + row: text_row, + col: text_col, + }, + tab_width, + )) } /// Translates a screen position to position in the text document.