chore: clean up code

pull/12759/head
Nikita Revenco 2025-02-01 16:56:12 +00:00 committed by Nik Revenco
parent 8fe3f90cbb
commit b10fc21169
3 changed files with 89 additions and 98 deletions

View File

@ -37,18 +37,24 @@ pub fn get_comment_token<'a, S: AsRef<str>>(
/// - Column of existing tokens, if the lines are commented; column to place tokens at otherwise. /// - Column of existing tokens, if the lines are commented; column to place tokens at otherwise.
/// - The margin to the right of the comment tokens /// - The margin to the right of the comment tokens
/// - Defaults to `1`. If any existing comment token is not followed by a space, changes to `0`. /// - Defaults to `1`. If any existing comment token is not followed by a space, changes to `0`.
fn find_line_comment( fn find_line_comment<'a>(
token: &str,
text: RopeSlice, text: RopeSlice,
lines: impl IntoIterator<Item = usize>, lines: impl IntoIterator<
Item = (
// line number
usize,
// token for this line
&'a str,
),
>,
) -> (bool, Vec<usize>, usize, usize) { ) -> (bool, Vec<usize>, usize, usize) {
let mut commented = true; let mut commented = true;
let mut to_change = Vec::new(); let mut to_change = Vec::new();
let mut min = usize::MAX; // minimum col for first_non_whitespace_char let mut min = usize::MAX; // minimum col for first_non_whitespace_char
let mut margin = 1; let mut margin = 1;
let token_len = token.chars().count();
for line in lines { for (line, token) in lines {
let token_len = token.chars().count();
let line_slice = text.line(line); let line_slice = text.line(line);
if let Some(pos) = line_slice.first_non_whitespace_char() { if let Some(pos) = line_slice.first_non_whitespace_char() {
let len = line_slice.len_chars(); let len = line_slice.len_chars();
@ -79,11 +85,8 @@ fn find_line_comment(
} }
// for a given range and syntax, determine if there are additional tokens to consider // for a given range and syntax, determine if there are additional tokens to consider
pub type InjectedTokens = fn( pub type InjectedTokens =
range: Range, Box<dyn FnMut(usize, usize) -> (Option<Vec<String>>, Option<Vec<BlockCommentToken>>)>;
syntax: Option<&crate::Syntax>,
rope: RopeSlice,
) -> (Option<Vec<String>>, Option<Vec<BlockCommentToken>>);
#[must_use] #[must_use]
pub fn toggle_line_comments( pub fn toggle_line_comments(
@ -97,7 +100,7 @@ pub fn toggle_line_comments(
let token = token.unwrap_or(DEFAULT_COMMENT_TOKEN); let token = token.unwrap_or(DEFAULT_COMMENT_TOKEN);
let comment = Tendril::from(format!("{} ", token)); let comment = Tendril::from(format!("{} ", token));
let mut lines: Vec<usize> = Vec::with_capacity(selection.len()); let mut lines: Vec<(usize, &str)> = Vec::with_capacity(selection.len());
let mut min_next_line = 0; let mut min_next_line = 0;
for selection in selection { for selection in selection {
@ -105,6 +108,9 @@ pub fn toggle_line_comments(
let start = start.clamp(min_next_line, text.len_lines()); let start = start.clamp(min_next_line, 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);
lines.extend(start..end); lines.extend(start..end);
min_next_line = end; min_next_line = end;
} }

View File

@ -504,7 +504,6 @@ impl MappableCommand {
hover, "Show docs for item under cursor", hover, "Show docs for item under cursor",
toggle_comments, "Comment/uncomment selections", toggle_comments, "Comment/uncomment selections",
toggle_line_comments, "Line comment/uncomment selections", toggle_line_comments, "Line comment/uncomment selections",
testing1234, "a",
toggle_block_comments, "Block comment/uncomment selections", toggle_block_comments, "Block comment/uncomment selections",
rotate_selections_forward, "Rotate selections forward", rotate_selections_forward, "Rotate selections forward",
rotate_selections_backward, "Rotate selections backward", rotate_selections_backward, "Rotate selections backward",
@ -5089,17 +5088,14 @@ pub fn completion(cx: &mut Context) {
.trigger_completions(cursor, doc.id(), view.id); .trigger_completions(cursor, doc.id(), view.id);
} }
// for a given range and syntax, determine if there are additional tokens to consider
pub type InjectedTokens =
Box<dyn FnMut(Range) -> (Option<Vec<String>>, Option<Vec<BlockCommentToken>>)>;
// comments // comments
type CommentTransactionFn = Box< type CommentTransactionFn = Box<
dyn FnMut( dyn FnMut(
Option<&str>, Option<&str>,
Option<&[BlockCommentToken]>, Option<&[BlockCommentToken]>,
&Rope,
&Selection, &Selection,
InjectedTokens, comment::InjectedTokens,
) -> Transaction, ) -> Transaction,
>; >;
@ -5121,6 +5117,7 @@ fn toggle_comments_impl(cx: &mut Context, mut comment_transaction: CommentTransa
let transaction = comment_transaction( let transaction = comment_transaction(
line_token, line_token,
block_tokens, block_tokens,
doc.text(),
doc.selection(view.id), doc.selection(view.id),
Box::new(|range: Range| { Box::new(|range: Range| {
let mut best_fit = None; let mut best_fit = None;
@ -5163,104 +5160,93 @@ fn toggle_comments_impl(cx: &mut Context, mut comment_transaction: CommentTransa
/// 4. all lines not commented and block tokens -> comment uncommented lines /// 4. all lines not commented and block tokens -> comment uncommented lines
/// 5. no comment tokens and not block commented -> line comment /// 5. no comment tokens and not block commented -> line comment
fn toggle_comments(cx: &mut Context) { fn toggle_comments(cx: &mut Context) {
toggle_comments_impl(cx, |line_token, block_tokens, selection, lol_fn| { toggle_comments_impl(
// only have line comment tokens cx,
if line_token.is_some() && block_tokens.is_none() { Box::new(|line_token, block_tokens, doc, selection, lol_fn| {
return comment::toggle_line_comments(doc, selection, line_token, lol_fn); let text = doc.slice(..);
}
let split_lines = comment::split_lines_of_selection(text, selection); // only have line comment tokens
if line_token.is_some() && block_tokens.is_none() {
return comment::toggle_line_comments(doc, selection, line_token, lol_fn);
}
let default_block_tokens = &[BlockCommentToken::default()]; let split_lines = comment::split_lines_of_selection(text, selection);
let block_comment_tokens = block_tokens.unwrap_or(default_block_tokens);
let (line_commented, line_comment_changes) = let default_block_tokens = &[BlockCommentToken::default()];
comment::find_block_comments(block_comment_tokens, text, &split_lines); let block_comment_tokens = block_tokens.unwrap_or(default_block_tokens);
// block commented by line would also be block commented so check this first let (line_commented, line_comment_changes) =
if line_commented { comment::find_block_comments(block_comment_tokens, text, &split_lines);
return comment::create_block_comment_transaction(
doc,
&split_lines,
line_commented,
line_comment_changes,
)
.0;
}
let (block_commented, comment_changes) = // block commented by line would also be block commented so check this first
comment::find_block_comments(block_comment_tokens, text, selection); if line_commented {
return comment::create_block_comment_transaction(
doc,
&split_lines,
line_commented,
line_comment_changes,
)
.0;
}
// check if selection has block comments let (block_commented, comment_changes) =
if block_commented { comment::find_block_comments(block_comment_tokens, text, selection);
return comment::create_block_comment_transaction(
doc,
selection,
block_commented,
comment_changes,
)
.0;
}
// not commented and only have block comment tokens // check if selection has block comments
if line_token.is_none() && block_tokens.is_some() { if block_commented {
return comment::create_block_comment_transaction( return comment::create_block_comment_transaction(
doc, doc,
&split_lines, selection,
line_commented, block_commented,
line_comment_changes, comment_changes,
) )
.0; .0;
} }
// not block commented at all and don't have any tokens // not commented and only have block comment tokens
comment::toggle_line_comments(doc, selection, line_token) if line_token.is_none() && block_tokens.is_some() {
}) return comment::create_block_comment_transaction(
} doc,
&split_lines,
line_commented,
line_comment_changes,
)
.0;
}
fn testing1234(cx: &mut Context) { // not block commented at all and don't have any tokens
let doc = doc!(cx.editor); comment::toggle_line_comments(doc, selection, line_token, lol_fn)
let syntax = doc.syntax().unwrap(); }),
)
// let a = syntax.layers.keys().next().unwrap();
let b = syntax
.layers
.values()
.map(|value| value.ranges.clone())
.collect::<Vec<_>>();
// b.depth
// let config = syntax.layer_config(a);
log::error!("{:#?}", b);
} }
fn toggle_line_comments(cx: &mut Context) { fn toggle_line_comments(cx: &mut Context) {
toggle_comments_impl(cx, |line_token, block_tokens, doc, selection| { toggle_comments_impl(cx, |line_token, block_tokens, doc, selection| {
if line_token.is_none() && block_tokens.is_some() { todo!();
let default_block_tokens = &[BlockCommentToken::default()]; // if line_token.is_none() && block_tokens.is_some() {
let block_comment_tokens = block_tokens.unwrap_or(default_block_tokens); // let default_block_tokens = &[BlockCommentToken::default()];
comment::toggle_block_comments( // let block_comment_tokens = block_tokens.unwrap_or(default_block_tokens);
doc, // comment::toggle_block_comments(
&comment::split_lines_of_selection(doc.slice(..), selection), // doc,
block_comment_tokens, // &comment::split_lines_of_selection(doc.slice(..), selection),
) // block_comment_tokens,
} else { // )
comment::toggle_line_comments(doc, selection, line_token) // } else {
} // comment::toggle_line_comments(doc, selection, line_token)
// }
}); });
} }
fn toggle_block_comments(cx: &mut Context) { fn toggle_block_comments(cx: &mut Context) {
toggle_comments_impl(cx, |line_token, block_tokens, doc, selection| { toggle_comments_impl(cx, |line_token, block_tokens, doc, selection| {
if line_token.is_some() && block_tokens.is_none() { todo!();
comment::toggle_line_comments(doc, selection, line_token) // if line_token.is_some() && block_tokens.is_none() {
} else { // comment::toggle_line_comments(doc, selection, line_token)
let default_block_tokens = &[BlockCommentToken::default()]; // } else {
let block_comment_tokens = block_tokens.unwrap_or(default_block_tokens); // let default_block_tokens = &[BlockCommentToken::default()];
comment::toggle_block_comments(doc, selection, block_comment_tokens) // let block_comment_tokens = block_tokens.unwrap_or(default_block_tokens);
} // comment::toggle_block_comments(doc, selection, block_comment_tokens)
// }
}); });
} }

View File

@ -6,7 +6,6 @@ use helix_core::hashmap;
pub fn default() -> HashMap<Mode, KeyTrie> { pub fn default() -> HashMap<Mode, KeyTrie> {
let normal = keymap!({ "Normal mode" let normal = keymap!({ "Normal mode"
"M" => testing1234,
"h" | "left" => move_char_left, "h" | "left" => move_char_left,
"j" | "down" => move_visual_line_down, "j" | "down" => move_visual_line_down,
"k" | "up" => move_visual_line_up, "k" | "up" => move_visual_line_up,