Skip to content

Commit

Permalink
Merge branch 'feature/continue-comment' of github.com:seanchen1991/he…
Browse files Browse the repository at this point in the history
…lix into continue-single-comment
  • Loading branch information
TornaxO7 committed Jun 19, 2024
2 parents 9b7dffb + 57261e5 commit e51c288
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 3 deletions.
6 changes: 6 additions & 0 deletions helix-core/src/chars.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! Utility functions to categorize a `char`.
use ropey::RopeSlice;

use crate::LineEnding;

#[derive(Debug, Eq, PartialEq)]
Expand Down Expand Up @@ -85,6 +87,10 @@ pub fn char_is_word(ch: char) -> bool {
ch.is_alphanumeric() || ch == '_'
}

pub fn find_first_non_whitespace_char(line: &RopeSlice) -> Option<usize> {
line.chars().position(|ch| !ch.is_whitespace())
}

#[cfg(test)]
mod test {
use super::*;
Expand Down
8 changes: 5 additions & 3 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 functionality for the following 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 smallvec::SmallVec;

Expand Down Expand Up @@ -318,7 +320,7 @@ mod test {
use super::*;

#[test]
fn test_find_line_comment() {
fn test_toggle_line_comments() {
// four lines, two space indented, except for line 1 which is blank.
let mut doc = Rope::from(" 1\n\n 2\n 3");
// select whole document
Expand Down
27 changes: 27 additions & 0 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3366,6 +3366,7 @@ fn open(cx: &mut Context, open: Open) {
enter_insert_mode(cx);
let (view, doc) = current!(cx.editor);

let config = doc.config.load();
let text = doc.text().slice(..);
let contents = doc.text();
let selection = doc.selection(view.id);
Expand Down Expand Up @@ -3415,6 +3416,11 @@ 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);

if config.continue_comments {
handle_comment_continue(doc, &mut text, cursor_line);
}

let text = text.repeat(count);

// calculate new selection ranges
Expand All @@ -3434,6 +3440,21 @@ 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);
}

// Currently only continues single-line comments
// TODO: Handle block comments as well
fn handle_comment_continue(doc: &Document, text: &mut String, cursor_line: usize) {
let line = doc.text().line(cursor_line);

if let Some(lang_config) = doc.language_config() {
let comment_tokens = &lang_config.comment_tokens;

comment::handle_comment_continue(&line, text, comment_tokens);
}
}

// o inserts a new line after each line with a selection
Expand Down Expand Up @@ -3826,6 +3847,7 @@ pub mod insert {

pub fn insert_newline(cx: &mut Context) {
let (view, doc) = current_ref!(cx.editor);
let config = doc.config.load();
let text = doc.text().slice(..);

let contents = doc.text();
Expand Down Expand Up @@ -3895,6 +3917,11 @@ pub mod insert {
new_text.reserve_exact(1 + indent.len());
new_text.push_str(doc.line_ending.as_str());
new_text.push_str(&indent);

if config.continue_comments {
handle_comment_continue(doc, &mut new_text, current_line);
}

new_text.chars().count()
};

Expand Down
3 changes: 3 additions & 0 deletions helix-view/src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,8 @@ pub struct Config {
deserialize_with = "deserialize_alphabet"
)]
pub jump_label_alphabet: Vec<char>,
/// Whether to prepend a comment token onto a new line that follows a commented line. Defaults to `true`.
pub continue_comments: bool,
}

#[derive(Debug, Clone, PartialEq, Deserialize, Serialize, Eq, PartialOrd, Ord)]
Expand Down Expand Up @@ -975,6 +977,7 @@ impl Default for Config {
popup_border: PopupBorderConfig::None,
indent_heuristic: IndentationHeuristic::default(),
jump_label_alphabet: ('a'..='z').collect(),
continue_comments: true,
}
}
}
Expand Down

0 comments on commit e51c288

Please sign in to comment.