mirror of https://github.com/helix-editor/helix
parent
6e451fe201
commit
63fb49c1b4
|
@ -92,20 +92,17 @@ pub fn toggle_line_comments(doc: &Rope, range: &Range, token: Option<&str>) -> V
|
||||||
let start = start.clamp(0, text.len_lines());
|
let start = start.clamp(0, text.len_lines());
|
||||||
let end = (end + 1).min(text.len_lines());
|
let end = (end + 1).min(text.len_lines());
|
||||||
|
|
||||||
// let start_byte = text.line_to_byte(start);
|
|
||||||
// let end_byte = text.line_to_byte(start);
|
|
||||||
|
|
||||||
let mut lines = vec![];
|
let mut lines = vec![];
|
||||||
lines.extend(start..end);
|
lines.extend(start..end);
|
||||||
|
|
||||||
let (commented, to_change, min, margin) = find_line_comment(token, text, lines);
|
let (was_commented, to_change, min, margin) = find_line_comment(token, text, lines);
|
||||||
|
|
||||||
let mut changes: Vec<Change> = Vec::with_capacity(to_change.len());
|
let mut changes: Vec<Change> = Vec::with_capacity(to_change.len());
|
||||||
|
|
||||||
for line in to_change {
|
for line in to_change {
|
||||||
let pos = text.line_to_char(line) + min;
|
let pos = text.line_to_char(line) + min;
|
||||||
|
|
||||||
if !commented {
|
if !was_commented {
|
||||||
// comment line
|
// comment line
|
||||||
changes.push((pos, pos, Some(comment.clone())));
|
changes.push((pos, pos, Some(comment.clone())));
|
||||||
} else {
|
} else {
|
||||||
|
@ -145,7 +142,7 @@ pub fn find_block_comments(
|
||||||
text: RopeSlice,
|
text: RopeSlice,
|
||||||
ranges: &Vec<Range>,
|
ranges: &Vec<Range>,
|
||||||
) -> (bool, Vec<CommentChange>) {
|
) -> (bool, Vec<CommentChange>) {
|
||||||
let mut commented = true;
|
let mut was_commented = true;
|
||||||
let mut only_whitespace = true;
|
let mut only_whitespace = true;
|
||||||
let mut comment_changes = Vec::with_capacity(ranges.len());
|
let mut comment_changes = Vec::with_capacity(ranges.len());
|
||||||
let default_tokens = tokens.first().cloned().unwrap_or_default();
|
let default_tokens = tokens.first().cloned().unwrap_or_default();
|
||||||
|
@ -200,7 +197,7 @@ pub fn find_block_comments(
|
||||||
start_token: default_tokens.start.clone(),
|
start_token: default_tokens.start.clone(),
|
||||||
end_token: default_tokens.end.clone(),
|
end_token: default_tokens.end.clone(),
|
||||||
});
|
});
|
||||||
commented = false;
|
was_commented = false;
|
||||||
} else {
|
} else {
|
||||||
comment_changes.push(CommentChange::Commented {
|
comment_changes.push(CommentChange::Commented {
|
||||||
range: *range,
|
range: *range,
|
||||||
|
@ -219,14 +216,13 @@ pub fn find_block_comments(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if only_whitespace {
|
if only_whitespace {
|
||||||
commented = false;
|
was_commented = false;
|
||||||
}
|
}
|
||||||
(commented, comment_changes)
|
(was_commented, comment_changes)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub fn create_block_comment_transaction(
|
pub fn create_block_comment_transaction(
|
||||||
_doc: &Rope,
|
|
||||||
ranges: &[Range],
|
ranges: &[Range],
|
||||||
was_commented: bool,
|
was_commented: bool,
|
||||||
comment_changes: Vec<CommentChange>,
|
comment_changes: Vec<CommentChange>,
|
||||||
|
@ -304,7 +300,7 @@ pub fn toggle_block_comments(
|
||||||
let text = doc.slice(..);
|
let text = doc.slice(..);
|
||||||
let (was_commented, comment_changes) = find_block_comments(tokens, text, ranges);
|
let (was_commented, comment_changes) = find_block_comments(tokens, text, ranges);
|
||||||
let (changes, new_ranges) =
|
let (changes, new_ranges) =
|
||||||
create_block_comment_transaction(doc, ranges, was_commented, comment_changes);
|
create_block_comment_transaction(ranges, was_commented, comment_changes);
|
||||||
|
|
||||||
if was_commented {
|
if was_commented {
|
||||||
for (range, changes) in new_ranges.iter().zip(changes.chunks_exact(2)) {
|
for (range, changes) in new_ranges.iter().zip(changes.chunks_exact(2)) {
|
||||||
|
|
|
@ -1429,6 +1429,7 @@ impl Syntax {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Get an injection layer's language configuration
|
||||||
pub fn layer_config(&self, layer_id: LayerId) -> Arc<LanguageConfiguration> {
|
pub fn layer_config(&self, layer_id: LayerId) -> Arc<LanguageConfiguration> {
|
||||||
let loader = &self.loader.load();
|
let loader = &self.loader.load();
|
||||||
let language_id = self.layers[layer_id].config.language_id;
|
let language_id = self.layers[layer_id].config.language_id;
|
||||||
|
|
|
@ -5090,8 +5090,6 @@ pub fn completion(cx: &mut Context) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// comments
|
// comments
|
||||||
// type CommentTransactionFn =
|
|
||||||
// Box<dyn FnMut(&Rope, &Selection, comment::GetCommentTokens) -> Transaction>;
|
|
||||||
|
|
||||||
pub type CommentTransactionFn<'a> = Box<
|
pub type CommentTransactionFn<'a> = Box<
|
||||||
dyn FnMut(
|
dyn FnMut(
|
||||||
|
@ -5136,26 +5134,7 @@ fn toggle_comments_impl<'a>(
|
||||||
let mut best_fit = None;
|
let mut best_fit = None;
|
||||||
let mut min_gap = usize::MAX;
|
let mut min_gap = usize::MAX;
|
||||||
|
|
||||||
// Find the injection with the most tightly encompassing
|
// Find the injection with the most tightly encompassing range.
|
||||||
// range.
|
|
||||||
//
|
|
||||||
// TODO: improve performance of this
|
|
||||||
//
|
|
||||||
// I'm thinking layer.ranges can be a
|
|
||||||
// BTreeSet<Range> instead of Vec<Range>
|
|
||||||
//
|
|
||||||
// The "Cmp" implementation can basically be
|
|
||||||
// range.start_byte.cmp(other.start_byte)
|
|
||||||
// .then(range.end_byte.cmp(other.end_byte))
|
|
||||||
//
|
|
||||||
// Since we can't implement Cmp for Range (external trate and struct)
|
|
||||||
// we need to use newtype pattern: struct TSRange(Range)
|
|
||||||
// But I tried that and I'm not sure it'll work
|
|
||||||
//
|
|
||||||
// I *could* just use the same Vec, and when we're inserting into it
|
|
||||||
// we'll do it in the correct order. Then do Vec::binary_search_by
|
|
||||||
//
|
|
||||||
// However, I don't know if that'll be a good idea.
|
|
||||||
if let Some(syntax) = &syntax {
|
if let Some(syntax) = &syntax {
|
||||||
for (layer_id, layer) in &syntax.layers {
|
for (layer_id, layer) in &syntax.layers {
|
||||||
for ts_range in &layer.ranges {
|
for ts_range in &layer.ranges {
|
||||||
|
@ -5239,7 +5218,6 @@ fn toggle_comments(cx: &mut Context) {
|
||||||
// block commented by line would also be block commented so check this first
|
// block commented by line would also be block commented so check this first
|
||||||
if line_commented {
|
if line_commented {
|
||||||
return comment::create_block_comment_transaction(
|
return comment::create_block_comment_transaction(
|
||||||
rope,
|
|
||||||
&split_lines,
|
&split_lines,
|
||||||
line_commented,
|
line_commented,
|
||||||
line_comment_changes,
|
line_comment_changes,
|
||||||
|
@ -5253,7 +5231,6 @@ fn toggle_comments(cx: &mut Context) {
|
||||||
// check if selection has block comments
|
// check if selection has block comments
|
||||||
if block_commented {
|
if block_commented {
|
||||||
return comment::create_block_comment_transaction(
|
return comment::create_block_comment_transaction(
|
||||||
rope,
|
|
||||||
&[*range],
|
&[*range],
|
||||||
block_commented,
|
block_commented,
|
||||||
comment_changes,
|
comment_changes,
|
||||||
|
@ -5264,7 +5241,6 @@ fn toggle_comments(cx: &mut Context) {
|
||||||
// not commented and only have block comment tokens
|
// not commented and only have block comment tokens
|
||||||
if line_token.is_none() && block_tokens.is_some() {
|
if line_token.is_none() && block_tokens.is_some() {
|
||||||
return comment::create_block_comment_transaction(
|
return comment::create_block_comment_transaction(
|
||||||
rope,
|
|
||||||
&split_lines,
|
&split_lines,
|
||||||
line_commented,
|
line_commented,
|
||||||
line_comment_changes,
|
line_comment_changes,
|
||||||
|
|
Loading…
Reference in New Issue