Skip to content

Commit c160b1e

Browse files
committed
refactor: collapse 2 map intoa a single map
1 parent f2156cb commit c160b1e

File tree

1 file changed

+16
-29
lines changed

1 file changed

+16
-29
lines changed

helix-core/src/comment.rs

Lines changed: 16 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ pub fn get_comment_token(
2222
let start_char = text.line_to_char(line_num) + start;
2323

2424
let injected_tokens = get_injected_tokens(syntax, start_char, start_char)
25-
// we don't care about block comment tokens
25+
// we only care about line comment tokens
2626
.0
2727
.and_then(|tokens| {
2828
tokens
@@ -32,7 +32,7 @@ pub fn get_comment_token(
3232
});
3333

3434
injected_tokens.or(
35-
// no comment tokens found for injection. Use doc tokens instead
35+
// no comment tokens found for injection, use doc comments if exists
3636
doc_default_tokens.and_then(|tokens| {
3737
tokens
3838
.iter()
@@ -51,14 +51,13 @@ pub fn get_injected_tokens(
5151
// Find the injection with the most tightly encompassing range.
5252
syntax
5353
.and_then(|syntax| {
54-
injection_for_range(syntax, start, end)
55-
.map(|language_id| syntax.layer_config(language_id))
56-
.map(|config| {
57-
(
58-
config.comment_tokens.clone(),
59-
config.block_comment_tokens.clone(),
60-
)
61-
})
54+
injection_for_range(syntax, start, end).map(|language_id| {
55+
let config = syntax.layer_config(language_id);
56+
(
57+
config.comment_tokens.clone(),
58+
config.block_comment_tokens.clone(),
59+
)
60+
})
6261
})
6362
.unwrap_or_default()
6463
}
@@ -73,20 +72,18 @@ pub fn injection_for_range(syntax: &Syntax, from: usize, to: usize) -> Option<La
7372
for ts_range in &layer.ranges {
7473
let is_encompassing = ts_range.start_byte <= from && ts_range.end_byte >= to;
7574
if is_encompassing {
76-
let this_gap = ts_range.end_byte - ts_range.start_byte;
75+
let gap = ts_range.end_byte - ts_range.start_byte;
7776
let config = syntax.layer_config(layer_id);
77+
// ignore the language family for which it won't make
78+
// sense to consider their comment.
79+
//
80+
// This includes, for instance, `comment`, `jsdoc`, `regex`
7881
let has_comment_tokens =
7982
config.comment_tokens.is_some() || config.block_comment_tokens.is_some();
8083

81-
if this_gap < min_gap
82-
// ignore the language family for which it won't make
83-
// sense to consider their comment.
84-
//
85-
// This includes, for instance, `comment`, `jsdoc`, `regex`
86-
&& has_comment_tokens
87-
{
84+
if gap < min_gap && has_comment_tokens {
8885
best_fit = Some(layer_id);
89-
min_gap = this_gap;
86+
min_gap = gap;
9087
}
9188
}
9289
}
@@ -145,16 +142,6 @@ fn find_line_comment(
145142
(commented, to_change, min, margin)
146143
}
147144

148-
// for a given range and syntax, determine if there are additional tokens to consider
149-
pub type GetInjectedTokens<'a> = Box<
150-
dyn FnMut(
151-
Option<&Syntax>,
152-
usize,
153-
usize,
154-
) -> (Option<Vec<String>>, Option<Vec<BlockCommentToken>>)
155-
+ 'a,
156-
>;
157-
158145
#[must_use]
159146
pub fn toggle_line_comments(doc: &Rope, range: &Range, token: Option<&str>) -> Vec<Change> {
160147
let text = doc.slice(..);

0 commit comments

Comments
 (0)