aboutsummaryrefslogtreecommitdiff
path: root/helix-core/src
diff options
context:
space:
mode:
authorSkyler Hawthorne2022-03-17 03:34:21 +0000
committerSkyler Hawthorne2022-06-19 03:54:03 +0000
commit0f3c10a021bbe79e20bde1f55b87465edeec476d (patch)
treea2d54e96885b5761e806eddd9bd8206071d15e8c /helix-core/src
parent502d3290fb88d8a871b0824adc7987a98104933d (diff)
Fix initial selection of Document in new view
When a new View of a Document is created, a default cursor of 0, 0 is created, and it does not get normalized to a single width cursor until at least one movement of the cursor happens. This appears to have no practical negative effect that I could find, but it makes tests difficult to work with, since the initial selection is not what you expect it to be. This changes the initial selection of a new View to be the width of the first grapheme in the text.
Diffstat (limited to 'helix-core/src')
-rw-r--r--helix-core/src/auto_pairs.rs14
-rw-r--r--helix-core/src/selection.rs10
2 files changed, 11 insertions, 13 deletions
diff --git a/helix-core/src/auto_pairs.rs b/helix-core/src/auto_pairs.rs
index bcd47356..1131178e 100644
--- a/helix-core/src/auto_pairs.rs
+++ b/helix-core/src/auto_pairs.rs
@@ -1,9 +1,7 @@
//! When typing the opening character of one of the possible pairs defined below,
//! this module provides the functionality to insert the paired closing character.
-use crate::{
- graphemes, movement::Direction, Range, Rope, RopeGraphemes, Selection, Tendril, Transaction,
-};
+use crate::{graphemes, movement::Direction, Range, Rope, Selection, Tendril, Transaction};
use std::collections::HashMap;
use log::debug;
@@ -149,14 +147,6 @@ fn prev_char(doc: &Rope, pos: usize) -> Option<char> {
doc.get_char(pos - 1)
}
-fn is_single_grapheme(doc: &Rope, range: &Range) -> bool {
- let mut graphemes = RopeGraphemes::new(doc.slice(range.from()..range.to()));
- let first = graphemes.next();
- let second = graphemes.next();
- debug!("first: {:#?}, second: {:#?}", first, second);
- first.is_some() && second.is_none()
-}
-
/// calculate what the resulting range should be for an auto pair insertion
fn get_next_range(
doc: &Rope,
@@ -189,8 +179,8 @@ fn get_next_range(
);
}
- let single_grapheme = is_single_grapheme(doc, start_range);
let doc_slice = doc.slice(..);
+ let single_grapheme = start_range.is_single_grapheme(doc_slice);
// just skip over graphemes
if len_inserted == 0 {
diff --git a/helix-core/src/selection.rs b/helix-core/src/selection.rs
index 1b2416f5..83bab5e3 100644
--- a/helix-core/src/selection.rs
+++ b/helix-core/src/selection.rs
@@ -8,7 +8,7 @@ use crate::{
prev_grapheme_boundary,
},
movement::Direction,
- Assoc, ChangeSet, RopeSlice,
+ Assoc, ChangeSet, RopeGraphemes, RopeSlice,
};
use smallvec::{smallvec, SmallVec};
use std::borrow::Cow;
@@ -339,6 +339,14 @@ impl Range {
pub fn cursor_line(&self, text: RopeSlice) -> usize {
text.char_to_line(self.cursor(text))
}
+
+ /// Returns true if this Range covers a single grapheme in the given text
+ pub fn is_single_grapheme(&self, doc: RopeSlice) -> bool {
+ let mut graphemes = RopeGraphemes::new(doc.slice(self.from()..self.to()));
+ let first = graphemes.next();
+ let second = graphemes.next();
+ first.is_some() && second.is_none()
+ }
}
impl From<(usize, usize)> for Range {