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.

Add outlines for CSS (#8706)

Ivan Buryakff8a4a89c73f
diff --git a/Cargo.lock b/Cargo.lock
index 8af76cf35bd..dc62b9c8902 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -5337,6 +5337,7 @@ dependencies = [
"tree-sitter-zig",
"unindent",
"util",
+ "workspace",
]
[[package]]
diff --git a/crates/languages/Cargo.toml b/crates/languages/Cargo.toml
index 35c4d3f01a5..8b8891bc9a6 100644
--- a/crates/languages/Cargo.toml
+++ b/crates/languages/Cargo.toml
@@ -86,3 +86,4 @@ util.workspace = true
text.workspace = true
theme.workspace = true
unindent.workspace = true
+workspace = { workspace = true, features = ["test-support"] }
diff --git a/crates/languages/src/css.rs b/crates/languages/src/css.rs
index fbc856efbd2..ba55ed31345 100644
--- a/crates/languages/src/css.rs
+++ b/crates/languages/src/css.rs
@@ -126,3 +126,62 @@ async fn get_cached_server_binary(
.await
.log_err()
}
+
+#[cfg(test)]
+mod tests {
+ use gpui::{Context, TestAppContext};
+ use text::BufferId;
+ use unindent::Unindent;
+
+ #[gpui::test]
+ async fn test_outline(cx: &mut TestAppContext) {
+ let language = crate::language("css", tree_sitter_css::language());
+
+ let text = r#"
+ /* Import statement */
+ @import './fonts.css';
+
+ /* multiline list of selectors with nesting */
+ .test-class,
+ div {
+ .nested-class {
+ color: red;
+ }
+ }
+
+ /* descendant selectors */
+ .test .descendant {}
+
+ /* pseudo */
+ .test:not(:hover) {}
+
+ /* media queries */
+ @media screen and (min-width: 3000px) {
+ .desktop-class {}
+ }
+ "#
+ .unindent();
+
+ let buffer = cx.new_model(|cx| {
+ language::Buffer::new(0, BufferId::new(cx.entity_id().as_u64()).unwrap(), text)
+ .with_language(language, cx)
+ });
+ let outline = buffer.update(cx, |buffer, _| buffer.snapshot().outline(None).unwrap());
+ assert_eq!(
+ outline
+ .items
+ .iter()
+ .map(|item| (item.text.as_str(), item.depth))
+ .collect::<Vec<_>>(),
+ &[
+ ("@import './fonts.css'", 0),
+ (".test-class, div", 0),
+ (".nested-class", 1),
+ (".test .descendant", 0),
+ (".test:not(:hover)", 0),
+ ("@media screen and (min-width: 3000px)", 0),
+ (".desktop-class", 1),
+ ]
+ );
+ }
+}
diff --git a/crates/languages/src/css/outline.scm b/crates/languages/src/css/outline.scm
new file mode 100644
index 00000000000..645616f9056
--- /dev/null
+++ b/crates/languages/src/css/outline.scm
@@ -0,0 +1,18 @@
+(stylesheet
+ (import_statement
+ "@import" @context
+ ((string_value) @name)) @item)
+
+
+(rule_set
+ (selectors
+ .
+ (_) @name
+ ("," @name (_) @name)*
+ )) @item
+
+(media_statement
+ "@media" @context
+ (_) @name
+ (block)
+) @item