Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

language: Fix bracket `not_in` scopes being ignored at end of buffer (#64840)

rn23thakur8e7fbcc131ec
diff --git a/crates/language/src/buffer.rs b/crates/language/src/buffer.rs
index 32b633d3e0d..c3e4030102f 100644
--- a/crates/language/src/buffer.rs
+++ b/crates/language/src/buffer.rs
@@ -4422,7 +4422,12 @@ impl BufferSnapshot {
let mut range = None;
loop {
let child_range = cursor.node().byte_range();
- if !child_range.contains(&offset) {
+ let contains_offset = child_range.contains(&offset)
+ // `Range::contains` is end-exclusive, which rejects every node at EOF
+ // (including the root). Accept the end boundary only at the buffer's end,
+ // so mid-buffer behavior is unchanged.
+ || (child_range.end == offset && offset == text.len());
+ if !contains_offset {
break;
}
diff --git a/crates/language/src/buffer_tests.rs b/crates/language/src/buffer_tests.rs
index b3b2313aeb4..1f4f1348da8 100644
--- a/crates/language/src/buffer_tests.rs
+++ b/crates/language/src/buffer_tests.rs
@@ -3442,6 +3442,80 @@ fn test_language_scope_at_with_rust(cx: &mut App) {
});
}
+#[gpui::test]
+fn test_language_scope_at_end_of_buffer(cx: &mut App) {
+ init_settings(cx, |_| {});
+
+ let make_language = || {
+ Language::new(
+ LanguageConfig {
+ name: "C".into(),
+ brackets: BracketPairConfig {
+ pairs: vec![
+ BracketPair {
+ start: "{".into(),
+ end: "}".into(),
+ close: true,
+ surround: true,
+ newline: false,
+ },
+ BracketPair {
+ start: "'".into(),
+ end: "'".into(),
+ close: true,
+ surround: true,
+ newline: false,
+ },
+ ],
+ disabled_scopes_by_bracket_ix: vec![
+ Vec::new(),
+ vec!["string".into(), "comment".into()],
+ ],
+ },
+ ..Default::default()
+ },
+ Some(tree_sitter_c::LANGUAGE.into()),
+ )
+ .with_override_query(
+ r#"
+ (comment) @comment.inclusive
+ [(string_literal) (char_literal)] @string
+ "#,
+ )
+ .unwrap()
+ };
+
+ // Comment runs to EOF: the quote pair must be disabled at EOF.
+ cx.new(|cx| {
+ let text = "// it ''";
+ let buffer = Buffer::local(text, cx).with_language(Arc::new(make_language()), cx);
+ let snapshot = buffer.snapshot();
+
+ let eof_config = snapshot.language_scope_at(text.len()).unwrap();
+ assert_eq!(
+ eof_config.brackets().map(|e| e.1).collect::<Vec<_>>(),
+ &[true, false]
+ );
+
+ buffer
+ });
+
+ // Trailing newline: EOF is past the comment, so both pairs stay enabled.
+ cx.new(|cx| {
+ let text = "// it ''\n";
+ let buffer = Buffer::local(text, cx).with_language(Arc::new(make_language()), cx);
+ let snapshot = buffer.snapshot();
+
+ let eof_config = snapshot.language_scope_at(text.len()).unwrap();
+ assert_eq!(
+ eof_config.brackets().map(|e| e.1).collect::<Vec<_>>(),
+ &[true, true]
+ );
+
+ buffer
+ });
+}
+
#[gpui::test]
fn test_language_scope_at_with_combined_injections(cx: &mut App) {
init_settings(cx, |_| {});