From cab09093dd5bf9b4707bfdfd8529b348c02670ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bla=C5=BE=20Hrastnik?= Date: Mon, 6 Dec 2021 12:24:25 +0900 Subject: [PATCH 01/81] fix: Normalize backtab into shift-tab Fixes #1150 --- book/src/remapping.md | 5 ++--- helix-term/src/ui/menu.rs | 2 +- helix-term/src/ui/picker.rs | 2 +- helix-term/src/ui/prompt.rs | 2 +- helix-view/src/input.rs | 24 ++++++++++++++---------- helix-view/src/keyboard.rs | 5 +---- 6 files changed, 20 insertions(+), 20 deletions(-) diff --git a/book/src/remapping.md b/book/src/remapping.md index fffd189b7..ad63b60bd 100644 --- a/book/src/remapping.md +++ b/book/src/remapping.md @@ -42,10 +42,9 @@ Control, Shift and Alt modifiers are encoded respectively with the prefixes | Down | `"down"` | | Home | `"home"` | | End | `"end"` | -| Page | `"pageup"` | -| Page | `"pagedown"` | +| Page Up | `"pageup"` | +| Page Down | `"pagedown"` | | Tab | `"tab"` | -| Back | `"backtab"` | | Delete | `"del"` | | Insert | `"ins"` | | Null | `"null"` | diff --git a/helix-term/src/ui/menu.rs b/helix-term/src/ui/menu.rs index e891c1492..9a885a36e 100644 --- a/helix-term/src/ui/menu.rs +++ b/helix-term/src/ui/menu.rs @@ -202,7 +202,7 @@ impl Component for Menu { return close_fn; } // arrow up/ctrl-p/shift-tab prev completion choice (including updating the doc) - shift!(BackTab) | key!(Up) | ctrl!('p') | ctrl!('k') => { + shift!(Tab) | key!(Up) | ctrl!('p') | ctrl!('k') => { self.move_up(); (self.callback_fn)(cx.editor, self.selection(), MenuEvent::Update); return EventResult::Consumed(None); diff --git a/helix-term/src/ui/picker.rs b/helix-term/src/ui/picker.rs index 16bf08a3e..1c963f971 100644 --- a/helix-term/src/ui/picker.rs +++ b/helix-term/src/ui/picker.rs @@ -410,7 +410,7 @@ impl Component for Picker { }))); match key_event.into() { - shift!(BackTab) | key!(Up) | ctrl!('p') | ctrl!('k') => { + shift!(Tab) | key!(Up) | ctrl!('p') | ctrl!('k') => { self.move_up(); } key!(Tab) | key!(Down) | ctrl!('n') | ctrl!('j') => { diff --git a/helix-term/src/ui/prompt.rs b/helix-term/src/ui/prompt.rs index e90b07727..a7ef231c4 100644 --- a/helix-term/src/ui/prompt.rs +++ b/helix-term/src/ui/prompt.rs @@ -505,7 +505,7 @@ impl Component for Prompt { self.change_completion_selection(CompletionDirection::Forward); (self.callback_fn)(cx, &self.line, PromptEvent::Update) } - shift!(BackTab) => { + shift!(Tab) => { self.change_completion_selection(CompletionDirection::Backward); (self.callback_fn)(cx, &self.line, PromptEvent::Update) } diff --git a/helix-view/src/input.rs b/helix-view/src/input.rs index 580204ccc..b207c3eda 100644 --- a/helix-view/src/input.rs +++ b/helix-view/src/input.rs @@ -36,7 +36,6 @@ pub(crate) mod keys { pub(crate) const PAGEUP: &str = "pageup"; pub(crate) const PAGEDOWN: &str = "pagedown"; pub(crate) const TAB: &str = "tab"; - pub(crate) const BACKTAB: &str = "backtab"; pub(crate) const DELETE: &str = "del"; pub(crate) const INSERT: &str = "ins"; pub(crate) const NULL: &str = "null"; @@ -82,7 +81,6 @@ impl fmt::Display for KeyEvent { KeyCode::PageUp => f.write_str(keys::PAGEUP)?, KeyCode::PageDown => f.write_str(keys::PAGEDOWN)?, KeyCode::Tab => f.write_str(keys::TAB)?, - KeyCode::BackTab => f.write_str(keys::BACKTAB)?, KeyCode::Delete => f.write_str(keys::DELETE)?, KeyCode::Insert => f.write_str(keys::INSERT)?, KeyCode::Null => f.write_str(keys::NULL)?, @@ -116,7 +114,6 @@ impl UnicodeWidthStr for KeyEvent { KeyCode::PageUp => keys::PAGEUP.len(), KeyCode::PageDown => keys::PAGEDOWN.len(), KeyCode::Tab => keys::TAB.len(), - KeyCode::BackTab => keys::BACKTAB.len(), KeyCode::Delete => keys::DELETE.len(), KeyCode::Insert => keys::INSERT.len(), KeyCode::Null => keys::NULL.len(), @@ -166,7 +163,6 @@ impl std::str::FromStr for KeyEvent { keys::PAGEUP => KeyCode::PageUp, keys::PAGEDOWN => KeyCode::PageDown, keys::TAB => KeyCode::Tab, - keys::BACKTAB => KeyCode::BackTab, keys::DELETE => KeyCode::Delete, keys::INSERT => KeyCode::Insert, keys::NULL => KeyCode::Null, @@ -220,12 +216,20 @@ impl<'de> Deserialize<'de> for KeyEvent { #[cfg(feature = "term")] impl From for KeyEvent { - fn from( - crossterm::event::KeyEvent { code, modifiers }: crossterm::event::KeyEvent, - ) -> KeyEvent { - KeyEvent { - code: code.into(), - modifiers: modifiers.into(), + fn from(crossterm::event::KeyEvent { code, modifiers }: crossterm::event::KeyEvent) -> Self { + if code == crossterm::event::KeyCode::BackTab { + // special case for BackTab -> Shift-Tab + let mut modifiers: KeyModifiers = modifiers.into(); + modifiers.insert(KeyModifiers::SHIFT); + Self { + code: KeyCode::Tab, + modifiers, + } + } else { + Self { + code: code.into(), + modifiers: modifiers.into(), + } } } } diff --git a/helix-view/src/keyboard.rs b/helix-view/src/keyboard.rs index 810aa0635..f17172091 100644 --- a/helix-view/src/keyboard.rs +++ b/helix-view/src/keyboard.rs @@ -79,8 +79,6 @@ pub enum KeyCode { PageDown, /// Tab key. Tab, - /// Shift + Tab key. - BackTab, /// Delete key. Delete, /// Insert key. @@ -116,7 +114,6 @@ impl From for crossterm::event::KeyCode { KeyCode::PageUp => CKeyCode::PageUp, KeyCode::PageDown => CKeyCode::PageDown, KeyCode::Tab => CKeyCode::Tab, - KeyCode::BackTab => CKeyCode::BackTab, KeyCode::Delete => CKeyCode::Delete, KeyCode::Insert => CKeyCode::Insert, KeyCode::F(f_number) => CKeyCode::F(f_number), @@ -144,7 +141,7 @@ impl From for KeyCode { CKeyCode::PageUp => KeyCode::PageUp, CKeyCode::PageDown => KeyCode::PageDown, CKeyCode::Tab => KeyCode::Tab, - CKeyCode::BackTab => KeyCode::BackTab, + CKeyCode::BackTab => unreachable!("BackTab should have been handled on KeyEvent level"), CKeyCode::Delete => KeyCode::Delete, CKeyCode::Insert => KeyCode::Insert, CKeyCode::F(f_number) => KeyCode::F(f_number), From a2b22ec15207926acf9bbf6617492925a6e50d27 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bla=C5=BE=20Hrastnik?= Date: Mon, 6 Dec 2021 12:48:25 +0900 Subject: [PATCH 02/81] Use binary_search when looking up diagnostics They're sorted by range so they should also be sorted by line --- helix-view/src/gutter.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/helix-view/src/gutter.rs b/helix-view/src/gutter.rs index 86773c1db..4c0edd90b 100644 --- a/helix-view/src/gutter.rs +++ b/helix-view/src/gutter.rs @@ -22,7 +22,7 @@ pub fn diagnostic<'doc>( Box::new(move |line: usize, _selected: bool, out: &mut String| { use helix_core::diagnostic::Severity; - if let Some(diagnostic) = diagnostics.iter().find(|d| d.line == line) { + if let Some(diagnostic) = diagnostics.binary_search_by_key(&line, |d| d.line) { write!(out, "●").unwrap(); return Some(match diagnostic.severity { Some(Severity::Error) => error, From 35ac8154095b7eb853e0bc9bfca0879ec5b60be9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bla=C5=BE=20Hrastnik?= Date: Mon, 6 Dec 2021 12:50:28 +0900 Subject: [PATCH 03/81] Fix compilation nix-direnv issues still mess with my shell.. --- helix-view/src/gutter.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/helix-view/src/gutter.rs b/helix-view/src/gutter.rs index 4c0edd90b..af016c56e 100644 --- a/helix-view/src/gutter.rs +++ b/helix-view/src/gutter.rs @@ -22,7 +22,8 @@ pub fn diagnostic<'doc>( Box::new(move |line: usize, _selected: bool, out: &mut String| { use helix_core::diagnostic::Severity; - if let Some(diagnostic) = diagnostics.binary_search_by_key(&line, |d| d.line) { + if let Ok(index) = diagnostics.binary_search_by_key(&line, |d| d.line) { + let diagnostic = &diagnostics[index]; write!(out, "●").unwrap(); return Some(match diagnostic.severity { Some(Severity::Error) => error, From 93e276cd9d7f121a4dcce6ea03d1c39fec4d3bc9 Mon Sep 17 00:00:00 2001 From: WindSoilder Date: Tue, 7 Dec 2021 00:44:04 +0800 Subject: [PATCH 04/81] Make kill_to_line_end behave like emacs (#1235) --- helix-term/src/commands.rs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 3d583ba8a..3e7ce7123 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -670,8 +670,15 @@ fn kill_to_line_end(cx: &mut Context) { let selection = doc.selection(view.id).clone().transform(|range| { let line = range.cursor_line(text); - let pos = line_end_char_index(&text, line); - range.put_cursor(text, pos, true) + let line_end_pos = line_end_char_index(&text, line); + let pos = range.cursor(text); + + let mut new_range = range.put_cursor(text, line_end_pos, true); + // don't want to remove the line separator itself if the cursor doesn't reach the end of line. + if pos != line_end_pos { + new_range.head = line_end_pos; + } + new_range }); delete_selection_insert_mode(doc, view, &selection); } From 178cd5ecfc75280ae12810cf3c0aeb22284bfdf2 Mon Sep 17 00:00:00 2001 From: Omnikar Date: Mon, 6 Dec 2021 11:44:50 -0500 Subject: [PATCH 05/81] Add note to `keymap.md` regarding `format_selections` (#1230) --- book/src/keymap.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/book/src/keymap.md b/book/src/keymap.md index 59353db90..e1b1bad84 100644 --- a/book/src/keymap.md +++ b/book/src/keymap.md @@ -69,7 +69,7 @@ | `"` `` | Select a register to yank to or paste from | `select_register` | | `>` | Indent selection | `indent` | | `<` | Unindent selection | `unindent` | -| `=` | Format selection (**LSP**) | `format_selections` | +| `=` | Format selection (currently nonfunctional/disabled) (**LSP**) | `format_selections` | | `d` | Delete selection | `delete_selection` | | `Alt-d` | Delete selection, without yanking | `delete_selection_noyank` | | `c` | Change selection (delete and enter insert mode) | `change_selection` | From 9bdbafa0759e482d628b7e1940809963c556ec95 Mon Sep 17 00:00:00 2001 From: Skyler Hawthorne Date: Tue, 7 Dec 2021 20:22:55 -0500 Subject: [PATCH 06/81] Fix solarized selection colors (#1236) * do not select a foreground color in selections, as this eliminates syntax coloring * select lighter color for selections * Make non-primary cursor cyan instead of green --- runtime/themes/solarized_dark.toml | 30 ++++++++++--------- runtime/themes/solarized_light.toml | 46 +++++++++++++++-------------- 2 files changed, 40 insertions(+), 36 deletions(-) diff --git a/runtime/themes/solarized_dark.toml b/runtime/themes/solarized_dark.toml index 984c86ee8..979fdaac7 100644 --- a/runtime/themes/solarized_dark.toml +++ b/runtime/themes/solarized_dark.toml @@ -58,13 +58,13 @@ "ui.highlight" = { fg = "red", modifiers = ["bold", "italic", "underlined"] } # 主光标/selectio -"ui.cursor.primary" = {fg = "base03", bg = "base1"} -"ui.selection.primary" = { fg = "base03", bg = "base01" } -"ui.cursor.select" = {fg = "base02", bg = "green"} -"ui.selection" = { fg = "base02", bg = "yellow" } +"ui.cursor.primary" = { fg = "base03", bg = "base1" } +"ui.cursor.select" = { fg = "base02", bg = "cyan" } +"ui.selection" = { bg = "base0175" } +"ui.selection.primary" = { bg = "base015" } # normal模式的光标 -"ui.cursor" = {fg = "base03", bg = "green"} +"ui.cursor" = {fg = "base02", bg = "cyan"} "ui.cursor.insert" = {fg = "base03", bg = "base3"} # 当前光标匹配的标点符号 "ui.cursor.match" = {modifiers = ["reversed"]} @@ -73,18 +73,20 @@ "error" = { fg = "red", modifiers= ["bold", "underlined"] } "info" = { fg = "blue", modifiers= ["bold", "underlined"] } "hint" = { fg = "base01", modifiers= ["bold", "underlined"] } -"diagnostic" = { mdifiers = ["underlined"] } +"diagnostic" = { modifiers = ["underlined"] } [palette] # 深色 越来越深 -base03 = "#002b36" -base02 = "#073642" -base01 = "#586e75" -base00 = "#657b83" -base0 = "#839496" -base1 = "#93a1a1" -base2 = "#eee8d5" -base3 = "#fdf6e3" +base03 = "#002b36" +base02 = "#073642" +base0175 = "#16404b" +base015 = "#2c4f59" +base01 = "#586e75" +base00 = "#657b83" +base0 = "#839496" +base1 = "#93a1a1" +base2 = "#eee8d5" +base3 = "#fdf6e3" # 浅色 越來越浅 yellow = "#b58900" diff --git a/runtime/themes/solarized_light.toml b/runtime/themes/solarized_light.toml index 0ab1b9626..ded90cc48 100644 --- a/runtime/themes/solarized_light.toml +++ b/runtime/themes/solarized_light.toml @@ -58,13 +58,13 @@ "ui.highlight" = { fg = "red", modifiers = ["bold", "italic", "underlined"] } # 主光标/selectio -"ui.cursor.primary" = {fg = "base03", bg = "base1"} -"ui.selection.primary" = { fg = "base03", bg = "base01" } -"ui.cursor.select" = {fg = "base02", bg = "green"} -"ui.selection" = { fg = "base02", bg = "yellow" } +"ui.cursor.primary" = { fg = "base03", bg = "base1" } +"ui.cursor.select" = { fg = "base02", bg = "cyan" } +"ui.selection" = { bg = "base0175" } +"ui.selection.primary" = { bg = "base015" } # normal模式的光标 -"ui.cursor" = {fg = "base03", bg = "green"} +"ui.cursor" = {fg = "base02", bg = "cyan"} "ui.cursor.insert" = {fg = "base03", bg = "base3"} # 当前光标匹配的标点符号 "ui.cursor.match" = {modifiers = ["reversed"]} @@ -73,26 +73,28 @@ "error" = { fg = "red", modifiers= ["bold", "underlined"] } "info" = { fg = "blue", modifiers= ["bold", "underlined"] } "hint" = { fg = "base01", modifiers= ["bold", "underlined"] } -"diagnostic" = { mdifiers = ["underlined"] } +"diagnostic" = { modifiers = ["underlined"] } [palette] -red = '#dc322f' -green = '#859900' -yellow = '#b58900' -blue = '#268bd2' -magenta = '#d33682' -cyan = '#2aa198' -orange = '#cb4b16' -violet = '#6c71c4' +red = '#dc322f' +green = '#859900' +yellow = '#b58900' +blue = '#268bd2' +magenta = '#d33682' +cyan = '#2aa198' +orange = '#cb4b16' +violet = '#6c71c4' # 深色 越来越深 -base0 = '#657b83' -base1 = '#586e75' -base2 = '#073642' -base3 = '#002b36' +base0 = '#657b83' +base1 = '#586e75' +base2 = '#073642' +base3 = '#002b36' ## 浅色 越來越浅 -base00 = '#839496' -base01 = '#93a1a1' -base02 = '#eee8d5' -base03 = '#fdf6e3' +base00 = '#839496' +base01 = '#93a1a1' +base015 = '#c5c8bd' +base0175 = '#dddbcc' +base02 = '#eee8d5' +base03 = '#fdf6e3' From 71292f9f11bd2b50568efd111239f693be26a36a Mon Sep 17 00:00:00 2001 From: Gokul Soumya Date: Wed, 17 Nov 2021 19:00:11 +0530 Subject: [PATCH 07/81] docs: Auto generate command list --- .cargo/config | 2 + Cargo.lock | 7 +++ Cargo.toml | 1 + README.md | 16 +----- book/src/SUMMARY.md | 3 +- book/src/commands.md | 5 ++ book/src/generated/typable-cmd.md | 43 ++++++++++++++++ docs/CONTRIBUTING.md | 37 +++++++++++++ helix-term/src/commands.rs | 8 +-- xtask/Cargo.toml | 9 ++++ xtask/src/main.rs | 86 +++++++++++++++++++++++++++++++ 11 files changed, 197 insertions(+), 20 deletions(-) create mode 100644 .cargo/config create mode 100644 book/src/commands.md create mode 100644 book/src/generated/typable-cmd.md create mode 100644 docs/CONTRIBUTING.md create mode 100644 xtask/Cargo.toml create mode 100644 xtask/src/main.rs diff --git a/.cargo/config b/.cargo/config new file mode 100644 index 000000000..35049cbcb --- /dev/null +++ b/.cargo/config @@ -0,0 +1,2 @@ +[alias] +xtask = "run --package xtask --" diff --git a/Cargo.lock b/Cargo.lock index 47a6c01e0..b30324650 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1259,3 +1259,10 @@ name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" + +[[package]] +name = "xtask" +version = "0.5.0" +dependencies = [ + "helix-term", +] diff --git a/Cargo.toml b/Cargo.toml index 580cccd64..8c3ee6717 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,6 +6,7 @@ members = [ "helix-tui", "helix-syntax", "helix-lsp", + "xtask", ] # Build helix-syntax in release mode to make the code path faster in development. diff --git a/README.md b/README.md index 3f4087b9f..f651e30cb 100644 --- a/README.md +++ b/README.md @@ -65,21 +65,7 @@ brew install helix # Contributing -Contributors are very welcome! **No contribution is too small and all contributions are valued.** - -Some suggestions to get started: - -- You can look at the [good first issue](https://github.com/helix-editor/helix/issues?q=is%3Aopen+label%3AE-easy+label%3AE-good-first-issue) label on the issue tracker. -- Help with packaging on various distributions needed! -- To use print debugging to the [Helix log file](https://github.com/helix-editor/helix/wiki/FAQ#access-the-log-file), you must: - * Print using `log::info!`, `warn!`, or `error!`. (`log::info!("helix!")`) - * Pass the appropriate verbosity level option for the desired log level. (`hx -v ` for info, more `v`s for higher severity inclusive) -- If your preferred language is missing, integrating a tree-sitter grammar for - it and defining syntax highlight queries for it is straight forward and - doesn't require much knowledge of the internals. - -We provide an [architecture.md](./docs/architecture.md) that should give you -a good overview of the internals. +Contributing guidelines can be found [here](./docs/CONTRIBUTING.md). # Getting help diff --git a/book/src/SUMMARY.md b/book/src/SUMMARY.md index 8cadb663f..4da79925e 100644 --- a/book/src/SUMMARY.md +++ b/book/src/SUMMARY.md @@ -2,10 +2,11 @@ - [Installation](./install.md) - [Usage](./usage.md) + - [Keymap](./keymap.md) + - [Commands](./commands.md) - [Migrating from Vim](./from-vim.md) - [Configuration](./configuration.md) - [Themes](./themes.md) - - [Keymap](./keymap.md) - [Key Remapping](./remapping.md) - [Hooks](./hooks.md) - [Languages](./languages.md) diff --git a/book/src/commands.md b/book/src/commands.md new file mode 100644 index 000000000..4c4a5c05c --- /dev/null +++ b/book/src/commands.md @@ -0,0 +1,5 @@ +# Commands + +Command mode can be activated by pressing `:`, similar to vim. Built-in commands: + +{{#include ./generated/typable-cmd.md}} diff --git a/book/src/generated/typable-cmd.md b/book/src/generated/typable-cmd.md new file mode 100644 index 000000000..5de5c787d --- /dev/null +++ b/book/src/generated/typable-cmd.md @@ -0,0 +1,43 @@ +| Name | Description | +| --- | --- | +| `:quit`, `:q` | Close the current view. | +| `:quit!`, `:q!` | Close the current view forcefully (ignoring unsaved changes). | +| `:open`, `:o` | Open a file from disk into the current view. | +| `:buffer-close`, `:bc`, `:bclose` | Close the current buffer. | +| `:buffer-close!`, `:bc!`, `:bclose!` | Close the current buffer forcefully (ignoring unsaved changes). | +| `:write`, `:w` | Write changes to disk. Accepts an optional path (:write some/path.txt) | +| `:new`, `:n` | Create a new scratch buffer. | +| `:format`, `:fmt` | Format the file using the LSP formatter. | +| `:indent-style` | Set the indentation style for editing. ('t' for tabs or 1-8 for number of spaces.) | +| `:line-ending` | Set the document's default line ending. Options: crlf, lf, cr, ff, nel. | +| `:earlier`, `:ear` | Jump back to an earlier point in edit history. Accepts a number of steps or a time span. | +| `:later`, `:lat` | Jump to a later point in edit history. Accepts a number of steps or a time span. | +| `:write-quit`, `:wq`, `:x` | Write changes to disk and close the current view. Accepts an optional path (:wq some/path.txt) | +| `:write-quit!`, `:wq!`, `:x!` | Write changes to disk and close the current view forcefully. Accepts an optional path (:wq! some/path.txt) | +| `:write-all`, `:wa` | Write changes from all views to disk. | +| `:write-quit-all`, `:wqa`, `:xa` | Write changes from all views to disk and close all views. | +| `:write-quit-all!`, `:wqa!`, `:xa!` | Write changes from all views to disk and close all views forcefully (ignoring unsaved changes). | +| `:quit-all`, `:qa` | Close all views. | +| `:quit-all!`, `:qa!` | Close all views forcefully (ignoring unsaved changes). | +| `:cquit`, `:cq` | Quit with exit code (default 1). Accepts an optional integer exit code (:cq 2). | +| `:theme` | Change the editor theme. | +| `:clipboard-yank` | Yank main selection into system clipboard. | +| `:clipboard-yank-join` | Yank joined selections into system clipboard. A separator can be provided as first argument. Default value is newline. | +| `:primary-clipboard-yank` | Yank main selection into system primary clipboard. | +| `:primary-clipboard-yank-join` | Yank joined selections into system primary clipboard. A separator can be provided as first argument. Default value is newline. | +| `:clipboard-paste-after` | Paste system clipboard after selections. | +| `:clipboard-paste-before` | Paste system clipboard before selections. | +| `:clipboard-paste-replace` | Replace selections with content of system clipboard. | +| `:primary-clipboard-paste-after` | Paste primary clipboard after selections. | +| `:primary-clipboard-paste-before` | Paste primary clipboard before selections. | +| `:primary-clipboard-paste-replace` | Replace selections with content of system primary clipboard. | +| `:show-clipboard-provider` | Show clipboard provider name in status bar. | +| `:change-current-directory`, `:cd` | Change the current working directory. | +| `:show-directory`, `:pwd` | Show the current working directory. | +| `:encoding` | Set encoding based on `https://encoding.spec.whatwg.org` | +| `:reload` | Discard changes and reload from the source file. | +| `:tree-sitter-scopes` | Display tree sitter scopes, primarily for theming and development. | +| `:vsplit`, `:vs` | Open the file in a vertical split. | +| `:hsplit`, `:hs`, `:sp` | Open the file in a horizontal split. | +| `:tutor` | Open the tutorial. | +| `:goto`, `:g` | Go to line number. | diff --git a/docs/CONTRIBUTING.md b/docs/CONTRIBUTING.md new file mode 100644 index 000000000..7b923db8c --- /dev/null +++ b/docs/CONTRIBUTING.md @@ -0,0 +1,37 @@ +# Contributing + +Contributors are very welcome! **No contribution is too small and all contributions are valued.** + +Some suggestions to get started: + +- You can look at the [good first issue][good-first-issue] label on the issue tracker. +- Help with packaging on various distributions needed! +- To use print debugging to the [Helix log file][log-file], you must: + * Print using `log::info!`, `warn!`, or `error!`. (`log::info!("helix!")`) + * Pass the appropriate verbosity level option for the desired log level. (`hx -v ` for info, more `v`s for higher severity inclusive) +- If your preferred language is missing, integrating a tree-sitter grammar for + it and defining syntax highlight queries for it is straight forward and + doesn't require much knowledge of the internals. + +We provide an [architecture.md][architecture.md] that should give you +a good overview of the internals. + +# Auto generated documentation + +Some parts of [the book][docs] are autogenerated from the code itself, +like the list of `:commands` and supported languages. To generate these +files, run + +```shell +cargo xtask bookgen +``` + +inside the project. We use [xtask][xtask] as an ad-hoc task runner and +thus do not require any dependencies other than `cargo` (You don't have +to `cargo install` anything either). + +[good-first-issue]: https://github.com/helix-editor/helix/labels/E-easy +[log-file]: https://github.com/helix-editor/helix/wiki/FAQ#access-the-log-file +[architecture.md]: ./architecture.md +[docs]: https://docs.helix-editor.com/ +[xtask]: https://github.com/matklad/cargo-xtask diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 3e7ce7123..4910790a7 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -1949,7 +1949,7 @@ fn append_mode(cx: &mut Context) { doc.set_selection(view.id, selection); } -mod cmd { +pub mod cmd { use super::*; use std::collections::HashMap; @@ -2679,7 +2679,7 @@ mod cmd { TypableCommand { name: "format", aliases: &["fmt"], - doc: "Format the file using a formatter.", + doc: "Format the file using the LSP formatter.", fun: format, completer: None, }, @@ -2770,7 +2770,7 @@ mod cmd { TypableCommand { name: "theme", aliases: &[], - doc: "Change the theme of current view. Requires theme name as argument (:theme )", + doc: "Change the editor theme.", fun: theme, completer: Some(completers::theme), }, @@ -2854,7 +2854,7 @@ mod cmd { TypableCommand { name: "change-current-directory", aliases: &["cd"], - doc: "Change the current working directory (:cd ).", + doc: "Change the current working directory.", fun: change_current_directory, completer: Some(completers::directory), }, diff --git a/xtask/Cargo.toml b/xtask/Cargo.toml new file mode 100644 index 000000000..cb890de9d --- /dev/null +++ b/xtask/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "xtask" +version = "0.5.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +helix-term = { version = "0.5", path = "../helix-term" } diff --git a/xtask/src/main.rs b/xtask/src/main.rs new file mode 100644 index 000000000..4bf0ae9f8 --- /dev/null +++ b/xtask/src/main.rs @@ -0,0 +1,86 @@ +use std::env; + +pub mod md_gen { + use super::path; + use std::fs; + + use helix_term::commands::cmd::TYPABLE_COMMAND_LIST; + + pub const TYPABLE_COMMANDS_MD_OUTPUT: &str = "typable-cmd.md"; + + pub fn typable_commands() -> String { + let mut md = String::new(); + md.push_str("| Name | Description |\n"); + md.push_str("| --- | --- |\n"); + + let cmdify = |s: &str| format!("`:{}`", s); + + for cmd in TYPABLE_COMMAND_LIST { + let names = std::iter::once(&cmd.name) + .chain(cmd.aliases.iter()) + .map(|a| cmdify(a)) + .collect::>() + .join(", "); + + let entry = format!("| {} | {} |\n", names, cmd.doc); + md.push_str(&entry); + } + + md + } + + pub fn write(filename: &str, data: &str) { + let error = format!("Could not write to {}", filename); + let path = path::book_gen().join(filename); + fs::write(path, data).expect(&error); + } +} + +pub mod path { + use std::path::{Path, PathBuf}; + + pub fn project_root() -> PathBuf { + Path::new(env!("CARGO_MANIFEST_DIR")) + .parent() + .unwrap() + .to_path_buf() + } + + pub fn book_gen() -> PathBuf { + project_root().join("book/src/generated/") + } +} + +pub mod tasks { + use super::md_gen; + + pub fn bookgen() { + md_gen::write( + md_gen::TYPABLE_COMMANDS_MD_OUTPUT, + &md_gen::typable_commands(), + ); + } + + pub fn print_help() { + println!( + " +Usage: Run with `cargo xtask `, eg. `cargo xtask bookgen`. + + Tasks: + bookgen: Generate files to be included in the mdbook output. +" + ); + } +} + +fn main() -> Result<(), String> { + let task = env::args().nth(1); + match task { + None => tasks::print_help(), + Some(t) => match t.as_str() { + "bookgen" => tasks::bookgen(), + invalid => return Err(format!("Invalid task name: {}", invalid)), + }, + }; + Ok(()) +} From a78b7894066b6ccf56c404b7543b45e2dfd99982 Mon Sep 17 00:00:00 2001 From: Gokul Soumya Date: Mon, 22 Nov 2021 00:25:08 +0530 Subject: [PATCH 08/81] Auto generate docs for language support --- Cargo.lock | 2 + book/src/SUMMARY.md | 1 + book/src/generated/lang-support.md | 41 ++++++ book/src/generated/typable-cmd.md | 2 +- book/src/lang-support.md | 10 ++ docs/CONTRIBUTING.md | 2 +- helix-core/src/indent.rs | 1 + helix-core/src/syntax.rs | 3 +- languages.toml | 40 +++++ xtask/Cargo.toml | 2 + xtask/src/main.rs | 229 ++++++++++++++++++++++++++--- 11 files changed, 311 insertions(+), 22 deletions(-) create mode 100644 book/src/generated/lang-support.md create mode 100644 book/src/lang-support.md diff --git a/Cargo.lock b/Cargo.lock index b30324650..25b4f6cd0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1264,5 +1264,7 @@ checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" name = "xtask" version = "0.5.0" dependencies = [ + "helix-core", "helix-term", + "toml", ] diff --git a/book/src/SUMMARY.md b/book/src/SUMMARY.md index 4da79925e..a8f165c01 100644 --- a/book/src/SUMMARY.md +++ b/book/src/SUMMARY.md @@ -4,6 +4,7 @@ - [Usage](./usage.md) - [Keymap](./keymap.md) - [Commands](./commands.md) + - [Language Support](./lang-support.md) - [Migrating from Vim](./from-vim.md) - [Configuration](./configuration.md) - [Themes](./themes.md) diff --git a/book/src/generated/lang-support.md b/book/src/generated/lang-support.md new file mode 100644 index 000000000..729801ad5 --- /dev/null +++ b/book/src/generated/lang-support.md @@ -0,0 +1,41 @@ +| Language | Syntax Highlighting | Treesitter Textobjects | Auto Indent | Default LSP | +| --- | --- | --- | --- | --- | +| Bash | ✓ | | | `bash-language-server` | +| C | ✓ | | | `clangd` | +| C# | ✓ | | | | +| CMake | ✓ | | | `cmake-language-server` | +| C++ | ✓ | | | `clangd` | +| CSS | ✓ | | | | +| Elixir | ✓ | | | `elixir-ls` | +| GLSL | ✓ | | ✓ | | +| Go | ✓ | ✓ | ✓ | `gopls` | +| HTML | ✓ | | | | +| Java | ✓ | | | | +| JavaScript | ✓ | | ✓ | | +| JSON | ✓ | | ✓ | | +| Julia | ✓ | | | `julia` | +| LaTeX | ✓ | | | | +| Ledger | ✓ | | | | +| LLVM | ✓ | | | | +| Lua | ✓ | | ✓ | | +| Mint | | | | `mint` | +| Nix | ✓ | | ✓ | `rnix-lsp` | +| OCaml | ✓ | | ✓ | | +| OCaml-Interface | ✓ | | | | +| Perl | ✓ | ✓ | | | +| PHP | ✓ | | ✓ | | +| Prolog | | | | `swipl` | +| Protobuf | ✓ | | ✓ | | +| Python | ✓ | ✓ | ✓ | `pylsp` | +| Racket | | | | `racket` | +| Ruby | ✓ | | | `solargraph` | +| Rust | ✓ | ✓ | ✓ | `rust-analyzer` | +| Svelte | ✓ | | ✓ | `svelteserver` | +| TOML | ✓ | | | | +| TSQ | ✓ | | | | +| TSX | ✓ | | | `typescript-language-server` | +| TypeScript | ✓ | | ✓ | `typescript-language-server` | +| Vue | ✓ | | | | +| WGSL | ✓ | | | | +| YAML | ✓ | | ✓ | | +| Zig | ✓ | | ✓ | `zls` | diff --git a/book/src/generated/typable-cmd.md b/book/src/generated/typable-cmd.md index 5de5c787d..bb21fd6b3 100644 --- a/book/src/generated/typable-cmd.md +++ b/book/src/generated/typable-cmd.md @@ -1,5 +1,5 @@ | Name | Description | -| --- | --- | +| --- | --- | | `:quit`, `:q` | Close the current view. | | `:quit!`, `:q!` | Close the current view forcefully (ignoring unsaved changes). | | `:open`, `:o` | Open a file from disk into the current view. | diff --git a/book/src/lang-support.md b/book/src/lang-support.md new file mode 100644 index 000000000..3920f3424 --- /dev/null +++ b/book/src/lang-support.md @@ -0,0 +1,10 @@ +# Language Support + +For more information like arguments passed to default LSP server, +extensions assosciated with a filetype, custom LSP settings, filetype +specific indent settings, etc see the default +[`languages.toml`][languages.toml] file. + +{{#include ./generated/lang-support.md}} + +[languages.toml]: https://github.com/helix-editor/helix/blob/master/languages.toml diff --git a/docs/CONTRIBUTING.md b/docs/CONTRIBUTING.md index 7b923db8c..bdd771aaf 100644 --- a/docs/CONTRIBUTING.md +++ b/docs/CONTRIBUTING.md @@ -23,7 +23,7 @@ like the list of `:commands` and supported languages. To generate these files, run ```shell -cargo xtask bookgen +cargo xtask docgen ``` inside the project. We use [xtask][xtask] as an ad-hoc task runner and diff --git a/helix-core/src/indent.rs b/helix-core/src/indent.rs index b6f5081ac..3ce3620a2 100644 --- a/helix-core/src/indent.rs +++ b/helix-core/src/indent.rs @@ -452,6 +452,7 @@ where file_types: vec!["rs".to_string()], shebangs: vec![], language_id: "Rust".to_string(), + display_name: "Rust".to_string(), highlight_config: OnceCell::new(), config: None, // diff --git a/helix-core/src/syntax.rs b/helix-core/src/syntax.rs index ba78adaaa..3c65ae33e 100644 --- a/helix-core/src/syntax.rs +++ b/helix-core/src/syntax.rs @@ -50,7 +50,8 @@ pub struct Configuration { #[serde(rename_all = "kebab-case", deny_unknown_fields)] pub struct LanguageConfiguration { #[serde(rename = "name")] - pub language_id: String, + pub language_id: String, // c-sharp, rust + pub display_name: String, // C#, Rust pub scope: String, // source.rust pub file_types: Vec, // filename ends_with? #[serde(default)] diff --git a/languages.toml b/languages.toml index 4208e4b68..ca339c98a 100644 --- a/languages.toml +++ b/languages.toml @@ -1,5 +1,6 @@ [[language]] name = "rust" +display-name = "Rust" scope = "source.rust" injection-regex = "rust" file-types = ["rs"] @@ -14,6 +15,7 @@ procMacro = { enable = false } [[language]] name = "toml" +display-name = "TOML" scope = "source.toml" injection-regex = "toml" file-types = ["toml"] @@ -24,6 +26,7 @@ indent = { tab-width = 2, unit = " " } [[language]] name = "protobuf" +display-name = "Protobuf" scope = "source.proto" injection-regex = "protobuf" file-types = ["proto"] @@ -34,6 +37,7 @@ indent = { tab-width = 2, unit = " " } [[language]] name = "elixir" +display-name = "Elixir" scope = "source.elixir" injection-regex = "elixir" file-types = ["ex", "exs"] @@ -46,6 +50,7 @@ indent = { tab-width = 2, unit = " " } [[language]] name = "mint" +display-name = "Mint" scope = "source.mint" injection-regex = "mint" file-types = ["mint"] @@ -58,6 +63,7 @@ indent = { tab-width = 2, unit = " " } [[language]] name = "json" +display-name = "JSON" scope = "source.json" injection-regex = "json" file-types = ["json"] @@ -67,6 +73,7 @@ indent = { tab-width = 2, unit = " " } [[language]] name = "c" +display-name = "C" scope = "source.c" injection-regex = "c" file-types = ["c"] # TODO: ["h"] @@ -78,6 +85,7 @@ indent = { tab-width = 2, unit = " " } [[language]] name = "cpp" +display-name = "C++" scope = "source.cpp" injection-regex = "cpp" file-types = ["cc", "hh", "cpp", "hpp", "h", "ipp", "tpp", "cxx", "hxx", "ixx", "txx", "ino"] @@ -89,6 +97,7 @@ indent = { tab-width = 2, unit = " " } [[language]] name = "c-sharp" +display-name = "C#" scope = "source.csharp" injection-regex = "c-?sharp" file-types = ["cs"] @@ -99,6 +108,7 @@ indent = { tab-width = 4, unit = "\t" } [[language]] name = "go" +display-name = "Go" scope = "source.go" injection-regex = "go" file-types = ["go"] @@ -112,6 +122,7 @@ indent = { tab-width = 4, unit = "\t" } [[language]] name = "javascript" +display-name = "JavaScript" scope = "source.js" injection-regex = "^(js|javascript)$" file-types = ["js", "mjs"] @@ -124,6 +135,7 @@ indent = { tab-width = 2, unit = " " } [[language]] name = "typescript" +display-name = "TypeScript" scope = "source.ts" injection-regex = "^(ts|typescript)$" file-types = ["ts"] @@ -136,6 +148,7 @@ indent = { tab-width = 2, unit = " " } [[language]] name = "tsx" +display-name = "TSX" scope = "source.tsx" injection-regex = "^(tsx)$" # |typescript file-types = ["tsx"] @@ -147,6 +160,7 @@ indent = { tab-width = 2, unit = " " } [[language]] name = "css" +display-name = "CSS" scope = "source.css" injection-regex = "css" file-types = ["css"] @@ -156,6 +170,7 @@ indent = { tab-width = 2, unit = " " } [[language]] name = "html" +display-name = "HTML" scope = "text.html.basic" injection-regex = "html" file-types = ["html"] @@ -165,6 +180,7 @@ indent = { tab-width = 2, unit = " " } [[language]] name = "python" +display-name = "Python" scope = "source.python" injection-regex = "python" file-types = ["py"] @@ -178,6 +194,7 @@ indent = { tab-width = 4, unit = " " } [[language]] name = "nix" +display-name = "Nix" scope = "source.nix" injection-regex = "nix" file-types = ["nix"] @@ -190,6 +207,7 @@ indent = { tab-width = 2, unit = " " } [[language]] name = "ruby" +display-name = "Ruby" scope = "source.ruby" injection-regex = "ruby" file-types = ["rb"] @@ -202,6 +220,7 @@ indent = { tab-width = 2, unit = " " } [[language]] name = "bash" +display-name = "Bash" scope = "source.bash" injection-regex = "bash" file-types = ["sh", "bash"] @@ -214,6 +233,7 @@ indent = { tab-width = 2, unit = " " } [[language]] name = "php" +display-name = "PHP" scope = "source.php" injection-regex = "php" file-types = ["php"] @@ -224,6 +244,7 @@ indent = { tab-width = 4, unit = " " } [[language]] name = "latex" +display-name = "LaTeX" scope = "source.tex" injection-regex = "tex" file-types = ["tex"] @@ -234,6 +255,7 @@ indent = { tab-width = 4, unit = "\t" } [[language]] name = "julia" +display-name = "Julia" scope = "source.julia" injection-regex = "julia" file-types = ["jl"] @@ -259,6 +281,7 @@ indent = { tab-width = 2, unit = " " } [[language]] name = "java" +display-name = "Java" scope = "source.java" injection-regex = "java" file-types = ["java"] @@ -267,6 +290,7 @@ indent = { tab-width = 4, unit = " " } [[language]] name = "ledger" +display-name = "Ledger" scope = "source.ledger" injection-regex = "ledger" file-types = ["ldg", "ledger", "journal"] @@ -276,6 +300,7 @@ indent = { tab-width = 4, unit = " " } [[language]] name = "ocaml" +display-name = "OCaml" scope = "source.ocaml" injection-regex = "ocaml" file-types = ["ml"] @@ -286,6 +311,7 @@ indent = { tab-width = 2, unit = " " } [[language]] name = "ocaml-interface" +display-name = "OCaml-Interface" scope = "source.ocaml.interface" file-types = ["mli"] shebangs = [] @@ -295,6 +321,7 @@ indent = { tab-width = 2, unit = " "} [[language]] name = "lua" +display-name = "Lua" scope = "source.lua" file-types = ["lua"] shebangs = ["lua"] @@ -304,6 +331,7 @@ indent = { tab-width = 2, unit = " " } [[language]] name = "svelte" +display-name = "Svelte" scope = "source.svelte" injection-regex = "svelte" file-types = ["svelte"] @@ -314,6 +342,7 @@ language-server = { command = "svelteserver", args = ["--stdio"] } [[language]] name = "vue" +display-name = "Vue" scope = "source.vue" injection-regex = "vue" file-types = ["vue"] @@ -322,6 +351,7 @@ indent = { tab-width = 2, unit = " " } [[language]] name = "yaml" +display-name = "YAML" scope = "source.yaml" file-types = ["yml", "yaml"] roots = [] @@ -330,6 +360,7 @@ indent = { tab-width = 2, unit = " " } # [[language]] # name = "haskell" +# display-name = "Haskell" # scope = "source.haskell" # injection-regex = "haskell" # file-types = ["hs"] @@ -340,6 +371,7 @@ indent = { tab-width = 2, unit = " " } [[language]] name = "zig" +display-name = "Zig" scope = "source.zig" injection-regex = "zig" file-types = ["zig"] @@ -352,6 +384,7 @@ indent = { tab-width = 4, unit = " " } [[language]] name = "prolog" +display-name = "Prolog" scope = "source.prolog" roots = [] file-types = ["pl", "prolog"] @@ -365,6 +398,7 @@ language-server = { command = "swipl", args = [ [[language]] name = "tsq" +display-name = "TSQ" scope = "source.tsq" file-types = ["scm"] roots = [] @@ -373,6 +407,7 @@ indent = { tab-width = 2, unit = " " } [[language]] name = "cmake" +display-name = "CMake" scope = "source.cmake" file-types = ["cmake", "CMakeLists.txt"] roots = [] @@ -382,6 +417,7 @@ language-server = { command = "cmake-language-server" } [[language]] name = "glsl" +display-name = "GLSL" scope = "source.glsl" file-types = ["glsl", "vert", "tesc", "tese", "geom", "frag", "comp" ] roots = [] @@ -390,6 +426,7 @@ indent = { tab-width = 4, unit = " " } [[language]] name = "perl" +display-name = "Perl" scope = "source.perl" file-types = ["pl", "pm"] shebangs = ["perl"] @@ -399,6 +436,7 @@ indent = { tab-width = 2, unit = " " } [[language]] name = "racket" +display-name = "Racket" scope = "source.rkt" roots = [] file-types = ["rkt"] @@ -408,6 +446,7 @@ language-server = { command = "racket", args = ["-l", "racket-langserver"] } [[language]] name = "wgsl" +display-name = "WGSL" scope = "source.wgsl" file-types = ["wgsl"] roots = [] @@ -416,6 +455,7 @@ indent = { tab-width = 4, unit = " " } [[language]] name = "llvm" +display-name = "LLVM" scope = "source.llvm" roots = [] file-types = ["ll"] diff --git a/xtask/Cargo.toml b/xtask/Cargo.toml index cb890de9d..fe5d55d40 100644 --- a/xtask/Cargo.toml +++ b/xtask/Cargo.toml @@ -7,3 +7,5 @@ edition = "2021" [dependencies] helix-term = { version = "0.5", path = "../helix-term" } +helix-core = { version = "0.5", path = "../helix-core" } +toml = "0.5" diff --git a/xtask/src/main.rs b/xtask/src/main.rs index 4bf0ae9f8..37e705928 100644 --- a/xtask/src/main.rs +++ b/xtask/src/main.rs @@ -1,17 +1,138 @@ -use std::env; +use std::{env, error::Error}; + +type DynError = Box; + +pub mod helpers { + use std::{ + fmt::Display, + path::{Path, PathBuf}, + }; + + use crate::path; + use helix_core::syntax::Configuration as LangConfig; + + #[derive(Copy, Clone)] + pub enum TsFeature { + Highlight, + TextObjects, + AutoIndent, + } + + impl TsFeature { + pub fn all() -> &'static [Self] { + &[Self::Highlight, Self::TextObjects, Self::AutoIndent] + } + + pub fn runtime_filename(&self) -> &'static str { + match *self { + Self::Highlight => "highlights.scm", + Self::TextObjects => "textobjects.scm", + Self::AutoIndent => "indents.toml", + } + } + } + + impl Display for TsFeature { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!( + f, + "{}", + match *self { + Self::Highlight => "Syntax Highlighting", + Self::TextObjects => "Treesitter Textobjects", + Self::AutoIndent => "Auto Indent", + } + ) + } + } + + /// Get the list of languages that support a particular tree-sitter + /// based feature. + pub fn ts_lang_support(feat: TsFeature) -> Vec { + let queries_dir = path::ts_queries(); + + find_files(&queries_dir, feat.runtime_filename()) + .iter() + .map(|f| { + // .../helix/runtime/queries/python/highlights.scm + let tail = f.strip_prefix(&queries_dir).unwrap(); // python/highlights.scm + let lang = tail.components().next().unwrap(); // python + lang.as_os_str().to_string_lossy().to_string() + }) + .collect() + } + + /// Get the list of languages that have any form of tree-sitter + /// queries defined in the runtime directory. + pub fn langs_with_ts_queries() -> Vec { + std::fs::read_dir(path::ts_queries()) + .unwrap() + .filter_map(|entry| { + let entry = entry.ok()?; + entry + .file_type() + .ok()? + .is_dir() + .then(|| entry.file_name().to_string_lossy().to_string()) + }) + .collect() + } + + // naive implementation, but suffices for our needs + pub fn find_files(dir: &Path, filename: &str) -> Vec { + std::fs::read_dir(dir) + .unwrap() + .filter_map(|entry| { + let path = entry.ok()?.path(); + if path.is_dir() { + Some(find_files(&path, filename)) + } else { + (path.file_name()?.to_string_lossy() == filename).then(|| vec![path]) + } + }) + .flatten() + .collect() + } + + pub fn lang_config() -> LangConfig { + let bytes = std::fs::read(path::lang_config()).unwrap(); + toml::from_slice(&bytes).unwrap() + } +} pub mod md_gen { - use super::path; + use crate::DynError; + + use crate::helpers; + use crate::path; use std::fs; use helix_term::commands::cmd::TYPABLE_COMMAND_LIST; pub const TYPABLE_COMMANDS_MD_OUTPUT: &str = "typable-cmd.md"; + pub const LANG_SUPPORT_MD_OUTPUT: &str = "lang-support.md"; + + fn md_table_heading(cols: &[String]) -> String { + let mut header = String::new(); + header += &md_table_row(cols); + header += &md_table_row(&vec!["---".to_string(); cols.len()]); + header + } + + fn md_table_row(cols: &[String]) -> String { + "| ".to_owned() + &cols.join(" | ") + " |\n" + } + + fn md_mono(s: &str) -> String { + format!("`{}`", s) + } - pub fn typable_commands() -> String { + pub fn typable_commands() -> Result { let mut md = String::new(); - md.push_str("| Name | Description |\n"); - md.push_str("| --- | --- |\n"); + md.push_str(&md_table_heading(&[ + "Name".to_owned(), + "Description".to_owned(), + ])); let cmdify = |s: &str| format!("`:{}`", s); @@ -22,11 +143,72 @@ pub mod md_gen { .collect::>() .join(", "); - let entry = format!("| {} | {} |\n", names, cmd.doc); - md.push_str(&entry); + md.push_str(&md_table_row(&[names.to_owned(), cmd.doc.to_owned()])); + } + + Ok(md) + } + + pub fn lang_features() -> Result { + let mut md = String::new(); + let ts_features = helpers::TsFeature::all(); + + let mut cols = vec!["Language".to_owned()]; + cols.append( + &mut ts_features + .iter() + .map(|t| t.to_string()) + .collect::>(), + ); + cols.push("Default LSP".to_owned()); + + md.push_str(&md_table_heading(&cols)); + let config = helpers::lang_config(); + + let mut langs = config + .language + .iter() + .map(|l| l.language_id.clone()) + .collect::>(); + langs.sort_unstable(); + + let mut ts_features_to_langs = Vec::new(); + for &feat in ts_features { + ts_features_to_langs.push((feat, helpers::ts_lang_support(feat))); } - md + let mut row = Vec::new(); + for lang in langs { + let lc = config + .language + .iter() + .find(|l| l.language_id == lang) + .unwrap(); // lang comes from config + row.push(lc.display_name.clone()); + + for (_feat, support_list) in &ts_features_to_langs { + row.push( + if support_list.contains(&lang) { + "✓" + } else { + "" + } + .to_owned(), + ); + } + row.push( + lc.language_server + .as_ref() + .map(|s| s.command.clone()) + .map(|c| md_mono(&c)) + .unwrap_or_default(), + ); + + md.push_str(&md_table_row(&row)); + row.clear(); + } + + Ok(md) } pub fn write(filename: &str, data: &str) { @@ -49,37 +231,46 @@ pub mod path { pub fn book_gen() -> PathBuf { project_root().join("book/src/generated/") } + + pub fn ts_queries() -> PathBuf { + project_root().join("runtime/queries") + } + + pub fn lang_config() -> PathBuf { + project_root().join("languages.toml") + } } pub mod tasks { - use super::md_gen; + use crate::md_gen; + use crate::DynError; - pub fn bookgen() { - md_gen::write( - md_gen::TYPABLE_COMMANDS_MD_OUTPUT, - &md_gen::typable_commands(), - ); + pub fn docgen() -> Result<(), DynError> { + use md_gen::*; + write(TYPABLE_COMMANDS_MD_OUTPUT, &typable_commands()?); + write(LANG_SUPPORT_MD_OUTPUT, &lang_features()?); + Ok(()) } pub fn print_help() { println!( " -Usage: Run with `cargo xtask `, eg. `cargo xtask bookgen`. +Usage: Run with `cargo xtask `, eg. `cargo xtask docgen`. Tasks: - bookgen: Generate files to be included in the mdbook output. + docgen: Generate files to be included in the mdbook output. " ); } } -fn main() -> Result<(), String> { +fn main() -> Result<(), DynError> { let task = env::args().nth(1); match task { None => tasks::print_help(), Some(t) => match t.as_str() { - "bookgen" => tasks::bookgen(), - invalid => return Err(format!("Invalid task name: {}", invalid)), + "docgen" => tasks::docgen()?, + invalid => return Err(format!("Invalid task name: {}", invalid).into()), }, }; Ok(()) From 70c989e122e498705606da3beb4154564b9a14d2 Mon Sep 17 00:00:00 2001 From: Gokul Soumya Date: Mon, 29 Nov 2021 20:24:24 +0530 Subject: [PATCH 09/81] Add github action to lint unmerged docs --- .github/workflows/build.yml | 48 +++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 216291804..7f18da6a4 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -137,3 +137,51 @@ jobs: with: command: clippy args: -- -D warnings + + docs: + name: Docs + runs-on: ubuntu-latest + steps: + - name: Checkout sources + uses: actions/checkout@v2 + with: + submodules: true + + - name: Install stable toolchain + uses: actions-rs/toolchain@v1 + with: + profile: minimal + toolchain: stable + override: true + + - name: Cache cargo registry + uses: actions/cache@v2.1.6 + with: + path: ~/.cargo/registry + key: ${{ runner.os }}-v2-cargo-registry-${{ hashFiles('**/Cargo.lock') }} + + - name: Cache cargo index + uses: actions/cache@v2.1.6 + with: + path: ~/.cargo/git + key: ${{ runner.os }}-v2-cargo-index-${{ hashFiles('**/Cargo.lock') }} + + - name: Cache cargo target dir + uses: actions/cache@v2.1.6 + with: + path: target + key: ${{ runner.os }}-v2-cargo-build-target-${{ hashFiles('**/Cargo.lock') }} + + - name: Generate docs + uses: actions-rs/cargo@v1 + with: + command: xtask + args: docgen + + - name: Check uncommitted documentation changes + run: | + git diff + git diff-files --quiet \ + || (echo "Run 'cargo xtask docgen', commit the changes and push again" \ + && exit 1) + From d08bdfa838098769afc59146b62f9d613d4a7ff4 Mon Sep 17 00:00:00 2001 From: Gokul Soumya Date: Tue, 7 Dec 2021 21:27:21 +0530 Subject: [PATCH 10/81] Use same name used in config files for langs in docs --- book/src/generated/lang-support.md | 78 +++++++++++++++--------------- helix-core/src/indent.rs | 1 - helix-core/src/syntax.rs | 1 - languages.toml | 41 ---------------- xtask/src/main.rs | 2 +- 5 files changed, 40 insertions(+), 83 deletions(-) diff --git a/book/src/generated/lang-support.md b/book/src/generated/lang-support.md index 729801ad5..96d9b6a07 100644 --- a/book/src/generated/lang-support.md +++ b/book/src/generated/lang-support.md @@ -1,41 +1,41 @@ | Language | Syntax Highlighting | Treesitter Textobjects | Auto Indent | Default LSP | | --- | --- | --- | --- | --- | -| Bash | ✓ | | | `bash-language-server` | -| C | ✓ | | | `clangd` | -| C# | ✓ | | | | -| CMake | ✓ | | | `cmake-language-server` | -| C++ | ✓ | | | `clangd` | -| CSS | ✓ | | | | -| Elixir | ✓ | | | `elixir-ls` | -| GLSL | ✓ | | ✓ | | -| Go | ✓ | ✓ | ✓ | `gopls` | -| HTML | ✓ | | | | -| Java | ✓ | | | | -| JavaScript | ✓ | | ✓ | | -| JSON | ✓ | | ✓ | | -| Julia | ✓ | | | `julia` | -| LaTeX | ✓ | | | | -| Ledger | ✓ | | | | -| LLVM | ✓ | | | | -| Lua | ✓ | | ✓ | | -| Mint | | | | `mint` | -| Nix | ✓ | | ✓ | `rnix-lsp` | -| OCaml | ✓ | | ✓ | | -| OCaml-Interface | ✓ | | | | -| Perl | ✓ | ✓ | | | -| PHP | ✓ | | ✓ | | -| Prolog | | | | `swipl` | -| Protobuf | ✓ | | ✓ | | -| Python | ✓ | ✓ | ✓ | `pylsp` | -| Racket | | | | `racket` | -| Ruby | ✓ | | | `solargraph` | -| Rust | ✓ | ✓ | ✓ | `rust-analyzer` | -| Svelte | ✓ | | ✓ | `svelteserver` | -| TOML | ✓ | | | | -| TSQ | ✓ | | | | -| TSX | ✓ | | | `typescript-language-server` | -| TypeScript | ✓ | | ✓ | `typescript-language-server` | -| Vue | ✓ | | | | -| WGSL | ✓ | | | | -| YAML | ✓ | | ✓ | | -| Zig | ✓ | | ✓ | `zls` | +| bash | ✓ | | | `bash-language-server` | +| c | ✓ | | | `clangd` | +| c-sharp | ✓ | | | | +| cmake | ✓ | | | `cmake-language-server` | +| cpp | ✓ | | | `clangd` | +| css | ✓ | | | | +| elixir | ✓ | | | `elixir-ls` | +| glsl | ✓ | | ✓ | | +| go | ✓ | ✓ | ✓ | `gopls` | +| html | ✓ | | | | +| java | ✓ | | | | +| javascript | ✓ | | ✓ | | +| json | ✓ | | ✓ | | +| julia | ✓ | | | `julia` | +| latex | ✓ | | | | +| ledger | ✓ | | | | +| llvm | ✓ | | | | +| lua | ✓ | | ✓ | | +| mint | | | | `mint` | +| nix | ✓ | | ✓ | `rnix-lsp` | +| ocaml | ✓ | | ✓ | | +| ocaml-interface | ✓ | | | | +| perl | ✓ | ✓ | | | +| php | ✓ | | ✓ | | +| prolog | | | | `swipl` | +| protobuf | ✓ | | ✓ | | +| python | ✓ | ✓ | ✓ | `pylsp` | +| racket | | | | `racket` | +| ruby | ✓ | | | `solargraph` | +| rust | ✓ | ✓ | ✓ | `rust-analyzer` | +| svelte | ✓ | | ✓ | `svelteserver` | +| toml | ✓ | | | | +| tsq | ✓ | | | | +| tsx | ✓ | | | `typescript-language-server` | +| typescript | ✓ | | ✓ | `typescript-language-server` | +| vue | ✓ | | | | +| wgsl | ✓ | | | | +| yaml | ✓ | | ✓ | | +| zig | ✓ | | ✓ | `zls` | diff --git a/helix-core/src/indent.rs b/helix-core/src/indent.rs index 3ce3620a2..b6f5081ac 100644 --- a/helix-core/src/indent.rs +++ b/helix-core/src/indent.rs @@ -452,7 +452,6 @@ where file_types: vec!["rs".to_string()], shebangs: vec![], language_id: "Rust".to_string(), - display_name: "Rust".to_string(), highlight_config: OnceCell::new(), config: None, // diff --git a/helix-core/src/syntax.rs b/helix-core/src/syntax.rs index 3c65ae33e..ef35fc756 100644 --- a/helix-core/src/syntax.rs +++ b/helix-core/src/syntax.rs @@ -51,7 +51,6 @@ pub struct Configuration { pub struct LanguageConfiguration { #[serde(rename = "name")] pub language_id: String, // c-sharp, rust - pub display_name: String, // C#, Rust pub scope: String, // source.rust pub file_types: Vec, // filename ends_with? #[serde(default)] diff --git a/languages.toml b/languages.toml index ca339c98a..428051a7f 100644 --- a/languages.toml +++ b/languages.toml @@ -1,6 +1,5 @@ [[language]] name = "rust" -display-name = "Rust" scope = "source.rust" injection-regex = "rust" file-types = ["rs"] @@ -15,7 +14,6 @@ procMacro = { enable = false } [[language]] name = "toml" -display-name = "TOML" scope = "source.toml" injection-regex = "toml" file-types = ["toml"] @@ -26,7 +24,6 @@ indent = { tab-width = 2, unit = " " } [[language]] name = "protobuf" -display-name = "Protobuf" scope = "source.proto" injection-regex = "protobuf" file-types = ["proto"] @@ -37,7 +34,6 @@ indent = { tab-width = 2, unit = " " } [[language]] name = "elixir" -display-name = "Elixir" scope = "source.elixir" injection-regex = "elixir" file-types = ["ex", "exs"] @@ -50,7 +46,6 @@ indent = { tab-width = 2, unit = " " } [[language]] name = "mint" -display-name = "Mint" scope = "source.mint" injection-regex = "mint" file-types = ["mint"] @@ -63,7 +58,6 @@ indent = { tab-width = 2, unit = " " } [[language]] name = "json" -display-name = "JSON" scope = "source.json" injection-regex = "json" file-types = ["json"] @@ -73,7 +67,6 @@ indent = { tab-width = 2, unit = " " } [[language]] name = "c" -display-name = "C" scope = "source.c" injection-regex = "c" file-types = ["c"] # TODO: ["h"] @@ -85,7 +78,6 @@ indent = { tab-width = 2, unit = " " } [[language]] name = "cpp" -display-name = "C++" scope = "source.cpp" injection-regex = "cpp" file-types = ["cc", "hh", "cpp", "hpp", "h", "ipp", "tpp", "cxx", "hxx", "ixx", "txx", "ino"] @@ -97,7 +89,6 @@ indent = { tab-width = 2, unit = " " } [[language]] name = "c-sharp" -display-name = "C#" scope = "source.csharp" injection-regex = "c-?sharp" file-types = ["cs"] @@ -108,7 +99,6 @@ indent = { tab-width = 4, unit = "\t" } [[language]] name = "go" -display-name = "Go" scope = "source.go" injection-regex = "go" file-types = ["go"] @@ -122,7 +112,6 @@ indent = { tab-width = 4, unit = "\t" } [[language]] name = "javascript" -display-name = "JavaScript" scope = "source.js" injection-regex = "^(js|javascript)$" file-types = ["js", "mjs"] @@ -135,7 +124,6 @@ indent = { tab-width = 2, unit = " " } [[language]] name = "typescript" -display-name = "TypeScript" scope = "source.ts" injection-regex = "^(ts|typescript)$" file-types = ["ts"] @@ -148,7 +136,6 @@ indent = { tab-width = 2, unit = " " } [[language]] name = "tsx" -display-name = "TSX" scope = "source.tsx" injection-regex = "^(tsx)$" # |typescript file-types = ["tsx"] @@ -160,7 +147,6 @@ indent = { tab-width = 2, unit = " " } [[language]] name = "css" -display-name = "CSS" scope = "source.css" injection-regex = "css" file-types = ["css"] @@ -170,7 +156,6 @@ indent = { tab-width = 2, unit = " " } [[language]] name = "html" -display-name = "HTML" scope = "text.html.basic" injection-regex = "html" file-types = ["html"] @@ -180,7 +165,6 @@ indent = { tab-width = 2, unit = " " } [[language]] name = "python" -display-name = "Python" scope = "source.python" injection-regex = "python" file-types = ["py"] @@ -194,7 +178,6 @@ indent = { tab-width = 4, unit = " " } [[language]] name = "nix" -display-name = "Nix" scope = "source.nix" injection-regex = "nix" file-types = ["nix"] @@ -207,7 +190,6 @@ indent = { tab-width = 2, unit = " " } [[language]] name = "ruby" -display-name = "Ruby" scope = "source.ruby" injection-regex = "ruby" file-types = ["rb"] @@ -220,7 +202,6 @@ indent = { tab-width = 2, unit = " " } [[language]] name = "bash" -display-name = "Bash" scope = "source.bash" injection-regex = "bash" file-types = ["sh", "bash"] @@ -233,7 +214,6 @@ indent = { tab-width = 2, unit = " " } [[language]] name = "php" -display-name = "PHP" scope = "source.php" injection-regex = "php" file-types = ["php"] @@ -244,7 +224,6 @@ indent = { tab-width = 4, unit = " " } [[language]] name = "latex" -display-name = "LaTeX" scope = "source.tex" injection-regex = "tex" file-types = ["tex"] @@ -255,7 +234,6 @@ indent = { tab-width = 4, unit = "\t" } [[language]] name = "julia" -display-name = "Julia" scope = "source.julia" injection-regex = "julia" file-types = ["jl"] @@ -271,7 +249,6 @@ language-server = { command = "julia", args = [ using Pkg; import StaticLint; env_path = dirname(Pkg.Types.Context().env.project_file); - server = LanguageServer.LanguageServerInstance(stdin, stdout, env_path, ""); server.runlinter = true; run(server); @@ -281,7 +258,6 @@ indent = { tab-width = 2, unit = " " } [[language]] name = "java" -display-name = "Java" scope = "source.java" injection-regex = "java" file-types = ["java"] @@ -290,7 +266,6 @@ indent = { tab-width = 4, unit = " " } [[language]] name = "ledger" -display-name = "Ledger" scope = "source.ledger" injection-regex = "ledger" file-types = ["ldg", "ledger", "journal"] @@ -300,7 +275,6 @@ indent = { tab-width = 4, unit = " " } [[language]] name = "ocaml" -display-name = "OCaml" scope = "source.ocaml" injection-regex = "ocaml" file-types = ["ml"] @@ -311,7 +285,6 @@ indent = { tab-width = 2, unit = " " } [[language]] name = "ocaml-interface" -display-name = "OCaml-Interface" scope = "source.ocaml.interface" file-types = ["mli"] shebangs = [] @@ -321,7 +294,6 @@ indent = { tab-width = 2, unit = " "} [[language]] name = "lua" -display-name = "Lua" scope = "source.lua" file-types = ["lua"] shebangs = ["lua"] @@ -331,7 +303,6 @@ indent = { tab-width = 2, unit = " " } [[language]] name = "svelte" -display-name = "Svelte" scope = "source.svelte" injection-regex = "svelte" file-types = ["svelte"] @@ -342,7 +313,6 @@ language-server = { command = "svelteserver", args = ["--stdio"] } [[language]] name = "vue" -display-name = "Vue" scope = "source.vue" injection-regex = "vue" file-types = ["vue"] @@ -351,7 +321,6 @@ indent = { tab-width = 2, unit = " " } [[language]] name = "yaml" -display-name = "YAML" scope = "source.yaml" file-types = ["yml", "yaml"] roots = [] @@ -360,7 +329,6 @@ indent = { tab-width = 2, unit = " " } # [[language]] # name = "haskell" -# display-name = "Haskell" # scope = "source.haskell" # injection-regex = "haskell" # file-types = ["hs"] @@ -371,7 +339,6 @@ indent = { tab-width = 2, unit = " " } [[language]] name = "zig" -display-name = "Zig" scope = "source.zig" injection-regex = "zig" file-types = ["zig"] @@ -384,7 +351,6 @@ indent = { tab-width = 4, unit = " " } [[language]] name = "prolog" -display-name = "Prolog" scope = "source.prolog" roots = [] file-types = ["pl", "prolog"] @@ -398,7 +364,6 @@ language-server = { command = "swipl", args = [ [[language]] name = "tsq" -display-name = "TSQ" scope = "source.tsq" file-types = ["scm"] roots = [] @@ -407,7 +372,6 @@ indent = { tab-width = 2, unit = " " } [[language]] name = "cmake" -display-name = "CMake" scope = "source.cmake" file-types = ["cmake", "CMakeLists.txt"] roots = [] @@ -417,7 +381,6 @@ language-server = { command = "cmake-language-server" } [[language]] name = "glsl" -display-name = "GLSL" scope = "source.glsl" file-types = ["glsl", "vert", "tesc", "tese", "geom", "frag", "comp" ] roots = [] @@ -426,7 +389,6 @@ indent = { tab-width = 4, unit = " " } [[language]] name = "perl" -display-name = "Perl" scope = "source.perl" file-types = ["pl", "pm"] shebangs = ["perl"] @@ -436,7 +398,6 @@ indent = { tab-width = 2, unit = " " } [[language]] name = "racket" -display-name = "Racket" scope = "source.rkt" roots = [] file-types = ["rkt"] @@ -446,7 +407,6 @@ language-server = { command = "racket", args = ["-l", "racket-langserver"] } [[language]] name = "wgsl" -display-name = "WGSL" scope = "source.wgsl" file-types = ["wgsl"] roots = [] @@ -455,7 +415,6 @@ indent = { tab-width = 4, unit = " " } [[language]] name = "llvm" -display-name = "LLVM" scope = "source.llvm" roots = [] file-types = ["ll"] diff --git a/xtask/src/main.rs b/xtask/src/main.rs index 37e705928..7256653a3 100644 --- a/xtask/src/main.rs +++ b/xtask/src/main.rs @@ -184,7 +184,7 @@ pub mod md_gen { .iter() .find(|l| l.language_id == lang) .unwrap(); // lang comes from config - row.push(lc.display_name.clone()); + row.push(lc.language_id.clone()); for (_feat, support_list) in &ts_features_to_langs { row.push( From 29c053e84e2624feb786f520ebae4c752bc23279 Mon Sep 17 00:00:00 2001 From: Kirawi <67773714+kirawi@users.noreply.github.com> Date: Wed, 8 Dec 2021 02:11:18 -0500 Subject: [PATCH 11/81] Only use a single documentation popup (#1241) --- helix-term/src/commands.rs | 8 ++++++-- helix-term/src/compositor.rs | 12 ++++++++++++ helix-term/src/ui/completion.rs | 2 +- helix-term/src/ui/popup.rs | 8 +++++++- 4 files changed, 26 insertions(+), 4 deletions(-) diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 4910790a7..1f7a22755 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -5130,8 +5130,12 @@ fn hover(cx: &mut Context) { // skip if contents empty let contents = ui::Markdown::new(contents, editor.syn_loader.clone()); - let popup = Popup::new(contents); - compositor.push(Box::new(popup)); + let popup = Popup::new("documentation", contents); + if let Some(doc_popup) = compositor.find_id("documentation") { + *doc_popup = popup; + } else { + compositor.push(Box::new(popup)); + } } }, ); diff --git a/helix-term/src/compositor.rs b/helix-term/src/compositor.rs index 3a644750e..37e679737 100644 --- a/helix-term/src/compositor.rs +++ b/helix-term/src/compositor.rs @@ -64,6 +64,10 @@ pub trait Component: Any + AnyComponent { fn type_name(&self) -> &'static str { std::any::type_name::() } + + fn id(&self) -> Option<&'static str> { + None + } } use anyhow::Error; @@ -184,6 +188,14 @@ impl Compositor { .find(|component| component.type_name() == type_name) .and_then(|component| component.as_any_mut().downcast_mut()) } + + pub fn find_id(&mut self, id: &'static str) -> Option<&mut T> { + let type_name = std::any::type_name::(); + self.layers + .iter_mut() + .find(|component| component.type_name() == type_name && component.id() == Some(id)) + .and_then(|component| component.as_any_mut().downcast_mut()) + } } // View casting, taken straight from Cursive diff --git a/helix-term/src/ui/completion.rs b/helix-term/src/ui/completion.rs index dd782d29d..fcd631998 100644 --- a/helix-term/src/ui/completion.rs +++ b/helix-term/src/ui/completion.rs @@ -168,7 +168,7 @@ impl Completion { } }; }); - let popup = Popup::new(menu); + let popup = Popup::new("completion", menu); let mut completion = Self { popup, start_offset, diff --git a/helix-term/src/ui/popup.rs b/helix-term/src/ui/popup.rs index 8f7921a11..a5310b231 100644 --- a/helix-term/src/ui/popup.rs +++ b/helix-term/src/ui/popup.rs @@ -16,15 +16,17 @@ pub struct Popup { position: Option, size: (u16, u16), scroll: usize, + id: &'static str, } impl Popup { - pub fn new(contents: T) -> Self { + pub fn new(id: &'static str, contents: T) -> Self { Self { contents, position: None, size: (0, 0), scroll: 0, + id, } } @@ -143,4 +145,8 @@ impl Component for Popup { self.contents.render(area, surface, cx); } + + fn id(&self) -> Option<&'static str> { + Some(self.id) + } } From a1e64815cbffe1a35d0692c61cd59cd777a77d67 Mon Sep 17 00:00:00 2001 From: Oskar Nehlin Date: Wed, 8 Dec 2021 16:26:33 +0100 Subject: [PATCH 12/81] Update book to include typable command remapping (#1240) * Update book to include typable command remapping * Add additional example --- book/src/remapping.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/book/src/remapping.md b/book/src/remapping.md index ad63b60bd..1cdf9b1f2 100644 --- a/book/src/remapping.md +++ b/book/src/remapping.md @@ -11,6 +11,8 @@ this: ```toml # At most one section each of 'keys.normal', 'keys.insert' and 'keys.select' [keys.normal] +C-s = ":w" # Maps the Control-s to the typable command :w which is an alias for :write (save file) +C-o = ":open ~/.config/helix/config.toml" # Maps the Control-o to opening of the helix config file a = "move_char_left" # Maps the 'a' key to the move_char_left command w = "move_line_up" # Maps the 'w' key move_line_up "C-S-esc" = "extend_line" # Maps Control-Shift-Escape to extend_line @@ -21,6 +23,7 @@ g = { a = "code_action" } # Maps `ga` to show possible code actions "A-x" = "normal_mode" # Maps Alt-X to enter normal mode j = { k = "normal_mode" } # Maps `jk` to exit insert mode ``` +> NOTE: Typable commands can also be remapped, remember to keep the `:` prefix to indicate it's a typable command. Control, Shift and Alt modifiers are encoded respectively with the prefixes `C-`, `S-` and `A-`. Special keys are encoded as follows: @@ -53,4 +56,4 @@ Control, Shift and Alt modifiers are encoded respectively with the prefixes Keys can be disabled by binding them to the `no_op` command. Commands can be found at [Keymap](https://docs.helix-editor.com/keymap.html) Commands. -> Commands can also be found in the source code at [`helix-term/src/commands.rs`](https://github.com/helix-editor/helix/blob/master/helix-term/src/commands.rs) at the invocation of `commands!` macro. +> Commands can also be found in the source code at [`helix-term/src/commands.rs`](https://github.com/helix-editor/helix/blob/master/helix-term/src/commands.rs) at the invocation of `static_commands!` macro and the `TypableCommandList`. From 44681c50572eb75dc3059e5de2c0f537e2c76706 Mon Sep 17 00:00:00 2001 From: Omnikar Date: Thu, 9 Dec 2021 21:04:31 -0500 Subject: [PATCH 13/81] Add `default-run = "hx"` to `helix-term/Cargo.toml` (#1244) Following the addition of `xtask`, `cargo run` has multiple possible targets, necessitating the usage of `cargo run --bin hx` to run Helix during development. This allows `cargo run` to be used to run `hx`. --- helix-term/Cargo.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/helix-term/Cargo.toml b/helix-term/Cargo.toml index a0079febe..623c5bb94 100644 --- a/helix-term/Cargo.toml +++ b/helix-term/Cargo.toml @@ -9,6 +9,7 @@ categories = ["editor", "command-line-utilities"] repository = "https://github.com/helix-editor/helix" homepage = "https://helix-editor.com" include = ["src/**/*", "README.md"] +default-run = "hx" [package.metadata.nix] build = true From b66d3d3d9dccbb9c28d8611c4a0fc5d74ccb27d6 Mon Sep 17 00:00:00 2001 From: Omnikar Date: Thu, 9 Dec 2021 21:46:24 -0500 Subject: [PATCH 14/81] Add `save_selection` command (#1247) --- book/src/keymap.md | 1 + helix-term/src/commands.rs | 7 +++++++ helix-term/src/keymap.rs | 2 +- 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/book/src/keymap.md b/book/src/keymap.md index e1b1bad84..5a804c3c0 100644 --- a/book/src/keymap.md +++ b/book/src/keymap.md @@ -34,6 +34,7 @@ | `Ctrl-d` | Move half page down | `half_page_down` | | `Ctrl-i` | Jump forward on the jumplist | `jump_forward` | | `Ctrl-o` | Jump backward on the jumplist | `jump_backward` | +| `Ctrl-s` | Save the current selection to the jumplist | `save_selection` | | `v` | Enter [select (extend) mode](#select--extend-mode) | `select_mode` | | `g` | Enter [goto mode](#goto-mode) | N/A | | `m` | Enter [match mode](#match-mode) | N/A | diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 1f7a22755..87c5a63f6 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -362,6 +362,7 @@ impl MappableCommand { expand_selection, "Expand selection to parent syntax node", jump_forward, "Jump forward on jumplist", jump_backward, "Jump backward on jumplist", + save_selection, "Save the current selection to the jumplist", jump_view_right, "Jump to the split to the right", jump_view_left, "Jump to the split to the left", jump_view_up, "Jump to the split above", @@ -5285,6 +5286,12 @@ fn jump_backward(cx: &mut Context) { }; } +fn save_selection(cx: &mut Context) { + push_jump(cx.editor); + cx.editor + .set_status("Selection saved to jumplist".to_owned()); +} + fn rotate_view(cx: &mut Context) { cx.editor.focus_next() } diff --git a/helix-term/src/keymap.rs b/helix-term/src/keymap.rs index 9debbbacf..b1613252b 100644 --- a/helix-term/src/keymap.rs +++ b/helix-term/src/keymap.rs @@ -641,7 +641,7 @@ impl Default for Keymaps { "tab" => jump_forward, // tab == "C-o" => jump_backward, - // "C-s" => save_selection, + "C-s" => save_selection, "space" => { "Space" "f" => file_picker, From 3307f44ce20a71273614f9b30dafb08822e557a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bla=C5=BE=20Hrastnik?= Date: Fri, 10 Dec 2021 19:23:34 +0900 Subject: [PATCH 15/81] ui: popup: Don't allow scrolling past the end of content --- helix-term/src/compositor.rs | 5 +++-- helix-term/src/ui/markdown.rs | 5 ----- helix-term/src/ui/popup.rs | 19 ++++++++++++++++--- 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/helix-term/src/compositor.rs b/helix-term/src/compositor.rs index 37e679737..30554ebb3 100644 --- a/helix-term/src/compositor.rs +++ b/helix-term/src/compositor.rs @@ -55,9 +55,10 @@ pub trait Component: Any + AnyComponent { /// May be used by the parent component to compute the child area. /// viewport is the maximum allowed area, and the child should stay within those bounds. + /// + /// The returned size might be larger than the viewport if the child is too big to fit. + /// In this case the parent can use the values to calculate scroll. fn required_size(&mut self, _viewport: (u16, u16)) -> Option<(u16, u16)> { - // TODO: for scrolling, the scroll wrapper should place a size + offset on the Context - // that way render can use it None } diff --git a/helix-term/src/ui/markdown.rs b/helix-term/src/ui/markdown.rs index ca8303dd9..46657fb9a 100644 --- a/helix-term/src/ui/markdown.rs +++ b/helix-term/src/ui/markdown.rs @@ -241,11 +241,6 @@ impl Component for Markdown { } else if content_width > text_width { text_width = content_width; } - - if height >= viewport.1 { - height = viewport.1; - break; - } } Some((text_width + padding, height)) diff --git a/helix-term/src/ui/popup.rs b/helix-term/src/ui/popup.rs index a5310b231..c55f030f9 100644 --- a/helix-term/src/ui/popup.rs +++ b/helix-term/src/ui/popup.rs @@ -15,6 +15,7 @@ pub struct Popup { contents: T, position: Option, size: (u16, u16), + child_size: (u16, u16), scroll: usize, id: &'static str, } @@ -25,6 +26,7 @@ impl Popup { contents, position: None, size: (0, 0), + child_size: (0, 0), scroll: 0, id, } @@ -70,6 +72,9 @@ impl Popup { pub fn scroll(&mut self, offset: usize, direction: bool) { if direction { self.scroll += offset; + + let max_offset = self.child_size.1.saturating_sub(self.size.1); + self.scroll = (self.scroll + offset).min(max_offset as usize); } else { self.scroll = self.scroll.saturating_sub(offset); } @@ -117,13 +122,21 @@ impl Component for Popup { // tab/enter/ctrl-k or whatever will confirm the selection/ ctrl-n/ctrl-p for scroll. } - fn required_size(&mut self, _viewport: (u16, u16)) -> Option<(u16, u16)> { + fn required_size(&mut self, viewport: (u16, u16)) -> Option<(u16, u16)> { + let max_width = 120.min(viewport.0); + let max_height = 26.min(viewport.1.saturating_sub(2)); // add some spacing in the viewport + let (width, height) = self .contents - .required_size((120, 26)) // max width, max height + .required_size((max_width, max_height)) .expect("Component needs required_size implemented in order to be embedded in a popup"); - self.size = (width, height); + self.child_size = (width, height); + self.size = (width.min(max_width), height.min(max_height)); + + // re-clamp scroll offset + let max_offset = self.child_size.1.saturating_sub(self.size.1); + self.scroll = self.scroll.min(max_offset as usize); Some(self.size) } From 3156577fbf1a97e07e90e11b51c66155f122c3b7 Mon Sep 17 00:00:00 2001 From: ath3 <45574139+ath3@users.noreply.github.com> Date: Sun, 12 Dec 2021 13:13:33 +0100 Subject: [PATCH 16/81] Open files with spaces in filename, allow opening multiple files (#1231) --- helix-core/src/lib.rs | 1 + helix-core/src/shellwords.rs | 164 +++++++++++++++++++++++++++++++++++ helix-term/src/commands.rs | 145 ++++++++++++++++--------------- 3 files changed, 239 insertions(+), 71 deletions(-) create mode 100644 helix-core/src/shellwords.rs diff --git a/helix-core/src/lib.rs b/helix-core/src/lib.rs index 4ae044cce..92a59f31e 100644 --- a/helix-core/src/lib.rs +++ b/helix-core/src/lib.rs @@ -17,6 +17,7 @@ mod position; pub mod register; pub mod search; pub mod selection; +pub mod shellwords; mod state; pub mod surround; pub mod syntax; diff --git a/helix-core/src/shellwords.rs b/helix-core/src/shellwords.rs new file mode 100644 index 000000000..13f6f3e99 --- /dev/null +++ b/helix-core/src/shellwords.rs @@ -0,0 +1,164 @@ +use std::borrow::Cow; + +/// Get the vec of escaped / quoted / doublequoted filenames from the input str +pub fn shellwords(input: &str) -> Vec> { + enum State { + Normal, + NormalEscaped, + Quoted, + QuoteEscaped, + Dquoted, + DquoteEscaped, + } + + use State::*; + + let mut state = Normal; + let mut args: Vec> = Vec::new(); + let mut escaped = String::with_capacity(input.len()); + + let mut start = 0; + let mut end = 0; + + for (i, c) in input.char_indices() { + state = match state { + Normal => match c { + '\\' => { + escaped.push_str(&input[start..i]); + start = i + 1; + NormalEscaped + } + '"' => { + end = i; + Dquoted + } + '\'' => { + end = i; + Quoted + } + c if c.is_ascii_whitespace() => { + end = i; + Normal + } + _ => Normal, + }, + NormalEscaped => Normal, + Quoted => match c { + '\\' => { + escaped.push_str(&input[start..i]); + start = i + 1; + QuoteEscaped + } + '\'' => { + end = i; + Normal + } + _ => Quoted, + }, + QuoteEscaped => Quoted, + Dquoted => match c { + '\\' => { + escaped.push_str(&input[start..i]); + start = i + 1; + DquoteEscaped + } + '"' => { + end = i; + Normal + } + _ => Dquoted, + }, + DquoteEscaped => Dquoted, + }; + + if i >= input.len() - 1 && end == 0 { + end = i + 1; + } + + if end > 0 { + let esc_trim = escaped.trim(); + let inp = &input[start..end]; + + if !(esc_trim.is_empty() && inp.trim().is_empty()) { + if esc_trim.is_empty() { + args.push(inp.into()); + } else { + args.push([escaped, inp.into()].concat().into()); + escaped = "".to_string(); + } + } + start = i + 1; + end = 0; + } + } + args +} + +#[cfg(test)] +mod test { + use super::*; + + #[test] + fn test_normal() { + let input = r#":o single_word twó wörds \three\ \"with\ escaping\\"#; + let result = shellwords(input); + let expected = vec![ + Cow::from(":o"), + Cow::from("single_word"), + Cow::from("twó"), + Cow::from("wörds"), + Cow::from(r#"three "with escaping\"#), + ]; + // TODO test is_owned and is_borrowed, once they get stabilized. + assert_eq!(expected, result); + } + + #[test] + fn test_quoted() { + let quoted = + r#":o 'single_word' 'twó wörds' '' ' ''\three\' \"with\ escaping\\' 'quote incomplete"#; + let result = shellwords(quoted); + let expected = vec![ + Cow::from(":o"), + Cow::from("single_word"), + Cow::from("twó wörds"), + Cow::from(r#"three' "with escaping\"#), + Cow::from("quote incomplete"), + ]; + assert_eq!(expected, result); + } + + #[test] + fn test_dquoted() { + let dquoted = r#":o "single_word" "twó wörds" "" " ""\three\' \"with\ escaping\\" "dquote incomplete"#; + let result = shellwords(dquoted); + let expected = vec![ + Cow::from(":o"), + Cow::from("single_word"), + Cow::from("twó wörds"), + Cow::from(r#"three' "with escaping\"#), + Cow::from("dquote incomplete"), + ]; + assert_eq!(expected, result); + } + + #[test] + fn test_mixed() { + let dquoted = r#":o single_word 'twó wörds' "\three\' \"with\ escaping\\""no space before"'and after' $#%^@ "%^&(%^" ')(*&^%''a\\\\\b' '"#; + let result = shellwords(dquoted); + let expected = vec![ + Cow::from(":o"), + Cow::from("single_word"), + Cow::from("twó wörds"), + Cow::from("three' \"with escaping\\"), + Cow::from("no space before"), + Cow::from("and after"), + Cow::from("$#%^@"), + Cow::from("%^&(%^"), + Cow::from(")(*&^%"), + Cow::from(r#"a\\b"#), + //last ' just changes to quoted but since we dont have anything after it, it should be ignored + ]; + assert_eq!(expected, result); + } +} diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 87c5a63f6..314cd11fd 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -10,7 +10,7 @@ use helix_core::{ movement::{self, Direction}, object, pos_at_coords, regex::{self, Regex, RegexBuilder}, - search, selection, surround, textobject, + search, selection, shellwords, surround, textobject, unicode::width::UnicodeWidthChar, LineEnding, Position, Range, Rope, RopeGraphemes, RopeSlice, Selection, SmallVec, Tendril, Transaction, @@ -173,14 +173,14 @@ impl MappableCommand { pub fn execute(&self, cx: &mut Context) { match &self { MappableCommand::Typable { name, args, doc: _ } => { - let args: Vec<&str> = args.iter().map(|arg| arg.as_str()).collect(); + let args: Vec> = args.iter().map(Cow::from).collect(); if let Some(command) = cmd::TYPABLE_COMMAND_MAP.get(name.as_str()) { let mut cx = compositor::Context { editor: cx.editor, jobs: cx.jobs, scroll: None, }; - if let Err(e) = (command.fun)(&mut cx, &args, PromptEvent::Validate) { + if let Err(e) = (command.fun)(&mut cx, &args[..], PromptEvent::Validate) { cx.editor.set_error(format!("{}", e)); } } @@ -1963,13 +1963,13 @@ pub mod cmd { pub aliases: &'static [&'static str], pub doc: &'static str, // params, flags, helper, completer - pub fun: fn(&mut compositor::Context, &[&str], PromptEvent) -> anyhow::Result<()>, + pub fun: fn(&mut compositor::Context, &[Cow], PromptEvent) -> anyhow::Result<()>, pub completer: Option, } fn quit( cx: &mut compositor::Context, - _args: &[&str], + _args: &[Cow], _event: PromptEvent, ) -> anyhow::Result<()> { // last view and we have unsaved changes @@ -1984,7 +1984,7 @@ pub mod cmd { fn force_quit( cx: &mut compositor::Context, - _args: &[&str], + _args: &[Cow], _event: PromptEvent, ) -> anyhow::Result<()> { cx.editor.close(view!(cx.editor).id); @@ -1994,17 +1994,19 @@ pub mod cmd { fn open( cx: &mut compositor::Context, - args: &[&str], + args: &[Cow], _event: PromptEvent, ) -> anyhow::Result<()> { - let path = args.get(0).context("wrong argument count")?; - let _ = cx.editor.open(path.into(), Action::Replace)?; + ensure!(!args.is_empty(), "wrong argument count"); + for arg in args { + let _ = cx.editor.open(arg.as_ref().into(), Action::Replace)?; + } Ok(()) } fn buffer_close( cx: &mut compositor::Context, - _args: &[&str], + _args: &[Cow], _event: PromptEvent, ) -> anyhow::Result<()> { let view = view!(cx.editor); @@ -2015,7 +2017,7 @@ pub mod cmd { fn force_buffer_close( cx: &mut compositor::Context, - _args: &[&str], + _args: &[Cow], _event: PromptEvent, ) -> anyhow::Result<()> { let view = view!(cx.editor); @@ -2024,15 +2026,12 @@ pub mod cmd { Ok(()) } - fn write_impl>( - cx: &mut compositor::Context, - path: Option

, - ) -> anyhow::Result<()> { + fn write_impl(cx: &mut compositor::Context, path: Option<&Cow>) -> anyhow::Result<()> { let jobs = &mut cx.jobs; let (_, doc) = current!(cx.editor); if let Some(ref path) = path { - doc.set_path(Some(path.as_ref())) + doc.set_path(Some(path.as_ref().as_ref())) .context("invalid filepath")?; } if doc.path().is_none() { @@ -2061,7 +2060,7 @@ pub mod cmd { fn write( cx: &mut compositor::Context, - args: &[&str], + args: &[Cow], _event: PromptEvent, ) -> anyhow::Result<()> { write_impl(cx, args.first()) @@ -2069,7 +2068,7 @@ pub mod cmd { fn new_file( cx: &mut compositor::Context, - _args: &[&str], + _args: &[Cow], _event: PromptEvent, ) -> anyhow::Result<()> { cx.editor.new_file(Action::Replace); @@ -2079,7 +2078,7 @@ pub mod cmd { fn format( cx: &mut compositor::Context, - _args: &[&str], + _args: &[Cow], _event: PromptEvent, ) -> anyhow::Result<()> { let (_, doc) = current!(cx.editor); @@ -2094,7 +2093,7 @@ pub mod cmd { } fn set_indent_style( cx: &mut compositor::Context, - args: &[&str], + args: &[Cow], _event: PromptEvent, ) -> anyhow::Result<()> { use IndentStyle::*; @@ -2114,7 +2113,7 @@ pub mod cmd { // Attempt to parse argument as an indent style. let style = match args.get(0) { Some(arg) if "tabs".starts_with(&arg.to_lowercase()) => Some(Tabs), - Some(&"0") => Some(Tabs), + Some(Cow::Borrowed("0")) => Some(Tabs), Some(arg) => arg .parse::() .ok() @@ -2133,7 +2132,7 @@ pub mod cmd { /// Sets or reports the current document's line ending setting. fn set_line_ending( cx: &mut compositor::Context, - args: &[&str], + args: &[Cow], _event: PromptEvent, ) -> anyhow::Result<()> { use LineEnding::*; @@ -2177,7 +2176,7 @@ pub mod cmd { fn earlier( cx: &mut compositor::Context, - args: &[&str], + args: &[Cow], _event: PromptEvent, ) -> anyhow::Result<()> { let uk = args.join(" ").parse::().map_err(|s| anyhow!(s))?; @@ -2193,7 +2192,7 @@ pub mod cmd { fn later( cx: &mut compositor::Context, - args: &[&str], + args: &[Cow], _event: PromptEvent, ) -> anyhow::Result<()> { let uk = args.join(" ").parse::().map_err(|s| anyhow!(s))?; @@ -2208,7 +2207,7 @@ pub mod cmd { fn write_quit( cx: &mut compositor::Context, - args: &[&str], + args: &[Cow], event: PromptEvent, ) -> anyhow::Result<()> { write_impl(cx, args.first())?; @@ -2217,7 +2216,7 @@ pub mod cmd { fn force_write_quit( cx: &mut compositor::Context, - args: &[&str], + args: &[Cow], event: PromptEvent, ) -> anyhow::Result<()> { write_impl(cx, args.first())?; @@ -2248,7 +2247,7 @@ pub mod cmd { fn write_all_impl( cx: &mut compositor::Context, - _args: &[&str], + _args: &[Cow], _event: PromptEvent, quit: bool, force: bool, @@ -2284,7 +2283,7 @@ pub mod cmd { fn write_all( cx: &mut compositor::Context, - args: &[&str], + args: &[Cow], event: PromptEvent, ) -> anyhow::Result<()> { write_all_impl(cx, args, event, false, false) @@ -2292,7 +2291,7 @@ pub mod cmd { fn write_all_quit( cx: &mut compositor::Context, - args: &[&str], + args: &[Cow], event: PromptEvent, ) -> anyhow::Result<()> { write_all_impl(cx, args, event, true, false) @@ -2300,7 +2299,7 @@ pub mod cmd { fn force_write_all_quit( cx: &mut compositor::Context, - args: &[&str], + args: &[Cow], event: PromptEvent, ) -> anyhow::Result<()> { write_all_impl(cx, args, event, true, true) @@ -2308,7 +2307,7 @@ pub mod cmd { fn quit_all_impl( editor: &mut Editor, - _args: &[&str], + _args: &[Cow], _event: PromptEvent, force: bool, ) -> anyhow::Result<()> { @@ -2327,7 +2326,7 @@ pub mod cmd { fn quit_all( cx: &mut compositor::Context, - args: &[&str], + args: &[Cow], event: PromptEvent, ) -> anyhow::Result<()> { quit_all_impl(cx.editor, args, event, false) @@ -2335,7 +2334,7 @@ pub mod cmd { fn force_quit_all( cx: &mut compositor::Context, - args: &[&str], + args: &[Cow], event: PromptEvent, ) -> anyhow::Result<()> { quit_all_impl(cx.editor, args, event, true) @@ -2343,7 +2342,7 @@ pub mod cmd { fn cquit( cx: &mut compositor::Context, - args: &[&str], + args: &[Cow], _event: PromptEvent, ) -> anyhow::Result<()> { let exit_code = args @@ -2362,7 +2361,7 @@ pub mod cmd { fn theme( cx: &mut compositor::Context, - args: &[&str], + args: &[Cow], _event: PromptEvent, ) -> anyhow::Result<()> { let theme = args.first().context("theme not provided")?; @@ -2371,7 +2370,7 @@ pub mod cmd { fn yank_main_selection_to_clipboard( cx: &mut compositor::Context, - _args: &[&str], + _args: &[Cow], _event: PromptEvent, ) -> anyhow::Result<()> { yank_main_selection_to_clipboard_impl(cx.editor, ClipboardType::Clipboard) @@ -2379,20 +2378,18 @@ pub mod cmd { fn yank_joined_to_clipboard( cx: &mut compositor::Context, - args: &[&str], + args: &[Cow], _event: PromptEvent, ) -> anyhow::Result<()> { let (_, doc) = current!(cx.editor); - let separator = args - .first() - .copied() - .unwrap_or_else(|| doc.line_ending.as_str()); + let default_sep = Cow::Borrowed(doc.line_ending.as_str()); + let separator = args.first().unwrap_or(&default_sep); yank_joined_to_clipboard_impl(cx.editor, separator, ClipboardType::Clipboard) } fn yank_main_selection_to_primary_clipboard( cx: &mut compositor::Context, - _args: &[&str], + _args: &[Cow], _event: PromptEvent, ) -> anyhow::Result<()> { yank_main_selection_to_clipboard_impl(cx.editor, ClipboardType::Selection) @@ -2400,20 +2397,18 @@ pub mod cmd { fn yank_joined_to_primary_clipboard( cx: &mut compositor::Context, - args: &[&str], + args: &[Cow], _event: PromptEvent, ) -> anyhow::Result<()> { let (_, doc) = current!(cx.editor); - let separator = args - .first() - .copied() - .unwrap_or_else(|| doc.line_ending.as_str()); + let default_sep = Cow::Borrowed(doc.line_ending.as_str()); + let separator = args.first().unwrap_or(&default_sep); yank_joined_to_clipboard_impl(cx.editor, separator, ClipboardType::Selection) } fn paste_clipboard_after( cx: &mut compositor::Context, - _args: &[&str], + _args: &[Cow], _event: PromptEvent, ) -> anyhow::Result<()> { paste_clipboard_impl(cx.editor, Paste::After, ClipboardType::Clipboard) @@ -2421,7 +2416,7 @@ pub mod cmd { fn paste_clipboard_before( cx: &mut compositor::Context, - _args: &[&str], + _args: &[Cow], _event: PromptEvent, ) -> anyhow::Result<()> { paste_clipboard_impl(cx.editor, Paste::After, ClipboardType::Clipboard) @@ -2429,7 +2424,7 @@ pub mod cmd { fn paste_primary_clipboard_after( cx: &mut compositor::Context, - _args: &[&str], + _args: &[Cow], _event: PromptEvent, ) -> anyhow::Result<()> { paste_clipboard_impl(cx.editor, Paste::After, ClipboardType::Selection) @@ -2437,7 +2432,7 @@ pub mod cmd { fn paste_primary_clipboard_before( cx: &mut compositor::Context, - _args: &[&str], + _args: &[Cow], _event: PromptEvent, ) -> anyhow::Result<()> { paste_clipboard_impl(cx.editor, Paste::After, ClipboardType::Selection) @@ -2467,7 +2462,7 @@ pub mod cmd { fn replace_selections_with_clipboard( cx: &mut compositor::Context, - _args: &[&str], + _args: &[Cow], _event: PromptEvent, ) -> anyhow::Result<()> { replace_selections_with_clipboard_impl(cx, ClipboardType::Clipboard) @@ -2475,7 +2470,7 @@ pub mod cmd { fn replace_selections_with_primary_clipboard( cx: &mut compositor::Context, - _args: &[&str], + _args: &[Cow], _event: PromptEvent, ) -> anyhow::Result<()> { replace_selections_with_clipboard_impl(cx, ClipboardType::Selection) @@ -2483,7 +2478,7 @@ pub mod cmd { fn show_clipboard_provider( cx: &mut compositor::Context, - _args: &[&str], + _args: &[Cow], _event: PromptEvent, ) -> anyhow::Result<()> { cx.editor @@ -2493,12 +2488,13 @@ pub mod cmd { fn change_current_directory( cx: &mut compositor::Context, - args: &[&str], + args: &[Cow], _event: PromptEvent, ) -> anyhow::Result<()> { let dir = helix_core::path::expand_tilde( args.first() .context("target directory not provided")? + .as_ref() .as_ref(), ); @@ -2516,7 +2512,7 @@ pub mod cmd { fn show_current_directory( cx: &mut compositor::Context, - _args: &[&str], + _args: &[Cow], _event: PromptEvent, ) -> anyhow::Result<()> { let cwd = std::env::current_dir().context("Couldn't get the new working directory")?; @@ -2528,7 +2524,7 @@ pub mod cmd { /// Sets the [`Document`]'s encoding.. fn set_encoding( cx: &mut compositor::Context, - args: &[&str], + args: &[Cow], _event: PromptEvent, ) -> anyhow::Result<()> { let (_, doc) = current!(cx.editor); @@ -2544,7 +2540,7 @@ pub mod cmd { /// Reload the [`Document`] from its source file. fn reload( cx: &mut compositor::Context, - _args: &[&str], + _args: &[Cow], _event: PromptEvent, ) -> anyhow::Result<()> { let (view, doc) = current!(cx.editor); @@ -2553,7 +2549,7 @@ pub mod cmd { fn tree_sitter_scopes( cx: &mut compositor::Context, - _args: &[&str], + _args: &[Cow], _event: PromptEvent, ) -> anyhow::Result<()> { let (view, doc) = current!(cx.editor); @@ -2567,15 +2563,18 @@ pub mod cmd { fn vsplit( cx: &mut compositor::Context, - args: &[&str], + args: &[Cow], _event: PromptEvent, ) -> anyhow::Result<()> { let id = view!(cx.editor).doc; - if let Some(path) = args.get(0) { - cx.editor.open(path.into(), Action::VerticalSplit)?; - } else { + if args.is_empty() { cx.editor.switch(id, Action::VerticalSplit); + } else { + for arg in args { + cx.editor + .open(PathBuf::from(arg.as_ref()), Action::VerticalSplit)?; + } } Ok(()) @@ -2583,15 +2582,18 @@ pub mod cmd { fn hsplit( cx: &mut compositor::Context, - args: &[&str], + args: &[Cow], _event: PromptEvent, ) -> anyhow::Result<()> { let id = view!(cx.editor).doc; - if let Some(path) = args.get(0) { - cx.editor.open(path.into(), Action::HorizontalSplit)?; - } else { + if args.is_empty() { cx.editor.switch(id, Action::HorizontalSplit); + } else { + for arg in args { + cx.editor + .open(PathBuf::from(arg.as_ref()), Action::HorizontalSplit)?; + } } Ok(()) @@ -2599,7 +2601,7 @@ pub mod cmd { fn tutor( cx: &mut compositor::Context, - _args: &[&str], + _args: &[Cow], _event: PromptEvent, ) -> anyhow::Result<()> { let path = helix_core::runtime_dir().join("tutor.txt"); @@ -2611,7 +2613,7 @@ pub mod cmd { pub(super) fn goto_line_number( cx: &mut compositor::Context, - args: &[&str], + args: &[Cow], _event: PromptEvent, ) -> anyhow::Result<()> { ensure!(!args.is_empty(), "Line number required"); @@ -2980,7 +2982,7 @@ fn command_mode(cx: &mut Context) { // If command is numeric, interpret as line number and go there. if parts.len() == 1 && parts[0].parse::().ok().is_some() { - if let Err(e) = cmd::goto_line_number(cx, &parts[0..], event) { + if let Err(e) = cmd::goto_line_number(cx, &[Cow::from(parts[0])], event) { cx.editor.set_error(format!("{}", e)); } return; @@ -2988,7 +2990,8 @@ fn command_mode(cx: &mut Context) { // Handle typable commands if let Some(cmd) = cmd::TYPABLE_COMMAND_MAP.get(parts[0]) { - if let Err(e) = (cmd.fun)(cx, &parts[1..], event) { + let args = shellwords::shellwords(input); + if let Err(e) = (cmd.fun)(cx, &args[1..], event) { cx.editor.set_error(format!("{}", e)); } } else { From e91d357fae04766b9781fe51a0809d35175fe1cf Mon Sep 17 00:00:00 2001 From: Omnikar Date: Sun, 12 Dec 2021 07:16:48 -0500 Subject: [PATCH 17/81] Macros (#1234) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Macros WIP `helix_term::compositor::Callback` changed to take a `&mut Context` as a parameter for use by `play_macro` * Default to `@` register for macros * Import `KeyEvent` * Special-case shift-tab -> backtab in `KeyEvent` conversion * Move key recording to the compositor * Add comment * Add persistent display of macro recording status When macro recording is active, the pending keys display will be shifted 3 characters left, and the register being recorded to will be displayed between brackets — e.g., `[@]` — right of the pending keys display. * Fix/add documentation --- book/src/keymap.md | 2 ++ helix-term/src/commands.rs | 59 ++++++++++++++++++++++++++++++++++-- helix-term/src/compositor.rs | 9 ++++-- helix-term/src/keymap.rs | 3 ++ helix-term/src/ui/editor.rs | 22 ++++++++++++-- helix-term/src/ui/menu.rs | 2 +- helix-term/src/ui/picker.rs | 2 +- helix-term/src/ui/popup.rs | 2 +- helix-term/src/ui/prompt.rs | 2 +- helix-view/src/editor.rs | 3 ++ helix-view/src/input.rs | 20 ++++++++++++ 11 files changed, 116 insertions(+), 10 deletions(-) diff --git a/book/src/keymap.md b/book/src/keymap.md index 5a804c3c0..f0a2cb302 100644 --- a/book/src/keymap.md +++ b/book/src/keymap.md @@ -77,6 +77,8 @@ | `Alt-c` | Change selection (delete and enter insert mode, without yanking) | `change_selection_noyank` | | `Ctrl-a` | Increment object (number) under cursor | `increment` | | `Ctrl-x` | Decrement object (number) under cursor | `decrement` | +| `q` | Start/stop macro recording to the selected register | `record_macro` | +| `Q` | Play back a recorded macro from the selected register | `play_macro` | #### Shell diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 314cd11fd..505547311 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -70,7 +70,7 @@ pub struct Context<'a> { impl<'a> Context<'a> { /// Push a new component onto the compositor. pub fn push_layer(&mut self, component: Box) { - self.callback = Some(Box::new(|compositor: &mut Compositor| { + self.callback = Some(Box::new(|compositor: &mut Compositor, _| { compositor.push(component) })); } @@ -395,6 +395,8 @@ impl MappableCommand { rename_symbol, "Rename symbol", increment, "Increment", decrement, "Decrement", + record_macro, "Record macro", + play_macro, "Play macro", ); } @@ -3441,7 +3443,7 @@ fn apply_workspace_edit( fn last_picker(cx: &mut Context) { // TODO: last picker does not seem to work well with buffer_picker - cx.callback = Some(Box::new(|compositor: &mut Compositor| { + cx.callback = Some(Box::new(|compositor: &mut Compositor, _| { if let Some(picker) = compositor.last_picker.take() { compositor.push(picker); } @@ -5870,3 +5872,56 @@ fn increment_impl(cx: &mut Context, amount: i64) { doc.append_changes_to_history(view.id); } } + +fn record_macro(cx: &mut Context) { + if let Some((reg, mut keys)) = cx.editor.macro_recording.take() { + // Remove the keypress which ends the recording + keys.pop(); + let s = keys + .into_iter() + .map(|key| format!("{}", key)) + .collect::>() + .join(" "); + cx.editor.registers.get_mut(reg).write(vec![s]); + cx.editor + .set_status(format!("Recorded to register {}", reg)); + } else { + let reg = cx.register.take().unwrap_or('@'); + cx.editor.macro_recording = Some((reg, Vec::new())); + cx.editor + .set_status(format!("Recording to register {}", reg)); + } +} + +fn play_macro(cx: &mut Context) { + let reg = cx.register.unwrap_or('@'); + let keys = match cx + .editor + .registers + .get(reg) + .and_then(|reg| reg.read().get(0)) + .context("Register empty") + .and_then(|s| { + s.split_whitespace() + .map(str::parse::) + .collect::, _>>() + .context("Failed to parse macro") + }) { + Ok(keys) => keys, + Err(e) => { + cx.editor.set_error(format!("{}", e)); + return; + } + }; + let count = cx.count(); + + cx.callback = Some(Box::new( + move |compositor: &mut Compositor, cx: &mut compositor::Context| { + for _ in 0..count { + for &key in keys.iter() { + compositor.handle_event(crossterm::event::Event::Key(key.into()), cx); + } + } + }, + )); +} diff --git a/helix-term/src/compositor.rs b/helix-term/src/compositor.rs index 30554ebb3..321f56a5e 100644 --- a/helix-term/src/compositor.rs +++ b/helix-term/src/compositor.rs @@ -7,7 +7,7 @@ use helix_view::graphics::{CursorKind, Rect}; use crossterm::event::Event; use tui::buffer::Buffer as Surface; -pub type Callback = Box; +pub type Callback = Box; // --> EventResult should have a callback that takes a context with methods like .popup(), // .prompt() etc. That way we can abstract it from the renderer. @@ -131,12 +131,17 @@ impl Compositor { } pub fn handle_event(&mut self, event: Event, cx: &mut Context) -> bool { + // If it is a key event and a macro is being recorded, push the key event to the recording. + if let (Event::Key(key), Some((_, keys))) = (event, &mut cx.editor.macro_recording) { + keys.push(key.into()); + } + // propagate events through the layers until we either find a layer that consumes it or we // run out of layers (event bubbling) for layer in self.layers.iter_mut().rev() { match layer.handle_event(event, cx) { EventResult::Consumed(Some(callback)) => { - callback(self); + callback(self, cx); return true; } EventResult::Consumed(None) => return true, diff --git a/helix-term/src/keymap.rs b/helix-term/src/keymap.rs index b1613252b..257d5f296 100644 --- a/helix-term/src/keymap.rs +++ b/helix-term/src/keymap.rs @@ -593,6 +593,9 @@ impl Default for Keymaps { // paste_all "P" => paste_before, + "q" => record_macro, + "Q" => play_macro, + ">" => indent, "<" => unindent, "=" => format_selections, diff --git a/helix-term/src/ui/editor.rs b/helix-term/src/ui/editor.rs index 39ee15b4c..bac1f1712 100644 --- a/helix-term/src/ui/editor.rs +++ b/helix-term/src/ui/editor.rs @@ -1100,13 +1100,31 @@ impl Component for EditorView { disp.push_str(&s); } } + let style = cx.editor.theme.get("ui.text"); + let macro_width = if cx.editor.macro_recording.is_some() { + 3 + } else { + 0 + }; surface.set_string( - area.x + area.width.saturating_sub(key_width), + area.x + area.width.saturating_sub(key_width + macro_width), area.y + area.height.saturating_sub(1), disp.get(disp.len().saturating_sub(key_width as usize)..) .unwrap_or(&disp), - cx.editor.theme.get("ui.text"), + style, ); + if let Some((reg, _)) = cx.editor.macro_recording { + let disp = format!("[{}]", reg); + let style = style + .fg(helix_view::graphics::Color::Yellow) + .add_modifier(Modifier::BOLD); + surface.set_string( + area.x + area.width.saturating_sub(3), + area.y + area.height.saturating_sub(1), + &disp, + style, + ); + } } if let Some(completion) = self.completion.as_mut() { diff --git a/helix-term/src/ui/menu.rs b/helix-term/src/ui/menu.rs index 9a885a36e..69053db3e 100644 --- a/helix-term/src/ui/menu.rs +++ b/helix-term/src/ui/menu.rs @@ -190,7 +190,7 @@ impl Component for Menu { _ => return EventResult::Ignored, }; - let close_fn = EventResult::Consumed(Some(Box::new(|compositor: &mut Compositor| { + let close_fn = EventResult::Consumed(Some(Box::new(|compositor: &mut Compositor, _| { // remove the layer compositor.pop(); }))); diff --git a/helix-term/src/ui/picker.rs b/helix-term/src/ui/picker.rs index 1c963f971..1ef94df01 100644 --- a/helix-term/src/ui/picker.rs +++ b/helix-term/src/ui/picker.rs @@ -404,7 +404,7 @@ impl Component for Picker { _ => return EventResult::Ignored, }; - let close_fn = EventResult::Consumed(Some(Box::new(|compositor: &mut Compositor| { + let close_fn = EventResult::Consumed(Some(Box::new(|compositor: &mut Compositor, _| { // remove the layer compositor.last_picker = compositor.pop(); }))); diff --git a/helix-term/src/ui/popup.rs b/helix-term/src/ui/popup.rs index c55f030f9..bf7510a25 100644 --- a/helix-term/src/ui/popup.rs +++ b/helix-term/src/ui/popup.rs @@ -100,7 +100,7 @@ impl Component for Popup { _ => return EventResult::Ignored, }; - let close_fn = EventResult::Consumed(Some(Box::new(|compositor: &mut Compositor| { + let close_fn = EventResult::Consumed(Some(Box::new(|compositor: &mut Compositor, _| { // remove the layer compositor.pop(); }))); diff --git a/helix-term/src/ui/prompt.rs b/helix-term/src/ui/prompt.rs index a7ef231c4..07e1b33c4 100644 --- a/helix-term/src/ui/prompt.rs +++ b/helix-term/src/ui/prompt.rs @@ -426,7 +426,7 @@ impl Component for Prompt { _ => return EventResult::Ignored, }; - let close_fn = EventResult::Consumed(Some(Box::new(|compositor: &mut Compositor| { + let close_fn = EventResult::Consumed(Some(Box::new(|compositor: &mut Compositor, _| { // remove the layer compositor.pop(); }))); diff --git a/helix-view/src/editor.rs b/helix-view/src/editor.rs index 9034d12c8..dcbcbe4f8 100644 --- a/helix-view/src/editor.rs +++ b/helix-view/src/editor.rs @@ -2,6 +2,7 @@ use crate::{ clipboard::{get_clipboard_provider, ClipboardProvider}, document::SCRATCH_BUFFER_NAME, graphics::{CursorKind, Rect}, + input::KeyEvent, theme::{self, Theme}, tree::{self, Tree}, Document, DocumentId, View, ViewId, @@ -160,6 +161,7 @@ pub struct Editor { pub count: Option, pub selected_register: Option, pub registers: Registers, + pub macro_recording: Option<(char, Vec)>, pub theme: Theme, pub language_servers: helix_lsp::Registry, pub clipboard_provider: Box, @@ -203,6 +205,7 @@ impl Editor { documents: BTreeMap::new(), count: None, selected_register: None, + macro_recording: None, theme: theme_loader.default(), language_servers, syn_loader, diff --git a/helix-view/src/input.rs b/helix-view/src/input.rs index b207c3eda..92caa5176 100644 --- a/helix-view/src/input.rs +++ b/helix-view/src/input.rs @@ -234,6 +234,26 @@ impl From for KeyEvent { } } +#[cfg(feature = "term")] +impl From for crossterm::event::KeyEvent { + fn from(KeyEvent { code, modifiers }: KeyEvent) -> Self { + if code == KeyCode::Tab && modifiers.contains(KeyModifiers::SHIFT) { + // special case for Shift-Tab -> BackTab + let mut modifiers = modifiers; + modifiers.remove(KeyModifiers::SHIFT); + crossterm::event::KeyEvent { + code: crossterm::event::KeyCode::BackTab, + modifiers: modifiers.into(), + } + } else { + crossterm::event::KeyEvent { + code: code.into(), + modifiers: modifiers.into(), + } + } + } +} + #[cfg(test)] mod test { use super::*; From b25d453f64ceaf11e877d4e0624ac9a3b6b695ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bla=C5=BE=20Hrastnik?= Date: Mon, 13 Dec 2021 14:36:16 +0900 Subject: [PATCH 18/81] minor: Shorten goto file(s) descriptions --- helix-term/src/commands.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 505547311..0ae225e94 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -289,9 +289,9 @@ impl MappableCommand { goto_implementation, "Goto implementation", goto_file_start, "Goto file start/line", goto_file_end, "Goto file end", - goto_file, "Goto files in the selection", - goto_file_hsplit, "Goto files in the selection in horizontal splits", - goto_file_vsplit, "Goto files in the selection in vertical splits", + goto_file, "Goto files in selection", + goto_file_hsplit, "Goto files in selection (hsplit)", + goto_file_vsplit, "Goto files in selection (vsplit)", goto_reference, "Goto references", goto_window_top, "Goto window top", goto_window_center, "Goto window center", From 43d17c482cda07fa0dc4985d60c14391a4926940 Mon Sep 17 00:00:00 2001 From: NNB Date: Thu, 11 Nov 2021 15:23:27 +0700 Subject: [PATCH 19/81] Fix Base16 Dark, add Base16 Light and Terminal Improve accuracy with line number and cursor color --- runtime/themes/base16_default_dark.toml | 25 +++++----- runtime/themes/base16_default_light.toml | 60 ++++++++++++++++++++++++ runtime/themes/base16_terminal.toml | 41 ++++++++++++++++ 3 files changed, 114 insertions(+), 12 deletions(-) create mode 100644 runtime/themes/base16_default_light.toml create mode 100644 runtime/themes/base16_terminal.toml diff --git a/runtime/themes/base16_default_dark.toml b/runtime/themes/base16_default_dark.toml index d65995c05..15c094a6f 100644 --- a/runtime/themes/base16_default_dark.toml +++ b/runtime/themes/base16_default_dark.toml @@ -1,27 +1,28 @@ -# Author: RayGervais +# Author: RayGervais "ui.background" = { bg = "base00" } "ui.menu" = "base01" "ui.menu.selected" = { fg = "base04", bg = "base01" } -"ui.linenr" = {fg = "base01" } +"ui.linenr" = { fg = "base03", bg = "base01" } "ui.popup" = { bg = "base01" } "ui.window" = { bg = "base01" } -"ui.liner.selected" = "base02" -"ui.selection" = "base02" -"comment" = "base03" -"ui.statusline" = {fg = "base04", bg = "base01" } +"ui.linenr.selected" = { fg = "base04", bg = "base01", modifiers = ["bold"] } +"ui.selection" = { bg = "base02" } +"comment" = { fg = "base03", modifiers = ["italic"] } +"ui.statusline" = { fg = "base04", bg = "base01" } "ui.help" = { fg = "base04", bg = "base01" } -"ui.cursor" = { fg = "base05", modifiers = ["reversed"] } -"ui.text" = { fg = "base05" } +"ui.cursor" = { fg = "base04", modifiers = ["reversed"] } +"ui.cursor.primary" = { fg = "base05", modifiers = ["reversed"] } +"ui.text" = "base05" "operator" = "base05" -"ui.text.focus" = { fg = "base05" } +"ui.text.focus" = "base05" "variable" = "base08" "constant.numeric" = "base09" "constant" = "base09" -"attributes" = "base09" +"attributes" = "base09" "type" = "base0A" "ui.cursor.match" = { fg = "base0A", modifiers = ["underlined"] } -"strings" = "base0B" +"string" = "base0B" "variable.other.member" = "base0B" "constant.character.escape" = "base0C" "function" = "base0D" @@ -32,7 +33,7 @@ "namespace" = "base0E" "ui.popup" = { bg = "base01" } "ui.window" = { bg = "base00" } -"ui.help" = { bg = "base01", fg = "base06" } +"ui.help" = { fg = "base06", bg = "base01" } "info" = "base03" "hint" = "base03" diff --git a/runtime/themes/base16_default_light.toml b/runtime/themes/base16_default_light.toml new file mode 100644 index 000000000..d9f1e0ba3 --- /dev/null +++ b/runtime/themes/base16_default_light.toml @@ -0,0 +1,60 @@ +# Author: NNB + +"ui.background" = { bg = "base00" } +"ui.menu" = "base01" +"ui.menu.selected" = { fg = "base04", bg = "base01" } +"ui.linenr" = { fg = "base03", bg = "base01" } +"ui.popup" = { bg = "base01" } +"ui.window" = { bg = "base01" } +"ui.linenr.selected" = { fg = "base04", bg = "base01", modifiers = ["bold"] } +"ui.selection" = { bg = "base02" } +"comment" = { fg = "base03", modifiers = ["italic"] } +"ui.statusline" = { fg = "base04", bg = "base01" } +"ui.help" = { fg = "base04", bg = "base01" } +"ui.cursor" = { fg = "base04", modifiers = ["reversed"] } +"ui.cursor.primary" = { fg = "base05", modifiers = ["reversed"] } +"ui.text" = "base05" +"operator" = "base05" +"ui.text.focus" = "base05" +"variable" = "base08" +"constant.numeric" = "base09" +"constant" = "base09" +"attributes" = "base09" +"type" = "base0A" +"ui.cursor.match" = { fg = "base0A", modifiers = ["underlined"] } +"string" = "base0B" +"variable.other.member" = "base0B" +"constant.character.escape" = "base0C" +"function" = "base0D" +"constructor" = "base0D" +"special" = "base0D" +"keyword" = "base0E" +"label" = "base0E" +"namespace" = "base0E" +"ui.popup" = { bg = "base01" } +"ui.window" = { bg = "base00" } +"ui.help" = { fg = "base06", bg = "base01" } + +"info" = "base03" +"hint" = "base03" +"debug" = "base03" +"diagnostic" = "base03" +"error" = "base0E" + +[palette] +base00 = "#f8f8f8" # Default Background +base01 = "#e8e8e8" # Lighter Background (Used for status bars, line number and folding marks) +base02 = "#d8d8d8" # Selection Background +base03 = "#b8b8b8" # Comments, Invisibles, Line Highlighting +base04 = "#585858" # Dark Foreground (Used for status bars) +base05 = "#383838" # Default Foreground, Caret, Delimiters, Operators +base06 = "#282828" # Light Foreground (Not often used) +base07 = "#181818" # Light Background (Not often used) +base08 = "#ab4642" # Variables, XML Tags, Markup Link Text, Markup Lists, Diff Deleted +base09 = "#dc9656" # Integers, Boolean, Constants, XML Attributes, Markup Link Url +base0A = "#f7ca88" # Classes, Markup Bold, Search Text Background +base0B = "#a1b56c" # Strings, Inherited Class, Markup Code, Diff Inserted +base0C = "#86c1b9" # Support, Regular Expressions, Escape Characters, Markup Quotes +base0D = "#7cafc2" # Functions, Methods, Attribute IDs, Headings +base0E = "#ba8baf" # Keywords, Storage, Selector, Markup Italic, Diff Changed +base0F = "#a16946" # Deprecated, Opening/Closing Embedded Language Tags, e.g. diff --git a/runtime/themes/base16_terminal.toml b/runtime/themes/base16_terminal.toml new file mode 100644 index 000000000..bf3c73f11 --- /dev/null +++ b/runtime/themes/base16_terminal.toml @@ -0,0 +1,41 @@ +# Author: NNB + +"ui.menu" = "black" +"ui.menu.selected" = { fg = "white", bg = "black" } +"ui.linenr" = { fg = "light-gray", bg = "black" } +"ui.popup" = { bg = "black" } +"ui.window" = { bg = "black" } +"ui.linenr.selected" = { fg = "white", bg = "black", modifiers = ["bold"] } +"ui.selection" = { fg = "gray", modifiers = ["reversed"] } +"comment" = { fg = "light-gray", modifiers = ["italic"] } +"ui.statusline" = { fg = "white", bg = "black" } +"ui.help" = { fg = "white", bg = "black" } +"ui.cursor" = { fg = "light-gray", modifiers = ["reversed"] } +"ui.cursor.primary" = { fg = "white", modifiers = ["reversed"] } #FIXME +"ui.text" = "white" #FIXME +"operator" = "white" #FIXME +"ui.text.focus" = "white" #FIXME +"variable" = "light-red" +"constant.numeric" = "yellow" +"constant" = "yellow" +"attributes" = "yellow" +"type" = "light-yellow" +"ui.cursor.match" = { fg = "light-yellow", modifiers = ["underlined"] } +"string" = "light-green" +"variable.other.member" = "light-green" +"constant.character.escape" = "light-cyan" +"function" = "light-blue" +"constructor" = "light-blue" +"special" = "light-blue" +"keyword" = "light-magenta" +"label" = "light-magenta" +"namespace" = "light-magenta" +"ui.popup" = { bg = "black" } +"ui.window" = { bg = "base00" } +"ui.help" = { fg = "white", bg = "black" } + +"info" = "light-gray" +"hint" = "light-gray" +"debug" = "light-gray" +"diagnostic" = "light-gray" +"error" = "light-magenta" From 98ce2a301da25152563137047377026d72fd644c Mon Sep 17 00:00:00 2001 From: Omnikar Date: Sun, 14 Nov 2021 07:26:48 -0500 Subject: [PATCH 20/81] Load alt default theme if true color is not supported * Move `runtime/themes/base16_default_terminal.toml` to `base16_theme.toml` alongside `theme.toml` * Use `terminfo` crate to detect whether the terminal supports true color and, if the user has no theme configured and their terminal does not support true color, load the alt default theme instead of the normal default. Remove `terminfo` dependency, use `COLORTERM` env instead Prevent user from switching to an unsupported theme Add `true-color-override` option If the terminal is wrongly detected to not support true color, `true-color-override = true` will override the detection. Rename `true-color-override` to `true-color` --- .../base16_terminal.toml => base16_theme.toml | 0 book/src/configuration.md | 1 + helix-term/src/application.rs | 28 +++++++++++++------ helix-term/src/commands.rs | 14 ++++++++-- helix-term/src/lib.rs | 6 ++++ helix-term/src/ui/mod.rs | 1 + helix-view/src/editor.rs | 12 ++------ helix-view/src/theme.rs | 20 +++++++++++++ 8 files changed, 62 insertions(+), 20 deletions(-) rename runtime/themes/base16_terminal.toml => base16_theme.toml (100%) diff --git a/runtime/themes/base16_terminal.toml b/base16_theme.toml similarity index 100% rename from runtime/themes/base16_terminal.toml rename to base16_theme.toml diff --git a/book/src/configuration.md b/book/src/configuration.md index 2ed48d51f..33a933b2b 100644 --- a/book/src/configuration.md +++ b/book/src/configuration.md @@ -23,6 +23,7 @@ To override global configuration parameters, create a `config.toml` file located | `idle-timeout` | Time in milliseconds since last keypress before idle timers trigger. Used for autocompletion, set to 0 for instant. | `400` | | `completion-trigger-len` | The min-length of word under cursor to trigger autocompletion | `2` | | `auto-info` | Whether to display infoboxes | `true` | +| `true-color` | Set to `true` to override automatic detection of terminal truecolor support in the event of a false negative. | `false` | `[editor.filepicker]` section of the config. Sets options for file picker and global search. All but the last key listed in the default file-picker configuration below are IgnoreOptions: whether hidden files and files listed within ignore files are ignored by (not visible in) the helix file picker and global search. There is also one other key, `max-depth` available, which is not defined by default. diff --git a/helix-term/src/application.rs b/helix-term/src/application.rs index 90330751a..3e0b6d592 100644 --- a/helix-term/src/application.rs +++ b/helix-term/src/application.rs @@ -76,17 +76,27 @@ impl Application { None => Ok(def_lang_conf), }; - let theme = if let Some(theme) = &config.theme { - match theme_loader.load(theme) { - Ok(theme) => theme, - Err(e) => { - log::warn!("failed to load theme `{}` - {}", theme, e); + let true_color = config.editor.true_color || crate::true_color(); + let theme = config + .theme + .as_ref() + .and_then(|theme| { + theme_loader + .load(theme) + .map_err(|e| { + log::warn!("failed to load theme `{}` - {}", theme, e); + e + }) + .ok() + .filter(|theme| (true_color || theme.is_16_color())) + }) + .unwrap_or_else(|| { + if true_color { theme_loader.default() + } else { + theme_loader.base16_default() } - } - } else { - theme_loader.default() - }; + }); let syn_loader_conf: helix_core::syntax::Configuration = lang_conf .and_then(|conf| conf.try_into()) diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 0ae225e94..22c23043d 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -2366,8 +2366,18 @@ pub mod cmd { args: &[Cow], _event: PromptEvent, ) -> anyhow::Result<()> { - let theme = args.first().context("theme not provided")?; - cx.editor.set_theme_from_name(theme) + let theme = args.first().context("Theme not provided")?; + let theme = cx + .editor + .theme_loader + .load(theme) + .with_context(|| format!("Failed setting theme {}", theme))?; + let true_color = cx.editor.config.true_color || crate::true_color(); + if !(true_color || theme.is_16_color()) { + bail!("Unsupported theme: theme requires true color support"); + } + cx.editor.set_theme(theme); + Ok(()) } fn yank_main_selection_to_clipboard( diff --git a/helix-term/src/lib.rs b/helix-term/src/lib.rs index f5e3a8cdd..4fe76cd59 100644 --- a/helix-term/src/lib.rs +++ b/helix-term/src/lib.rs @@ -9,3 +9,9 @@ pub mod config; pub mod job; pub mod keymap; pub mod ui; + +fn true_color() -> bool { + std::env::var("COLORTERM") + .map(|v| matches!(v.as_str(), "truecolor" | "24bit")) + .unwrap_or(false) +} diff --git a/helix-term/src/ui/mod.rs b/helix-term/src/ui/mod.rs index cdf423110..f57e2e2bd 100644 --- a/helix-term/src/ui/mod.rs +++ b/helix-term/src/ui/mod.rs @@ -186,6 +186,7 @@ pub mod completers { &helix_core::config_dir().join("themes"), )); names.push("default".into()); + names.push("base16_default".into()); let mut names: Vec<_> = names .into_iter() diff --git a/helix-view/src/editor.rs b/helix-view/src/editor.rs index dcbcbe4f8..753defa13 100644 --- a/helix-view/src/editor.rs +++ b/helix-view/src/editor.rs @@ -105,6 +105,8 @@ pub struct Config { /// Whether to display infoboxes. Defaults to true. pub auto_info: bool, pub file_picker: FilePickerConfig, + /// Set to `true` to override automatic detection of terminal truecolor support in the event of a false negative. Defaults to `false`. + pub true_color: bool, } #[derive(Debug, Copy, Clone, PartialEq, Eq, Deserialize)] @@ -137,6 +139,7 @@ impl Default for Config { completion_trigger_len: 2, auto_info: true, file_picker: FilePickerConfig::default(), + true_color: false, } } } @@ -265,15 +268,6 @@ impl Editor { self._refresh(); } - pub fn set_theme_from_name(&mut self, theme: &str) -> anyhow::Result<()> { - let theme = self - .theme_loader - .load(theme.as_ref()) - .with_context(|| format!("failed setting theme `{}`", theme))?; - self.set_theme(theme); - Ok(()) - } - /// Refreshes the language server for a given document pub fn refresh_language_server(&mut self, doc_id: DocumentId) -> Option<()> { let doc = self.documents.get_mut(&doc_id)?; diff --git a/helix-view/src/theme.rs b/helix-view/src/theme.rs index 47fc6262f..ddcdad991 100644 --- a/helix-view/src/theme.rs +++ b/helix-view/src/theme.rs @@ -15,6 +15,10 @@ pub use crate::graphics::{Color, Modifier, Style}; pub static DEFAULT_THEME: Lazy = Lazy::new(|| { toml::from_slice(include_bytes!("../../theme.toml")).expect("Failed to parse default theme") }); +pub static BASE16_DEFAULT_THEME: Lazy = Lazy::new(|| { + toml::from_slice(include_bytes!("../../base16_theme.toml")) + .expect("Failed to parse base 16 default theme") +}); #[derive(Clone, Debug)] pub struct Loader { @@ -35,6 +39,9 @@ impl Loader { if name == "default" { return Ok(self.default()); } + if name == "base16_default" { + return Ok(self.base16_default()); + } let filename = format!("{}.toml", name); let user_path = self.user_dir.join(&filename); @@ -74,6 +81,11 @@ impl Loader { pub fn default(&self) -> Theme { DEFAULT_THEME.clone() } + + /// Returns the alternative 16-color default theme + pub fn base16_default(&self) -> Theme { + BASE16_DEFAULT_THEME.clone() + } } #[derive(Clone, Debug)] @@ -154,6 +166,14 @@ impl Theme { pub fn find_scope_index(&self, scope: &str) -> Option { self.scopes().iter().position(|s| s == scope) } + + pub fn is_16_color(&self) -> bool { + self.styles.iter().all(|(_, style)| { + [style.fg, style.bg] + .into_iter() + .all(|color| !matches!(color, Some(Color::Rgb(..)))) + }) + } } struct ThemePalette { From cff5344a135aee6ab101e5782a7278e7370ddd12 Mon Sep 17 00:00:00 2001 From: NNB Date: Thu, 2 Dec 2021 20:02:07 +0700 Subject: [PATCH 21/81] Rename base16_theme.toml to base16_terminal.toml --- base16_theme.toml => base16_terminal.toml | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename base16_theme.toml => base16_terminal.toml (100%) diff --git a/base16_theme.toml b/base16_terminal.toml similarity index 100% rename from base16_theme.toml rename to base16_terminal.toml From a9a9d498e87a91d9c069946cffd0480c4c3b50c6 Mon Sep 17 00:00:00 2001 From: NNB Date: Thu, 2 Dec 2021 20:03:02 +0700 Subject: [PATCH 22/81] Update theme.rs --- helix-view/src/theme.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/helix-view/src/theme.rs b/helix-view/src/theme.rs index ddcdad991..a5dc06098 100644 --- a/helix-view/src/theme.rs +++ b/helix-view/src/theme.rs @@ -16,7 +16,7 @@ pub static DEFAULT_THEME: Lazy = Lazy::new(|| { toml::from_slice(include_bytes!("../../theme.toml")).expect("Failed to parse default theme") }); pub static BASE16_DEFAULT_THEME: Lazy = Lazy::new(|| { - toml::from_slice(include_bytes!("../../base16_theme.toml")) + toml::from_slice(include_bytes!("../../base16_tty.toml")) .expect("Failed to parse base 16 default theme") }); From 3080be82687548e06a05e517e09a36721b769f03 Mon Sep 17 00:00:00 2001 From: NNB Date: Thu, 2 Dec 2021 17:22:27 +0700 Subject: [PATCH 23/81] Fix error color, add tty theme --- base16_terminal.toml | 2 +- runtime/themes/base16_default_dark.toml | 2 +- runtime/themes/base16_default_light.toml | 2 +- runtime/themes/base16_tty.toml | 41 ++++++++++++++++++++++++ 4 files changed, 44 insertions(+), 3 deletions(-) create mode 100644 runtime/themes/base16_tty.toml diff --git a/base16_terminal.toml b/base16_terminal.toml index bf3c73f11..e613526f4 100644 --- a/base16_terminal.toml +++ b/base16_terminal.toml @@ -38,4 +38,4 @@ "hint" = "light-gray" "debug" = "light-gray" "diagnostic" = "light-gray" -"error" = "light-magenta" +"error" = "light-red" diff --git a/runtime/themes/base16_default_dark.toml b/runtime/themes/base16_default_dark.toml index 15c094a6f..3472162bd 100644 --- a/runtime/themes/base16_default_dark.toml +++ b/runtime/themes/base16_default_dark.toml @@ -39,7 +39,7 @@ "hint" = "base03" "debug" = "base03" "diagnostic" = "base03" -"error" = "base0E" +"error" = "base08" [palette] base00 = "#181818" # Default Background diff --git a/runtime/themes/base16_default_light.toml b/runtime/themes/base16_default_light.toml index d9f1e0ba3..d864fd5fe 100644 --- a/runtime/themes/base16_default_light.toml +++ b/runtime/themes/base16_default_light.toml @@ -39,7 +39,7 @@ "hint" = "base03" "debug" = "base03" "diagnostic" = "base03" -"error" = "base0E" +"error" = "base08" [palette] base00 = "#f8f8f8" # Default Background diff --git a/runtime/themes/base16_tty.toml b/runtime/themes/base16_tty.toml new file mode 100644 index 000000000..480bcd9c3 --- /dev/null +++ b/runtime/themes/base16_tty.toml @@ -0,0 +1,41 @@ +# Author: NNB + +"ui.menu" = "black" +"ui.menu.selected" = { fg = "white", bg = "black" } +"ui.linenr" = { fg = "gray", bg = "black" } +"ui.popup" = { bg = "black" } +"ui.window" = { bg = "black" } +"ui.linenr.selected" = { fg = "white", bg = "black", modifiers = ["bold"] } +"ui.selection" = { fg = "black", bg = "blue" } +"ui.selection.primary" = { fg = "white", bg = "blue" } +"comment" = { fg = "gray" } +"ui.statusline" = { fg = "white", bg = "black" } +"ui.help" = { fg = "white", bg = "black" } +"ui.cursor" = { fg = "white", modifiers = ["reversed"] } #FIXME +"ui.text" = "white" #FIXME +"operator" = "white" #FIXME +"ui.text.focus" = "white" #FIXME +"variable" = "red" +"constant.numeric" = "yellow" +"constant" = "yellow" +"attributes" = "yellow" +"type" = "yellow" +"ui.cursor.match" = { fg = "yellow", modifiers = ["underlined"] } +"string" = "green" +"variable.other.member" = "green" +"constant.character.escape" = "cyan" +"function" = "blue" +"constructor" = "blue" +"special" = "blue" +"keyword" = "magenta" +"label" = "magenta" +"namespace" = "magenta" +"ui.popup" = { bg = "black" } +"ui.window" = { bg = "base00" } +"ui.help" = { fg = "white", bg = "black" } + +"info" = "gray" +"hint" = "gray" +"debug" = "gray" +"diagnostic" = "gray" +"error" = "red" From d9727868dd7a496d33a321faeacfcd02d8c1c06e Mon Sep 17 00:00:00 2001 From: NNB Date: Thu, 2 Dec 2021 19:33:31 +0700 Subject: [PATCH 24/81] change to .unwrap_or_default() and fix ui.window and ui.statusline --- base16_terminal.toml | 10 +++------- helix-view/src/theme.rs | 2 +- runtime/themes/base16_default_dark.toml | 4 +--- runtime/themes/base16_default_light.toml | 4 +--- runtime/themes/base16_tty.toml | 17 ++++++----------- 5 files changed, 12 insertions(+), 25 deletions(-) diff --git a/base16_terminal.toml b/base16_terminal.toml index e613526f4..a76cfd7e8 100644 --- a/base16_terminal.toml +++ b/base16_terminal.toml @@ -1,7 +1,7 @@ # Author: NNB "ui.menu" = "black" -"ui.menu.selected" = { fg = "white", bg = "black" } +"ui.menu.selected" = { modifiers = ["reversed"] } "ui.linenr" = { fg = "light-gray", bg = "black" } "ui.popup" = { bg = "black" } "ui.window" = { bg = "black" } @@ -9,12 +9,10 @@ "ui.selection" = { fg = "gray", modifiers = ["reversed"] } "comment" = { fg = "light-gray", modifiers = ["italic"] } "ui.statusline" = { fg = "white", bg = "black" } +"ui.statusline.inactive" = { fg = "gray", bg = "black" } "ui.help" = { fg = "white", bg = "black" } "ui.cursor" = { fg = "light-gray", modifiers = ["reversed"] } -"ui.cursor.primary" = { fg = "white", modifiers = ["reversed"] } #FIXME -"ui.text" = "white" #FIXME -"operator" = "white" #FIXME -"ui.text.focus" = "white" #FIXME +"ui.cursor.primary" = { modifiers = ["reversed"] } "variable" = "light-red" "constant.numeric" = "yellow" "constant" = "yellow" @@ -30,8 +28,6 @@ "keyword" = "light-magenta" "label" = "light-magenta" "namespace" = "light-magenta" -"ui.popup" = { bg = "black" } -"ui.window" = { bg = "base00" } "ui.help" = { fg = "white", bg = "black" } "info" = "light-gray" diff --git a/helix-view/src/theme.rs b/helix-view/src/theme.rs index a5dc06098..6ca021a90 100644 --- a/helix-view/src/theme.rs +++ b/helix-view/src/theme.rs @@ -151,7 +151,7 @@ impl Theme { pub fn get(&self, scope: &str) -> Style { self.try_get(scope) - .unwrap_or_else(|| Style::default().fg(Color::Rgb(0, 0, 255))) + .unwrap_or_default() } pub fn try_get(&self, scope: &str) -> Option