commands: Wire up toggle comments as ctrl-c

This commit is contained in:
Blaž Hrastnik 2021-02-19 13:59:24 +09:00
parent 4ab5631d65
commit 7a1ff5e45f
4 changed files with 35 additions and 16 deletions

27
TODO.md
View file

@ -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

View file

@ -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<usize>,
) -> (bool, Vec<usize>, 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<Change> = 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
}
}

View file

@ -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);
}

View file

@ -198,6 +198,9 @@ pub fn default() -> Keymaps {
code: KeyCode::Tab,
modifiers: Modifiers::NONE
}] => commands::next_view,
// move under <space>c
vec![ctrl!('c')] => commands::toggle_comments,
),
Mode::Insert => hashmap!(
vec![Key {