Skip to content

Commit

Permalink
add has_line_comment method and fix starts_with for RopeSlice
Browse files Browse the repository at this point in the history
  • Loading branch information
TornaxO7 committed Jun 20, 2024
1 parent e51c288 commit 8fa3cc9
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 29 deletions.
6 changes: 0 additions & 6 deletions helix-core/src/chars.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
//! Utility functions to categorize a `char`.

use ropey::RopeSlice;

use crate::LineEnding;

#[derive(Debug, Eq, PartialEq)]
Expand Down Expand Up @@ -87,10 +85,6 @@ 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: 3 additions & 5 deletions helix-core/src/comment.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
//! 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
//! This module contains the functionality toggle comments on lines over the selection
//! using the comment character defined in the user's `languages.toml`

use smallvec::SmallVec;

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

#[test]
fn test_toggle_line_comments() {
fn test_find_line_comment() {
// 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
2 changes: 1 addition & 1 deletion helix-stdx/src/rope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ impl<'a> RopeSliceExt<'a> for RopeSlice<'a> {
if len < text.len() {
return false;
}
self.get_byte_slice(..len - text.len())
self.get_byte_slice(..text.len())
.map_or(false, |start| start == text)
}

Expand Down
52 changes: 35 additions & 17 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ use helix_core::{
};
use helix_view::{
document::{FormatterError, Mode, SCRATCH_BUFFER_NAME},
editor::Action,
editor::{Action, Config},
info::Info,
input::KeyEvent,
keyboard::KeyCode,
Expand Down Expand Up @@ -1668,6 +1668,34 @@ fn switch_to_lowercase(cx: &mut Context) {
});
}

/// Checks, if the line from line `line_num` is commented with one of the comment tokens in the language config and
/// returns the token which comments out the line.
fn has_line_comment(config: &Config, doc: &Document, line_num: usize) -> Option<String> {
if config.continue_comments {
let line_tokens = {
let mut line_tokens: Vec<String> = doc
.language_config()
.and_then(|lc| lc.comment_tokens.as_ref())?
.clone();
line_tokens.sort_by(|a, b| a.len().partial_cmp(&b.len()).unwrap().reverse());
line_tokens
};

let text = doc.text();
let line = text.line(line_num);

if let Some(pos) = line.first_non_whitespace_char() {
let text_line = line.slice(pos..);
return line_tokens
.iter()
.find(|&token| text_line.starts_with(token.as_str()))
.map(|token| token.clone());
}
}

None
}

pub fn scroll(cx: &mut Context, offset: usize, direction: Direction, sync_cursor: bool) {
use Direction::*;
let config = cx.editor.config();
Expand Down Expand Up @@ -3417,8 +3445,9 @@ fn open(cx: &mut Context, open: Open) {
text.push_str(doc.line_ending.as_str());
text.push_str(&indent);

if config.continue_comments {
handle_comment_continue(doc, &mut text, cursor_line);
if let Some(comment_token) = has_line_comment(&config, &doc, line_num) {
text.push_str(&comment_token);
text.push(' ');
}

let text = text.repeat(count);
Expand All @@ -3445,18 +3474,6 @@ fn open(cx: &mut Context, open: Open) {
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
fn open_below(cx: &mut Context) {
open(cx, Open::Below)
Expand Down Expand Up @@ -3918,8 +3935,9 @@ pub mod insert {
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);
if let Some(comment_token) = has_line_comment(&config, &doc, current_line) {
new_text.push_str(&comment_token);
new_text.push(' ');
}

new_text.chars().count()
Expand Down

0 comments on commit 8fa3cc9

Please sign in to comment.