mirror of https://github.com/helix-editor/helix
feat: add new params
parent
70f27b390d
commit
38bede20ef
|
@ -1097,7 +1097,7 @@ thread_local! {
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Syntax {
|
pub struct Syntax {
|
||||||
layers: HopSlotMap<LayerId, LanguageLayer>,
|
pub layers: HopSlotMap<LayerId, LanguageLayer>,
|
||||||
root: LayerId,
|
root: LayerId,
|
||||||
loader: Arc<ArcSwap<Loader>>,
|
loader: Arc<ArcSwap<Loader>>,
|
||||||
}
|
}
|
||||||
|
|
|
@ -5095,6 +5095,12 @@ type CommentTransactionFn = fn(
|
||||||
block_tokens: Option<&[BlockCommentToken]>,
|
block_tokens: Option<&[BlockCommentToken]>,
|
||||||
doc: &Rope,
|
doc: &Rope,
|
||||||
selection: &Selection,
|
selection: &Selection,
|
||||||
|
syntax: Option<&Syntax>,
|
||||||
|
injected_lang_tokens: fn(
|
||||||
|
range: Range,
|
||||||
|
syntax: Option<&Syntax>,
|
||||||
|
rope: RopeSlice,
|
||||||
|
) -> (Option<Vec<String>>, Option<Vec<BlockCommentToken>>),
|
||||||
) -> Transaction;
|
) -> Transaction;
|
||||||
|
|
||||||
fn toggle_comments_impl(cx: &mut Context, comment_transaction: CommentTransactionFn) {
|
fn toggle_comments_impl(cx: &mut Context, comment_transaction: CommentTransactionFn) {
|
||||||
|
@ -5109,8 +5115,41 @@ 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 transaction =
|
let transaction = comment_transaction(
|
||||||
comment_transaction(line_token, block_tokens, doc.text(), doc.selection(view.id));
|
line_token,
|
||||||
|
block_tokens,
|
||||||
|
doc.text(),
|
||||||
|
doc.selection(view.id),
|
||||||
|
doc.syntax(),
|
||||||
|
|range: Range, syntax: Option<&Syntax>, rope: RopeSlice| {
|
||||||
|
let mut best_fit = None;
|
||||||
|
let mut min_gap = usize::MAX;
|
||||||
|
|
||||||
|
// TODO: improve performance of this
|
||||||
|
if let Some(syntax) = syntax {
|
||||||
|
for (layer_id, layer) in syntax.layers {
|
||||||
|
for ts_range in layer.ranges {
|
||||||
|
let (start, end) = range.into_byte_range(rope);
|
||||||
|
let is_encompassing =
|
||||||
|
ts_range.start_byte <= start && ts_range.end_byte >= end;
|
||||||
|
if is_encompassing {
|
||||||
|
let this_gap = ts_range.end_byte - ts_range.start_byte;
|
||||||
|
if this_gap < min_gap {
|
||||||
|
best_fit = Some(layer_id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(best_fit) = best_fit {
|
||||||
|
let config = syntax.layer_config(best_fit);
|
||||||
|
return (config.comment_tokens, config.block_comment_tokens);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
(None, None)
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
doc.apply(&transaction, view.id);
|
doc.apply(&transaction, view.id);
|
||||||
exit_select_mode(cx);
|
exit_select_mode(cx);
|
||||||
|
@ -5123,7 +5162,10 @@ 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(cx, |line_token, block_tokens, doc, selection| {
|
toggle_comments_impl(
|
||||||
|
cx,
|
||||||
|
|line_token, block_tokens, doc, selection, syntax, lol_fn| {
|
||||||
|
let (injected_line_tokens, injected_block_tokens) = lol_fn()
|
||||||
let text = doc.slice(..);
|
let text = doc.slice(..);
|
||||||
|
|
||||||
// only have line comment tokens
|
// only have line comment tokens
|
||||||
|
@ -5177,14 +5219,26 @@ fn toggle_comments(cx: &mut Context) {
|
||||||
|
|
||||||
// not block commented at all and don't have any tokens
|
// not block commented at all and don't have any tokens
|
||||||
comment::toggle_line_comments(doc, selection, line_token)
|
comment::toggle_line_comments(doc, selection, line_token)
|
||||||
})
|
},
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn testing1234(cx: &mut Context) {
|
fn testing1234(cx: &mut Context) {
|
||||||
let doc = doc!(cx.editor);
|
let doc = doc!(cx.editor);
|
||||||
let syntax = doc.syntax();
|
let syntax = doc.syntax().unwrap();
|
||||||
|
|
||||||
log::error!("{syntax:#?}");
|
// 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) {
|
||||||
|
|
Loading…
Reference in New Issue