on master help sign in
Fix panic when entering unicode in command prompt

It was attempted to use `String::insert` and `String::remove` to insert without taking care of unicodes. Fixes https://github.com/helix-editor/helix/issues/282

commit42142cf680197a2076b9fa8ec864b91e67068082 authorBenoît CORTIER <benoit.cortier@fried-world.eu> dateThu 17 Jun 2021, 18:24 (5 years ago) parent8664d70
1 file changed+17−2
helix-term/src/ui/prompt.rs+17−2
helix-term/src/ui/prompt.rs+17−2
@@ -53,7 +53,16 @@ impl Prompt {
5353 }
5454
5555 pub fn insert_char(&mut self, c: char) {
56− self.line.insert(self.cursor, c);
56+ let pos = if self.line.is_empty() {
57+ 0
58+ } else {
59+ self.line
60+ .char_indices()
61+ .nth(self.cursor)
62+ .map(|(pos, _)| pos)
63+ .unwrap_or_else(|| self.line.len())
64+ };
65+ self.line.insert(pos, c);
5766 self.cursor += 1;
5867 self.completion = (self.completion_fn)(&self.line);
5968 self.exit_selection();
@@ -79,7 +88,13 @@ impl Prompt {
7988
8089 pub fn delete_char_backwards(&mut self) {
8190 if self.cursor > 0 {
82− self.line.remove(self.cursor - 1);
91+ let pos = self
92+ .line
93+ .char_indices()
94+ .nth(self.cursor - 1)
95+ .map(|(pos, _)| pos)
96+ .expect("line is not empty");
97+ self.line.remove(pos);
8398 self.cursor -= 1;
8499 self.completion = (self.completion_fn)(&self.line);
85100 }