Account for greedy tree-sitter bracket matches (#43607)

Current approach is to colorize brackets based on their depth, which was
broken for markdown:

<img width="388" height="50" alt="image"
src="https://github.com/user-attachments/assets/bd8b6c2f-5a26-4d6b-a301-88675bf05920"
/>

Markdown grammar, for bracket queries
https://github.com/zed-industries/zed/blob/00e93bfa113a3daed6e4a97a7244ad04d58453ee/crates/languages/src/markdown/brackets.scm#L1-L8

and markdown document `[LLM-powered features](./ai/overview.md), [bring
and configure your own API
keys](./ai/llm-providers.md#use-your-own-keys)`, matches first bracket
(offset 0) with two different ones:

* `[LLM-powered features]`

* `[LLM-powered features](./ai/overview.md), [bring and configure your
own API keys]`

which mix and add different color markers.

Now, in case multiple pairs exist for the same first bracket, Zed will
only colorize the shortest one:
<img width="373" height="33" alt="image"
src="https://github.com/user-attachments/assets/04b3f7af-8927-4a8b-8f52-de8b5bb063ac"
/>


Release Notes:

- Fixed bracket colorization mixing colors in markdown files
This commit is contained in:
Kirill Bulatov
2025-11-26 23:22:13 +00:00
committed by GitHub
parent 958f1098b7
commit 54309f4a48
2 changed files with 82 additions and 24 deletions
+26 -1
View File
@@ -161,7 +161,7 @@ mod tests {
use gpui::{AppContext as _, UpdateGlobal as _};
use indoc::indoc;
use itertools::Itertools;
use language::Capability;
use language::{Capability, markdown_lang};
use languages::rust_lang;
use multi_buffer::{ExcerptRange, MultiBuffer};
use pretty_assertions::assert_eq;
@@ -261,6 +261,31 @@ where
);
}
#[gpui::test]
async fn test_markdown_bracket_colorization(cx: &mut gpui::TestAppContext) {
init_test(cx, |language_settings| {
language_settings.defaults.colorize_brackets = Some(true);
});
let mut cx = EditorLspTestContext::new(
Arc::into_inner(markdown_lang()).unwrap(),
lsp::ServerCapabilities::default(),
cx,
)
.await;
cx.set_state(indoc! {r#"ˇ[LLM-powered features](./ai/overview.md), [bring and configure your own API keys](./ai/llm-providers.md#use-your-own-keys)"#});
cx.executor().advance_clock(Duration::from_millis(100));
cx.executor().run_until_parked();
assert_eq!(
r#"«1[LLM-powered features]1»«1(./ai/overview.md)1», «1[bring and configure your own API keys]1»«1(./ai/llm-providers.md#use-your-own-keys)1»
1 hsla(207.80, 16.20%, 69.19%, 1.00)
"#,
&bracket_colors_markup(&mut cx),
"All markdown brackets should be colored based on their depth"
);
}
#[gpui::test]
async fn test_bracket_colorization_when_editing(cx: &mut gpui::TestAppContext) {
init_test(cx, |language_settings| {