on master help sign in
Merge branch 'master' into great_line_ending_and_cursor_range_cleanup
commitf62ec6e51e99dce9e93600801c3637c196c592b6 authorNathan Vegdahl <cessen@cessen.com> dateMon 26 Jul 2021, 11:19 (5 years ago) parent5ee6ba588d6f65
21 files changed+1122−725
Cargo.lock+12−12 book/src/keymap.md+41−31 flake.lock+12−12 flake.nix+40−15 helix-core/src/comment.rs+58−30 helix-core/src/indent.rs+2−4 helix-core/src/register.rs+7−7 helix-lsp/src/client.rs+47 helix-term/src/commands.rs+258−352 helix-term/src/config.rs+10−24 helix-term/src/keymap.rs+492−200 helix-term/src/ui/editor.rs+57−19 helix-term/src/ui/info.rs+1−1 helix-term/src/ui/mod.rs+1 helix-term/src/ui/picker.rs+1 helix-term/src/ui/prompt.rs+58 helix-view/src/editor.rs−3 helix-view/src/info.rs+10−7 helix-view/src/input.rs+11−1 runtime/queries/rust/indents.toml+3−7 runtime/themes/bogster.toml+1
Cargo.lock+12−12
@@ -422,9 +422,9 @@ dependencies = [
422422
423423[[package]]
424424name = "hermit-abi"
425−version = "0.1.18"
425+version = "0.1.19"
426426source = "registry+https://github.com/rust-lang/crates.io-index"
427−checksum = "322f4de77956e22ed0e5032c359a0f1273f1f7f0d79bfa3b8ffbc730d7fbcc5c"
427+checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33"
428428dependencies = [
429429 "libc",
430430]
@@ -460,9 +460,9 @@ dependencies = [
460460
461461[[package]]
462462name = "instant"
463−version = "0.1.9"
463+version = "0.1.10"
464464source = "registry+https://github.com/rust-lang/crates.io-index"
465−checksum = "61124eeebbd69b8190558df225adf7e4caafce0d743919e5d6b19652314ec5ec"
465+checksum = "bee0328b1209d157ef001c94dd85b4f8f64139adb0eac2659f4b08382b2f474d"
466466dependencies = [
467467 "cfg-if 1.0.0",
468468]
@@ -494,9 +494,9 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
494494
495495[[package]]
496496name = "libc"
497−version = "0.2.97"
497+version = "0.2.98"
498498source = "registry+https://github.com/rust-lang/crates.io-index"
499−checksum = "12b8adadd720df158f4d70dfe7ccc6adb0472d7c55ca83445f6a5ab3e36f8fb6"
499+checksum = "320cfe77175da3a483efed4bc0adc1968ca050b098ce4f2f1c13a56626128790"
500500
501501[[package]]
502502name = "libloading"
@@ -662,9 +662,9 @@ checksum = "d4fd5641d01c8f18a23da7b6fe29298ff4b55afcccdf78973b24cf3175fee32e"
662662
663663[[package]]
664664name = "pin-project-lite"
665−version = "0.2.6"
665+version = "0.2.7"
666666source = "registry+https://github.com/rust-lang/crates.io-index"
667−checksum = "dc0e1f259c92177c30a4c9d177246edd0a3568b25756a977d0632cf8fa37e905"
667+checksum = "8d31d11c69a6b52a174b42bdc0c30e5e11670f90788b2c471c31c1d17d449443"
668668
669669[[package]]
670670name = "pin-utils"
@@ -994,9 +994,9 @@ dependencies = [
994994
995995[[package]]
996996name = "tinyvec"
997−version = "1.2.0"
997+version = "1.3.0"
998998source = "registry+https://github.com/rust-lang/crates.io-index"
999−checksum = "5b5220f05bb7de7f3f53c7c065e1199b3172696fe2db9f9c4d8ad9b4ee74c342"
999+checksum = "4ac2e1d4bd0f75279cfd5a076e0d578bbf02c22b7c39e766c437dd49b3ec43e0"
10001000dependencies = [
10011001 "tinyvec_macros",
10021002]
@@ -1029,9 +1029,9 @@ dependencies = [
10291029
10301030[[package]]
10311031name = "tokio-macros"
1032−version = "1.2.0"
1032+version = "1.3.0"
10331033source = "registry+https://github.com/rust-lang/crates.io-index"
1034−checksum = "c49e3df43841dafb86046472506755d8501c5615673955f6aa17181125d13c37"
1034+checksum = "54473be61f4ebe4efd09cec9bd5d16fa51d70ea0192213d754d2d500457db110"
10351035dependencies = [
10361036 "proc-macro2",
10371037 "quote",
book/src/keymap.md+41−31
@@ -41,29 +41,29 @@
4141
4242### Changes
4343
44−| Key | Description |
45−| ----- | ----------- |
46−| `r` | Replace with a character |
47−| `R` | Replace with yanked text |
48−| `~` | Switch case of the selected text |
49−| `\`` | Set the selected text to upper case |
50−| `Alt-\`` | Set the selected text to lower case |
51−| `i` | Insert before selection |
52−| `a` | Insert after selection (append) |
53−| `I` | Insert at the start of the line |
54−| `A` | Insert at the end of the line |
55−| `o` | Open new line below selection |
56−| `o` | Open new line above selection |
57−| `u` | Undo change |
58−| `U` | Redo change |
59−| `y` | Yank selection |
60−| `p` | Paste after selection |
61−| `P` | Paste before selection |
62−| `>` | Indent selection |
63−| `<` | Unindent selection |
64−| `=` | Format selection |
65−| `d` | Delete selection |
66−| `c` | Change selection (delete and enter insert mode) |
44+| Key | Description |
45+| ----- | ----------- |
46+| `r` | Replace with a character |
47+| `R` | Replace with yanked text |
48+| `~` | Switch case of the selected text |
49+| `` ` `` | Set the selected text to lower case |
50+| `` Alt-` `` | Set the selected text to upper case |
51+| `i` | Insert before selection |
52+| `a` | Insert after selection (append) |
53+| `I` | Insert at the start of the line |
54+| `A` | Insert at the end of the line |
55+| `o` | Open new line below selection |
56+| `o` | Open new line above selection |
57+| `u` | Undo change |
58+| `U` | Redo change |
59+| `y` | Yank selection |
60+| `p` | Paste after selection |
61+| `P` | Paste before selection |
62+| `>` | Indent selection |
63+| `<` | Unindent selection |
64+| `=` | Format selection |
65+| `d` | Delete selection |
66+| `c` | Change selection (delete and enter insert mode) |
6767
6868### Selection manipulation
6969
@@ -78,11 +78,19 @@
7878| `x` | Select current line, if already selected, extend to next line |
7979| `X` | Extend selection to line bounds (line-wise selection) |
8080| | Expand selection to parent syntax node TODO: pick a key |
81−| `J` | join lines inside selection |
82−| `K` | keep selections matching the regex TODO: overlapped by hover help |
83−| `Space` | keep only the primary selection TODO: overlapped by space mode |
81+| `J` | Join lines inside selection |
82+| `K` | Keep selections matching the regex TODO: overlapped by hover help |
83+| `Space` | Keep only the primary selection TODO: overlapped by space mode |
8484| `Ctrl-c` | Comment/uncomment the selections |
8585
86+### Insert Mode
87+
88+| Key | Description |
89+| ----- | ----------- |
90+| `Escape` | Switch to normal mode |
91+| `Ctrl-x` | Autocomplete |
92+| `Ctrl-w` | Delete previous word |
93+
8694### Search
8795
8896> TODO: The search implementation isn't ideal yet -- we don't support searching
@@ -190,13 +198,15 @@ This layer is a kludge of mappings I had under leader key in neovim.
190198| `f` | Open file picker |
191199| `b` | Open buffer picker |
192200| `s` | Open symbol picker (current document) |
201+| `a` | Apply code action |
202+| `'` | Open last fuzzy picker |
193203| `w` | Enter [window mode](#window-mode) |
194204| `space` | Keep primary selection TODO: it's here because space mode replaced it |
195−| `p` | paste system clipboard after selections |
196−| `P` | paste system clipboard before selections |
197−| `y` | join and yank selections to clipboard |
198−| `Y` | yank main selection to clipboard |
199−| `R` | replace selections by clipboard contents |
205+| `p` | Paste system clipboard after selections |
206+| `P` | Paste system clipboard before selections |
207+| `y` | Join and yank selections to clipboard |
208+| `Y` | Yank main selection to clipboard |
209+| `R` | Replace selections by clipboard contents |
200210
201211# Picker
202212
flake.lock+12−12
@@ -2,11 +2,11 @@
22 "nodes": {
33 "devshell": {
44 "locked": {
5− "lastModified": 1622711433,
6− "narHash": "sha256-rGjXz7FA7HImAT3TtoqwecByLO5yhVPSwPdaYPBFRQw=",
5+ "lastModified": 1625086391,
6+ "narHash": "sha256-IpNPv1v8s4L3CoxhwcgZIitGpcrnNgnj09X7TA0QV3k=",
77 "owner": "numtide",
88 "repo": "devshell",
9− "rev": "1f4fb67b662b65fa7cfe696fc003fcc1e8f7cc36",
9+ "rev": "4b5ac7cf7d9a1cc60b965bb51b59922f2210cbc7",
1010 "type": "github"
1111 },
1212 "original": {
@@ -40,11 +40,11 @@
4040 "rustOverlay": "rustOverlay"
4141 },
4242 "locked": {
43− "lastModified": 1624244973,
44− "narHash": "sha256-h+b4CwPjyibgwMYAeBaT5qBnxI0fsmGf66k23FqEH5Y=",
43+ "lastModified": 1627106928,
44+ "narHash": "sha256-JaQE0BEk1G1eT539WbYyrA2re4YYL9xo7cB+ZiV4nNM=",
4545 "owner": "yusdacra",
4646 "repo": "nix-cargo-integration",
47− "rev": "00f5df6d8e7eeeac2764b7fa2c57e2e81f5d47cd",
47+ "rev": "e08af05413a2d53dadbd1a39976e1da0e5385970",
4848 "type": "github"
4949 },
5050 "original": {
@@ -55,11 +55,11 @@
5555 },
5656 "nixpkgs": {
5757 "locked": {
58− "lastModified": 1624024598,
59− "narHash": "sha256-X++38oH5MKEmPW4/2WdMaHQvwJzO8pJfbnzMD7DbG1E=",
58+ "lastModified": 1626852498,
59+ "narHash": "sha256-lOXUJvi0FJUXHTVSiC5qsMRtEUgqM4mGZpMESLuGhmo=",
6060 "owner": "nixos",
6161 "repo": "nixpkgs",
62− "rev": "33d42ad7cf2769ce6364ed4e52afa8e9d1439d58",
62+ "rev": "16105403bdd843540cbef9c63fc0f16c1c6eaa70",
6363 "type": "github"
6464 },
6565 "original": {
@@ -79,11 +79,11 @@
7979 "rustOverlay": {
8080 "flake": false,
8181 "locked": {
82− "lastModified": 1624242197,
83− "narHash": "sha256-J0+j4DYFaE0O0marb4QN/S1bUhpGwAjQ4O04kIYKcb8=",
82+ "lastModified": 1627092891,
83+ "narHash": "sha256-6nN+rfsP+SNpnL3UPbrcwZe4qfh9/NH0LWtXhn9w/a4=",
8484 "owner": "oxalica",
8585 "repo": "rust-overlay",
86− "rev": "df5d330f34b64194d64dcbafb91e82e01a89a229",
86+ "rev": "939f2cf1aebc86bc3e9544645b495cd05995524a",
8787 "type": "github"
8888 },
8989 "original": {
flake.nix+40−15
@@ -28,27 +28,53 @@
2828 preConfigure = "ln -s ${common.root}/{languages.toml,theme.toml} ..";
2929 buildInputs = (prev.buildInputs or [ ]) ++ [ common.cCompiler.cc.lib ];
3030 };
31− # link runtime since helix-core expects it because of embed_runtime feature
32− helix-core = _: { preConfigure = "ln -s ${common.root + "/runtime"} ../runtime"; };
3331 # link languages and theme toml files since helix-view expects them
3432 helix-view = _: { preConfigure = "ln -s ${common.root}/{languages.toml,theme.toml} .."; };
35− helix-syntax = prev:
36− let
37− helix = common.pkgs.fetchgit {
38− url = "https://github.com/helix-editor/helix.git";
39− rev = "9fd17d4ff5b81211317da1a28d2b30442a512ffc";
40− fetchSubmodules = true;
41− sha256 = "sha256-y652sn/tCc1XoKr3YxDZv6bS2Cmr6+9K/wzzNAMFZJw=";
42− };
43− in
44− {
45− src = common.pkgs.runCommand prev.src.name { } ''
33+ helix-syntax = prev: {
34+ src =
35+ let
36+ pkgs = common.pkgs;
37+ helix = pkgs.fetchgit {
38+ url = "https://github.com/helix-editor/helix.git";
39+ rev = "d4bd5b37669708361a0a6cd2917464b010e6b7f5";
40+ fetchSubmodules = true;
41+ sha256 = "sha256-KayR7K7UC0mT6EjHsZsCYY9IVDJzft63fGpPKGSY8nQ=";
42+ };
43+ in
44+ pkgs.runCommand prev.src.name { } ''
4645 mkdir -p $out
4746 ln -s ${prev.src}/* $out
4847 ln -sf ${helix}/helix-syntax/languages $out
4948 '';
50− };
49+ preConfigure = "mkdir -p ../runtime/grammars";
50+ postInstall = "cp -r ../runtime $out/runtime";
51+ };
5152 };
53+ mainBuild = common: prev:
54+ let
55+ inherit (common) pkgs lib;
56+ helixSyntax = lib.buildCrate {
57+ root = self;
58+ memberName = "helix-syntax";
59+ defaultCrateOverrides = {
60+ helix-syntax = common.crateOverrides.helix-syntax;
61+ };
62+ release = false;
63+ };
64+ runtimeDir = pkgs.runCommand "helix-runtime" { } ''
65+ mkdir -p $out
66+ ln -s ${common.root}/runtime/* $out
67+ ln -sf ${helixSyntax}/runtime/grammars $out
68+ '';
69+ in
70+ lib.optionalAttrs (common.memberName == "helix-term") {
71+ nativeBuildInputs = [ pkgs.makeWrapper ];
72+ postFixup = ''
73+ if [ -f "$out/bin/hx" ]; then
74+ wrapProgram "$out/bin/hx" --set HELIX_RUNTIME "${runtimeDir}"
75+ fi
76+ '';
77+ };
5278 shell = common: prev: {
5379 packages = prev.packages ++ (with common.pkgs; [ lld_10 lldb cargo-tarpaulin ]);
5480 env = prev.env ++ [
@@ -57,7 +83,6 @@
5783 { name = "RUSTFLAGS"; value = "-C link-arg=-fuse-ld=lld -C target-cpu=native"; }
5884 ];
5985 };
60− build = _: prev: { rootFeatures = prev.rootFeatures ++ [ "embed_runtime" ]; };
6186 };
6287 };
6388}
helix-core/src/comment.rs+58−30
@@ -1,17 +1,27 @@
11use crate::{
22 find_first_non_whitespace_char, Change, Rope, RopeSlice, Selection, Tendril, Transaction,
33};
4−use core::ops::Range;
54use std::borrow::Cow;
65
6+/// Given text, a comment token, and a set of line indices, returns the following:
7+/// - Whether the given lines should be considered commented
8+/// - If any of the lines are uncommented, all lines are considered as such.
9+/// - The lines to change for toggling comments
10+/// - This is all provided lines excluding blanks lines.
11+/// - The column of the comment tokens
12+/// - Column of existing tokens, if the lines are commented; column to place tokens at otherwise.
13+/// - The margin to the right of the comment tokens
14+/// - Defaults to `1`. If any existing comment token is not followed by a space, changes to `0`.
715fn find_line_comment(
816 token: &str,
917 text: RopeSlice,
10− lines: Range<usize>,
11−) -> (bool, Vec<usize>, usize) {
18+ lines: impl IntoIterator<Item = usize>,
19+) -> (bool, Vec<usize>, usize, usize) {
1220 let mut commented = true;
13− let mut skipped = Vec::new();
21+ let mut to_change = Vec::new();
1422 let mut min = usize::MAX; // minimum col for find_first_non_whitespace_char
23+ let mut margin = 1;
24+ let token_len = token.chars().count();
1525 for line in lines {
1626 let line_slice = text.line(line);
1727 if let Some(pos) = find_first_non_whitespace_char(line_slice) {
@@ -29,46 +39,55 @@ fn find_line_comment(
2939 // considered uncommented.
3040 commented = false;
3141 }
32− } else {
33− // blank line
34− skipped.push(line);
42+
43+ // determine margin of 0 or 1 for uncommenting; if any comment token is not followed by a space,
44+ // a margin of 0 is used for all lines.
45+ if matches!(line_slice.get_char(pos + token_len), Some(c) if c != ' ') {
46+ margin = 0;
47+ }
48+
49+ // blank lines don't get pushed.
50+ to_change.push(line);
3551 }
3652 }
37− (commented, skipped, min)
53+ (commented, to_change, min, margin)
3854}
3955
4056#[must_use]
4157pub fn toggle_line_comments(doc: &Rope, selection: &Selection, token: Option<&str>) -> Transaction {
4258 let text = doc.slice(..);
43− let mut changes: Vec<Change> = Vec::new();
4459
4560 let token = token.unwrap_or("//");
4661 let comment = Tendril::from(format!("{} ", token));
4762
63+ let mut lines: Vec<usize> = Vec::new();
64+
65+ let mut min_next_line = 0;
4866 for selection in selection {
4967 let (start, end) = selection.line_range(text);
50− let lines = start..end + 1;
51− let (commented, skipped, min) = find_line_comment(&token, text, lines.clone());
68+ let start = start.max(min_next_line).min(text.len_lines());
69+ let end = (end + 1).min(text.len_lines());
5270
53− changes.reserve((end - start).saturating_sub(skipped.len()));
71+ lines.extend(start..end);
72+ min_next_line = end + 1;
73+ }
5474
55− for line in lines {
56− if skipped.contains(&line) {
57− continue;
58− }
75+ let (commented, to_change, min, margin) = find_line_comment(&token, text, lines);
5976
60− let pos = text.line_to_char(line) + min;
77+ let mut changes: Vec<Change> = Vec::with_capacity(to_change.len());
6178
62− if !commented {
63− // comment line
64− changes.push((pos, pos, Some(comment.clone())))
65− } else {
66− // uncomment line
67− let margin = 1; // TODO: margin is hardcoded 1 but could easily be 0
68− changes.push((pos, pos + token.len() + margin, None))
69− }
79+ for line in to_change {
80+ let pos = text.line_to_char(line) + min;
81+
82+ if !commented {
83+ // comment line
84+ changes.push((pos, pos, Some(comment.clone())));
85+ } else {
86+ // uncomment line
87+ changes.push((pos, pos + token.len() + margin, None));
7088 }
7189 }
90+
7291 Transaction::change(doc, changes.into_iter())
7392}
7493
@@ -90,23 +109,32 @@ mod test {
90109 let text = state.doc.slice(..);
91110
92111 let res = find_line_comment("//", text, 0..3);
93− // (commented = true, skipped = [line 1], min = col 2)
94− assert_eq!(res, (false, vec![1], 2));
112+ // (commented = true, to_change = [line 0, line 2], min = col 2, margin = 1)
113+ assert_eq!(res, (false, vec![0, 2], 2, 1));
95114
96115 // comment
97116 let transaction = toggle_line_comments(&state.doc, &state.selection, None);
98117 transaction.apply(&mut state.doc);
99− state.selection = state.selection.clone().map(transaction.changes());
118+ state.selection = state.selection.map(transaction.changes());
100119
101120 assert_eq!(state.doc, " // 1\n\n // 2\n // 3");
102121
103122 // uncomment
104123 let transaction = toggle_line_comments(&state.doc, &state.selection, None);
105124 transaction.apply(&mut state.doc);
106− state.selection = state.selection.clone().map(transaction.changes());
125+ state.selection = state.selection.map(transaction.changes());
126+ assert_eq!(state.doc, " 1\n\n 2\n 3");
127+
128+ // 0 margin comments
129+ state.doc = Rope::from(" //1\n\n //2\n //3");
130+ // reset the selection.
131+ state.selection = Selection::single(0, state.doc.len_chars() - 1);
132+
133+ let transaction = toggle_line_comments(&state.doc, &state.selection, None);
134+ transaction.apply(&mut state.doc);
135+ state.selection = state.selection.map(transaction.changes());
107136 assert_eq!(state.doc, " 1\n\n 2\n 3");
108137
109− // TODO: account for no margin after comment
110138 // TODO: account for uncommenting with uneven comment indentation
111139 }
112140}
helix-core/src/indent.rs+2−4
@@ -47,7 +47,7 @@ fn calculate_indentation(query: &IndentQuery, node: Option<Node>, newline: bool)
4747 // NOTE: can't use contains() on query because of comparing Vec<String> and &str
4848 // https://doc.rust-lang.org/std/vec/struct.Vec.html#method.contains
4949
50− let mut increment: i32 = 0;
50+ let mut increment: isize = 0;
5151
5252 let mut node = match node {
5353 Some(node) => node,
@@ -93,9 +93,7 @@ fn calculate_indentation(query: &IndentQuery, node: Option<Node>, newline: bool)
9393 node = parent;
9494 }
9595
96− assert!(increment >= 0);
97−
98− increment as usize
96+ increment.max(0) as usize
9997}
10098
10199#[allow(dead_code)]
helix-core/src/register.rs+7−7
@@ -22,13 +22,17 @@ impl Register {
2222 self.name
2323 }
2424
25− pub fn read(&self) -> &Vec<String> {
25+ pub fn read(&self) -> &[String] {
2626 &self.values
2727 }
2828
2929 pub fn write(&mut self, values: Vec<String>) {
3030 self.values = values;
3131 }
32+
33+ pub fn push(&mut self, value: String) {
34+ self.values.push(value);
35+ }
3236}
3337
3438/// Currently just wraps a `HashMap` of `Register`s
@@ -42,11 +46,7 @@ impl Registers {
4246 self.inner.get(&name)
4347 }
4448
45− pub fn get_mut(&mut self, name: char) -> Option<&mut Register> {
46− self.inner.get_mut(&name)
47− }
48−
49− pub fn get_or_insert(&mut self, name: char) -> &mut Register {
49+ pub fn get_mut(&mut self, name: char) -> &mut Register {
5050 self.inner
5151 .entry(name)
5252 .or_insert_with(|| Register::new(name))
@@ -57,7 +57,7 @@ impl Registers {
5757 .insert(name, Register::new_with_values(name, values));
5858 }
5959
60− pub fn read(&self, name: char) -> Option<&Vec<String>> {
60+ pub fn read(&self, name: char) -> Option<&[String]> {
6161 self.get(name).map(|reg| reg.read())
6262 }
6363}
helix-lsp/src/client.rs+47
@@ -247,6 +247,26 @@ impl Client {
247247 content_format: Some(vec![lsp::MarkupKind::Markdown]),
248248 ..Default::default()
249249 }),
250+ code_action: Some(lsp::CodeActionClientCapabilities {
251+ code_action_literal_support: Some(lsp::CodeActionLiteralSupport {
252+ code_action_kind: lsp::CodeActionKindLiteralSupport {
253+ value_set: [
254+ lsp::CodeActionKind::EMPTY,
255+ lsp::CodeActionKind::QUICKFIX,
256+ lsp::CodeActionKind::REFACTOR,
257+ lsp::CodeActionKind::REFACTOR_EXTRACT,
258+ lsp::CodeActionKind::REFACTOR_INLINE,
259+ lsp::CodeActionKind::REFACTOR_REWRITE,
260+ lsp::CodeActionKind::SOURCE,
261+ lsp::CodeActionKind::SOURCE_ORGANIZE_IMPORTS,
262+ ]
263+ .iter()
264+ .map(|kind| kind.as_str().to_string())
265+ .collect(),
266+ },
267+ }),
268+ ..Default::default()
269+ }),
250270 ..Default::default()
251271 }),
252272 window: Some(lsp::WindowClientCapabilities {
@@ -713,4 +733,31 @@ impl Client {
713733
714734 self.call::<lsp::request::DocumentSymbolRequest>(params)
715735 }
736+
737+ // empty string to get all symbols
738+ pub fn workspace_symbols(&self, query: String) -> impl Future<Output = Result<Value>> {
739+ let params = lsp::WorkspaceSymbolParams {
740+ query,
741+ work_done_progress_params: lsp::WorkDoneProgressParams::default(),
742+ partial_result_params: lsp::PartialResultParams::default(),
743+ };
744+
745+ self.call::<lsp::request::WorkspaceSymbol>(params)
746+ }
747+
748+ pub fn code_actions(
749+ &self,
750+ text_document: lsp::TextDocumentIdentifier,
751+ range: lsp::Range,
752+ ) -> impl Future<Output = Result<Value>> {
753+ let params = lsp::CodeActionParams {
754+ text_document,
755+ range,
756+ context: lsp::CodeActionContext::default(),
757+ work_done_progress_params: lsp::WorkDoneProgressParams::default(),
758+ partial_result_params: lsp::PartialResultParams::default(),
759+ };
760+
761+ self.call::<lsp::request::CodeActionRequest>(params)
762+ }
716763}
helix-term/src/commands.rs+258−352
@@ -13,7 +13,6 @@ use helix_core::{
1313use helix_view::{
1414 document::{IndentStyle, Mode},
1515 editor::Action,
16− info::Info,
1716 input::KeyEvent,
1817 keyboard::KeyCode,
1918 view::{View, PADDING},
@@ -36,7 +35,6 @@ use crate::{
3635
3736use crate::job::{self, Job, Jobs};
3837use futures_util::{FutureExt, TryFutureExt};
39−use std::collections::HashMap;
4038use std::num::NonZeroUsize;
4139use std::{fmt, future::Future};
4240
@@ -45,7 +43,7 @@ use std::{
4543 path::{Path, PathBuf},
4644};
4745
48−use once_cell::sync::{Lazy, OnceCell};
46+use once_cell::sync::Lazy;
4947use serde::de::{self, Deserialize, Deserializer};
5048
5149pub struct Context<'a> {
@@ -74,18 +72,6 @@ impl<'a> Context<'a> {
7472 self.on_next_key_callback = Some(Box::new(on_next_key_callback));
7573 }
7674
77− #[inline]
78− pub fn on_next_key_mode(&mut self, map: HashMap<KeyEvent, fn(&mut Context)>) {
79− let count = self.count;
80− self.on_next_key(move |cx, event| {
81− cx.count = count;
82− cx.editor.autoinfo = None;
83− if let Some(func) = map.get(&event) {
84− func(cx);
85− }
86− });
87− }
88−
8975 #[inline]
9076 pub fn callback<T, F>(
9177 &mut self,
@@ -139,13 +125,21 @@ fn align_view(doc: &Document, view: &mut View, align: Align) {
139125/// A command is composed of a static name, and a function that takes the current state plus a count,
140126/// and does a side-effect on the state (usually by creating and applying a transaction).
141127#[derive(Copy, Clone)]
142−pub struct Command(&'static str, fn(cx: &mut Context));
128+pub struct Command {
129+ name: &'static str,
130+ fun: fn(cx: &mut Context),
131+ doc: &'static str,
132+}
143133
144134macro_rules! commands {
145− ( $($name:ident),* ) => {
135+ ( $($name:ident, $doc:literal),* ) => {
146136 $(
147137 #[allow(non_upper_case_globals)]
148− pub const $name: Self = Self(stringify!($name), $name);
138+ pub const $name: Self = Self {
139+ name: stringify!($name),
140+ fun: $name,
141+ doc: $doc
142+ };
149143 )*
150144
151145 pub const COMMAND_LIST: &'static [Self] = &[
@@ -156,144 +150,159 @@ macro_rules! commands {
156150
157151impl Command {
158152 pub fn execute(&self, cx: &mut Context) {
159− (self.1)(cx);
153+ (self.fun)(cx);
160154 }
161155
162156 pub fn name(&self) -> &'static str {
163− self.0
157+ self.name
158+ }
159+
160+ pub fn doc(&self) -> &'static str {
161+ self.doc
164162 }
165163
164+ #[rustfmt::skip]
166165 commands!(
167− move_char_left,
168− move_char_right,
169− move_line_up,
170− move_line_down,
171− move_next_word_start,
172− move_prev_word_start,
173− move_next_word_end,
174− move_next_long_word_start,
175− move_prev_long_word_start,
176− move_next_long_word_end,
177− extend_next_word_start,
178− extend_prev_word_start,
179− extend_next_word_end,
180− find_till_char,
181− find_next_char,
182− extend_till_char,
183− extend_next_char,
184− till_prev_char,
185− find_prev_char,
186− extend_till_prev_char,
187− extend_prev_char,
188− replace,
189− switch_case,
190− switch_to_uppercase,
191− switch_to_lowercase,
192− page_up,
193− page_down,
194− half_page_up,
195− half_page_down,
196− extend_char_left,
197− extend_char_right,
198− extend_line_up,
199− extend_line_down,
200− select_all,
201− select_regex,
202− split_selection,
203− split_selection_on_newline,
204− search,
205− search_next,
206− extend_search_next,
207− search_selection,
208− extend_line,
209− extend_to_line_bounds,
210− delete_selection,
211− change_selection,
212− collapse_selection,
213− flip_selections,
214− insert_mode,
215− append_mode,
216− command_mode,
217− file_picker,
218− buffer_picker,
219− symbol_picker,
220− last_picker,
221− prepend_to_line,
222− append_to_line,
223− open_below,
224− open_above,
225− normal_mode,
226− goto_mode,
227− select_mode,
228− exit_select_mode,
229− goto_definition,
230− goto_type_definition,
231− goto_implementation,
232− goto_file_start,
233− goto_file_end,
234− goto_reference,
235− goto_first_diag,
236− goto_last_diag,
237− goto_next_diag,
238− goto_prev_diag,
239− goto_line_start,
240− goto_line_end,
241− goto_line_end_newline,
242− goto_first_nonwhitespace,
243− signature_help,
244− insert_tab,
245− insert_newline,
246− delete_char_backward,
247− delete_char_forward,
248− delete_word_backward,
249− undo,
250− redo,
251− yank,
252− yank_joined_to_clipboard,
253− yank_main_selection_to_clipboard,
254− replace_with_yanked,
255− replace_selections_with_clipboard,
256− paste_after,
257− paste_before,
258− paste_clipboard_after,
259− paste_clipboard_before,
260− indent,
261− unindent,
262− format_selections,
263− join_selections,
264− keep_selections,
265− keep_primary_selection,
266− completion,
267− hover,
268− toggle_comments,
269− expand_selection,
270− match_brackets,
271− jump_forward,
272− jump_backward,
273− window_mode,
274− rotate_view,
275− hsplit,
276− vsplit,
277− wclose,
278− select_register,
279− space_mode,
280− view_mode,
281− left_bracket_mode,
282− right_bracket_mode,
283− match_mode
166+ move_char_left, "Move left",
167+ move_char_right, "Move right",
168+ move_line_up, "Move up",
169+ move_line_down, "Move down",
170+ extend_char_left, "Extend left",
171+ extend_char_right, "Extend right",
172+ extend_line_up, "Extend up",
173+ extend_line_down, "Extend down",
174+ move_next_word_start, "Move to beginning of next word",
175+ move_prev_word_start, "Move to beginning of previous word",
176+ move_next_word_end, "Move to end of next word",
177+ move_next_long_word_start, "Move to beginning of next long word",
178+ move_prev_long_word_start, "Move to beginning of previous long word",
179+ move_next_long_word_end, "Move to end of next long word",
180+ extend_next_word_start, "Extend to beginning of next word",
181+ extend_prev_word_start, "Extend to beginning of previous word",
182+ extend_next_word_end, "Extend to end of next word",
183+ find_till_char, "Move till next occurance of char",
184+ find_next_char, "Move to next occurance of char",
185+ extend_till_char, "Extend till next occurance of char",
186+ extend_next_char, "Extend to next occurance of char",
187+ till_prev_char, "Move till previous occurance of char",
188+ find_prev_char, "Move to previous occurance of char",
189+ extend_till_prev_char, "Extend till previous occurance of char",
190+ extend_prev_char, "Extend to previous occurance of char",
191+ replace, "Replace with new char",
192+ switch_case, "Switch (toggle) case",
193+ switch_to_uppercase, "Switch to uppercase",
194+ switch_to_lowercase, "Switch to lowercase",
195+ page_up, "Move page up",
196+ page_down, "Move page down",
197+ half_page_up, "Move half page up",
198+ half_page_down, "Move half page down",
199+ select_all, "Select whole document",
200+ select_regex, "Select all regex matches inside selections",
201+ split_selection, "Split selection into subselections on regex matches",
202+ split_selection_on_newline, "Split selection on newlines",
203+ search, "Search for regex pattern",
204+ search_next, "Select next search match",
205+ extend_search_next, "Add next search match to selection",
206+ search_selection, "Use current selection as search pattern",
207+ extend_line, "Select current line, if already selected, extend to next line",
208+ extend_to_line_bounds, "Extend selection to line bounds (line-wise selection)",
209+ delete_selection, "Delete selection",
210+ change_selection, "Change selection (delete and enter insert mode)",
211+ collapse_selection, "Collapse selection onto a single cursor",
212+ flip_selections, "Flip selection cursor and anchor",
213+ insert_mode, "Insert before selection",
214+ append_mode, "Insert after selection (append)",
215+ command_mode, "Enter command mode",
216+ file_picker, "Open file picker",
217+ code_action, "Perform code action",
218+ buffer_picker, "Open buffer picker",
219+ symbol_picker, "Open symbol picker",
220+ last_picker, "Open last picker",
221+ prepend_to_line, "Insert at start of line",
222+ append_to_line, "Insert at end of line",
223+ open_below, "Open new line below selection",
224+ open_above, "Open new line above selection",
225+ normal_mode, "Enter normal mode",
226+ select_mode, "Enter selection extend mode",
227+ exit_select_mode, "Exit selection mode",
228+ goto_definition, "Goto definition",
229+ goto_type_definition, "Goto type definition",
230+ goto_implementation, "Goto implementation",
231+ goto_file_start, "Goto file start",
232+ goto_file_end, "Goto file end",
233+ goto_reference, "Goto references",
234+ goto_window_top, "Goto window top",
235+ goto_window_middle, "Goto window middle",
236+ goto_window_bottom, "Goto window bottom",
237+ goto_last_accessed_file, "Goto last accessed file",
238+ goto_first_diag, "Goto first diagnostic",
239+ goto_last_diag, "Goto last diagnostic",
240+ goto_next_diag, "Goto next diagnostic",
241+ goto_prev_diag, "Goto previous diagnostic",
242+ goto_line_start, "Goto line start",
243+ goto_line_end, "Goto line end",
244+ // TODO: different description ?
245+ goto_line_end_newline, "Goto line end",
246+ goto_first_nonwhitespace, "Goto first non-blank in line",
247+ signature_help, "Show signature help",
248+ insert_tab, "Insert tab char",
249+ insert_newline, "Insert newline char",
250+ delete_char_backward, "Delete previous char",
251+ delete_char_forward, "Delete next char",
252+ delete_word_backward, "Delete previous word",
253+ undo, "Undo change",
254+ redo, "Redo change",
255+ yank, "Yank selection",
256+ yank_joined_to_clipboard, "Join and yank selections to clipboard",
257+ yank_main_selection_to_clipboard, "Yank main selection to clipboard",
258+ replace_with_yanked, "Replace with yanked text",
259+ replace_selections_with_clipboard, "Replace selections by clipboard content",
260+ paste_after, "Paste after selection",
261+ paste_before, "Paste before selection",
262+ paste_clipboard_after, "Paste clipboard after selections",
263+ paste_clipboard_before, "Paste clipboard before selections",
264+ indent, "Indent selection",
265+ unindent, "Unindent selection",
266+ format_selections, "Format selection",
267+ join_selections, "Join lines inside selection",
268+ keep_selections, "Keep selections matching regex",
269+ keep_primary_selection, "Keep primary selection",
270+ completion, "Invoke completion popup",
271+ hover, "Show docs for item under cursor",
272+ toggle_comments, "Comment/uncomment selections",
273+ expand_selection, "Expand selection to parent syntax node",
274+ jump_forward, "Jump forward on jumplist",
275+ jump_backward, "Jump backward on jumplist",
276+ rotate_view, "Goto next window",
277+ hsplit, "Horizontal bottom split",
278+ vsplit, "Vertical right split",
279+ wclose, "Close window",
280+ select_register, "Select register",
281+ align_view_middle, "Align view middle",
282+ align_view_top, "Align view top",
283+ align_view_center, "Align view center",
284+ align_view_bottom, "Align view bottom",
285+ scroll_up, "Scroll view up",
286+ scroll_down, "Scroll view down",
287+ match_brackets, "Goto matching bracket",
288+ surround_add, "Surround add",
289+ surround_replace, "Surround replace",
290+ surround_delete, "Surround delete",
291+ select_textobject_around, "Select around object",
292+ select_textobject_inner, "Select inside object"
284293 );
285294}
286295
287296impl fmt::Debug for Command {
288297 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
289− let Command(name, _) = self;
298+ let Command { name, .. } = self;
290299 f.debug_tuple("Command").field(name).finish()
291300 }
292301}
293302
294303impl fmt::Display for Command {
295304 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
296− let Command(name, _) = self;
305+ let Command { name, .. } = self;
297306 f.write_str(name)
298307 }
299308}
@@ -305,7 +314,7 @@ impl std::str::FromStr for Command {
305314 Command::COMMAND_LIST
306315 .iter()
307316 .copied()
308− .find(|cmd| cmd.0 == s)
317+ .find(|cmd| cmd.name == s)
309318 .ok_or_else(|| anyhow!("No command named '{}'", s))
310319 }
311320}
@@ -1171,7 +1180,7 @@ fn delete_selection(cx: &mut Context) {
11711180 let reg_name = cx.selected_register.name();
11721181 let (view, doc) = current!(cx.editor);
11731182 let registers = &mut cx.editor.registers;
1174− let reg = registers.get_or_insert(reg_name);
1183+ let reg = registers.get_mut(reg_name);
11751184 delete_selection_impl(reg, doc, view.id);
11761185
11771186 doc.append_changes_to_history(view.id);
@@ -1184,7 +1193,7 @@ fn change_selection(cx: &mut Context) {
11841193 let reg_name = cx.selected_register.name();
11851194 let (view, doc) = current!(cx.editor);
11861195 let registers = &mut cx.editor.registers;
1187− let reg = registers.get_or_insert(reg_name);
1196+ let reg = registers.get_mut(reg_name);
11881197 delete_selection_impl(reg, doc, view.id);
11891198 enter_insert_mode(doc);
11901199}
@@ -1974,6 +1983,7 @@ mod cmd {
19741983fn command_mode(cx: &mut Context) {
19751984 let mut prompt = Prompt::new(
19761985 ":".to_owned(),
1986+ Some(':'),
19771987 |input: &str| {
19781988 // we use .this over split_whitespace() because we care about empty segments
19791989 let parts = input.split(' ').collect::<Vec<&str>>();
@@ -2147,6 +2157,112 @@ fn symbol_picker(cx: &mut Context) {
21472157 )
21482158}
21492159
2160+pub fn code_action(cx: &mut Context) {
2161+ let (view, doc) = current!(cx.editor);
2162+
2163+ let language_server = match doc.language_server() {
2164+ Some(language_server) => language_server,
2165+ None => return,
2166+ };
2167+
2168+ let range = range_to_lsp_range(
2169+ doc.text(),
2170+ doc.selection(view.id).primary(),
2171+ language_server.offset_encoding(),
2172+ );
2173+
2174+ let future = language_server.code_actions(doc.identifier(), range);
2175+ let offset_encoding = language_server.offset_encoding();
2176+
2177+ cx.callback(
2178+ future,
2179+ move |_editor: &mut Editor,
2180+ compositor: &mut Compositor,
2181+ response: Option<lsp::CodeActionResponse>| {
2182+ if let Some(actions) = response {
2183+ let picker = Picker::new(
2184+ actions,
2185+ |action| match action {
2186+ lsp::CodeActionOrCommand::CodeAction(action) => {
2187+ action.title.as_str().into()
2188+ }
2189+ lsp::CodeActionOrCommand::Command(command) => command.title.as_str().into(),
2190+ },
2191+ move |editor, code_action, _action| match code_action {
2192+ lsp::CodeActionOrCommand::Command(command) => {
2193+ log::debug!("code action command: {:?}", command);
2194+ editor.set_error(String::from("Handling code action command is not implemented yet, see https://github.com/helix-editor/helix/issues/183"));
2195+ }
2196+ lsp::CodeActionOrCommand::CodeAction(code_action) => {
2197+ log::debug!("code action: {:?}", code_action);
2198+ if let Some(ref workspace_edit) = code_action.edit {
2199+ apply_workspace_edit(editor, offset_encoding, workspace_edit)
2200+ }
2201+ }
2202+ },
2203+ );
2204+ compositor.push(Box::new(picker))
2205+ }
2206+ },
2207+ )
2208+}
2209+
2210+fn apply_workspace_edit(
2211+ editor: &mut Editor,
2212+ offset_encoding: OffsetEncoding,
2213+ workspace_edit: &lsp::WorkspaceEdit,
2214+) {
2215+ if let Some(ref changes) = workspace_edit.changes {
2216+ log::debug!("workspace changes: {:?}", changes);
2217+ editor.set_error(String::from("Handling workspace changesis not implemented yet, see https://github.com/helix-editor/helix/issues/183"));
2218+ return;
2219+ // Not sure if it works properly, it'll be safer to just panic here to avoid breaking some parts of code on which code actions will be used
2220+ // TODO: find some example that uses workspace changes, and test it
2221+ // for (url, edits) in changes.iter() {
2222+ // let file_path = url.origin().ascii_serialization();
2223+ // let file_path = std::path::PathBuf::from(file_path);
2224+ // let file = std::fs::File::open(file_path).unwrap();
2225+ // let mut text = Rope::from_reader(file).unwrap();
2226+ // let transaction = edits_to_changes(&text, edits);
2227+ // transaction.apply(&mut text);
2228+ // }
2229+ }
2230+
2231+ if let Some(ref document_changes) = workspace_edit.document_changes {
2232+ match document_changes {
2233+ lsp::DocumentChanges::Edits(document_edits) => {
2234+ for document_edit in document_edits {
2235+ let (view, doc) = current!(editor);
2236+ assert_eq!(doc.url().unwrap(), document_edit.text_document.uri);
2237+ let edits = document_edit
2238+ .edits
2239+ .iter()
2240+ .map(|edit| match edit {
2241+ lsp::OneOf::Left(text_edit) => text_edit,
2242+ lsp::OneOf::Right(annotated_text_edit) => {
2243+ &annotated_text_edit.text_edit
2244+ }
2245+ })
2246+ .cloned()
2247+ .collect();
2248+
2249+ let transaction = helix_lsp::util::generate_transaction_from_edits(
2250+ doc.text(),
2251+ edits,
2252+ offset_encoding,
2253+ );
2254+ doc.apply(&transaction, view.id);
2255+ doc.append_changes_to_history(view.id);
2256+ }
2257+ }
2258+ lsp::DocumentChanges::Operations(operations) => {
2259+ log::debug!("document changes - operations: {:?}", operations);
2260+ editor.set_error(String::from("Handling document operations is not implemented yet, see https://github.com/helix-editor/helix/issues/183"));
2261+ }
2262+ }
2263+ }
2264+}
2265+
21502266fn last_picker(cx: &mut Context) {
21512267 // TODO: last picker does not seemed to work well with buffer_picker
21522268 cx.callback = Some(Box::new(|compositor: &mut Compositor| {
@@ -2360,20 +2476,6 @@ fn exit_select_mode(cx: &mut Context) {
23602476 doc_mut!(cx.editor).mode = Mode::Normal;
23612477}
23622478
2363−fn goto_prehook(cx: &mut Context) -> bool {
2364− if let Some(count) = cx.count {
2365− push_jump(cx.editor);
2366−
2367− let (view, doc) = current!(cx.editor);
2368− let line_idx = std::cmp::min(count.get() - 1, doc.text().len_lines().saturating_sub(1));
2369− let pos = doc.text().line_to_char(line_idx);
2370− doc.set_selection(view.id, Selection::point(pos));
2371− true
2372− } else {
2373− false
2374− }
2375−}
2376−
23772479fn goto_impl(
23782480 editor: &mut Editor,
23792481 compositor: &mut Compositor,
@@ -3823,199 +3925,3 @@ fn surround_delete(cx: &mut Context) {
38233925 }
38243926 })
38253927}
3826−
3827−/// Do nothing, just for modeinfo.
3828−fn noop(_cx: &mut Context) -> bool {
3829− false
3830−}
3831−
3832−/// Generate modeinfo.
3833−///
3834−/// If prehook returns true then it will stop the rest.
3835−macro_rules! mode_info {
3836− // TODO: reuse $mode for $stat
3837− (@join $first:expr $(,$rest:expr)*) => {
3838− concat!($first, $(", ", $rest),*)
3839− };
3840− (@name #[doc = $name:literal] $(#[$rest:meta])*) => {
3841− $name
3842− };
3843− {
3844− #[doc = $name:literal] $(#[$doc:meta])* $mode:ident, $stat:ident,
3845− $(#[doc = $desc:literal] $($key:tt)|+ => $func:expr),+,
3846− } => {
3847− mode_info! {
3848− #[doc = $name]
3849− $(#[$doc])*
3850− $mode, $stat, noop,
3851− $(
3852− #[doc = $desc]
3853− $($key)|+ => $func
3854− ),+,
3855− }
3856− };
3857− {
3858− #[doc = $name:literal] $(#[$doc:meta])* $mode:ident, $stat:ident, $prehook:expr,
3859− $(#[doc = $desc:literal] $($key:tt)|+ => $func:expr),+,
3860− } => {
3861− #[doc = $name]
3862− $(#[$doc])*
3863− #[doc = ""]
3864− #[doc = "<table><tr><th>key</th><th>desc</th></tr><tbody>"]
3865− $(
3866− #[doc = "<tr><td>"]
3867− // TODO switch to this once we use rust 1.54
3868− // right now it will produce multiple rows
3869− // #[doc = mode_info!(@join $($key),+)]
3870− $(
3871− #[doc = $key]
3872− )+
3873− // <-
3874− #[doc = "</td><td>"]
3875− #[doc = $desc]
3876− #[doc = "</td></tr>"]
3877− )+
3878− #[doc = "</tbody></table>"]
3879− pub fn $mode(cx: &mut Context) {
3880− if $prehook(cx) {
3881− return;
3882− }
3883− static $stat: OnceCell<Info> = OnceCell::new();
3884− cx.editor.autoinfo = Some($stat.get_or_init(|| Info::key(
3885− $name.trim(),
3886− vec![$((&[$($key.parse().unwrap()),+], $desc)),+],
3887− )));
3888− use helix_core::hashmap;
3889− // TODO: try and convert this to match later
3890− let map = hashmap! {
3891− $($($key.parse::<KeyEvent>().unwrap() => $func as for<'r, 's> fn(&'r mut Context<'s>)),+),*
3892− };
3893− cx.on_next_key_mode(map);
3894− }
3895− };
3896−}
3897−
3898−mode_info! {
3899− /// space mode
3900− space_mode, SPACE_MODE,
3901− /// resume last picker
3902− "'" => last_picker,
3903− /// file picker
3904− "f" => file_picker,
3905− /// buffer picker
3906− "b" => buffer_picker,
3907− /// symbol picker
3908− "s" => symbol_picker,
3909− /// window mode
3910− "w" => window_mode,
3911− /// yank joined to clipboard
3912− "y" => yank_joined_to_clipboard,
3913− /// yank main selection to clipboard
3914− "Y" => yank_main_selection_to_clipboard,
3915− /// paste system clipboard after selections
3916− "p" => paste_clipboard_after,
3917− /// paste system clipboard before selections
3918− "P" => paste_clipboard_before,
3919− /// replace selections with clipboard
3920− "R" => replace_selections_with_clipboard,
3921− /// keep primary selection
3922− "space" => keep_primary_selection,
3923−}
3924−
3925−mode_info! {
3926− /// goto
3927− ///
3928− /// When specified with a count, it will go to that line without entering the mode.
3929− goto_mode, GOTO_MODE, goto_prehook,
3930− /// file start
3931− "g" => goto_file_start,
3932− /// file end
3933− "e" => goto_file_end,
3934− /// line start
3935− "h" => goto_line_start,
3936− /// line end
3937− "l" => goto_line_end,
3938− /// line first non blank
3939− "s" => goto_first_nonwhitespace,
3940− /// definition
3941− "d" => goto_definition,
3942− /// type references
3943− "y" => goto_type_definition,
3944− /// references
3945− "r" => goto_reference,
3946− /// implementation
3947− "i" => goto_implementation,
3948− /// window top
3949− "t" => goto_window_top,
3950− /// window middle
3951− "m" => goto_window_middle,
3952− /// window bottom
3953− "b" => goto_window_bottom,
3954− /// last accessed file
3955− "a" => goto_last_accessed_file,
3956−}
3957−
3958−mode_info! {
3959− /// window
3960− window_mode, WINDOW_MODE,
3961− /// rotate
3962− "w" | "C-w" => rotate_view,
3963− /// horizontal split
3964− "h" => hsplit,
3965− /// vertical split
3966− "v" => vsplit,
3967− /// close
3968− "q" => wclose,
3969−}
3970−
3971−mode_info! {
3972− /// match
3973− match_mode, MATCH_MODE,
3974− /// matching character
3975− "m" => match_brackets,
3976− /// surround add
3977− "s" => surround_add,
3978− /// surround replace
3979− "r" => surround_replace,
3980− /// surround delete
3981− "d" => surround_delete,
3982− /// around object
3983− "a" => select_textobject_around,
3984− /// inside object
3985− "i" => select_textobject_inner,
3986−}
3987−
3988−mode_info! {
3989− /// select to previous
3990− left_bracket_mode, LEFT_BRACKET_MODE,
3991− /// previous diagnostic
3992− "d" => goto_prev_diag,
3993− /// diagnostic (first)
3994− "D" => goto_first_diag,
3995−}
3996−
3997−mode_info! {
3998− /// select to next
3999− right_bracket_mode, RIGHT_BRACKET_MODE,
4000− /// diagnostic
4001− "d" => goto_next_diag,
4002− /// diagnostic (last)
4003− "D" => goto_last_diag,
4004−}
4005−
4006−mode_info! {
4007− /// view
4008− view_mode, VIEW_MODE,
4009− /// align view top
4010− "t" => align_view_top,
4011− /// align view center
4012− "z" | "c" => align_view_center,
4013− /// align view bottom
4014− "b" => align_view_bottom,
4015− /// align view middle
4016− "m" => align_view_middle,
4017− /// scroll up
4018− "k" => scroll_up,
4019− /// scroll down
4020− "j" => scroll_down,
4021−}
helix-term/src/config.rs+10−24
@@ -2,9 +2,6 @@ use serde::Deserialize;
22
33use crate::keymap::Keymaps;
44
5−#[cfg(test)]
6−use crate::commands::Command;
7−
85#[derive(Debug, Default, Clone, PartialEq, Deserialize)]
96pub struct Config {
107 pub theme: Option<String>,
@@ -22,12 +19,10 @@ pub struct LspConfig {
2219
2320#[test]
2421fn parsing_keymaps_config_file() {
22+ use crate::keymap;
23+ use crate::keymap::Keymap;
2524 use helix_core::hashmap;
26− use helix_view::{
27− document::Mode,
28− input::KeyEvent,
29− keyboard::{KeyCode, KeyModifiers},
30− };
25+ use helix_view::document::Mode;
3126
3227 let sample_keymaps = r#"
3328 [keys.insert]
@@ -42,22 +37,13 @@ fn parsing_keymaps_config_file() {
4237 toml::from_str::<Config>(sample_keymaps).unwrap(),
4338 Config {
4439 keys: Keymaps(hashmap! {
45− Mode::Insert => hashmap! {
46− KeyEvent {
47− code: KeyCode::Char('y'),
48− modifiers: KeyModifiers::NONE,
49− } => Command::move_line_down,
50− KeyEvent {
51− code: KeyCode::Char('a'),
52− modifiers: KeyModifiers::SHIFT | KeyModifiers::CONTROL,
53− } => Command::delete_selection,
54− },
55− Mode::Normal => hashmap! {
56− KeyEvent {
57− code: KeyCode::F(12),
58− modifiers: KeyModifiers::ALT,
59− } => Command::move_next_word_end,
60− },
40+ Mode::Insert => Keymap::new(keymap!({ "Insert mode"
41+ "y" => move_line_down,
42+ "S-C-a" => delete_selection,
43+ })),
44+ Mode::Normal => Keymap::new(keymap!({ "Normal mode"
45+ "A-F12" => move_next_word_end,
46+ })),
6147 }),
6248 ..Default::default()
6349 }
helix-term/src/keymap.rs+492−200
@@ -1,7 +1,7 @@
11pub use crate::commands::Command;
22use crate::config::Config;
33use helix_core::hashmap;
4−use helix_view::{document::Mode, input::KeyEvent};
4+use helix_view::{document::Mode, info::Info, input::KeyEvent};
55use serde::Deserialize;
66use std::{
77 collections::HashMap,
@@ -24,30 +24,276 @@ macro_rules! key {
2424 };
2525}
2626
27−macro_rules! ctrl {
28− ($($ch:tt)*) => {
29− KeyEvent {
30− code: ::helix_view::keyboard::KeyCode::Char($($ch)*),
31− modifiers: ::helix_view::keyboard::KeyModifiers::CONTROL,
27+/// Macro for defining the root of a `Keymap` object. Example:
28+///
29+/// ```
30+/// # use helix_core::hashmap;
31+/// # use helix_term::keymap;
32+/// # use helix_term::keymap::Keymap;
33+/// let normal_mode = keymap!({ "Normal mode"
34+/// "i" => insert_mode,
35+/// "g" => { "Goto"
36+/// "g" => goto_file_start,
37+/// "e" => goto_file_end,
38+/// },
39+/// "j" | "down" => move_line_down,
40+/// });
41+/// let keymap = Keymap::new(normal_mode);
42+/// ```
43+#[macro_export]
44+macro_rules! keymap {
45+ (@trie $cmd:ident) => {
46+ $crate::keymap::KeyTrie::Leaf($crate::commands::Command::$cmd)
47+ };
48+
49+ (@trie
50+ { $label:literal $($($key:literal)|+ => $value:tt,)+ }
51+ ) => {
52+ keymap!({ $label $($($key)|+ => $value,)+ })
53+ };
54+
55+ (
56+ { $label:literal $($($key:literal)|+ => $value:tt,)+ }
57+ ) => {
58+ // modified from the hashmap! macro
59+ {
60+ let _cap = hashmap!(@count $($($key),+),*);
61+ let mut _map = ::std::collections::HashMap::with_capacity(_cap);
62+ let mut _order = ::std::vec::Vec::with_capacity(_cap);
63+ $(
64+ $(
65+ let _key = $key.parse::<::helix_view::input::KeyEvent>().unwrap();
66+ _map.insert(
67+ _key,
68+ keymap!(@trie $value)
69+ );
70+ _order.push(_key);
71+ )+
72+ )*
73+ $crate::keymap::KeyTrie::Node($crate::keymap::KeyTrieNode::new($label, _map, _order))
3274 }
3375 };
3476}
3577
36−macro_rules! alt {
37− ($($ch:tt)*) => {
38− KeyEvent {
39− code: ::helix_view::keyboard::KeyCode::Char($($ch)*),
40− modifiers: ::helix_view::keyboard::KeyModifiers::ALT,
78+#[derive(Debug, Clone, Deserialize)]
79+pub struct KeyTrieNode {
80+ /// A label for keys coming under this node, like "Goto mode"
81+ #[serde(skip)]
82+ name: String,
83+ #[serde(flatten)]
84+ map: HashMap<KeyEvent, KeyTrie>,
85+ #[serde(skip)]
86+ order: Vec<KeyEvent>,
87+}
88+
89+impl KeyTrieNode {
90+ pub fn new(name: &str, map: HashMap<KeyEvent, KeyTrie>, order: Vec<KeyEvent>) -> Self {
91+ Self {
92+ name: name.to_string(),
93+ map,
94+ order,
4195 }
42− };
96+ }
97+
98+ pub fn name(&self) -> &str {
99+ &self.name
100+ }
101+
102+ /// Merge another Node in. Leaves and subnodes from the other node replace
103+ /// corresponding keyevent in self, except when both other and self have
104+ /// subnodes for same key. In that case the merge is recursive.
105+ pub fn merge(&mut self, mut other: Self) {
106+ for (key, trie) in std::mem::take(&mut other.map) {
107+ if let Some(KeyTrie::Node(node)) = self.map.get_mut(&key) {
108+ if let KeyTrie::Node(other_node) = trie {
109+ node.merge(other_node);
110+ continue;
111+ }
112+ }
113+ self.map.insert(key, trie);
114+ }
115+
116+ for &key in self.map.keys() {
117+ if !self.order.contains(&key) {
118+ self.order.push(key);
119+ }
120+ }
121+ }
122+}
123+
124+impl From<KeyTrieNode> for Info {
125+ fn from(node: KeyTrieNode) -> Self {
126+ let mut body: Vec<(&str, Vec<KeyEvent>)> = Vec::with_capacity(node.len());
127+ for (&key, trie) in node.iter() {
128+ let desc = match trie {
129+ KeyTrie::Leaf(cmd) => cmd.doc(),
130+ KeyTrie::Node(n) => n.name(),
131+ };
132+ match body.iter().position(|(d, _)| d == &desc) {
133+ // FIXME: multiple keys are ordered randomly (use BTreeSet)
134+ Some(pos) => body[pos].1.push(key),
135+ None => body.push((desc, vec![key])),
136+ }
137+ }
138+ body.sort_unstable_by_key(|(_, keys)| {
139+ node.order.iter().position(|&k| k == keys[0]).unwrap()
140+ });
141+ let prefix = format!("{} ", node.name());
142+ if body.iter().all(|(desc, _)| desc.starts_with(&prefix)) {
143+ body = body
144+ .into_iter()
145+ .map(|(desc, keys)| (desc.strip_prefix(&prefix).unwrap(), keys))
146+ .collect();
147+ }
148+ Info::key(node.name(), body)
149+ }
150+}
151+
152+impl Default for KeyTrieNode {
153+ fn default() -> Self {
154+ Self::new("", HashMap::new(), Vec::new())
155+ }
156+}
157+
158+impl PartialEq for KeyTrieNode {
159+ fn eq(&self, other: &Self) -> bool {
160+ self.map == other.map
161+ }
162+}
163+
164+impl Deref for KeyTrieNode {
165+ type Target = HashMap<KeyEvent, KeyTrie>;
166+
167+ fn deref(&self) -> &Self::Target {
168+ &self.map
169+ }
170+}
171+
172+impl DerefMut for KeyTrieNode {
173+ fn deref_mut(&mut self) -> &mut Self::Target {
174+ &mut self.map
175+ }
176+}
177+
178+#[derive(Debug, Clone, PartialEq, Deserialize)]
179+#[serde(untagged)]
180+pub enum KeyTrie {
181+ Leaf(Command),
182+ Node(KeyTrieNode),
183+}
184+
185+impl KeyTrie {
186+ pub fn node(&self) -> Option<&KeyTrieNode> {
187+ match *self {
188+ KeyTrie::Node(ref node) => Some(node),
189+ KeyTrie::Leaf(_) => None,
190+ }
191+ }
192+
193+ pub fn node_mut(&mut self) -> Option<&mut KeyTrieNode> {
194+ match *self {
195+ KeyTrie::Node(ref mut node) => Some(node),
196+ KeyTrie::Leaf(_) => None,
197+ }
198+ }
199+
200+ /// Merge another KeyTrie in, assuming that this KeyTrie and the other
201+ /// are both Nodes. Panics otherwise.
202+ pub fn merge_nodes(&mut self, mut other: Self) {
203+ let node = std::mem::take(other.node_mut().unwrap());
204+ self.node_mut().unwrap().merge(node);
205+ }
206+
207+ pub fn search(&self, keys: &[KeyEvent]) -> Option<&KeyTrie> {
208+ let mut trie = self;
209+ for key in keys {
210+ trie = match trie {
211+ KeyTrie::Node(map) => map.get(key),
212+ // leaf encountered while keys left to process
213+ KeyTrie::Leaf(_) => None,
214+ }?
215+ }
216+ Some(trie)
217+ }
218+}
219+
220+#[derive(Debug, Clone, PartialEq)]
221+pub enum KeymapResult {
222+ /// Needs more keys to execute a command. Contains valid keys for next keystroke.
223+ Pending(KeyTrieNode),
224+ Matched(Command),
225+ /// Key was not found in the root keymap
226+ NotFound,
227+ /// Key is invalid in combination with previous keys. Contains keys leading upto
228+ /// and including current (invalid) key.
229+ Cancelled(Vec<KeyEvent>),
230+}
231+
232+#[derive(Debug, Clone, PartialEq, Deserialize)]
233+pub struct Keymap {
234+ /// Always a Node
235+ #[serde(flatten)]
236+ root: KeyTrie,
237+ #[serde(skip)]
238+ state: Vec<KeyEvent>,
239+}
240+
241+impl Keymap {
242+ pub fn new(root: KeyTrie) -> Self {
243+ Keymap {
244+ root,
245+ state: Vec::new(),
246+ }
247+ }
248+
249+ pub fn root(&self) -> &KeyTrie {
250+ &self.root
251+ }
252+
253+ /// Lookup `key` in the keymap to try and find a command to execute
254+ pub fn get(&mut self, key: KeyEvent) -> KeymapResult {
255+ let &first = self.state.get(0).unwrap_or(&key);
256+ let trie = match self.root.search(&[first]) {
257+ Some(&KeyTrie::Leaf(cmd)) => return KeymapResult::Matched(cmd),
258+ None => return KeymapResult::NotFound,
259+ Some(t) => t,
260+ };
261+ self.state.push(key);
262+ match trie.search(&self.state[1..]) {
263+ Some(&KeyTrie::Node(ref map)) => KeymapResult::Pending(map.clone()),
264+ Some(&KeyTrie::Leaf(command)) => {
265+ self.state.clear();
266+ KeymapResult::Matched(command)
267+ }
268+ None => KeymapResult::Cancelled(self.state.drain(..).collect()),
269+ }
270+ }
271+
272+ pub fn merge(&mut self, other: Self) {
273+ self.root.merge_nodes(other.root);
274+ }
275+}
276+
277+impl Deref for Keymap {
278+ type Target = KeyTrieNode;
279+
280+ fn deref(&self) -> &Self::Target {
281+ &self.root.node().unwrap()
282+ }
283+}
284+
285+impl Default for Keymap {
286+ fn default() -> Self {
287+ Self::new(KeyTrie::Node(KeyTrieNode::default()))
288+ }
43289}
44290
45291#[derive(Debug, Clone, PartialEq, Deserialize)]
46292#[serde(transparent)]
47−pub struct Keymaps(pub HashMap<Mode, HashMap<KeyEvent, Command>>);
293+pub struct Keymaps(pub HashMap<Mode, Keymap>);
48294
49295impl Deref for Keymaps {
50− type Target = HashMap<Mode, HashMap<KeyEvent, Command>>;
296+ type Target = HashMap<Mode, Keymap>;
51297
52298 fn deref(&self) -> &Self::Target {
53299 &self.0
@@ -62,252 +308,298 @@ impl DerefMut for Keymaps {
62308
63309impl Default for Keymaps {
64310 fn default() -> Keymaps {
65− let normal = hashmap!(
66− key!('h') => Command::move_char_left,
67− key!('j') => Command::move_line_down,
68− key!('k') => Command::move_line_up,
69− key!('l') => Command::move_char_right,
70−
71− key!(Left) => Command::move_char_left,
72− key!(Down) => Command::move_line_down,
73− key!(Up) => Command::move_line_up,
74− key!(Right) => Command::move_char_right,
75−
76− key!('t') => Command::find_till_char,
77− key!('f') => Command::find_next_char,
78− key!('T') => Command::till_prev_char,
79− key!('F') => Command::find_prev_char,
80− // and matching set for select mode (extend)
81− //
82− key!('r') => Command::replace,
83− key!('R') => Command::replace_with_yanked,
84−
85− key!('~') => Command::switch_case,
86− alt!('`') => Command::switch_to_uppercase,
87− key!('`') => Command::switch_to_lowercase,
88−
89− key!(Home) => Command::goto_line_start,
90− key!(End) => Command::goto_line_end,
91−
92− key!('w') => Command::move_next_word_start,
93− key!('b') => Command::move_prev_word_start,
94− key!('e') => Command::move_next_word_end,
95−
96− key!('W') => Command::move_next_long_word_start,
97− key!('B') => Command::move_prev_long_word_start,
98− key!('E') => Command::move_next_long_word_end,
99−
100− key!('v') => Command::select_mode,
101− key!('g') => Command::goto_mode,
102− key!(':') => Command::command_mode,
103−
104− key!('i') => Command::insert_mode,
105− key!('I') => Command::prepend_to_line,
106− key!('a') => Command::append_mode,
107− key!('A') => Command::append_to_line,
108− key!('o') => Command::open_below,
109− key!('O') => Command::open_above,
311+ let normal = keymap!({ "Normal mode"
312+ "h" | "left" => move_char_left,
313+ "j" | "down" => move_line_down,
314+ "k" | "up" => move_line_up,
315+ "l" | "right" => move_char_right,
316+
317+ "t" => find_till_char,
318+ "f" => find_next_char,
319+ "T" => till_prev_char,
320+ "F" => find_prev_char,
321+ "r" => replace,
322+ "R" => replace_with_yanked,
323+
324+ "~" => switch_case,
325+ "`" => switch_to_lowercase,
326+ "A-`" => switch_to_uppercase,
327+
328+ "home" => goto_line_start,
329+ "end" => goto_line_end,
330+
331+ "w" => move_next_word_start,
332+ "b" => move_prev_word_start,
333+ "e" => move_next_word_end,
334+
335+ "W" => move_next_long_word_start,
336+ "B" => move_prev_long_word_start,
337+ "E" => move_next_long_word_end,
338+
339+ "v" => select_mode,
340+ "g" => { "Goto"
341+ "g" => goto_file_start,
342+ "e" => goto_file_end,
343+ "h" => goto_line_start,
344+ "l" => goto_line_end,
345+ "s" => goto_first_nonwhitespace,
346+ "d" => goto_definition,
347+ "y" => goto_type_definition,
348+ "r" => goto_reference,
349+ "i" => goto_implementation,
350+ "t" => goto_window_top,
351+ "m" => goto_window_middle,
352+ "b" => goto_window_bottom,
353+ "a" => goto_last_accessed_file,
354+ },
355+ ":" => command_mode,
356+
357+ "i" => insert_mode,
358+ "I" => prepend_to_line,
359+ "a" => append_mode,
360+ "A" => append_to_line,
361+ "o" => open_below,
362+ "O" => open_above,
110363 // [<space> ]<space> equivalents too (add blank new line, no edit)
111364
112−
113− key!('d') => Command::delete_selection,
365+ "d" => delete_selection,
114366 // TODO: also delete without yanking
115− key!('c') => Command::change_selection,
367+ "c" => change_selection,
116368 // TODO: also change delete without yanking
117369
118− // key!('r') => Command::replace_with_char,
119−
120− key!('s') => Command::select_regex,
121− alt!('s') => Command::split_selection_on_newline,
122− key!('S') => Command::split_selection,
123− key!(';') => Command::collapse_selection,
124− alt!(';') => Command::flip_selections,
125− key!('%') => Command::select_all,
126− key!('x') => Command::extend_line,
127− key!('X') => Command::extend_to_line_bounds,
370+ "s" => select_regex,
371+ "A-s" => split_selection_on_newline,
372+ "S" => split_selection,
373+ ";" => collapse_selection,
374+ "A-;" => flip_selections,
375+ "%" => select_all,
376+ "x" => extend_line,
377+ "X" => extend_to_line_bounds,
128378 // crop_to_whole_line
129379
380+ "m" => { "Match"
381+ "m" => match_brackets,
382+ "s" => surround_add,
383+ "r" => surround_replace,
384+ "d" => surround_delete,
385+ "a" => select_textobject_around,
386+ "i" => select_textobject_inner,
387+ },
388+ "[" => { "Left bracket"
389+ "d" => goto_prev_diag,
390+ "D" => goto_first_diag,
391+ },
392+ "]" => { "Right bracket"
393+ "d" => goto_next_diag,
394+ "D" => goto_last_diag,
395+ },
130396
131− key!('m') => Command::match_mode,
132− key!('[') => Command::left_bracket_mode,
133− key!(']') => Command::right_bracket_mode,
134−
135− key!('/') => Command::search,
397+ "/" => search,
136398 // ? for search_reverse
137− key!('n') => Command::search_next,
138− key!('N') => Command::extend_search_next,
399+ "n" => search_next,
400+ "N" => extend_search_next,
139401 // N for search_prev
140− key!('*') => Command::search_selection,
402+ "*" => search_selection,
141403
142− key!('u') => Command::undo,
143− key!('U') => Command::redo,
404+ "u" => undo,
405+ "U" => redo,
144406
145− key!('y') => Command::yank,
407+ "y" => yank,
146408 // yank_all
147− key!('p') => Command::paste_after,
409+ "p" => paste_after,
148410 // paste_all
149− key!('P') => Command::paste_before,
411+ "P" => paste_before,
150412
151− key!('>') => Command::indent,
152− key!('<') => Command::unindent,
153− key!('=') => Command::format_selections,
154− key!('J') => Command::join_selections,
413+ ">" => indent,
414+ "<" => unindent,
415+ "=" => format_selections,
416+ "J" => join_selections,
155417 // TODO: conflicts hover/doc
156− key!('K') => Command::keep_selections,
418+ "K" => keep_selections,
157419 // TODO: and another method for inverse
158420
159421 // TODO: clashes with space mode
160− key!(' ') => Command::keep_primary_selection,
422+ "space" => keep_primary_selection,
161423
162− // key!('q') => Command::record_macro,
163− // key!('Q') => Command::replay_macro,
424+ // "q" => record_macro,
425+ // "Q" => replay_macro,
164426
165− // ~ / apostrophe => change case
166427 // & align selections
167428 // _ trim selections
168429
169430 // C / altC = copy (repeat) selections on prev/next lines
170431
171− key!(Esc) => Command::normal_mode,
172− key!(PageUp) => Command::page_up,
173− key!(PageDown) => Command::page_down,
174− ctrl!('b') => Command::page_up,
175− ctrl!('f') => Command::page_down,
176− ctrl!('u') => Command::half_page_up,
177− ctrl!('d') => Command::half_page_down,
178−
179− ctrl!('w') => Command::window_mode,
432+ "esc" => normal_mode,
433+ "C-b" | "pageup" => page_up,
434+ "C-f" | "pagedown" => page_down,
435+ "C-u" => half_page_up,
436+ "C-d" => half_page_down,
437+
438+ "C-w" => { "Window"
439+ "C-w" | "w" => rotate_view,
440+ "C-h" | "h" => hsplit,
441+ "C-v" | "v" => vsplit,
442+ "C-q" | "q" => wclose,
443+ },
180444
181445 // move under <space>c
182− ctrl!('c') => Command::toggle_comments,
183− key!('K') => Command::hover,
446+ "C-c" => toggle_comments,
447+ "K" => hover,
184448
185449 // z family for save/restore/combine from/to sels from register
186450
187− // supposedly ctrl!('i') but did not work
188− key!(Tab) => Command::jump_forward,
189− ctrl!('o') => Command::jump_backward,
190− // ctrl!('s') => Command::save_selection,
191−
192− key!(' ') => Command::space_mode,
193− key!('z') => Command::view_mode,
451+ // supposedly "C-i" but did not work
452+ "tab" => jump_forward,
453+ "C-o" => jump_backward,
454+ // "C-s" => save_selection,
455+
456+ "space" => { "Space"
457+ "f" => file_picker,
458+ "b" => buffer_picker,
459+ "s" => symbol_picker,
460+ "a" => code_action,
461+ "'" => last_picker,
462+ "w" => { "Window"
463+ "C-w" | "w" => rotate_view,
464+ "C-h" | "h" => hsplit,
465+ "C-v" | "v" => vsplit,
466+ "C-q" | "q" => wclose,
467+ },
468+ "y" => yank_joined_to_clipboard,
469+ "Y" => yank_main_selection_to_clipboard,
470+ "p" => paste_clipboard_after,
471+ "P" => paste_clipboard_before,
472+ "R" => replace_selections_with_clipboard,
473+ "space" => keep_primary_selection,
474+ },
475+ "z" => { "View"
476+ "z" | "c" => align_view_center,
477+ "t" => align_view_top,
478+ "b" => align_view_bottom,
479+ "m" => align_view_middle,
480+ "k" => scroll_up,
481+ "j" => scroll_down,
482+ },
194483
195− key!('"') => Command::select_register,
196− );
484+ "\"" => select_register,
485+ });
197486 // TODO: decide whether we want normal mode to also be select mode (kakoune-like), or whether
198487 // we keep this separate select mode. More keys can fit into normal mode then, but it's weird
199488 // because some selection operations can now be done from normal mode, some from select mode.
200489 let mut select = normal.clone();
201− select.extend(
202− hashmap!(
203− key!('h') => Command::extend_char_left,
204− key!('j') => Command::extend_line_down,
205− key!('k') => Command::extend_line_up,
206− key!('l') => Command::extend_char_right,
207−
208− key!(Left) => Command::extend_char_left,
209− key!(Down) => Command::extend_line_down,
210− key!(Up) => Command::extend_line_up,
211− key!(Right) => Command::extend_char_right,
212−
213− key!('w') => Command::extend_next_word_start,
214− key!('b') => Command::extend_prev_word_start,
215− key!('e') => Command::extend_next_word_end,
216−
217− key!('t') => Command::extend_till_char,
218− key!('f') => Command::extend_next_char,
219−
220− key!('T') => Command::extend_till_prev_char,
221− key!('F') => Command::extend_prev_char,
222− key!(Home) => Command::goto_line_start,
223− key!(End) => Command::goto_line_end,
224− key!(Esc) => Command::exit_select_mode,
225− )
226− .into_iter(),
227− );
228−
490+ select.merge_nodes(keymap!({ "Select mode"
491+ "h" | "left" => extend_char_left,
492+ "j" | "down" => extend_line_down,
493+ "k" | "up" => extend_line_up,
494+ "l" | "right" => extend_char_right,
495+
496+ "w" => extend_next_word_start,
497+ "b" => extend_prev_word_start,
498+ "e" => extend_next_word_end,
499+
500+ "t" => extend_till_char,
501+ "f" => extend_next_char,
502+ "T" => extend_till_prev_char,
503+ "F" => extend_prev_char,
504+
505+ "home" => goto_line_start,
506+ "end" => goto_line_end,
507+ "esc" => exit_select_mode,
508+ }));
509+ let insert = keymap!({ "Insert mode"
510+ "esc" => normal_mode,
511+
512+ "backspace" => delete_char_backward,
513+ "del" => delete_char_forward,
514+ "ret" => insert_newline,
515+ "tab" => insert_tab,
516+ "C-w" => delete_word_backward,
517+
518+ "left" => move_char_left,
519+ "down" => move_line_down,
520+ "up" => move_line_up,
521+ "right" => move_char_right,
522+ "pageup" => page_up,
523+ "pagedown" => page_down,
524+ "home" => goto_line_start,
525+ "end" => goto_line_end_newline,
526+
527+ "C-x" => completion,
528+ });
229529 Keymaps(hashmap!(
230− // as long as you cast the first item, rust is able to infer the other cases
231− // TODO: select could be normal mode with some bindings merged over
232− Mode::Normal => normal,
233− Mode::Select => select,
234− Mode::Insert => hashmap!(
235− key!(Esc) => Command::normal_mode as Command,
236− key!(Backspace) => Command::delete_char_backward,
237− key!(Delete) => Command::delete_char_forward,
238− key!(Enter) => Command::insert_newline,
239− key!(Tab) => Command::insert_tab,
240− key!(Left) => Command::move_char_left,
241− key!(Down) => Command::move_line_down,
242− key!(Up) => Command::move_line_up,
243− key!(Right) => Command::move_char_right,
244− key!(PageUp) => Command::page_up,
245− key!(PageDown) => Command::page_down,
246− key!(Home) => Command::goto_line_start,
247− key!(End) => Command::goto_line_end_newline,
248− ctrl!('x') => Command::completion,
249− ctrl!('w') => Command::delete_word_backward,
250− ),
530+ Mode::Normal => Keymap::new(normal),
531+ Mode::Select => Keymap::new(select),
532+ Mode::Insert => Keymap::new(insert),
251533 ))
252534 }
253535}
254536
255−/// Merge default config keys with user overwritten keys for custom
256−/// user config.
537+/// Merge default config keys with user overwritten keys for custom user config.
257538pub fn merge_keys(mut config: Config) -> Config {
258539 let mut delta = std::mem::take(&mut config.keys);
259540 for (mode, keys) in &mut *config.keys {
260− keys.extend(delta.remove(mode).unwrap_or_default());
541+ keys.merge(delta.remove(mode).unwrap_or_default())
261542 }
262543 config
263544}
264545
265546#[test]
266547fn merge_partial_keys() {
267− use helix_view::keyboard::{KeyCode, KeyModifiers};
268548 let config = Config {
269549 keys: Keymaps(hashmap! {
270− Mode::Normal => hashmap! {
271− KeyEvent {
272− code: KeyCode::Char('i'),
273− modifiers: KeyModifiers::NONE,
274− } => Command::normal_mode,
275− KeyEvent { // key that does not exist
276− code: KeyCode::Char('无'),
277− modifiers: KeyModifiers::NONE,
278− } => Command::insert_mode,
279− },
550+ Mode::Normal => Keymap::new(
551+ keymap!({ "Normal mode"
552+ "i" => normal_mode,
553+ "无" => insert_mode,
554+ "z" => jump_backward,
555+ "g" => { "Merge into goto mode"
556+ "$" => goto_line_end,
557+ "g" => delete_char_forward,
558+ },
559+ })
560+ )
280561 }),
281562 ..Default::default()
282563 };
283− let merged_config = merge_keys(config.clone());
564+ let mut merged_config = merge_keys(config.clone());
284565 assert_ne!(config, merged_config);
566+
567+ let keymap = merged_config.keys.0.get_mut(&Mode::Normal).unwrap();
285568 assert_eq!(
286− *merged_config
287− .keys
288− .0
289− .get(&Mode::Normal)
290− .unwrap()
291− .get(&KeyEvent {
292− code: KeyCode::Char('i'),
293− modifiers: KeyModifiers::NONE
294− })
295− .unwrap(),
296− Command::normal_mode
569+ keymap.get(key!('i')),
570+ KeymapResult::Matched(Command::normal_mode),
571+ "Leaf should replace leaf"
297572 );
298573 assert_eq!(
299− *merged_config
300− .keys
301− .0
302− .get(&Mode::Normal)
303− .unwrap()
304− .get(&KeyEvent {
305− code: KeyCode::Char('无'),
306− modifiers: KeyModifiers::NONE
307− })
308− .unwrap(),
309− Command::insert_mode
574+ keymap.get(key!('无')),
575+ KeymapResult::Matched(Command::insert_mode),
576+ "New leaf should be present in merged keymap"
310577 );
578+ // Assumes that z is a node in the default keymap
579+ assert_eq!(
580+ keymap.get(key!('z')),
581+ KeymapResult::Matched(Command::jump_backward),
582+ "Leaf should replace node"
583+ );
584+ // Assumes that `g` is a node in default keymap
585+ assert_eq!(
586+ keymap.root().search(&[key!('g'), key!('$')]).unwrap(),
587+ &KeyTrie::Leaf(Command::goto_line_end),
588+ "Leaf should be present in merged subnode"
589+ );
590+ // Assumes that `gg` is in default keymap
591+ assert_eq!(
592+ keymap.root().search(&[key!('g'), key!('g')]).unwrap(),
593+ &KeyTrie::Leaf(Command::delete_char_forward),
594+ "Leaf should replace old leaf in merged subnode"
595+ );
596+ // Assumes that `ge` is in default keymap
597+ assert_eq!(
598+ keymap.root().search(&[key!('g'), key!('e')]).unwrap(),
599+ &KeyTrie::Leaf(Command::goto_file_end),
600+ "Old leaves in subnode should be present in merged node"
601+ );
602+
311603 assert!(merged_config.keys.0.get(&Mode::Normal).unwrap().len() > 1);
312604 assert!(merged_config.keys.0.get(&Mode::Insert).unwrap().len() > 0);
313605}
helix-term/src/ui/editor.rs+57−19
@@ -2,7 +2,7 @@ use crate::{
22 commands,
33 compositor::{Component, Context, EventResult},
44 key,
5− keymap::Keymaps,
5+ keymap::{KeymapResult, Keymaps},
66 ui::{Completion, ProgressSpinners},
77};
88
@@ -15,6 +15,7 @@ use helix_core::{
1515use helix_view::{
1616 document::Mode,
1717 graphics::{CursorKind, Modifier, Rect, Style},
18+ info::Info,
1819 input::KeyEvent,
1920 keyboard::{KeyCode, KeyModifiers},
2021 Document, Editor, Theme, View,
@@ -30,6 +31,7 @@ pub struct EditorView {
3031 last_insert: (commands::Command, Vec<KeyEvent>),
3132 completion: Option<Completion>,
3233 spinners: ProgressSpinners,
34+ pub autoinfo: Option<Info>,
3335}
3436
3537const OFFSET: u16 = 7; // 1 diagnostic + 5 linenr + 1 gutter
@@ -48,6 +50,7 @@ impl EditorView {
4850 last_insert: (commands::Command::normal_mode, Vec::new()),
4951 completion: None,
5052 spinners: ProgressSpinners::default(),
53+ autoinfo: None,
5154 }
5255 }
5356
@@ -594,19 +597,53 @@ impl EditorView {
594597 );
595598 }
596599
597− fn insert_mode(&self, cx: &mut commands::Context, event: KeyEvent) {
598− if let Some(command) = self.keymaps[&Mode::Insert].get(&event) {
599− command.execute(cx);
600− } else if let KeyEvent {
601− code: KeyCode::Char(ch),
602− ..
603− } = event
604− {
605− commands::insert::insert_char(cx, ch);
600+ /// Handle events by looking them up in `self.keymaps`. Returns None
601+ /// if event was handled (a command was executed or a subkeymap was
602+ /// activated). Only KeymapResult::{NotFound, Cancelled} is returned
603+ /// otherwise.
604+ fn handle_keymap_event(
605+ &mut self,
606+ mode: Mode,
607+ cxt: &mut commands::Context,
608+ event: KeyEvent,
609+ ) -> Option<KeymapResult> {
610+ self.autoinfo = None;
611+ match self.keymaps.get_mut(&mode).unwrap().get(event) {
612+ KeymapResult::Matched(command) => command.execute(cxt),
613+ KeymapResult::Pending(node) => self.autoinfo = Some(node.into()),
614+ k @ KeymapResult::NotFound | k @ KeymapResult::Cancelled(_) => return Some(k),
606615 }
616+ None
607617 }
608618
609− fn command_mode(&self, mode: Mode, cxt: &mut commands::Context, event: KeyEvent) {
619+ fn insert_mode(&mut self, cx: &mut commands::Context, event: KeyEvent) {
620+ if let Some(keyresult) = self.handle_keymap_event(Mode::Insert, cx, event) {
621+ match keyresult {
622+ KeymapResult::NotFound => {
623+ if let Some(ch) = event.char() {
624+ commands::insert::insert_char(cx, ch)
625+ }
626+ }
627+ KeymapResult::Cancelled(pending) => {
628+ for ev in pending {
629+ match ev.char() {
630+ Some(ch) => commands::insert::insert_char(cx, ch),
631+ None => {
632+ if let KeymapResult::Matched(command) =
633+ self.keymaps.get_mut(&Mode::Insert).unwrap().get(ev)
634+ {
635+ command.execute(cx);
636+ }
637+ }
638+ }
639+ }
640+ }
641+ _ => unreachable!(),
642+ }
643+ }
644+ }
645+
646+ fn command_mode(&mut self, mode: Mode, cxt: &mut commands::Context, event: KeyEvent) {
610647 match event {
611648 // count handling
612649 key!(i @ '0'..='9') => {
@@ -619,8 +656,8 @@ impl EditorView {
619656 // first execute whatever put us into insert mode
620657 self.last_insert.0.execute(cxt);
621658 // then replay the inputs
622− for key in &self.last_insert.1 {
623− self.insert_mode(cxt, *key)
659+ for &key in &self.last_insert.1.clone() {
660+ self.insert_mode(cxt, key)
624661 }
625662 }
626663 _ => {
@@ -633,9 +670,7 @@ impl EditorView {
633670 // set the register
634671 cxt.selected_register = cxt.editor.selected_register.take();
635672
636− if let Some(command) = self.keymaps[&mode].get(&event) {
637− command.execute(cxt);
638− }
673+ self.handle_keymap_event(mode, cxt, event);
639674 }
640675 }
641676 }
@@ -749,7 +784,11 @@ impl Component for EditorView {
749784 // how we entered insert mode is important, and we should track that so
750785 // we can repeat the side effect.
751786
752− self.last_insert.0 = self.keymaps[&mode][&key];
787+ self.last_insert.0 = match self.keymaps.get_mut(&mode).unwrap().get(key) {
788+ KeymapResult::Matched(command) => command,
789+ // FIXME: insert mode can only be entered through single KeyCodes
790+ _ => unimplemented!(),
791+ };
753792 self.last_insert.1.clear();
754793 }
755794 (Mode::Insert, Mode::Normal) => {
@@ -787,9 +826,8 @@ impl Component for EditorView {
787826 );
788827 }
789828
790− if let Some(info) = std::mem::take(&mut cx.editor.autoinfo) {
829+ if let Some(ref info) = self.autoinfo {
791830 info.render(area, surface, cx);
792− cx.editor.autoinfo = Some(info);
793831 }
794832
795833 // render status msg
helix-term/src/ui/info.rs+1−1
@@ -8,7 +8,7 @@ impl Component for Info {
88 fn render(&self, viewport: Rect, surface: &mut Surface, cx: &mut Context) {
99 let style = cx.editor.theme.get("ui.popup");
1010 let block = Block::default()
11− .title(self.title)
11+ .title(self.title.as_str())
1212 .borders(Borders::ALL)
1313 .border_style(style);
1414 let Info { width, height, .. } = self;
helix-term/src/ui/mod.rs+1
@@ -36,6 +36,7 @@ pub fn regex_prompt(
3636
3737 Prompt::new(
3838 prompt,
39+ None,
3940 |_input: &str| Vec::new(), // this is fine because Vec::new() doesn't allocate
4041 move |cx: &mut crate::compositor::Context, input: &str, event: PromptEvent| {
4142 match event {
helix-term/src/ui/picker.rs+1
@@ -43,6 +43,7 @@ impl<T> Picker<T> {
4343 ) -> Self {
4444 let prompt = Prompt::new(
4545 "".to_string(),
46+ None,
4647 |_pattern: &str| Vec::new(),
4748 |_editor: &mut Context, _pattern: &str, _event: PromptEvent| {
4849 //
helix-term/src/ui/prompt.rs+58
@@ -20,6 +20,8 @@ pub struct Prompt {
2020 cursor: usize,
2121 completion: Vec<Completion>,
2222 selection: Option<usize>,
23+ history_register: Option<char>,
24+ history_pos: Option<usize>,
2325 completion_fn: Box<dyn FnMut(&str) -> Vec<Completion>>,
2426 callback_fn: Box<dyn FnMut(&mut Context, &str, PromptEvent)>,
2527 pub doc_fn: Box<dyn Fn(&str) -> Option<&'static str>>,
@@ -54,6 +56,7 @@ pub enum Movement {
5456impl Prompt {
5557 pub fn new(
5658 prompt: String,
59+ history_register: Option<char>,
5760 mut completion_fn: impl FnMut(&str) -> Vec<Completion> + 'static,
5861 callback_fn: impl FnMut(&mut Context, &str, PromptEvent) + 'static,
5962 ) -> Self {
@@ -63,6 +66,8 @@ impl Prompt {
6366 cursor: 0,
6467 completion: completion_fn(""),
6568 selection: None,
69+ history_register,
70+ history_pos: None,
6671 completion_fn: Box::new(completion_fn),
6772 callback_fn: Box::new(callback_fn),
6873 doc_fn: Box::new(|_| None),
@@ -226,6 +231,28 @@ impl Prompt {
226231 self.exit_selection();
227232 }
228233
234+ pub fn change_history(&mut self, register: &[String], direction: CompletionDirection) {
235+ if register.is_empty() {
236+ return;
237+ }
238+
239+ let end = register.len().saturating_sub(1);
240+
241+ let index = match direction {
242+ CompletionDirection::Forward => self.history_pos.map_or(0, |i| i + 1),
243+ CompletionDirection::Backward => {
244+ self.history_pos.unwrap_or(register.len()).saturating_sub(1)
245+ }
246+ }
247+ .min(end);
248+
249+ self.line = register[index].clone();
250+
251+ self.history_pos = Some(index);
252+
253+ self.move_end();
254+ }
255+
229256 pub fn change_completion_selection(&mut self, direction: CompletionDirection) {
230257 if self.completion.is_empty() {
231258 return;
@@ -468,9 +495,40 @@ impl Component for Prompt {
468495 self.exit_selection();
469496 } else {
470497 (self.callback_fn)(cx, &self.line, PromptEvent::Validate);
498+
499+ if let Some(register) = self.history_register {
500+ // store in history
501+ let register = cx.editor.registers.get_mut(register);
502+ register.push(self.line.clone());
503+ }
471504 return close_fn;
472505 }
473506 }
507+ KeyEvent {
508+ code: KeyCode::Char('p'),
509+ modifiers: KeyModifiers::CONTROL,
510+ }
511+ | KeyEvent {
512+ code: KeyCode::Up, ..
513+ } => {
514+ if let Some(register) = self.history_register {
515+ let register = cx.editor.registers.get_mut(register);
516+ self.change_history(register.read(), CompletionDirection::Backward);
517+ }
518+ }
519+ KeyEvent {
520+ code: KeyCode::Char('n'),
521+ modifiers: KeyModifiers::CONTROL,
522+ }
523+ | KeyEvent {
524+ code: KeyCode::Down,
525+ ..
526+ } => {
527+ if let Some(register) = self.history_register {
528+ let register = cx.editor.registers.get_mut(register);
529+ self.change_history(register.read(), CompletionDirection::Forward);
530+ }
531+ }
474532 KeyEvent {
475533 code: KeyCode::Tab, ..
476534 } => self.change_completion_selection(CompletionDirection::Forward),
helix-view/src/editor.rs−3
@@ -1,7 +1,6 @@
11use crate::{
22 clipboard::{get_clipboard_provider, ClipboardProvider},
33 graphics::{CursorKind, Rect},
4− info::Info,
54 theme::{self, Theme},
65 tree::Tree,
76 Document, DocumentId, RegisterSelection, View, ViewId,
@@ -33,7 +32,6 @@ pub struct Editor {
3332 pub syn_loader: Arc<syntax::Loader>,
3433 pub theme_loader: Arc<theme::Loader>,
3534
36− pub autoinfo: Option<&'static Info>,
3735 pub status_msg: Option<(String, Severity)>,
3836}
3937
@@ -67,7 +65,6 @@ impl Editor {
6765 theme_loader: themes,
6866 registers: Registers::default(),
6967 clipboard_provider: get_clipboard_provider(),
70− autoinfo: None,
7168 status_msg: None,
7269 }
7370 }
helix-view/src/info.rs+10−7
@@ -5,9 +5,9 @@ use std::fmt::Write;
55#[derive(Debug)]
66/// Info box used in editor. Rendering logic will be in other crate.
77pub struct Info {
8− /// Title kept as static str for now.
9− pub title: &'static str,
10− /// Text body, should contains newline.
8+ /// Title shown at top.
9+ pub title: String,
10+ /// Text body, should contain newlines.
1111 pub text: String,
1212 /// Body width.
1313 pub width: u16,
@@ -16,17 +16,20 @@ pub struct Info {
1616}
1717
1818impl Info {
19− pub fn key(title: &'static str, body: Vec<(&[KeyEvent], &'static str)>) -> Info {
19+ // body is a BTreeMap instead of a HashMap because keymaps are represented
20+ // with nested hashmaps with no ordering, and each invocation of infobox would
21+ // show different orders of items
22+ pub fn key(title: &str, body: Vec<(&str, Vec<KeyEvent>)>) -> Info {
2023 let (lpad, mpad, rpad) = (1, 2, 1);
2124 let keymaps_width: u16 = body
2225 .iter()
23− .map(|r| r.0.iter().map(|e| e.width() as u16 + 2).sum::<u16>() - 2)
26+ .map(|r| r.1.iter().map(|e| e.width() as u16 + 2).sum::<u16>() - 2)
2427 .max()
2528 .unwrap();
2629 let mut text = String::new();
2730 let mut width = 0;
2831 let height = body.len() as u16;
29− for (keyevents, desc) in body {
32+ for (desc, keyevents) in body {
3033 let keyevent = keyevents[0];
3134 let mut left = keymaps_width - keyevent.width() as u16;
3235 for _ in 0..lpad {
@@ -48,7 +51,7 @@ impl Info {
4851 writeln!(text, "{}", desc).ok();
4952 }
5053 Info {
51− title,
54+ title: title.to_string(),
5255 text,
5356 width,
5457 height,
helix-view/src/input.rs+11−1
@@ -14,6 +14,16 @@ pub struct KeyEvent {
1414 pub modifiers: KeyModifiers,
1515}
1616
17+impl KeyEvent {
18+ /// Get only the character involved in this event
19+ pub fn char(&self) -> Option<char> {
20+ match self.code {
21+ KeyCode::Char(ch) => Some(ch),
22+ _ => None,
23+ }
24+ }
25+}
26+
1727pub(crate) mod keys {
1828 pub(crate) const BACKSPACE: &str = "backspace";
1929 pub(crate) const ENTER: &str = "ret";
@@ -168,7 +178,7 @@ impl std::str::FromStr for KeyEvent {
168178 keys::MINUS => KeyCode::Char('-'),
169179 keys::SEMICOLON => KeyCode::Char(';'),
170180 keys::PERCENT => KeyCode::Char('%'),
171− single if single.len() == 1 => KeyCode::Char(single.chars().next().unwrap()),
181+ single if single.chars().count() == 1 => KeyCode::Char(single.chars().next().unwrap()),
172182 function if function.len() > 1 && function.starts_with('F') => {
173183 let function: String = function.chars().skip(1).collect();
174184 let function = str::parse::<u8>(&function)?;
runtime/queries/rust/indents.toml+3−7
@@ -1,11 +1,4 @@
11indent = [
2− "while_expression",
3− "for_expression",
4− "loop_expression",
5− "if_expression",
6− "if_let_expression",
7− "tuple_expression",
8− "array_expression",
92 "use_list",
103 "block",
114 "match_block",
@@ -17,8 +10,11 @@ indent = [
1710 "struct_pattern",
1811 "tuple_pattern",
1912 "enum_variant_list",
13+ "call_expression",
2014 "binary_expression",
2115 "field_expression",
16+ "tuple_expression",
17+ "array_expression",
2218 "where_clause",
2319 "macro_invocation"
2420]
runtime/themes/bogster.toml+1
@@ -42,6 +42,7 @@
4242
4343"ui.selection" = { bg = "#313f4e" }
4444# "ui.cursor.match" # TODO might want to override this because dimmed is not widely supported
45+"ui.cursor.primary" = { fg = "#ABB2BF", modifiers = ["reversed"] }
4546"ui.menu.selected" = { fg = "#e5ded6", bg = "#313f4e" }
4647
4748"warning" = "#dc7759"