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.

Prevent outline items from accidentally spanning multiple lines

Max Brunsfeldd9948bf77273
diff --git a/crates/language/src/buffer.rs b/crates/language/src/buffer.rs
index 4b79009a68e..a52ee34962e 100644
--- a/crates/language/src/buffer.rs
+++ b/crates/language/src/buffer.rs
@@ -2168,8 +2168,6 @@ impl BufferSnapshot {
continue;
}
- // TODO - move later, after processing captures
-
let mut text = String::new();
let mut name_ranges = Vec::new();
let mut highlight_ranges = Vec::new();
@@ -2183,7 +2181,13 @@ impl BufferSnapshot {
continue;
}
- let range = capture.node.start_byte()..capture.node.end_byte();
+ let mut range = capture.node.start_byte()..capture.node.end_byte();
+ let start = capture.node.start_position();
+ if capture.node.end_position().row > start.row {
+ range.end =
+ range.start + self.line_len(start.row as u32) as usize - start.column;
+ }
+
if !text.is_empty() {
text.push(' ');
}
diff --git a/crates/language/src/buffer_tests.rs b/crates/language/src/buffer_tests.rs
index b7f35e9ad5c..5aa91522052 100644
--- a/crates/language/src/buffer_tests.rs
+++ b/crates/language/src/buffer_tests.rs
@@ -455,6 +455,32 @@ async fn test_outline(cx: &mut gpui::TestAppContext) {
}
}
+#[gpui::test]
+async fn test_outline_nodes_with_newlines(cx: &mut gpui::TestAppContext) {
+ let text = r#"
+ impl A for B<
+ C
+ > {
+ };
+ "#
+ .unindent();
+
+ let buffer =
+ cx.add_model(|cx| Buffer::new(0, text, cx).with_language(Arc::new(rust_lang()), cx));
+ let outline = buffer
+ .read_with(cx, |buffer, _| buffer.snapshot().outline(None))
+ .unwrap();
+
+ assert_eq!(
+ outline
+ .items
+ .iter()
+ .map(|item| (item.text.as_str(), item.depth))
+ .collect::<Vec<_>>(),
+ &[("impl A for B<", 0)]
+ );
+}
+
#[gpui::test]
async fn test_symbols_containing(cx: &mut gpui::TestAppContext) {
let text = r#"
diff --git a/crates/zed/src/languages/go/outline.scm b/crates/zed/src/languages/go/outline.scm
index 849d2bdebf6..2ff7ef25a0a 100644
--- a/crates/zed/src/languages/go/outline.scm
+++ b/crates/zed/src/languages/go/outline.scm
@@ -40,5 +40,4 @@
")" @context)) @item
(field_declaration
- name: (_) @name
- type: (_) @context) @item
\ No newline at end of file
+ name: (_) @name) @item
\ No newline at end of file