on master help sign in
commands: Implement join_selections.
2c9b020Blaž Hrastnik
helix-core/src/state.rs+4−2
@@ -292,7 +292,8 @@ fn categorize(ch: char) -> Category {
292 }
292 }
293}
293}
294
294
295+#[inline]
295−fn skip_over_next<F>(slice: RopeSlice, pos: &mut usize, fun: F)
296+pub fn skip_over_next<F>(slice: RopeSlice, pos: &mut usize, fun: F)
296where
297where
297 F: Fn(char) -> bool,
298 F: Fn(char) -> bool,
298{
299{
@@ -306,7 +307,8 @@ where
306 }
307 }
307}
308}
308
309
310+#[inline]
309−fn skip_over_prev<F>(slice: RopeSlice, pos: &mut usize, fun: F)
311+pub fn skip_over_prev<F>(slice: RopeSlice, pos: &mut usize, fun: F)
310where
312where
311 F: Fn(char) -> bool,
313 F: Fn(char) -> bool,
312{
314{
helix-term/src/commands.rs+45
@@ -308,6 +308,7 @@ pub fn select_regex(cx: &mut Context) {
308 let prompt = ui::regex_prompt(cx, "select:".to_string(), |doc, regex| {
308 let prompt = ui::regex_prompt(cx, "select:".to_string(), |doc, regex| {
309 let text = doc.text().slice(..);
309 let text = doc.text().slice(..);
310 // TODO: if select on matches returns empty range, we need to abort
310 // TODO: if select on matches returns empty range, we need to abort
311+ // if regex empty or no matches, return
311 let selection =
312 let selection =
312 selection::select_on_matches(text, doc.selection(), &regex).expect("no matches");
313 selection::select_on_matches(text, doc.selection(), &regex).expect("no matches");
313 doc.set_selection(selection);
314 doc.set_selection(selection);
@@ -929,6 +930,50 @@ pub fn format_selections(cx: &mut Context) {
929 doc.append_changes_to_history();
930 doc.append_changes_to_history();
930}
931}
931
932
933+pub fn join_selections(cx: &mut Context) {
934+ use helix_core::state::skip_over_next;
935+ let doc = cx.doc();
936+ let text = doc.text();
937+ let slice = doc.text().slice(..);
938+
939+ let mut changes = Vec::new();
940+ let fragment = Tendril::from(" ");
941+
942+ for selection in doc.selection().ranges() {
943+ let start = text.char_to_line(selection.from());
944+ let mut end = text.char_to_line(selection.to());
945+ if start == end {
946+ end += 1
947+ }
948+ let lines = start..end;
949+
950+ changes.reserve(lines.len());
951+
952+ for line in lines {
953+ let mut start = text.line_to_char(line + 1).saturating_sub(1);
954+ let mut end = start + 1;
955+ skip_over_next(slice, &mut end, |ch| matches!(ch, ' ' | '\t'));
956+
957+ // need to skip from start, not end
958+ let change = (start, end, Some(fragment.clone()));
959+ changes.push(change);
960+ }
961+ }
962+
963+ changes.sort_unstable_by_key(|(from, _to, _text)| *from);
964+ changes.dedup();
965+
966+ // TODO: joining multiple empty lines should be replaced by a single space.
967+ // need to merge change ranges that touch
968+
969+ let transaction = Transaction::change(&doc.state, changes.into_iter());
970+ // TODO: select inserted spaces
971+ // .with_selection(selection);
972+
973+ doc.apply(&transaction);
974+ doc.append_changes_to_history();
975+}
976+
932//
977//
933
978
934pub fn save(cx: &mut Context) {
979pub fn save(cx: &mut Context) {
helix-term/src/keymap.rs+15−3
@@ -138,28 +138,35 @@ pub fn default() -> Keymaps {
138 vec![key!('h')] => commands::move_char_left as Command,
138 vec![key!('h')] => commands::move_char_left as Command,
139 vec![key!('j')] => commands::move_line_down,
139 vec![key!('j')] => commands::move_line_down,
140 vec![key!('k')] => commands::move_line_up,
140 vec![key!('k')] => commands::move_line_up,
141+ vec![key!('l')] => commands::move_char_right,
142+
141 vec![key!('0')] => commands::move_line_start,
143 vec![key!('0')] => commands::move_line_start,
142 vec![key!('$')] => commands::move_line_end,
144 vec![key!('$')] => commands::move_line_end,
143− vec![key!('l')] => commands::move_char_right,
145+
144 vec![shift!('H')] => commands::extend_char_left,
146 vec![shift!('H')] => commands::extend_char_left,
145 vec![shift!('J')] => commands::extend_line_down,
147 vec![shift!('J')] => commands::extend_line_down,
146 vec![shift!('K')] => commands::extend_line_up,
148 vec![shift!('K')] => commands::extend_line_up,
147 vec![shift!('L')] => commands::extend_char_right,
149 vec![shift!('L')] => commands::extend_char_right,
150+
148 vec![key!('w')] => commands::move_next_word_start,
151 vec![key!('w')] => commands::move_next_word_start,
149 vec![shift!('W')] => commands::extend_next_word_start,
152 vec![shift!('W')] => commands::extend_next_word_start,
150 vec![key!('b')] => commands::move_prev_word_start,
153 vec![key!('b')] => commands::move_prev_word_start,
151 vec![shift!('B')] => commands::extend_prev_word_start,
154 vec![shift!('B')] => commands::extend_prev_word_start,
152 vec![key!('e')] => commands::move_next_word_end,
155 vec![key!('e')] => commands::move_next_word_end,
153 vec![key!('E')] => commands::extend_next_word_end,
156 vec![key!('E')] => commands::extend_next_word_end,
154− // TODO: E
157+
155 vec![key!('g')] => commands::goto_mode,
158 vec![key!('g')] => commands::goto_mode,
159+ vec![key!(':')] => commands::command_mode,
160+
156 vec![key!('i')] => commands::insert_mode,
161 vec![key!('i')] => commands::insert_mode,
157 vec![shift!('I')] => commands::prepend_to_line,
162 vec![shift!('I')] => commands::prepend_to_line,
158 vec![key!('a')] => commands::append_mode,
163 vec![key!('a')] => commands::append_mode,
159 vec![shift!('A')] => commands::append_to_line,
164 vec![shift!('A')] => commands::append_to_line,
160 vec![key!('o')] => commands::open_below,
165 vec![key!('o')] => commands::open_below,
166+
161 vec![key!('d')] => commands::delete_selection,
167 vec![key!('d')] => commands::delete_selection,
162 vec![key!('c')] => commands::change_selection,
168 vec![key!('c')] => commands::change_selection,
169+
163 vec![key!('s')] => commands::select_regex,
170 vec![key!('s')] => commands::select_regex,
164 vec![alt!('s')] => commands::split_selection_on_newline,
171 vec![alt!('s')] => commands::split_selection_on_newline,
165 vec![shift!('S')] => commands::split_selection,
172 vec![shift!('S')] => commands::split_selection,
@@ -167,19 +174,24 @@ pub fn default() -> Keymaps {
167 vec![alt!(';')] => commands::flip_selections,
174 vec![alt!(';')] => commands::flip_selections,
168 vec![key!('%')] => commands::select_all,
175 vec![key!('%')] => commands::select_all,
169 vec![key!('x')] => commands::select_line,
176 vec![key!('x')] => commands::select_line,
177+
170 // TODO: figure out what key to use
178 // TODO: figure out what key to use
171 vec![key!('[')] => commands::expand_selection,
179 vec![key!('[')] => commands::expand_selection,
180+
172 vec![key!('/')] => commands::search,
181 vec![key!('/')] => commands::search,
173 vec![key!('n')] => commands::search_next,
182 vec![key!('n')] => commands::search_next,
174 vec![key!('*')] => commands::search_selection,
183 vec![key!('*')] => commands::search_selection,
184+
175 vec![key!('u')] => commands::undo,
185 vec![key!('u')] => commands::undo,
176 vec![shift!('U')] => commands::redo,
186 vec![shift!('U')] => commands::redo,
187+
177 vec![key!('y')] => commands::yank,
188 vec![key!('y')] => commands::yank,
178 vec![key!('p')] => commands::paste,
189 vec![key!('p')] => commands::paste,
190+
179 vec![key!('>')] => commands::indent,
191 vec![key!('>')] => commands::indent,
180 vec![key!('<')] => commands::unindent,
192 vec![key!('<')] => commands::unindent,
181 vec![key!('=')] => commands::format_selections,
193 vec![key!('=')] => commands::format_selections,
182− vec![key!(':')] => commands::command_mode,
194+ vec![ctrl!('j')] => commands::join_selections,
183 vec![Key {
195 vec![Key {
184 code: KeyCode::Esc,
196 code: KeyCode::Esc,
185 modifiers: Modifiers::NONE
197 modifiers: Modifiers::NONE