Skip to content

Commit 8fa3cc9

Browse files
committed
add has_line_comment method and fix starts_with for RopeSlice
1 parent e51c288 commit 8fa3cc9

File tree

4 files changed

+39
-29
lines changed

4 files changed

+39
-29
lines changed

helix-core/src/chars.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
//! Utility functions to categorize a `char`.
22
3-
use ropey::RopeSlice;
4-
53
use crate::LineEnding;
64

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

90-
pub fn find_first_non_whitespace_char(line: &RopeSlice) -> Option<usize> {
91-
line.chars().position(|ch| !ch.is_whitespace())
92-
}
93-
9488
#[cfg(test)]
9589
mod test {
9690
use super::*;

helix-core/src/comment.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
//! This module contains the functionality for the following comment-related features
2-
//! using the comment character defined in the user's `languages.toml`:
3-
//! * toggle comments on lines over the selection
4-
//! * continue comment when opening a new line
1+
//! This module contains the functionality toggle comments on lines over the selection
2+
//! using the comment character defined in the user's `languages.toml`
53
64
use smallvec::SmallVec;
75

@@ -320,7 +318,7 @@ mod test {
320318
use super::*;
321319

322320
#[test]
323-
fn test_toggle_line_comments() {
321+
fn test_find_line_comment() {
324322
// four lines, two space indented, except for line 1 which is blank.
325323
let mut doc = Rope::from(" 1\n\n 2\n 3");
326324
// select whole document

helix-stdx/src/rope.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ impl<'a> RopeSliceExt<'a> for RopeSlice<'a> {
5151
if len < text.len() {
5252
return false;
5353
}
54-
self.get_byte_slice(..len - text.len())
54+
self.get_byte_slice(..text.len())
5555
.map_or(false, |start| start == text)
5656
}
5757

helix-term/src/commands.rs

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ use helix_core::{
4242
};
4343
use helix_view::{
4444
document::{FormatterError, Mode, SCRATCH_BUFFER_NAME},
45-
editor::Action,
45+
editor::{Action, Config},
4646
info::Info,
4747
input::KeyEvent,
4848
keyboard::KeyCode,
@@ -1668,6 +1668,34 @@ fn switch_to_lowercase(cx: &mut Context) {
16681668
});
16691669
}
16701670

1671+
/// Checks, if the line from line `line_num` is commented with one of the comment tokens in the language config and
1672+
/// returns the token which comments out the line.
1673+
fn has_line_comment(config: &Config, doc: &Document, line_num: usize) -> Option<String> {
1674+
if config.continue_comments {
1675+
let line_tokens = {
1676+
let mut line_tokens: Vec<String> = doc
1677+
.language_config()
1678+
.and_then(|lc| lc.comment_tokens.as_ref())?
1679+
.clone();
1680+
line_tokens.sort_by(|a, b| a.len().partial_cmp(&b.len()).unwrap().reverse());
1681+
line_tokens
1682+
};
1683+
1684+
let text = doc.text();
1685+
let line = text.line(line_num);
1686+
1687+
if let Some(pos) = line.first_non_whitespace_char() {
1688+
let text_line = line.slice(pos..);
1689+
return line_tokens
1690+
.iter()
1691+
.find(|&token| text_line.starts_with(token.as_str()))
1692+
.map(|token| token.clone());
1693+
}
1694+
}
1695+
1696+
None
1697+
}
1698+
16711699
pub fn scroll(cx: &mut Context, offset: usize, direction: Direction, sync_cursor: bool) {
16721700
use Direction::*;
16731701
let config = cx.editor.config();
@@ -3417,8 +3445,9 @@ fn open(cx: &mut Context, open: Open) {
34173445
text.push_str(doc.line_ending.as_str());
34183446
text.push_str(&indent);
34193447

3420-
if config.continue_comments {
3421-
handle_comment_continue(doc, &mut text, cursor_line);
3448+
if let Some(comment_token) = has_line_comment(&config, &doc, line_num) {
3449+
text.push_str(&comment_token);
3450+
text.push(' ');
34223451
}
34233452

34243453
let text = text.repeat(count);
@@ -3445,18 +3474,6 @@ fn open(cx: &mut Context, open: Open) {
34453474
goto_line_end_newline(cx);
34463475
}
34473476

3448-
// Currently only continues single-line comments
3449-
// TODO: Handle block comments as well
3450-
fn handle_comment_continue(doc: &Document, text: &mut String, cursor_line: usize) {
3451-
let line = doc.text().line(cursor_line);
3452-
3453-
if let Some(lang_config) = doc.language_config() {
3454-
let comment_tokens = &lang_config.comment_tokens;
3455-
3456-
comment::handle_comment_continue(&line, text, comment_tokens);
3457-
}
3458-
}
3459-
34603477
// o inserts a new line after each line with a selection
34613478
fn open_below(cx: &mut Context) {
34623479
open(cx, Open::Below)
@@ -3918,8 +3935,9 @@ pub mod insert {
39183935
new_text.push_str(doc.line_ending.as_str());
39193936
new_text.push_str(&indent);
39203937

3921-
if config.continue_comments {
3922-
handle_comment_continue(doc, &mut new_text, current_line);
3938+
if let Some(comment_token) = has_line_comment(&config, &doc, current_line) {
3939+
new_text.push_str(&comment_token);
3940+
new_text.push(' ');
39233941
}
39243942

39253943
new_text.chars().count()

0 commit comments

Comments
 (0)