on master help sign in
commands: Wire up toggle comments as ctrl-c
7a1ff5eBlaž Hrastnik
TODO.md+16−11
@@ -27,21 +27,26 @@
27
27
28- [ ] regex search / select next
28- [ ] regex search / select next
29- [ ] f / t mappings
29- [ ] 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
30
33
31
34
322
352
36+- [ ] tab completion for paths on the prompt
33−- extend selection (treesitter select parent node) (replaces viw, vi(, va( etc )
37+- [ ] extend selection (treesitter select parent node) (replaces viw, vi(, va( etc )
34−- bracket pairs
38+- [ ] bracket pairs
35−- comment block (gcc)
39+- [x] comment block (gcc)
36−- completion signature popups/docs
40+- [ ] completion signature popups/docs
37−- multiple views into the same file
41+- [ ] multiple views into the same file
38−- selection align
42+- [ ] selection align
43+- [ ] store some state: file positions, prompt history
39
44
403
453
41−- diagnostics popups
46+- [ ] diagnostics popups
42−- diff mode with highlighting?
47+- [ ] diff mode with highlighting?
43−- snippet support (tab to jump between marks)
48+- [ ] snippet support (tab to jump between marks)
44−- gamelisp/wasm scripting
49+- [ ] gamelisp/wasm scripting
45
50
46X
51X
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};
2use core::ops::Range;
2use core::ops::Range;
3use std::borrow::Cow;
3use std::borrow::Cow;
4
4
@@ -7,7 +7,6 @@ fn find_line_comment(
7 text: RopeSlice,
7 text: RopeSlice,
8 lines: Range<usize>,
8 lines: Range<usize>,
9) -> (bool, Vec<usize>, usize) {
9) -> (bool, Vec<usize>, usize) {
10− // selection ranges map char_to_line from() .. to()
11 let mut commented = true;
10 let mut commented = true;
12 let mut skipped = Vec::new();
11 let mut skipped = Vec::new();
13 let mut min = usize::MAX; // minimum col for find_first_non_whitespace_char
12 let mut min = usize::MAX; // minimum col for find_first_non_whitespace_char
@@ -36,11 +35,12 @@ fn find_line_comment(
36 (commented, skipped, min)
35 (commented, skipped, min)
37}
36}
38
37
39−fn toggle_line_comments(state: &State) -> Transaction {
38+pub fn toggle_line_comments(state: &State) -> Transaction {
40 let text = state.doc.slice(..);
39 let text = state.doc.slice(..);
41 let mut changes: Vec<Change> = Vec::new();
40 let mut changes: Vec<Change> = Vec::new();
42
41
43 let token = "//";
42 let token = "//";
43+ let comment = Tendril::from(format!("{} ", token));
44
44
45 for selection in state.selection.ranges() {
45 for selection in state.selection.ranges() {
46 let start = text.char_to_line(selection.from());
46 let start = text.char_to_line(selection.from());
@@ -59,7 +59,7 @@ fn toggle_line_comments(state: &State) -> Transaction {
59
59
60 if !commented {
60 if !commented {
61 // comment line
61 // comment line
62− changes.push((pos, pos, Some(format!("{} ", token).into())))
62+ changes.push((pos, pos, Some(comment.clone())))
63 } else {
63 } else {
64 // uncomment line
64 // uncomment line
65 let margin = 1; // TODO: margin is hardcoded 1 but could easily be 0
65 let margin = 1; // TODO: margin is hardcoded 1 but could easily be 0
@@ -101,5 +101,8 @@ mod test {
101 let transaction = toggle_line_comments(&state);
101 let transaction = toggle_line_comments(&state);
102 transaction.apply(&mut state);
102 transaction.apply(&mut state);
103 assert_eq!(state.doc, " 1\n\n 2\n 3");
103 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
104 }
107 }
105}
108}
helix-term/src/commands.rs+9−1
@@ -1,5 +1,5 @@
1use helix_core::{
1use helix_core::{
2− graphemes,
2+ comment, graphemes,
3 indent::TAB_WIDTH,
3 indent::TAB_WIDTH,
4 regex::Regex,
4 regex::Regex,
5 register, selection,
5 register, selection,
@@ -1077,3 +1077,11 @@ pub fn completion(cx: &mut Context) {
1077pub fn next_view(cx: &mut Context) {
1077pub fn next_view(cx: &mut Context) {
1078 cx.editor.tree.focus_next()
1078 cx.editor.tree.focus_next()
1079}
1079}
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 {
198 code: KeyCode::Tab,
198 code: KeyCode::Tab,
199 modifiers: Modifiers::NONE
199 modifiers: Modifiers::NONE
200 }] => commands::next_view,
200 }] => commands::next_view,
201+
202+ // move under <space>c
203+ vec![ctrl!('c')] => commands::toggle_comments,
201 ),
204 ),
202 Mode::Insert => hashmap!(
205 Mode::Insert => hashmap!(
203 vec![Key {
206 vec![Key {