From 7a1ff5e45f829df17dfdcbd4923ae9e83fc2bf3f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bla=C5=BE=20Hrastnik?= Date: Fri, 19 Feb 2021 13:59:24 +0900 Subject: [PATCH] commands: Wire up toggle comments as ctrl-c --- TODO.md | 27 ++++++++++++++++----------- helix-core/src/comment.rs | 11 +++++++---- helix-term/src/commands.rs | 10 +++++++++- helix-term/src/keymap.rs | 3 +++ 4 files changed, 35 insertions(+), 16 deletions(-) diff --git a/TODO.md b/TODO.md index b719aadcb..8318b0bfb 100644 --- a/TODO.md +++ b/TODO.md @@ -27,21 +27,26 @@ - [ ] regex search / select next - [ ] f / t mappings +- [ ] = for auto indent line/selection +- [ ] q should only close the view, if all are closed, close the editor +- [ ] buffers should sit on editor.buffers, view simply refs them 2 -- extend selection (treesitter select parent node) (replaces viw, vi(, va( etc ) -- bracket pairs -- comment block (gcc) -- completion signature popups/docs -- multiple views into the same file -- selection align +- [ ] tab completion for paths on the prompt +- [ ] extend selection (treesitter select parent node) (replaces viw, vi(, va( etc ) +- [ ] bracket pairs +- [x] comment block (gcc) +- [ ] completion signature popups/docs +- [ ] multiple views into the same file +- [ ] selection align +- [ ] store some state: file positions, prompt history 3 -- diagnostics popups -- diff mode with highlighting? -- snippet support (tab to jump between marks) -- gamelisp/wasm scripting +- [ ] diagnostics popups +- [ ] diff mode with highlighting? +- [ ] snippet support (tab to jump between marks) +- [ ] gamelisp/wasm scripting X -- rendering via skulpin/skia or raw wgpu +- [ ] rendering via skulpin/skia or raw wgpu diff --git a/helix-core/src/comment.rs b/helix-core/src/comment.rs index 87becfa31..9109e2d61 100644 --- a/helix-core/src/comment.rs +++ b/helix-core/src/comment.rs @@ -1,4 +1,4 @@ -use crate::{find_first_non_whitespace_char2, Change, RopeSlice, State, Transaction}; +use crate::{find_first_non_whitespace_char2, Change, RopeSlice, State, Tendril, Transaction}; use core::ops::Range; use std::borrow::Cow; @@ -7,7 +7,6 @@ fn find_line_comment( text: RopeSlice, lines: Range, ) -> (bool, Vec, usize) { - // selection ranges map char_to_line from() .. to() let mut commented = true; let mut skipped = Vec::new(); let mut min = usize::MAX; // minimum col for find_first_non_whitespace_char @@ -36,11 +35,12 @@ fn find_line_comment( (commented, skipped, min) } -fn toggle_line_comments(state: &State) -> Transaction { +pub fn toggle_line_comments(state: &State) -> Transaction { let text = state.doc.slice(..); let mut changes: Vec = Vec::new(); let token = "//"; + let comment = Tendril::from(format!("{} ", token)); for selection in state.selection.ranges() { let start = text.char_to_line(selection.from()); @@ -59,7 +59,7 @@ fn toggle_line_comments(state: &State) -> Transaction { if !commented { // comment line - changes.push((pos, pos, Some(format!("{} ", token).into()))) + changes.push((pos, pos, Some(comment.clone()))) } else { // uncomment line let margin = 1; // TODO: margin is hardcoded 1 but could easily be 0 @@ -101,5 +101,8 @@ mod test { let transaction = toggle_line_comments(&state); transaction.apply(&mut state); assert_eq!(state.doc, " 1\n\n 2\n 3"); + + // TODO: account for no margin after comment + // TODO: account for uncommenting with uneven comment indentation } } diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 621e1223f..4fe809717 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -1,5 +1,5 @@ use helix_core::{ - graphemes, + comment, graphemes, indent::TAB_WIDTH, regex::Regex, register, selection, @@ -1077,3 +1077,11 @@ pub fn completion(cx: &mut Context) { pub fn next_view(cx: &mut Context) { cx.editor.tree.focus_next() } + +// comments +pub fn toggle_comments(cx: &mut Context) { + let doc = cx.doc(); + let transaction = comment::toggle_line_comments(&doc.state); + + doc.apply(&transaction); +} diff --git a/helix-term/src/keymap.rs b/helix-term/src/keymap.rs index 932a64313..124456c96 100644 --- a/helix-term/src/keymap.rs +++ b/helix-term/src/keymap.rs @@ -198,6 +198,9 @@ pub fn default() -> Keymaps { code: KeyCode::Tab, modifiers: Modifiers::NONE }] => commands::next_view, + + // move under c + vec![ctrl!('c')] => commands::toggle_comments, ), Mode::Insert => hashmap!( vec![Key {