on master help sign in
Fix cursor rendering & placement on append mode.
commit5945815d978da07d0970502d481c17ef02525c4d authorBlaž Hrastnik <blaz@mxxn.io> dateThu 1 Oct 2020, 18:44 (6 years ago) parentd9d59cd
3 files changed+28−2
helix-core/src/state.rs+3 helix-term/src/editor.rs+11−1 helix-view/src/commands.rs+14−1
helix-core/src/state.rs+3
@@ -20,6 +20,8 @@ pub struct State {
2020 pub selection: Selection,
2121 pub mode: Mode,
2222
23+ pub restore_cursor: bool,
24+
2325 //
2426 pub syntax: Option<Syntax>,
2527}
@@ -46,6 +48,7 @@ impl State {
4648 selection: Selection::single(0, 0),
4749 mode: Mode::Normal,
4850 syntax: None,
51+ restore_cursor: false,
4952 }
5053 }
5154
helix-term/src/editor.rs+11−1
@@ -127,7 +127,7 @@ impl Editor {
127127 .ranges()
128128 .iter()
129129 // TODO: limit selection to one in viewport
130− .filter(|range| !range.is_empty()) // && range.overlaps(&Range::new(start, end + 1))
130+ // .filter(|range| !range.is_empty()) // && range.overlaps(&Range::new(start, end + 1))
131131 .copied()
132132 .collect();
133133
@@ -178,6 +178,7 @@ impl Editor {
178178 let grapheme = Cow::from(grapheme);
179179 let width = grapheme_width(&grapheme) as u16;
180180
181+ // TODO: this should really happen as an after pass
181182 let style = if visible_selections
182183 .iter()
183184 .any(|range| range.contains(char_index))
@@ -188,6 +189,15 @@ impl Editor {
188189 style
189190 };
190191
192+ let style = if visible_selections
193+ .iter()
194+ .any(|range| range.head == char_index)
195+ {
196+ style.clone().bg(Color::Rgb(255, 255, 255))
197+ } else {
198+ style
199+ };
200+
191201 // TODO: paint cursor heads except primary
192202
193203 self.surface.set_string(
helix-view/src/commands.rs+14−1
@@ -186,6 +186,7 @@ pub fn insert_mode(view: &mut View, _count: usize) {
186186// inserts at the end of each selection
187187pub fn append_mode(view: &mut View, _count: usize) {
188188 view.state.mode = Mode::Insert;
189+ view.state.restore_cursor = true;
189190
190191 // TODO: as transaction
191192 let text = &view.state.doc.slice(..);
@@ -268,8 +269,20 @@ pub fn open_below(view: &mut View, _count: usize) {
268269// O inserts a new line before each line with a selection
269270
270271pub fn normal_mode(view: &mut View, _count: usize) {
271− // TODO: if leaving append mode, move cursor back by 1
272272 view.state.mode = Mode::Normal;
273+
274+ // if leaving append mode, move cursor back by 1
275+ if view.state.restore_cursor {
276+ let text = &view.state.doc.slice(..);
277+ view.state.selection = view.state.selection.transform(|range| {
278+ Range::new(
279+ range.from(),
280+ graphemes::prev_grapheme_boundary(text, range.to()),
281+ )
282+ });
283+
284+ view.state.restore_cursor = false;
285+ }
273286}
274287
275288// TODO: insert means add text just before cursor, on exit we should be on the last letter.