|
| 1 | +use crate::{ |
| 2 | + converters::{ |
| 3 | + line_index::LineIndex, |
| 4 | + to_proto::{position, range}, |
| 5 | + PositionEncoding, |
| 6 | + }, |
| 7 | + css_data::{CssCustomData, Status}, |
| 8 | + service::LanguageService, |
| 9 | +}; |
| 10 | +use biome_css_syntax::{CssLanguage, CssSyntaxKind}; |
| 11 | +use biome_rowan::{AstNode, SyntaxNode, TextSize}; |
| 12 | +use lsp_types::{DocumentSymbol, Range, SymbolKind, SymbolTag, TextDocumentItem}; |
| 13 | + |
| 14 | +// From a given CSS node, recursively extract document symbols based on their kind in the CSS Syntax Tree. |
| 15 | +fn extract_document_symbols( |
| 16 | + node: &SyntaxNode<CssLanguage>, |
| 17 | + line_index: &LineIndex, |
| 18 | + encoding: PositionEncoding, |
| 19 | + custom_data: &Vec<&CssCustomData>, |
| 20 | +) -> Vec<DocumentSymbol> { |
| 21 | + let mut symbols = Vec::new(); |
| 22 | + for child in node.children() { |
| 23 | + let symbol: Option<DocumentSymbol> = match child.kind() { |
| 24 | + // Handle CSS at-rules, e.g. `@media`, `@keyframes`, etc. |
| 25 | + CssSyntaxKind::CSS_AT_RULE => child.first_child().and_then(|at_rule| { |
| 26 | + at_rule.first_token().map(|token| { |
| 27 | + create_symbol( |
| 28 | + String::from("@") + token.text_trimmed(), |
| 29 | + // For some at-rules, we want to include more details, e.g. `@media` should include the media query list. |
| 30 | + at_rule |
| 31 | + .first_child() |
| 32 | + .filter(|child| child.kind() == CssSyntaxKind::CSS_MEDIA_QUERY_LIST) |
| 33 | + .map(|child| child.text_trimmed().to_string()), |
| 34 | + SymbolKind::NAMESPACE, |
| 35 | + range(line_index, child.text_trimmed_range(), encoding).unwrap(), |
| 36 | + Range::new( |
| 37 | + position( |
| 38 | + line_index, |
| 39 | + // We need to include the `@` symbol in the selection range. |
| 40 | + token.text_trimmed_range().start() - TextSize::from(1), |
| 41 | + encoding, |
| 42 | + ) |
| 43 | + .unwrap(), |
| 44 | + position(line_index, token.text_trimmed_range().end(), encoding) |
| 45 | + .unwrap(), |
| 46 | + ), |
| 47 | + false, |
| 48 | + ) |
| 49 | + }) |
| 50 | + }), |
| 51 | + CssSyntaxKind::CSS_GENERIC_PROPERTY => child.children().find_map(|c| { |
| 52 | + match c.kind() { |
| 53 | + // Handle CSS variables, e.g. `--foo`, `--bar`, etc. |
| 54 | + CssSyntaxKind::CSS_DASHED_IDENTIFIER => c.first_token().map(|property_node| { |
| 55 | + create_symbol( |
| 56 | + property_node.text_trimmed().to_string(), |
| 57 | + None, |
| 58 | + SymbolKind::VARIABLE, |
| 59 | + range(line_index, child.text_trimmed_range(), encoding).unwrap(), |
| 60 | + range(line_index, property_node.text_trimmed_range(), encoding) |
| 61 | + .unwrap(), |
| 62 | + false, |
| 63 | + ) |
| 64 | + }), |
| 65 | + // Handle CSS properties, e.g. `color`, `font-size`, etc. |
| 66 | + CssSyntaxKind::CSS_IDENTIFIER => c.first_token().map(|property_node| { |
| 67 | + create_symbol( |
| 68 | + property_node.text_trimmed().to_string(), |
| 69 | + None, |
| 70 | + SymbolKind::PROPERTY, |
| 71 | + range(line_index, child.text_trimmed_range(), encoding).unwrap(), |
| 72 | + range(line_index, property_node.text_trimmed_range(), encoding) |
| 73 | + .unwrap(), |
| 74 | + is_property_deprecated(property_node.text_trimmed(), custom_data), |
| 75 | + ) |
| 76 | + }), |
| 77 | + _ => None, |
| 78 | + } |
| 79 | + }), |
| 80 | + // Handle CSS selectors, e.g. `.foo`, `#bar`, `div > span`, etc. Even when nested. |
| 81 | + CssSyntaxKind::CSS_QUALIFIED_RULE | CssSyntaxKind::CSS_NESTED_QUALIFIED_RULE => child |
| 82 | + .children() |
| 83 | + .find(|c| { |
| 84 | + c.kind() == CssSyntaxKind::CSS_SELECTOR_LIST |
| 85 | + || c.kind() == CssSyntaxKind::CSS_SUB_SELECTOR_LIST |
| 86 | + || c.kind() == CssSyntaxKind::CSS_RELATIVE_SELECTOR_LIST |
| 87 | + }) |
| 88 | + .map(|selector| { |
| 89 | + create_symbol( |
| 90 | + selector.text_trimmed().to_string(), |
| 91 | + None, |
| 92 | + SymbolKind::CLASS, |
| 93 | + range(line_index, child.text_trimmed_range(), encoding).unwrap(), |
| 94 | + range(line_index, selector.text_trimmed_range(), encoding).unwrap(), |
| 95 | + false, |
| 96 | + ) |
| 97 | + }), |
| 98 | + _ => None, |
| 99 | + }; |
| 100 | + |
| 101 | + // If we have a symbol, search for nested children symbols. |
| 102 | + if let Some(mut sym) = symbol { |
| 103 | + let children_symbols = |
| 104 | + extract_document_symbols(&child, line_index, encoding, custom_data); |
| 105 | + if !children_symbols.is_empty() { |
| 106 | + sym.children = Some(children_symbols); |
| 107 | + } |
| 108 | + symbols.push(sym); |
| 109 | + } else { |
| 110 | + // Even if we don't have a symbol, we still want to search for nested symbols. |
| 111 | + let nested_symbols = |
| 112 | + extract_document_symbols(&child, line_index, encoding, custom_data); |
| 113 | + symbols.extend(nested_symbols); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + symbols |
| 118 | +} |
| 119 | + |
| 120 | +// Create a LSP `DocumentSymbol` based on the given parameters. |
| 121 | +fn create_symbol( |
| 122 | + name: String, |
| 123 | + detail: Option<String>, |
| 124 | + kind: SymbolKind, |
| 125 | + range: lsp_types::Range, |
| 126 | + selection_range: lsp_types::Range, |
| 127 | + is_deprecated: bool, |
| 128 | +) -> DocumentSymbol { |
| 129 | + // TMP: deprecated is deprecated, but—for now—we still need to intialize it to None, and hide the warning. |
| 130 | + #[allow(deprecated)] |
| 131 | + DocumentSymbol { |
| 132 | + name, |
| 133 | + detail, |
| 134 | + kind, |
| 135 | + tags: is_deprecated.then(|| vec![SymbolTag::DEPRECATED]), |
| 136 | + deprecated: None, |
| 137 | + range, |
| 138 | + selection_range, |
| 139 | + children: None, |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +// Use the custom CSS data to determine if a given CSS property is deprecated. |
| 144 | +fn is_property_deprecated(property: &str, custom_data: &[&CssCustomData]) -> bool { |
| 145 | + custom_data.iter().any(|data| { |
| 146 | + data.properties.as_ref().map_or(false, |properties| { |
| 147 | + properties |
| 148 | + .iter() |
| 149 | + .any(|prop| prop.name == property && matches!(prop.status, Some(Status::Obsolete))) |
| 150 | + }) |
| 151 | + }) |
| 152 | +} |
| 153 | + |
| 154 | +impl LanguageService { |
| 155 | + /// Extracts document symbols from the given CSS document. |
| 156 | + pub fn get_document_symbols( |
| 157 | + &mut self, |
| 158 | + document: TextDocumentItem, |
| 159 | + ) -> Option<Vec<DocumentSymbol>> { |
| 160 | + let store_entry = self.store.get(&document.uri); |
| 161 | + |
| 162 | + match store_entry { |
| 163 | + Some(store_entry) => Some(extract_document_symbols( |
| 164 | + store_entry.css_tree.tree().syntax(), |
| 165 | + &store_entry.line_index, |
| 166 | + self.options.encoding, |
| 167 | + &self.get_css_custom_data(), |
| 168 | + )), |
| 169 | + None => None, |
| 170 | + } |
| 171 | + } |
| 172 | +} |
| 173 | + |
| 174 | +#[cfg(feature = "wasm")] |
| 175 | +mod wasm_bindings { |
| 176 | + use std::str::FromStr; |
| 177 | + |
| 178 | + use super::extract_document_symbols; |
| 179 | + use crate::service::wasm_bindings::WASMLanguageService; |
| 180 | + use biome_rowan::AstNode; |
| 181 | + use lsp_types::Uri; |
| 182 | + use serde_wasm_bindgen; |
| 183 | + use wasm_bindgen::prelude::*; |
| 184 | + |
| 185 | + #[wasm_bindgen(typescript_custom_section)] |
| 186 | + const TS_APPEND_CONTENT: &'static str = r#" |
| 187 | + export async function get_document_symbols(documentUri: string): import("vscode-languageserver-types").DocumentSymbol[];"#; |
| 188 | + |
| 189 | + #[wasm_bindgen] |
| 190 | + impl WASMLanguageService { |
| 191 | + #[wasm_bindgen(skip_typescript, js_name = getDocumentSymbols)] |
| 192 | + pub fn get_document_symbols(&self, document_uri: String) -> JsValue { |
| 193 | + let store_document = self.store.get(&Uri::from_str(&document_uri).unwrap()); |
| 194 | + |
| 195 | + let symbols = match store_document { |
| 196 | + Some(store_document) => extract_document_symbols( |
| 197 | + store_document.css_tree.tree().syntax(), |
| 198 | + &store_document.line_index, |
| 199 | + self.options.encoding, |
| 200 | + &self.get_css_custom_data(), |
| 201 | + ), |
| 202 | + None => Vec::new(), |
| 203 | + }; |
| 204 | + |
| 205 | + serde_wasm_bindgen::to_value(&symbols).unwrap() |
| 206 | + } |
| 207 | + } |
| 208 | +} |
0 commit comments