on master help sign in
Fix panic when entering unicode in command prompt
42142cfBenoît CORTIER
helix-term/src/ui/prompt.rs+17−2
@@ -53,7 +53,16 @@ impl Prompt {
53 }
53 }
54
54
55 pub fn insert_char(&mut self, c: char) {
55 pub fn insert_char(&mut self, c: char) {
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+ };
56− self.line.insert(self.cursor, c);
65+ self.line.insert(pos, c);
57 self.cursor += 1;
66 self.cursor += 1;
58 self.completion = (self.completion_fn)(&self.line);
67 self.completion = (self.completion_fn)(&self.line);
59 self.exit_selection();
68 self.exit_selection();
@@ -79,7 +88,13 @@ impl Prompt {
79
88
80 pub fn delete_char_backwards(&mut self) {
89 pub fn delete_char_backwards(&mut self) {
81 if self.cursor > 0 {
90 if self.cursor > 0 {
91+ let pos = self
92+ .line
93+ .char_indices()
82− self.line.remove(self.cursor - 1);
94+ .nth(self.cursor - 1)
95+ .map(|(pos, _)| pos)
96+ .expect("line is not empty");
97+ self.line.remove(pos);
83 self.cursor -= 1;
98 self.cursor -= 1;
84 self.completion = (self.completion_fn)(&self.line);
99 self.completion = (self.completion_fn)(&self.line);
85 }
100 }