on master help sign in
commands: Wire up toggle comments as ctrl-c
commit7a1ff5e45f829df17dfdcbd4923ae9e83fc2bf3f authorBlaž Hrastnik <blaz@mxxn.io> dateFri 19 Feb 2021, 13:59 (6 years ago) parent4ab5631
4 files changed+35−16
TODO.md+16−11 helix-core/src/comment.rs+7−4 helix-term/src/commands.rs+9−1 helix-term/src/keymap.rs+3
TODO.md+16−11
@@ -27,21 +27,26 @@
2727
2828- [ ] regex search / select next
2929- [ ] f / t mappings
30+- [ ] = for auto indent line/selection
31+- [ ] q should only close the view, if all are closed, close the editor
32+- [ ] buffers should sit on editor.buffers, view simply refs them
3033
3134
32352
33−- extend selection (treesitter select parent node) (replaces viw, vi(, va( etc )
34−- bracket pairs
35−- comment block (gcc)
36−- completion signature popups/docs
37−- multiple views into the same file
38−- selection align
36+- [ ] tab completion for paths on the prompt
37+- [ ] extend selection (treesitter select parent node) (replaces viw, vi(, va( etc )
38+- [ ] bracket pairs
39+- [x] comment block (gcc)
40+- [ ] completion signature popups/docs
41+- [ ] multiple views into the same file
42+- [ ] selection align
43+- [ ] store some state: file positions, prompt history
3944
40453
41−- diagnostics popups
42−- diff mode with highlighting?
43−- snippet support (tab to jump between marks)
44−- gamelisp/wasm scripting
46+- [ ] diagnostics popups
47+- [ ] diff mode with highlighting?
48+- [ ] snippet support (tab to jump between marks)
49+- [ ] gamelisp/wasm scripting
4550
4651X
47−- rendering via skulpin/skia or raw wgpu
52+- [ ] rendering via skulpin/skia or raw wgpu
helix-core/src/comment.rs+7−4
@@ -1,4 +1,4 @@
1−use crate::{find_first_non_whitespace_char2, Change, RopeSlice, State, Transaction};
1+use crate::{find_first_non_whitespace_char2, Change, RopeSlice, State, Tendril, Transaction};
22use core::ops::Range;
33use std::borrow::Cow;
44
@@ -7,7 +7,6 @@ fn find_line_comment(
77 text: RopeSlice,
88 lines: Range<usize>,
99) -> (bool, Vec<usize>, usize) {
10− // selection ranges map char_to_line from() .. to()
1110 let mut commented = true;
1211 let mut skipped = Vec::new();
1312 let mut min = usize::MAX; // minimum col for find_first_non_whitespace_char
@@ -36,11 +35,12 @@ fn find_line_comment(
3635 (commented, skipped, min)
3736}
3837
39−fn toggle_line_comments(state: &State) -> Transaction {
38+pub fn toggle_line_comments(state: &State) -> Transaction {
4039 let text = state.doc.slice(..);
4140 let mut changes: Vec<Change> = Vec::new();
4241
4342 let token = "//";
43+ let comment = Tendril::from(format!("{} ", token));
4444
4545 for selection in state.selection.ranges() {
4646 let start = text.char_to_line(selection.from());
@@ -59,7 +59,7 @@ fn toggle_line_comments(state: &State) -> Transaction {
5959
6060 if !commented {
6161 // comment line
62− changes.push((pos, pos, Some(format!("{} ", token).into())))
62+ changes.push((pos, pos, Some(comment.clone())))
6363 } else {
6464 // uncomment line
6565 let margin = 1; // TODO: margin is hardcoded 1 but could easily be 0
@@ -101,5 +101,8 @@ mod test {
101101 let transaction = toggle_line_comments(&state);
102102 transaction.apply(&mut state);
103103 assert_eq!(state.doc, " 1\n\n 2\n 3");
104+
105+ // TODO: account for no margin after comment
106+ // TODO: account for uncommenting with uneven comment indentation
104107 }
105108}
helix-term/src/commands.rs+9−1
@@ -1,5 +1,5 @@
11use helix_core::{
2− graphemes,
2+ comment, graphemes,
33 indent::TAB_WIDTH,
44 regex::Regex,
55 register, selection,
@@ -1077,3 +1077,11 @@ pub fn completion(cx: &mut Context) {
10771077pub fn next_view(cx: &mut Context) {
10781078 cx.editor.tree.focus_next()
10791079}
1080+
1081+// comments
1082+pub fn toggle_comments(cx: &mut Context) {
1083+ let doc = cx.doc();
1084+ let transaction = comment::toggle_line_comments(&doc.state);
1085+
1086+ doc.apply(&transaction);
1087+}
helix-term/src/keymap.rs+3
@@ -198,6 +198,9 @@ pub fn default() -> Keymaps {
198198 code: KeyCode::Tab,
199199 modifiers: Modifiers::NONE
200200 }] => commands::next_view,
201+
202+ // move under <space>c
203+ vec![ctrl!('c')] => commands::toggle_comments,
201204 ),
202205 Mode::Insert => hashmap!(
203206 vec![Key {