mirror of https://github.com/helix-editor/helix
chore: clean up code
parent
8fe3f90cbb
commit
b10fc21169
|
@ -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.
|
||||
/// - 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`.
|
||||
fn find_line_comment(
|
||||
token: &str,
|
||||
fn find_line_comment<'a>(
|
||||
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) {
|
||||
let mut commented = true;
|
||||
let mut to_change = Vec::new();
|
||||
let mut min = usize::MAX; // minimum col for first_non_whitespace_char
|
||||
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);
|
||||
if let Some(pos) = line_slice.first_non_whitespace_char() {
|
||||
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
|
||||
pub type InjectedTokens = fn(
|
||||
range: Range,
|
||||
syntax: Option<&crate::Syntax>,
|
||||
rope: RopeSlice,
|
||||
) -> (Option<Vec<String>>, Option<Vec<BlockCommentToken>>);
|
||||
pub type InjectedTokens =
|
||||
Box<dyn FnMut(usize, usize) -> (Option<Vec<String>>, Option<Vec<BlockCommentToken>>)>;
|
||||
|
||||
#[must_use]
|
||||
pub fn toggle_line_comments(
|
||||
|
@ -97,7 +100,7 @@ pub fn toggle_line_comments(
|
|||
let token = token.unwrap_or(DEFAULT_COMMENT_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;
|
||||
for selection in selection {
|
||||
|
@ -105,6 +108,9 @@ pub fn toggle_line_comments(
|
|||
let start = start.clamp(min_next_line, 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);
|
||||
min_next_line = end;
|
||||
}
|
||||
|
|
|
@ -504,7 +504,6 @@ impl MappableCommand {
|
|||
hover, "Show docs for item under cursor",
|
||||
toggle_comments, "Comment/uncomment selections",
|
||||
toggle_line_comments, "Line comment/uncomment selections",
|
||||
testing1234, "a",
|
||||
toggle_block_comments, "Block comment/uncomment selections",
|
||||
rotate_selections_forward, "Rotate selections forward",
|
||||
rotate_selections_backward, "Rotate selections backward",
|
||||
|
@ -5089,17 +5088,14 @@ pub fn completion(cx: &mut Context) {
|
|||
.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
|
||||
type CommentTransactionFn = Box<
|
||||
dyn FnMut(
|
||||
Option<&str>,
|
||||
Option<&[BlockCommentToken]>,
|
||||
&Rope,
|
||||
&Selection,
|
||||
InjectedTokens,
|
||||
comment::InjectedTokens,
|
||||
) -> Transaction,
|
||||
>;
|
||||
|
||||
|
@ -5121,6 +5117,7 @@ fn toggle_comments_impl(cx: &mut Context, mut comment_transaction: CommentTransa
|
|||
let transaction = comment_transaction(
|
||||
line_token,
|
||||
block_tokens,
|
||||
doc.text(),
|
||||
doc.selection(view.id),
|
||||
Box::new(|range: Range| {
|
||||
let mut best_fit = None;
|
||||
|
@ -5163,7 +5160,11 @@ fn toggle_comments_impl(cx: &mut Context, mut comment_transaction: CommentTransa
|
|||
/// 4. all lines not commented and block tokens -> comment uncommented lines
|
||||
/// 5. no comment tokens and not block commented -> line comment
|
||||
fn toggle_comments(cx: &mut Context) {
|
||||
toggle_comments_impl(cx, |line_token, block_tokens, selection, lol_fn| {
|
||||
toggle_comments_impl(
|
||||
cx,
|
||||
Box::new(|line_token, block_tokens, doc, selection, lol_fn| {
|
||||
let text = doc.slice(..);
|
||||
|
||||
// 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);
|
||||
|
@ -5214,53 +5215,38 @@ fn toggle_comments(cx: &mut Context) {
|
|||
}
|
||||
|
||||
// not block commented at all and don't have any tokens
|
||||
comment::toggle_line_comments(doc, selection, line_token)
|
||||
})
|
||||
}
|
||||
|
||||
fn testing1234(cx: &mut Context) {
|
||||
let doc = doc!(cx.editor);
|
||||
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);
|
||||
comment::toggle_line_comments(doc, selection, line_token, lol_fn)
|
||||
}),
|
||||
)
|
||||
}
|
||||
|
||||
fn toggle_line_comments(cx: &mut Context) {
|
||||
toggle_comments_impl(cx, |line_token, block_tokens, doc, selection| {
|
||||
if line_token.is_none() && block_tokens.is_some() {
|
||||
let default_block_tokens = &[BlockCommentToken::default()];
|
||||
let block_comment_tokens = block_tokens.unwrap_or(default_block_tokens);
|
||||
comment::toggle_block_comments(
|
||||
doc,
|
||||
&comment::split_lines_of_selection(doc.slice(..), selection),
|
||||
block_comment_tokens,
|
||||
)
|
||||
} else {
|
||||
comment::toggle_line_comments(doc, selection, line_token)
|
||||
}
|
||||
todo!();
|
||||
// if line_token.is_none() && block_tokens.is_some() {
|
||||
// let default_block_tokens = &[BlockCommentToken::default()];
|
||||
// let block_comment_tokens = block_tokens.unwrap_or(default_block_tokens);
|
||||
// comment::toggle_block_comments(
|
||||
// doc,
|
||||
// &comment::split_lines_of_selection(doc.slice(..), selection),
|
||||
// block_comment_tokens,
|
||||
// )
|
||||
// } else {
|
||||
// comment::toggle_line_comments(doc, selection, line_token)
|
||||
// }
|
||||
});
|
||||
}
|
||||
|
||||
fn toggle_block_comments(cx: &mut Context) {
|
||||
toggle_comments_impl(cx, |line_token, block_tokens, doc, selection| {
|
||||
if line_token.is_some() && block_tokens.is_none() {
|
||||
comment::toggle_line_comments(doc, selection, line_token)
|
||||
} else {
|
||||
let default_block_tokens = &[BlockCommentToken::default()];
|
||||
let block_comment_tokens = block_tokens.unwrap_or(default_block_tokens);
|
||||
comment::toggle_block_comments(doc, selection, block_comment_tokens)
|
||||
}
|
||||
todo!();
|
||||
// if line_token.is_some() && block_tokens.is_none() {
|
||||
// comment::toggle_line_comments(doc, selection, line_token)
|
||||
// } else {
|
||||
// let default_block_tokens = &[BlockCommentToken::default()];
|
||||
// let block_comment_tokens = block_tokens.unwrap_or(default_block_tokens);
|
||||
// comment::toggle_block_comments(doc, selection, block_comment_tokens)
|
||||
// }
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
@ -6,7 +6,6 @@ use helix_core::hashmap;
|
|||
|
||||
pub fn default() -> HashMap<Mode, KeyTrie> {
|
||||
let normal = keymap!({ "Normal mode"
|
||||
"M" => testing1234,
|
||||
"h" | "left" => move_char_left,
|
||||
"j" | "down" => move_visual_line_down,
|
||||
"k" | "up" => move_visual_line_up,
|
||||
|
|
Loading…
Reference in New Issue