on master help sign in
Extend selection commands.
commit3020077da8efbf914a9cb0a2cbb50362d339a39a authorBlaž Hrastnik <blaz@mxxn.io> dateTue 29 Sep 2020, 01:00 (6 years ago) parentfbe3137
13 files changed+182−45
Cargo.lock+24−22 helix-core/Cargo.toml+8−7 helix-core/src/lib.rs+1 helix-core/src/selection.rs+29 helix-core/src/syntax.rs+3−3 helix-core/src/transaction.rs+3−2 helix-syntax/Cargo.toml+1−1 helix-syntax/src/lib.rs+5−3 helix-term/Cargo.toml+3−3 helix-term/src/editor.rs+30−2 helix-view/Cargo.toml+3−2 helix-view/src/commands.rs+40 helix-view/src/keymap.rs+32
Cargo.lock+24−22
@@ -52,9 +52,9 @@ checksum = "e1ba68f4276a778591e36a0c348a269888f3a177c8d2054969389e3b59611ff5"
5252
5353[[package]]
5454name = "async-barrier"
55−version = "1.0.1"
55+version = "1.1.0"
5656source = "registry+https://github.com/rust-lang/crates.io-index"
57−checksum = "c06293698675eb72e1155867e5982f199d6b6c230dca35bc5ffd9852f470c22a"
57+checksum = "04b50fe84d0aea412a4e751cb01b9e26e2be533b2708607c2a2a739b192ee45a"
5858dependencies = [
5959 "async-mutex",
6060 "event-listener",
@@ -97,9 +97,9 @@ dependencies = [
9797
9898[[package]]
9999name = "async-io"
100−version = "1.1.2"
100+version = "1.1.3"
101101source = "registry+https://github.com/rust-lang/crates.io-index"
102−checksum = "64c629684e697f58c0e99e5e2d84a840e3b336afbcfdbac7b44c3b1e222c2fd8"
102+checksum = "a9951f92a2b4f7793f8fc06a80bdb89b62c618c993497d4606474fb8c34941b5"
103103dependencies = [
104104 "concurrent-queue",
105105 "fastrand",
@@ -115,9 +115,9 @@ dependencies = [
115115
116116[[package]]
117117name = "async-lock"
118−version = "2.1.3"
118+version = "2.2.0"
119119source = "registry+https://github.com/rust-lang/crates.io-index"
120−checksum = "ab3ad7fb4345397e57c19566844b0eba274b92e5d2d2791bb0664cc441697b95"
120+checksum = "76000290eb3c67dfe4e2bdf6b6155847f8e16fc844377a7bd0b5e97622656362"
121121dependencies = [
122122 "async-barrier",
123123 "async-mutex",
@@ -127,9 +127,9 @@ dependencies = [
127127
128128[[package]]
129129name = "async-mutex"
130−version = "1.3.0"
130+version = "1.4.0"
131131source = "registry+https://github.com/rust-lang/crates.io-index"
132−checksum = "66941c2577c4fa351e4ce5fdde8f86c69b88d623f3b955be1bc7362a23434632"
132+checksum = "479db852db25d9dbf6204e6cb6253698f175c15726470f78af0d918e99d6156e"
133133dependencies = [
134134 "event-listener",
135135]
@@ -164,9 +164,9 @@ dependencies = [
164164
165165[[package]]
166166name = "async-rwlock"
167−version = "1.2.1"
167+version = "1.3.0"
168168source = "registry+https://github.com/rust-lang/crates.io-index"
169−checksum = "806b1cc0828c2b1611ccbdd743fc0cc7af09009e62c95a0501c1e5da7b142a22"
169+checksum = "261803dcc39ba9e72760ba6e16d0199b1eef9fc44e81bffabbebb9f5aea3906c"
170170dependencies = [
171171 "async-mutex",
172172 "event-listener",
@@ -174,18 +174,18 @@ dependencies = [
174174
175175[[package]]
176176name = "async-semaphore"
177−version = "1.1.0"
177+version = "1.2.0"
178178source = "registry+https://github.com/rust-lang/crates.io-index"
179−checksum = "66d2be5973230861689460806b8db059bbd8bcb507cabaa71646ae89f5b2f2ee"
179+checksum = "538c756e85eb6ffdefaec153804afb6da84b033e2e5ec3e9d459c34b4bf4d3f6"
180180dependencies = [
181181 "event-listener",
182182]
183183
184184[[package]]
185185name = "async-task"
186−version = "4.0.1"
186+version = "4.0.2"
187187source = "registry+https://github.com/rust-lang/crates.io-index"
188−checksum = "6725e96011a83fae25074a8734932e8d67763522839be7473dcfe8a0d6a378b1"
188+checksum = "8ab27c1aa62945039e44edaeee1dc23c74cc0c303dd5fe0fb462a184f1c3a518"
189189
190190[[package]]
191191name = "atomic-waker"
@@ -306,9 +306,9 @@ dependencies = [
306306
307307[[package]]
308308name = "event-listener"
309−version = "2.4.0"
309+version = "2.5.1"
310310source = "registry+https://github.com/rust-lang/crates.io-index"
311−checksum = "e1cd41440ae7e4734bbd42302f63eaba892afc93a3912dad84006247f0dedb0e"
311+checksum = "f7531096570974c3a9dcf9e4b8e1cede1ec26cf5046219fb3b9d897503b9be59"
312312
313313[[package]]
314314name = "fastrand"
@@ -340,9 +340,9 @@ checksum = "de27142b013a8e869c14957e6d2edeef89e97c289e69d042ee3a49acd8b51789"
340340
341341[[package]]
342342name = "futures-lite"
343−version = "1.7.0"
343+version = "1.8.0"
344344source = "registry+https://github.com/rust-lang/crates.io-index"
345−checksum = "5b77e08e656f472d8ea84c472fa8b0a7a917883048e1cf2d4e34a323cd0aaf63"
345+checksum = "0db18c5f58083b54b0c416638ea73066722c2815c1e54dd8ba85ee3def593c3a"
346346dependencies = [
347347 "fastrand",
348348 "futures-core",
@@ -387,6 +387,7 @@ dependencies = [
387387 "anyhow",
388388 "helix-syntax",
389389 "once_cell",
390+ "regex",
390391 "ropey",
391392 "smallvec",
392393 "tendril",
@@ -425,6 +426,7 @@ dependencies = [
425426 "anyhow",
426427 "crossterm",
427428 "helix-core",
429+ "once_cell",
428430 "tui",
429431]
430432
@@ -774,9 +776,9 @@ dependencies = [
774776
775777[[package]]
776778name = "tree-sitter"
777−version = "0.16.1"
779+version = "0.17.0"
778780source = "registry+https://github.com/rust-lang/crates.io-index"
779−checksum = "1df33680edb07e4fb76edcbdd9c7b849b96709fb878afcf0ada678d6bda167af"
781+checksum = "70ee7370fec3aecde3862a7d64c571048f70a7298daef1815e8fc68b9de54b5c"
780782dependencies = [
781783 "cc",
782784 "regex",
@@ -784,9 +786,9 @@ dependencies = [
784786
785787[[package]]
786788name = "tui"
787−version = "0.10.0"
789+version = "0.11.0"
788790source = "registry+https://github.com/rust-lang/crates.io-index"
789−checksum = "a977b0bb2e2033a6fef950f218f13622c3c34e59754b704ce3492dedab1dfe95"
791+checksum = "36626dee5ede9fd34015e9fb4fd7eedf3f3d05bdf1436aaef15b7d0a24233778"
790792dependencies = [
791793 "bitflags",
792794 "cassowary",
helix-core/Cargo.toml+8−7
@@ -9,12 +9,13 @@ edition = "2018"
99[dependencies]
1010helix-syntax = { path = "../helix-syntax" }
1111
12−ropey = "1.2.0"
13−anyhow = "1.0.31"
14−smallvec = "1.4.0"
12+ropey = "1.2"
13+anyhow = "1"
14+smallvec = "1.4"
1515tendril = { git = "https://github.com/servo/tendril" }
16−unicode-segmentation = "1.6.0"
17−unicode-width = "0.1.7"
16+unicode-segmentation = "1.6"
17+unicode-width = "0.1"
1818# slab = "0.4.2"
19−tree-sitter = "0.16.1"
20−once_cell = "1.4.1"
19+tree-sitter = "0.17"
20+once_cell = "1.4"
21+regex = "1"
helix-core/src/lib.rs+1
@@ -8,6 +8,7 @@ pub mod syntax;
88mod transaction;
99
1010pub use ropey::{Rope, RopeSlice};
11+
1112pub use tendril::StrTendril as Tendril;
1213
1314pub use position::Position;
helix-core/src/selection.rs+29
@@ -60,6 +60,18 @@ impl Range {
6060 }
6161 }
6262
63+ pub fn contains(&self, pos: usize) -> bool {
64+ if self.is_empty() {
65+ return false;
66+ }
67+
68+ if self.anchor < self.head {
69+ self.anchor <= pos && pos < self.head
70+ } else {
71+ self.head < pos && pos <= self.anchor
72+ }
73+ }
74+
6375 /// Map a range through a set of changes. Returns a new range representing the same position
6476 /// after the changes are applied.
6577 pub fn map(self, changes: &ChangeSet) -> Self {
@@ -283,4 +295,21 @@ mod test {
283295
284296 assert_eq!(res, "8/10,10/12");
285297 }
298+
299+ #[test]
300+ fn test_contains() {
301+ let range = Range::new(10, 12);
302+
303+ assert_eq!(range.contains(9), false);
304+ assert_eq!(range.contains(10), true);
305+ assert_eq!(range.contains(11), true);
306+ assert_eq!(range.contains(12), false);
307+ assert_eq!(range.contains(13), false);
308+
309+ let range = Range::new(9, 6);
310+ assert_eq!(range.contains(9), true);
311+ assert_eq!(range.contains(7), true);
312+ assert_eq!(range.contains(6), false);
313+ }
314+
286315}
helix-core/src/syntax.rs+3−3
@@ -48,7 +48,7 @@ impl LanguageConfiguration {
4848 if highlights_query.is_empty() {
4949 Ok(None)
5050 } else {
51− let language = get_language(&self.language_id);
51+ let language = get_language(self.language_id);
5252 let mut config = HighlightConfiguration::new(
5353 language,
5454 &highlights_query,
@@ -1456,7 +1456,7 @@ fn test_parser() {
14561456 .map(String::from)
14571457 .collect();
14581458
1459− let language = get_language(&LANG::Rust);
1459+ let language = get_language(LANG::Rust);
14601460 let mut config = HighlightConfiguration::new(
14611461 language,
14621462 &std::fs::read_to_string(
@@ -1478,7 +1478,7 @@ fn test_parser() {
14781478 fn main() {}
14791479 ",
14801480 );
1481− let syntax = Syntax::new(LANG::Rust, &source, config);
1481+ let syntax = Syntax::new(&source, Arc::new(config));
14821482 let tree = syntax.root_layer.tree.unwrap();
14831483 let root = tree.root_node();
14841484 assert_eq!(root.kind(), "source_file");
helix-core/src/transaction.rs+3−2
@@ -1,4 +1,5 @@
11use crate::{Range, Rope, Selection, State, Tendril};
2+use std::convert::TryFrom;
23
34/// (from, to, replacement)
45pub type Change = (usize, usize, Option<Tendril>);
@@ -112,7 +113,7 @@ impl ChangeSet {
112113 let (pos, _) = s.char_indices().nth(len - j).unwrap();
113114 // calculate the difference
114115 let to_drop = s.len() - pos;
115− s.pop_back(to_drop as u32);
116+ s.pop_back(u32::try_from(to_drop).unwrap());
116117 head_a = Some(Insert(s));
117118 head_b = changes_b.next();
118119 }
@@ -136,7 +137,7 @@ impl ChangeSet {
136137 let (pos, _) = s.char_indices().nth(j).unwrap();
137138 // calculate the difference
138139 let to_drop = s.len() - pos;
139− s.pop_back(to_drop as u32);
140+ s.pop_back(u32::try_from(to_drop).unwrap());
140141 head_a = Some(Insert(s));
141142 head_b = changes_b.next();
142143 }
helix-syntax/Cargo.toml+1−1
@@ -7,7 +7,7 @@ edition = "2018"
77# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
88
99[dependencies]
10−tree-sitter = "0.16"
10+tree-sitter = "0.17"
1111enum-iterator = "0.6"
1212
1313[build-dependencies]
helix-syntax/src/lib.rs+5−3
@@ -13,7 +13,7 @@ macro_rules! mk_extern {
1313#[macro_export]
1414macro_rules! mk_enum {
1515 ( $( $camel:ident ),* ) => {
16− #[derive(Clone, Debug, IntoEnumIterator, PartialEq)]
16+ #[derive(Clone, Copy, Debug, IntoEnumIterator, PartialEq)]
1717 pub enum LANG {
1818 $(
1919 $camel,
@@ -25,7 +25,8 @@ macro_rules! mk_enum {
2525#[macro_export]
2626macro_rules! mk_get_language {
2727 ( $( ($camel:ident, $name:ident) ),* ) => {
28− pub fn get_language(lang: &LANG) -> Language {
28+ #[must_use]
29+ pub fn get_language(lang: LANG) -> Language {
2930 unsafe {
3031 match lang {
3132 $(
@@ -40,7 +41,8 @@ macro_rules! mk_get_language {
4041#[macro_export]
4142macro_rules! mk_get_language_name {
4243 ( $( $camel:ident ),* ) => {
43− pub fn get_language_name(lang: &LANG) -> &'static str {
44+ #[must_use]
45+ pub const fn get_language_name(lang: LANG) -> &'static str {
4446 match lang {
4547 $(
4648 LANG::$camel => stringify!($camel),
helix-term/Cargo.toml+3−3
@@ -15,9 +15,9 @@ helix-core = { path = "../helix-core" }
1515helix-view = { path = "../helix-view", features = ["term"]}
1616
1717anyhow = "1"
18−argh = "0.1.3"
18+argh = "0.1"
1919
2020smol = "1"
21−num_cpus = "1.13.0"
22−tui = { version = "0.10.0", default-features = false, features = ["crossterm"] }
21+num_cpus = "1.13"
22+tui = { version = "0.11", default-features = false, features = ["crossterm"] }
2323crossterm = { version = "0.17", features = ["event-stream"] }
helix-term/src/editor.rs+30−2
@@ -1,8 +1,9 @@
11use crate::Args;
2−use helix_core::{state::coords_at_pos, state::Mode, syntax::HighlightEvent, State};
2+use helix_core::{state::coords_at_pos, state::Mode, syntax::HighlightEvent, Range, State};
33use helix_view::{commands, keymap, View};
44
55use std::{
6+ borrow::Cow,
67 io::{self, stdout, Write},
78 path::PathBuf,
89 time::Duration,
@@ -120,6 +121,16 @@ impl Editor {
120121 let mut visual_x = 0;
121122 let mut line = 0u16;
122123
124+ let visible_selections: Vec<Range> = view
125+ .state
126+ .selection()
127+ .ranges()
128+ .iter()
129+ // TODO: limit selection to one in viewport
130+ .filter(|range| !range.is_empty()) // && range.overlaps(&Range::new(start, end + 1))
131+ .copied()
132+ .collect();
133+
123134 'outer: for event in highlights {
124135 match event.unwrap() {
125136 HighlightEvent::HighlightStart(span) => {
@@ -149,6 +160,8 @@ impl Editor {
149160 // way if only the selection/cursor changes we can copy from cache
150161 // and paint the new cursor.
151162
163+ let mut char_index = start;
164+
152165 // iterate over range char by char
153166 for grapheme in RopeGraphemes::new(&text) {
154167 // TODO: track current char_index
@@ -164,8 +177,21 @@ impl Editor {
164177 } else {
165178 // Cow will prevent allocations if span contained in a single slice
166179 // which should really be the majority case
167− let grapheme = std::borrow::Cow::from(grapheme);
180+ let grapheme = Cow::from(grapheme);
168181 let width = grapheme_width(&grapheme) as u16;
182+
183+ let style = if visible_selections
184+ .iter()
185+ .any(|range| range.contains(char_index))
186+ {
187+ // cedar
188+ style.clone().bg(Color::Rgb(128, 47, 0))
189+ } else {
190+ style
191+ };
192+
193+ // TODO: paint cursor heads except primary
194+
169195 self.surface.set_string(
170196 offset + visual_x,
171197 line,
@@ -176,6 +202,8 @@ impl Editor {
176202 visual_x += width;
177203 }
178204 // if grapheme == "\t"
205+
206+ char_index += 1;
179207 }
180208 }
181209 }
helix-view/Cargo.toml+3−2
@@ -10,9 +10,10 @@ edition = "2018"
1010term = ["tui", "crossterm"]
1111
1212[dependencies]
13−anyhow = "1.0.32"
13+anyhow = "1"
1414helix-core = { path = "../helix-core" }
1515
1616# Conversion traits
17−tui = { version = "0.10.0", default-features = false, features = ["crossterm"], optional = true}
17+tui = { version = "0.11", default-features = false, features = ["crossterm"], optional = true}
1818crossterm = { version = "0.17", features = ["event-stream"], optional = true}
19+once_cell = "1.4"
helix-view/src/commands.rs+40
@@ -115,6 +115,46 @@ pub fn move_next_word_end(view: &mut View, count: usize) {
115115
116116// avoid select by default by having a visual mode switch that makes movements into selects
117117
118+pub fn extend_char_left(view: &mut View, count: usize) {
119+ // TODO: use a transaction
120+ let selection = view
121+ .state
122+ .extend_selection(Direction::Backward, Granularity::Character, count);
123+ view.state.selection = selection;
124+}
125+
126+pub fn extend_char_right(view: &mut View, count: usize) {
127+ // TODO: use a transaction
128+ view.state.selection =
129+ view.state
130+ .extend_selection(Direction::Forward, Granularity::Character, count);
131+}
132+
133+pub fn extend_line_up(view: &mut View, count: usize) {
134+ // TODO: use a transaction
135+ view.state.selection =
136+ view.state
137+ .extend_selection(Direction::Backward, Granularity::Line, count);
138+}
139+
140+pub fn extend_line_down(view: &mut View, count: usize) {
141+ // TODO: use a transaction
142+ view.state.selection =
143+ view.state
144+ .extend_selection(Direction::Forward, Granularity::Line, count);
145+}
146+
147+pub fn delete_selection(view: &mut View, _count: usize) {
148+ let transaction =
149+ Transaction::change_by_selection(&view.state, |range| (range.from(), range.to(), None));
150+ transaction.apply(&mut view.state);
151+}
152+
153+pub fn change_selection(view: &mut View, count: usize) {
154+ delete_selection(view, count);
155+ insert_mode(view, count);
156+}
157+
118158// insert mode:
119159// first we calculate the correct cursors/selections
120160// then we just append at each cursor
helix-view/src/keymap.rs+32
@@ -7,7 +7,9 @@ use std::collections::HashMap;
77// normal = {
88// q = record_macro
99// w = (next) word
10+// W = next WORD
1011// e = end of word
12+// E = end of WORD
1113// r =
1214// t = 'till char
1315// y = yank
@@ -55,6 +57,12 @@ use std::collections::HashMap;
5557// & = align cursor
5658// ? = extend to next given regex match (alt = to prev)
5759//
60+// in kakoune these are alt-h alt-l / gh gl
61+// select from curs to begin end / move curs to begin end
62+// 0 = start of line
63+// ^ = start of line (first non blank char)
64+// $ = end of line
65+//
5866// z = save selections
5967// Z = restore selections
6068// x = select line
@@ -110,6 +118,22 @@ pub fn default() -> Keymaps {
110118 code: KeyCode::Char('l'),
111119 modifiers: Modifiers::NONE
112120 }] => commands::move_char_right as Command,
121+ vec![Key {
122+ code: KeyCode::Char('H'),
123+ modifiers: Modifiers::SHIFT
124+ }] => commands::extend_char_left as Command,
125+ vec![Key {
126+ code: KeyCode::Char('J'),
127+ modifiers: Modifiers::SHIFT
128+ }] => commands::extend_line_down as Command,
129+ vec![Key {
130+ code: KeyCode::Char('K'),
131+ modifiers: Modifiers::SHIFT
132+ }] => commands::extend_line_up as Command,
133+ vec![Key {
134+ code: KeyCode::Char('L'),
135+ modifiers: Modifiers::SHIFT
136+ }] => commands::extend_char_right as Command,
113137 vec![Key {
114138 code: KeyCode::Char('w'),
115139 modifiers: Modifiers::NONE
@@ -142,6 +166,14 @@ pub fn default() -> Keymaps {
142166 code: KeyCode::Char('o'),
143167 modifiers: Modifiers::NONE
144168 }] => commands::open_below as Command,
169+ vec![Key {
170+ code: KeyCode::Char('d'),
171+ modifiers: Modifiers::NONE
172+ }] => commands::delete_selection as Command,
173+ vec![Key {
174+ code: KeyCode::Char('c'),
175+ modifiers: Modifiers::NONE
176+ }] => commands::change_selection as Command,
145177 vec![Key {
146178 code: KeyCode::Esc,
147179 modifiers: Modifiers::NONE