Skip to content

Commit 46af400

Browse files
authored
feat: determine auto-pairs from injection (#14697)
1 parent fefd0a4 commit 46af400

File tree

3 files changed

+27
-9
lines changed

3 files changed

+27
-9
lines changed

helix-term/src/commands.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ use helix_view::{
5858
};
5959

6060
use anyhow::{anyhow, bail, ensure, Context as _};
61+
use arc_swap::access::DynAccess;
6162
use insert::*;
6263
use movement::Movement;
6364

@@ -4152,7 +4153,9 @@ pub mod insert {
41524153
let (view, doc) = current_ref!(cx.editor);
41534154
let text = doc.text();
41544155
let selection = doc.selection(view.id);
4155-
let auto_pairs = doc.auto_pairs(cx.editor);
4156+
4157+
let loader: &helix_core::syntax::Loader = &cx.editor.syn_loader.load();
4158+
let auto_pairs = doc.auto_pairs(cx.editor, loader, view);
41564159

41574160
let transaction = auto_pairs
41584161
.as_ref()
@@ -4320,11 +4323,12 @@ pub mod insert {
43204323
),
43214324
};
43224325

4326+
let loader: &helix_core::syntax::Loader = &cx.editor.syn_loader.load();
43234327
// If we are between pairs (such as brackets), we want to
43244328
// insert an additional line which is indented one level
43254329
// more and place the cursor there
43264330
let on_auto_pair = doc
4327-
.auto_pairs(cx.editor)
4331+
.auto_pairs(cx.editor, loader, view)
43284332
.and_then(|pairs| pairs.get(prev))
43294333
.is_some_and(|pair| pair.open == prev && pair.close == curr);
43304334

@@ -4412,7 +4416,9 @@ pub mod insert {
44124416
let text = doc.text().slice(..);
44134417
let tab_width = doc.tab_width();
44144418
let indent_width = doc.indent_width();
4415-
let auto_pairs = doc.auto_pairs(cx.editor);
4419+
4420+
let loader: &helix_core::syntax::Loader = &cx.editor.syn_loader.load();
4421+
let auto_pairs = doc.auto_pairs(cx.editor, loader, view);
44164422

44174423
let transaction =
44184424
Transaction::delete_by_selection(doc.text(), doc.selection(view.id), |range| {

helix-term/src/commands/typed.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1862,7 +1862,7 @@ fn tree_sitter_layers(
18621862
bail!("Syntax information is not available");
18631863
};
18641864

1865-
let loader = cx.editor.syn_loader.load();
1865+
let loader: &helix_core::syntax::Loader = &cx.editor.syn_loader.load();
18661866
let text = doc.text().slice(..);
18671867
let cursor = doc.selection(view.id).primary().cursor(text);
18681868
let byte = text.char_to_byte(cursor) as u32;

helix-view/src/document.rs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2170,7 +2170,12 @@ impl Document {
21702170
/// language config with auto pairs configured, returns that;
21712171
/// otherwise, falls back to the global auto pairs config. If the global
21722172
/// config is false, then ignore language settings.
2173-
pub fn auto_pairs<'a>(&'a self, editor: &'a Editor) -> Option<&'a AutoPairs> {
2173+
pub fn auto_pairs<'a>(
2174+
&'a self,
2175+
editor: &'a Editor,
2176+
loader: &'a syntax::Loader,
2177+
view: &View,
2178+
) -> Option<&'a AutoPairs> {
21742179
let global_config = (editor.auto_pairs).as_ref();
21752180

21762181
// NOTE: If the user specifies the global auto pairs config as false, then
@@ -2182,10 +2187,17 @@ impl Document {
21822187
}
21832188
}
21842189

2185-
match &self.language {
2186-
Some(lang) => lang.as_ref().auto_pairs.as_ref().or(global_config),
2187-
None => global_config,
2188-
}
2190+
self.syntax
2191+
.as_ref()
2192+
.and_then(|syntax| {
2193+
let selection = self.selection(view.id).primary();
2194+
let (start, end) = selection.into_byte_range(self.text().slice(..));
2195+
let layer = syntax.layer_for_byte_range(start as u32, end as u32);
2196+
2197+
let lang_config = loader.language(syntax.layer(layer).language).config();
2198+
lang_config.auto_pairs.as_ref()
2199+
})
2200+
.or(global_config)
21892201
}
21902202

21912203
pub fn snippet_ctx(&self) -> SnippetRenderCtx {

0 commit comments

Comments
 (0)