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 - [ ] regex search / select next
- [ ] f / t mappings - [ ] 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 2
- extend selection (treesitter select parent node) (replaces viw, vi(, va( etc ) - [ ] tab completion for paths on the prompt
- bracket pairs - [ ] extend selection (treesitter select parent node) (replaces viw, vi(, va( etc )
- comment block (gcc) - [ ] bracket pairs
- completion signature popups/docs - [x] comment block (gcc)
- multiple views into the same file - [ ] completion signature popups/docs
- selection align - [ ] multiple views into the same file
- [ ] selection align
- [ ] store some state: file positions, prompt history
3 3
- diagnostics popups - [ ] diagnostics popups
- diff mode with highlighting? - [ ] diff mode with highlighting?
- snippet support (tab to jump between marks) - [ ] snippet support (tab to jump between marks)
- gamelisp/wasm scripting - [ ] gamelisp/wasm scripting
X 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 core::ops::Range;
use std::borrow::Cow; use std::borrow::Cow;
@ -7,7 +7,6 @@ fn find_line_comment(
text: RopeSlice, text: RopeSlice,
lines: Range<usize>, lines: Range<usize>,
) -> (bool, Vec<usize>, usize) { ) -> (bool, Vec<usize>, usize) {
// selection ranges map char_to_line from() .. to()
let mut commented = true; let mut commented = true;
let mut skipped = Vec::new(); let mut skipped = Vec::new();
let mut min = usize::MAX; // minimum col for find_first_non_whitespace_char let mut min = usize::MAX; // minimum col for find_first_non_whitespace_char
@ -36,11 +35,12 @@ fn find_line_comment(
(commented, skipped, min) (commented, skipped, min)
} }
fn toggle_line_comments(state: &State) -> Transaction { pub fn toggle_line_comments(state: &State) -> Transaction {
let text = state.doc.slice(..); let text = state.doc.slice(..);
let mut changes: Vec<Change> = Vec::new(); let mut changes: Vec<Change> = Vec::new();
let token = "//"; let token = "//";
let comment = Tendril::from(format!("{} ", token));
for selection in state.selection.ranges() { for selection in state.selection.ranges() {
let start = text.char_to_line(selection.from()); let start = text.char_to_line(selection.from());
@ -59,7 +59,7 @@ fn toggle_line_comments(state: &State) -> Transaction {
if !commented { if !commented {
// comment line // comment line
changes.push((pos, pos, Some(format!("{} ", token).into()))) changes.push((pos, pos, Some(comment.clone())))
} else { } else {
// uncomment line // uncomment line
let margin = 1; // TODO: margin is hardcoded 1 but could easily be 0 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); let transaction = toggle_line_comments(&state);
transaction.apply(&mut state); transaction.apply(&mut state);
assert_eq!(state.doc, " 1\n\n 2\n 3"); 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::{ use helix_core::{
graphemes, comment, graphemes,
indent::TAB_WIDTH, indent::TAB_WIDTH,
regex::Regex, regex::Regex,
register, selection, register, selection,
@ -1077,3 +1077,11 @@ pub fn completion(cx: &mut Context) {
pub fn next_view(cx: &mut Context) { pub fn next_view(cx: &mut Context) {
cx.editor.tree.focus_next() 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, code: KeyCode::Tab,
modifiers: Modifiers::NONE modifiers: Modifiers::NONE
}] => commands::next_view, }] => commands::next_view,
// move under <space>c
vec![ctrl!('c')] => commands::toggle_comments,
), ),
Mode::Insert => hashmap!( Mode::Insert => hashmap!(
vec![Key { vec![Key {