mirror of https://github.com/helix-editor/helix
feat: use FnMut
parent
38bede20ef
commit
8fe3f90cbb
|
@ -4,7 +4,8 @@
|
||||||
use smallvec::SmallVec;
|
use smallvec::SmallVec;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
syntax::BlockCommentToken, Change, Range, Rope, RopeSlice, Selection, Tendril, Transaction,
|
syntax::{BlockCommentToken, InjectionLanguageMarker},
|
||||||
|
Change, Range, Rope, RopeSlice, Selection, Tendril, Transaction,
|
||||||
};
|
};
|
||||||
use helix_stdx::rope::RopeSliceExt;
|
use helix_stdx::rope::RopeSliceExt;
|
||||||
use std::borrow::Cow;
|
use std::borrow::Cow;
|
||||||
|
@ -77,8 +78,20 @@ fn find_line_comment(
|
||||||
(commented, to_change, min, margin)
|
(commented, to_change, min, margin)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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>>);
|
||||||
|
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub fn toggle_line_comments(doc: &Rope, selection: &Selection, token: Option<&str>) -> Transaction {
|
pub fn toggle_line_comments(
|
||||||
|
doc: &Rope,
|
||||||
|
selection: &Selection,
|
||||||
|
token: Option<&str>,
|
||||||
|
lol_fn: InjectedTokens,
|
||||||
|
) -> Transaction {
|
||||||
let text = doc.slice(..);
|
let text = doc.slice(..);
|
||||||
|
|
||||||
let token = token.unwrap_or(DEFAULT_COMMENT_TOKEN);
|
let token = token.unwrap_or(DEFAULT_COMMENT_TOKEN);
|
||||||
|
|
|
@ -5089,21 +5089,21 @@ pub fn completion(cx: &mut Context) {
|
||||||
.trigger_completions(cursor, doc.id(), view.id);
|
.trigger_completions(cursor, doc.id(), view.id);
|
||||||
}
|
}
|
||||||
|
|
||||||
// comments
|
// for a given range and syntax, determine if there are additional tokens to consider
|
||||||
type CommentTransactionFn = fn(
|
pub type InjectedTokens =
|
||||||
line_token: Option<&str>,
|
Box<dyn FnMut(Range) -> (Option<Vec<String>>, Option<Vec<BlockCommentToken>>)>;
|
||||||
block_tokens: Option<&[BlockCommentToken]>,
|
|
||||||
doc: &Rope,
|
|
||||||
selection: &Selection,
|
|
||||||
syntax: Option<&Syntax>,
|
|
||||||
injected_lang_tokens: fn(
|
|
||||||
range: Range,
|
|
||||||
syntax: Option<&Syntax>,
|
|
||||||
rope: RopeSlice,
|
|
||||||
) -> (Option<Vec<String>>, Option<Vec<BlockCommentToken>>),
|
|
||||||
) -> Transaction;
|
|
||||||
|
|
||||||
fn toggle_comments_impl(cx: &mut Context, comment_transaction: CommentTransactionFn) {
|
// comments
|
||||||
|
type CommentTransactionFn = Box<
|
||||||
|
dyn FnMut(
|
||||||
|
Option<&str>,
|
||||||
|
Option<&[BlockCommentToken]>,
|
||||||
|
&Selection,
|
||||||
|
InjectedTokens,
|
||||||
|
) -> Transaction,
|
||||||
|
>;
|
||||||
|
|
||||||
|
fn toggle_comments_impl(cx: &mut Context, mut comment_transaction: CommentTransactionFn) {
|
||||||
let (view, doc) = current!(cx.editor);
|
let (view, doc) = current!(cx.editor);
|
||||||
let line_token: Option<&str> = doc
|
let line_token: Option<&str> = doc
|
||||||
.language_config()
|
.language_config()
|
||||||
|
@ -5115,13 +5115,14 @@ fn toggle_comments_impl(cx: &mut Context, comment_transaction: CommentTransactio
|
||||||
.and_then(|lc| lc.block_comment_tokens.as_ref())
|
.and_then(|lc| lc.block_comment_tokens.as_ref())
|
||||||
.map(|tc| &tc[..]);
|
.map(|tc| &tc[..]);
|
||||||
|
|
||||||
|
let syntax = doc.syntax();
|
||||||
|
let rope = doc.text().slice(..);
|
||||||
|
|
||||||
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),
|
||||||
doc.syntax(),
|
Box::new(|range: Range| {
|
||||||
|range: Range, syntax: Option<&Syntax>, rope: RopeSlice| {
|
|
||||||
let mut best_fit = None;
|
let mut best_fit = None;
|
||||||
let mut min_gap = usize::MAX;
|
let mut min_gap = usize::MAX;
|
||||||
|
|
||||||
|
@ -5148,7 +5149,7 @@ fn toggle_comments_impl(cx: &mut Context, comment_transaction: CommentTransactio
|
||||||
}
|
}
|
||||||
|
|
||||||
(None, None)
|
(None, None)
|
||||||
},
|
}),
|
||||||
);
|
);
|
||||||
|
|
||||||
doc.apply(&transaction, view.id);
|
doc.apply(&transaction, view.id);
|
||||||
|
@ -5162,65 +5163,59 @@ fn toggle_comments_impl(cx: &mut Context, comment_transaction: CommentTransactio
|
||||||
/// 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(
|
toggle_comments_impl(cx, |line_token, block_tokens, selection, lol_fn| {
|
||||||
cx,
|
// only have line comment tokens
|
||||||
|line_token, block_tokens, doc, selection, syntax, lol_fn| {
|
if line_token.is_some() && block_tokens.is_none() {
|
||||||
let (injected_line_tokens, injected_block_tokens) = lol_fn()
|
return comment::toggle_line_comments(doc, selection, line_token, lol_fn);
|
||||||
let text = doc.slice(..);
|
}
|
||||||
|
|
||||||
// only have line comment tokens
|
let split_lines = comment::split_lines_of_selection(text, selection);
|
||||||
if line_token.is_some() && block_tokens.is_none() {
|
|
||||||
return comment::toggle_line_comments(doc, selection, line_token);
|
|
||||||
}
|
|
||||||
|
|
||||||
let split_lines = comment::split_lines_of_selection(text, selection);
|
let default_block_tokens = &[BlockCommentToken::default()];
|
||||||
|
let block_comment_tokens = block_tokens.unwrap_or(default_block_tokens);
|
||||||
|
|
||||||
let default_block_tokens = &[BlockCommentToken::default()];
|
let (line_commented, line_comment_changes) =
|
||||||
let block_comment_tokens = block_tokens.unwrap_or(default_block_tokens);
|
comment::find_block_comments(block_comment_tokens, text, &split_lines);
|
||||||
|
|
||||||
let (line_commented, line_comment_changes) =
|
// block commented by line would also be block commented so check this first
|
||||||
comment::find_block_comments(block_comment_tokens, text, &split_lines);
|
if line_commented {
|
||||||
|
return comment::create_block_comment_transaction(
|
||||||
|
doc,
|
||||||
|
&split_lines,
|
||||||
|
line_commented,
|
||||||
|
line_comment_changes,
|
||||||
|
)
|
||||||
|
.0;
|
||||||
|
}
|
||||||
|
|
||||||
// block commented by line would also be block commented so check this first
|
let (block_commented, comment_changes) =
|
||||||
if line_commented {
|
comment::find_block_comments(block_comment_tokens, text, selection);
|
||||||
return comment::create_block_comment_transaction(
|
|
||||||
doc,
|
|
||||||
&split_lines,
|
|
||||||
line_commented,
|
|
||||||
line_comment_changes,
|
|
||||||
)
|
|
||||||
.0;
|
|
||||||
}
|
|
||||||
|
|
||||||
let (block_commented, comment_changes) =
|
// check if selection has block comments
|
||||||
comment::find_block_comments(block_comment_tokens, text, selection);
|
if block_commented {
|
||||||
|
return comment::create_block_comment_transaction(
|
||||||
|
doc,
|
||||||
|
selection,
|
||||||
|
block_commented,
|
||||||
|
comment_changes,
|
||||||
|
)
|
||||||
|
.0;
|
||||||
|
}
|
||||||
|
|
||||||
// check if selection has block comments
|
// not commented and only have block comment tokens
|
||||||
if block_commented {
|
if line_token.is_none() && block_tokens.is_some() {
|
||||||
return comment::create_block_comment_transaction(
|
return comment::create_block_comment_transaction(
|
||||||
doc,
|
doc,
|
||||||
selection,
|
&split_lines,
|
||||||
block_commented,
|
line_commented,
|
||||||
comment_changes,
|
line_comment_changes,
|
||||||
)
|
)
|
||||||
.0;
|
.0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// not commented and only have block comment tokens
|
// not block commented at all and don't have any tokens
|
||||||
if line_token.is_none() && block_tokens.is_some() {
|
comment::toggle_line_comments(doc, selection, line_token)
|
||||||
return comment::create_block_comment_transaction(
|
})
|
||||||
doc,
|
|
||||||
&split_lines,
|
|
||||||
line_commented,
|
|
||||||
line_comment_changes,
|
|
||||||
)
|
|
||||||
.0;
|
|
||||||
}
|
|
||||||
|
|
||||||
// not block commented at all and don't have any tokens
|
|
||||||
comment::toggle_line_comments(doc, selection, line_token)
|
|
||||||
},
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn testing1234(cx: &mut Context) {
|
fn testing1234(cx: &mut Context) {
|
||||||
|
|
Loading…
Reference in New Issue