diff --git a/crates/ide-host/src/ops/nav.rs b/crates/ide-host/src/ops/nav.rs index 9cfbe3415..c110d842f 100644 --- a/crates/ide-host/src/ops/nav.rs +++ b/crates/ide-host/src/ops/nav.rs @@ -7,7 +7,7 @@ use crate::Project; use ide_command::command; use ide_core::{DocId, IdeResult, IpcPath, LanguageId, Location, Pos}; use ide_index::{ExtensionCount, FileMatch, IndexStatus, SymbolFilter, SymbolMatch}; -use ide_language::{FileReference, WebReference}; +use ide_language::{CSS_VARIABLE, FileReference, WebReference}; use ide_workspace::file_refs::{self, PathCompletion}; use serde::{Deserialize, Serialize}; use specta::Type; @@ -25,6 +25,18 @@ pub struct WebGoto { const MAX_WEB_TARGETS: usize = 200; +/// A custom property declaration (`--accent: #3b82f6`), for `var(--…)` completion and hover. +#[derive(Clone, Debug, Serialize, Deserialize, Type)] +#[serde(rename_all = "camelCase")] +pub struct CssVariable { + pub name: String, + /// The declared value, whitespace collapsed. + pub value: Option, + pub location: Location, +} + +const MAX_CSS_VARIABLES: usize = 5000; + /// A Python class definition and the base names written in its source. The UI builds a /// name → locations index from every class in the project (attached dependency roots /// included) to compute ↑ overrides / ↓ overridden gutter markers without a language @@ -185,6 +197,41 @@ pub async fn web_goto(host: &Host, window: &str, doc: DocId, pos: Pos) -> IdeRes .await } +/// Custom properties declared in `doc` (from its current text, first) and in the project's +/// other stylesheets (from the index, so kept up to date as files change). +#[command(trust = always)] +pub async fn web_css_variables(host: &Host, window: &str, doc: DocId) -> IdeResult> { + let project = host.project(window)?; + let syntax = host.syntax(); + blocking(move || { + let snap = project.documents.snapshot(doc)?; + let own = if snap.large { Vec::new() } else { syntax.web_definitions(&snap.language, &snap.text) }; + let mut out: Vec = own + .into_iter() + .filter(|t| t.kind == CSS_VARIABLE) + .map(|t| CssVariable { + name: t.name, + value: t.container, + location: Location { path: snap.path.clone(), range: t.range, uri: None }, + }) + .collect(); + out.extend( + project + .index + .symbols_prefixed("--", CSS_VARIABLE, MAX_CSS_VARIABLES) + .into_iter() + .filter(|m| *m.path != *snap.path) + .map(|m| CssVariable { + name: m.name, + value: m.container, + location: Location { path: m.path, range: m.range, uri: None }, + }), + ); + Ok(out) + }) + .await +} + /// The file a Python import path (`a.b.c`, or `.x` relative to `from`) names; `None` when /// no project or content root has it. #[command(trust = always)] @@ -450,6 +497,7 @@ ide_command::domain!( recent_files, remove_recent, scope_files, + web_css_variables, web_goto, web_unresolved_references, ); diff --git a/crates/ide-index/src/lib.rs b/crates/ide-index/src/lib.rs index 837042fa2..46a84501f 100644 --- a/crates/ide-index/src/lib.rs +++ b/crates/ide-index/src/lib.rs @@ -391,6 +391,11 @@ impl ProjectIndex { matching::symbols_named_within(&self.shared.data.read(), name, kind, roots, limit) } + /// Symbols of `kind` whose name starts with `prefix`, by path then position. + pub fn symbols_prefixed(&self, prefix: &str, kind: SymbolKind, limit: usize) -> Vec { + matching::symbols_prefixed(&self.shared.data.read(), prefix, kind, limit) + } + /// Symbols named exactly one of `names` whose file is inside `root`, at most `limit` per name. pub fn symbols_named_any(&self, names: &[String], root: &Path, limit: usize) -> Vec { matching::symbols_named_any(&self.shared.data.read(), names, root, limit) diff --git a/crates/ide-index/src/matching.rs b/crates/ide-index/src/matching.rs index 7ba91e488..1a3810e3b 100644 --- a/crates/ide-index/src/matching.rs +++ b/crates/ide-index/src/matching.rs @@ -716,6 +716,34 @@ pub(crate) fn symbols_named_within( out } +/// Symbols of `kind` whose name starts with `prefix` (CSS custom properties: `--`), by path +/// then position. +pub(crate) fn symbols_prefixed(data: &IndexData, prefix: &str, kind: SymbolKind, limit: usize) -> Vec { + let syms = &data.syms; + let mut out: Vec = data + .files + .iter() + .flatten() + .flat_map(|e| { + (e.sym_start..e.sym_start + e.sym_len) + .filter(|&i| syms.kind(i) == kind && syms.name(i).starts_with(prefix)) + .map(move |i| SymbolMatch { + name: syms.name(i).to_string(), + kind, + container: syms.container(i).map(str::to_owned), + path: e.path.clone().into(), + rel_path: e.rel.to_string(), + range: syms.range(i), + score: 0, + indices: Vec::new(), + }) + }) + .collect(); + out.sort_by(|a, b| a.rel_path.cmp(&b.rel_path).then_with(|| a.range.cmp(&b.range))); + out.truncate(limit); + out +} + /// Symbols named exactly one of `names` whose file is inside `root`, at most `limit` per /// name, by path then position: one walk over the index for every name an agent reply shows. pub(crate) fn symbols_named_any(data: &IndexData, names: &[String], root: &Path, limit: usize) -> Vec { diff --git a/crates/ide-index/src/tests.rs b/crates/ide-index/src/tests.rs index 63aaf14df..cd067cd26 100644 --- a/crates/ide-index/src/tests.rs +++ b/crates/ide-index/src/tests.rs @@ -675,6 +675,37 @@ fn css_selectors_are_found_by_exact_name() { assert!(idx.symbols_named(".card", Some(SymbolKind::Field), 10).is_empty()); } +#[test] +fn css_custom_properties_are_listed_with_their_values_and_follow_edits() { + let f = Fixture::new(&[ + ("theme/light.css", ":root {\n --bg: #fff;\n --gap:\n 4px 8px;\n}\n"), + ("theme/dark.scss", "[data-theme=dark] { .x { --bg: rgb(0 0 0) !important; } }\n"), + ("theme/extra.less", ".a { --ring: var(--bg); color: red; }\n"), + ]); + let idx = f.open(); + let vars = |idx: &ProjectIndex| -> Vec<(String, String, Option)> { + idx.symbols_prefixed("--", SymbolKind::Variable, 100) + .into_iter() + .map(|m| (m.rel_path, m.name, m.container)) + .collect() + }; + assert_eq!( + vars(&idx), + vec![ + ("theme/dark.scss".into(), "--bg".into(), Some("rgb(0 0 0) !important".into())), + ("theme/extra.less".into(), "--ring".into(), Some("var(--bg)".into())), + ("theme/light.css".into(), "--bg".into(), Some("#fff".into())), + ("theme/light.css".into(), "--gap".into(), Some("4px 8px".into())), + ] + ); + let light = f.root.join("theme/light.css"); + std::fs::write(&light, ":root { --fg: #222; }\n").unwrap(); + idx.update(std::slice::from_ref(&light), &[]); + assert!(idx.wait_idle(WAIT)); + let names: Vec = vars(&idx).into_iter().filter(|v| v.0 == "theme/light.css").map(|v| v.1).collect(); + assert_eq!(names, vec!["--fg".to_string()]); +} + #[test] fn symbols_named_within_keeps_out_of_root_matches_from_evicting_a_root() { let f = Fixture::new(&[ diff --git a/crates/ide-language/src/syntax/web.rs b/crates/ide-language/src/syntax/web.rs index b7d817ecd..4d1ae2da5 100644 --- a/crates/ide-language/src/syntax/web.rs +++ b/crates/ide-language/src/syntax/web.rs @@ -4,7 +4,8 @@ //! //! Definitions (`.card`, `#main`, `--accent`) come from stylesheets and the //! `