Skip to content

Commit 2c23557

Browse files
committed
feat: determine auto-pairs from injection
1 parent 7895719 commit 2c23557

File tree

2 files changed

+24
-8
lines changed

2 files changed

+24
-8
lines changed

helix-term/src/commands.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4152,7 +4152,7 @@ pub mod insert {
41524152
let (view, doc) = current_ref!(cx.editor);
41534153
let text = doc.text();
41544154
let selection = doc.selection(view.id);
4155-
let auto_pairs = doc.auto_pairs(cx.editor);
4155+
let auto_pairs = doc.auto_pairs(cx.editor, view, doc);
41564156

41574157
let transaction = auto_pairs
41584158
.as_ref()
@@ -4324,7 +4324,8 @@ pub mod insert {
43244324
// insert an additional line which is indented one level
43254325
// more and place the cursor there
43264326
let on_auto_pair = doc
4327-
.auto_pairs(cx.editor)
4327+
.auto_pairs(cx.editor, view, doc)
4328+
.as_deref()
43284329
.and_then(|pairs| pairs.get(prev))
43294330
.is_some_and(|pair| pair.open == prev && pair.close == curr);
43304331

@@ -4412,7 +4413,9 @@ pub mod insert {
44124413
let text = doc.text().slice(..);
44134414
let tab_width = doc.tab_width();
44144415
let indent_width = doc.indent_width();
4415-
let auto_pairs = doc.auto_pairs(cx.editor);
4416+
4417+
let auto_pairs = doc.auto_pairs(cx.editor, view, doc);
4418+
let auto_pairs = auto_pairs.as_deref();
44164419

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

helix-view/src/document.rs

Lines changed: 18 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+
view: &View,
2177+
doc: &Document,
2178+
) -> Option<Cow<'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,18 @@ 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(doc.text().slice(..));
2195+
let layer = syntax.layer_for_byte_range(start as u32, end as u32);
2196+
2197+
let loader: &syntax::Loader = &editor.syn_loader.load();
2198+
let lang_config = loader.language(syntax.layer(layer).language).config();
2199+
lang_config.auto_pairs.clone().map(Cow::Owned)
2200+
})
2201+
.or(global_config.map(Cow::Borrowed))
21892202
}
21902203

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

0 commit comments

Comments
 (0)