Skip to content

Commit

Permalink
continue-comments: Fix panic case and add test
Browse files Browse the repository at this point in the history
  • Loading branch information
TornaxO7 committed Oct 7, 2024
1 parent aca48e8 commit 5a2a96e
Showing 1 changed file with 24 additions and 6 deletions.
30 changes: 24 additions & 6 deletions helix-core/src/comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,25 @@ use std::borrow::Cow;
pub const DEFAULT_COMMENT_TOKEN: &str = "//";

/// Returns the longest matching comment token of the given line (if it exists).
pub fn get_comment_token<'a>(
pub fn get_comment_token<'a, S: AsRef<str>>(
text: RopeSlice,
tokens: &'a [String],
tokens: &'a [S],
line_num: usize,
) -> Option<&'a str> {
let mut used_token: Option<&'a str> = None;

let line = text.line(line_num);
let start = line.char_to_byte(line.first_non_whitespace_char()?);
let start = line.first_non_whitespace_char()?;

let mut max_token_len = 0;
for token in tokens {
let end = std::cmp::min(start + token.len(), line.len_bytes());
if line.byte_slice(start..end) == *token {
used_token = Some(token.as_str());
let token_str = token.as_ref();
let end = std::cmp::min(start + token_str.len(), line.len_bytes());

let matches_token = line.slice(start..end).starts_with(token_str);
if matches_token && token_str.len() >= max_token_len {
used_token = Some(token_str);
max_token_len = token_str.len();
}
}

Expand Down Expand Up @@ -481,4 +486,17 @@ mod test {
transaction.apply(&mut doc);
assert_eq!(doc, "");
}

// Test, if `get_comment_tokens` works, even if the content of the file includes chars, whose
// byte size unequal the amount of chars
#[test]
fn test_get_comment_with_char_boundaries() {
let rope = Rope::from("··");
let tokens = ["//", "///"];

assert_eq!(
super::get_comment_token(rope.slice(..), tokens.as_slice(), 0),
None
);
}
}

0 comments on commit 5a2a96e

Please sign in to comment.