Skip to content

Commit

Permalink
Continue comments when opening a new line
Browse files Browse the repository at this point in the history
  • Loading branch information
antoyo committed Apr 3, 2022
1 parent ec21de0 commit 3f0dbe1
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
22 changes: 20 additions & 2 deletions helix-core/src/comment.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! This module contains the functionality toggle comments on lines over the selection
//! using the comment character defined in the user's `languages.toml`
//! This module contains the some comment-related features
//! using the comment character defined in the user's `languages.toml`:
//! * toggle comments on lines over the selection.
//! * continue comment when opening a new line.

use crate::{
find_first_non_whitespace_char, Change, Rope, RopeSlice, Selection, Tendril, Transaction,
Expand Down Expand Up @@ -94,6 +96,22 @@ pub fn toggle_line_comments(doc: &Rope, selection: &Selection, token: Option<&st
Transaction::change(doc, changes.into_iter())
}

pub fn continue_comment<'a>(doc: &Rope, line: usize, token: Option<&'a str>) -> Option<&'a str> {
let token = token?;

let line_slice = doc.line(line);
let commented = find_first_non_whitespace_char(line_slice)
.map(|pos| {
let len = line_slice.len_chars();
// line can be shorter than pos + token len
let fragment = Cow::from(line_slice.slice(pos..std::cmp::min(pos + token.len(), len)));
fragment == token
})
.unwrap_or_default();

commented.then(|| token)
}

#[cfg(test)]
mod test {
use super::*;
Expand Down
23 changes: 23 additions & 0 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2293,6 +2293,16 @@ fn open(cx: &mut Context, open: Open) {
let mut text = String::with_capacity(1 + indent_len);
text.push_str(doc.line_ending.as_str());
text.push_str(&indent);

let token = doc
.language_config()
.and_then(|lc| lc.comment_token.as_ref())
.map(|tc| tc.as_ref());
if let Some(token) = comment::continue_comment(doc.text(), cursor_line, token) {
text.push_str(token);
text.push(' ');
}

let text = text.repeat(count);

// calculate new selection ranges
Expand All @@ -2312,6 +2322,9 @@ fn open(cx: &mut Context, open: Open) {
transaction = transaction.with_selection(Selection::new(ranges, selection.primary_index()));

doc.apply(&transaction, view.id);

// Since we might have added a comment token, move to the end of the line.
goto_line_end_newline(cx);
}

// o inserts a new line after each line with a selection
Expand Down Expand Up @@ -2774,6 +2787,16 @@ pub mod insert {
text.reserve_exact(1 + indent.len());
text.push_str(doc.line_ending.as_str());
text.push_str(&indent);

let token = doc
.language_config()
.and_then(|lc| lc.comment_token.as_ref())
.map(|tc| tc.as_ref());
if let Some(token) = comment::continue_comment(doc.text(), current_line, token) {
text.push_str(token);
text.push(' ');
}

pos + offs + text.chars().count()
};

Expand Down

0 comments on commit 3f0dbe1

Please sign in to comment.