1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
use gpui::ViewContext;

use crate::{Editor, RangeToAnchorExt};

enum MatchingBracketHighlight {}

pub fn refresh_matching_bracket_highlights(editor: &mut Editor, cx: &mut ViewContext<Editor>) {
    // editor.clear_background_highlights::<MatchingBracketHighlight>(cx);

    let newest_selection = editor.selections.newest::<usize>(cx);
    // Don't highlight brackets if the selection isn't empty
    if !newest_selection.is_empty() {
        return;
    }

    let head = newest_selection.head();
    let snapshot = editor.snapshot(cx);
    if let Some((opening_range, closing_range)) = snapshot
        .buffer_snapshot
        .innermost_enclosing_bracket_ranges(head..head)
    {
        editor.highlight_background::<MatchingBracketHighlight>(
            vec![
                opening_range.to_anchors(&snapshot.buffer_snapshot),
                closing_range.to_anchors(&snapshot.buffer_snapshot),
            ],
            |theme| theme.editor_document_highlight_read_background,
            cx,
        )
    }
}

// #[cfg(test)]
// mod tests {
//     use super::*;
//     use crate::{editor_tests::init_test, test::editor_lsp_test_context::EditorLspTestContext};
//     use indoc::indoc;
//     use language::{BracketPair, BracketPairConfig, Language, LanguageConfig};

//     #[gpui::test]
//     async fn test_matching_bracket_highlights(cx: &mut gpui::TestAppContext) {
//         init_test(cx, |_| {});

//         let mut cx = EditorLspTestContext::new(
//             Language::new(
//                 LanguageConfig {
//                     name: "Rust".into(),
//                     path_suffixes: vec!["rs".to_string()],
//                     brackets: BracketPairConfig {
//                         pairs: vec![
//                             BracketPair {
//                                 start: "{".to_string(),
//                                 end: "}".to_string(),
//                                 close: false,
//                                 newline: true,
//                             },
//                             BracketPair {
//                                 start: "(".to_string(),
//                                 end: ")".to_string(),
//                                 close: false,
//                                 newline: true,
//                             },
//                         ],
//                         ..Default::default()
//                     },
//                     ..Default::default()
//                 },
//                 Some(tree_sitter_rust::language()),
//             )
//             .with_brackets_query(indoc! {r#"
//                 ("{" @open "}" @close)
//                 ("(" @open ")" @close)
//                 "#})
//             .unwrap(),
//             Default::default(),
//             cx,
//         )
//         .await;

//         // positioning cursor inside bracket highlights both
//         cx.set_state(indoc! {r#"
//             pub fn test("Test ˇargument") {
//                 another_test(1, 2, 3);
//             }
//         "#});
//         cx.assert_editor_background_highlights::<MatchingBracketHighlight>(indoc! {r#"
//             pub fn test«(»"Test argument"«)» {
//                 another_test(1, 2, 3);
//             }
//         "#});

//         cx.set_state(indoc! {r#"
//             pub fn test("Test argument") {
//                 another_test(1, ˇ2, 3);
//             }
//         "#});
//         cx.assert_editor_background_highlights::<MatchingBracketHighlight>(indoc! {r#"
//             pub fn test("Test argument") {
//                 another_test«(»1, 2, 3«)»;
//             }
//         "#});

//         cx.set_state(indoc! {r#"
//             pub fn test("Test argument") {
//                 anotherˇ_test(1, 2, 3);
//             }
//         "#});
//         cx.assert_editor_background_highlights::<MatchingBracketHighlight>(indoc! {r#"
//             pub fn test("Test argument") «{»
//                 another_test(1, 2, 3);
//             «}»
//         "#});

//         // positioning outside of brackets removes highlight
//         cx.set_state(indoc! {r#"
//             pub fˇn test("Test argument") {
//                 another_test(1, 2, 3);
//             }
//         "#});
//         cx.assert_editor_background_highlights::<MatchingBracketHighlight>(indoc! {r#"
//             pub fn test("Test argument") {
//                 another_test(1, 2, 3);
//             }
//         "#});

//         // non empty selection dismisses highlight
//         cx.set_state(indoc! {r#"
//             pub fn test("Te«st argˇ»ument") {
//                 another_test(1, 2, 3);
//             }
//         "#});
//         cx.assert_editor_background_highlights::<MatchingBracketHighlight>(indoc! {r#"
//             pub fn test("Test argument") {
//                 another_test(1, 2, 3);
//             }
//         "#});
//     }
// }