Skip to content

Commit

Permalink
support deprecated tags
Browse files Browse the repository at this point in the history
  • Loading branch information
goulvenclech committed Dec 15, 2024
1 parent f7fe094 commit 63567c5
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 6 deletions.
20 changes: 15 additions & 5 deletions crates/csslsrs/src/features/document_symbols.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{
to_proto::{position, range},
PositionEncoding,
},
css_data::CssCustomData,
css_data::{CssCustomData, Status},
service::LanguageService,
};
use biome_css_syntax::{CssLanguage, CssSyntaxKind};
Expand All @@ -16,7 +16,7 @@ fn extract_document_symbols(
node: &SyntaxNode<CssLanguage>,
line_index: &LineIndex,
encoding: PositionEncoding,
_custom_data: &Vec<&CssCustomData>,
custom_data: &Vec<&CssCustomData>,
) -> Vec<DocumentSymbol> {
let mut symbols = Vec::new();
for child in node.children() {
Expand Down Expand Up @@ -53,7 +53,7 @@ fn extract_document_symbols(
SymbolKind::PROPERTY,
range(line_index, child.text_trimmed_range(), encoding).unwrap(),
range(line_index, token.text_trimmed_range(), encoding).unwrap(),
false,
is_property_deprecated(token.text_trimmed(), custom_data),
)
}),
// Handle CSS selectors, e.g. `.foo`, `#bar`, `div > span`, etc. Even when nested.
Expand All @@ -79,15 +79,15 @@ fn extract_document_symbols(
// If we have a symbol, search for nested children symbols.
if let Some(mut sym) = symbol {
let children_symbols =
extract_document_symbols(&child, line_index, encoding, _custom_data);
extract_document_symbols(&child, line_index, encoding, custom_data);
if !children_symbols.is_empty() {
sym.children = Some(children_symbols);
}
symbols.push(sym);
} else {
// Even if we don't have a symbol, we still want to search for nested symbols.
let nested_symbols =
extract_document_symbols(&child, line_index, encoding, _custom_data);
extract_document_symbols(&child, line_index, encoding, custom_data);
symbols.extend(nested_symbols);
}
}
Expand Down Expand Up @@ -116,6 +116,16 @@ fn create_symbol(
}
}

fn is_property_deprecated(property: &str, custom_data: &[&CssCustomData]) -> bool {
custom_data.iter().any(|data| {
data.properties.as_ref().map_or(false, |properties| {
properties
.iter()
.any(|prop| prop.name == property && matches!(prop.status, Some(Status::Obsolete)))
})
})
}

impl LanguageService {
/// Extracts document symbols from the given CSS document.
pub fn get_document_symbols(
Expand Down
67 changes: 66 additions & 1 deletion crates/csslsrs/tests/document_symbols.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![allow(deprecated)]
// TMP: deprecated is deprecated in Document Symbol, but we still need to intialize it to None, and hide the warning.
use csslsrs::service::LanguageService;
use lsp_types::{DocumentSymbol, Position, Range, SymbolKind, TextDocumentItem, Uri};
use lsp_types::{DocumentSymbol, Position, Range, SymbolKind, SymbolTag, TextDocumentItem, Uri};
use std::str::FromStr;

fn assert_document_symbols(
Expand Down Expand Up @@ -418,3 +418,68 @@ fn test_selector_with_properties() {
}],
);
}

#[test]
fn test_deprecated_property() {
let mut ls = LanguageService::default();

assert_document_symbols(
&mut ls,
".foo { box-align: center; }",
vec![DocumentSymbol {
name: ".foo".to_string(),
detail: None,
kind: SymbolKind::CLASS,
tags: None,
deprecated: None,
range: Range {
start: Position {
line: 0,
character: 0,
},
end: Position {
line: 0,
character: 27,
},
},
selection_range: Range {
start: Position {
line: 0,
character: 0,
},
end: Position {
line: 0,
character: 4,
},
},
children: Some(vec![DocumentSymbol {
name: "box-align".to_string(),
detail: None,
kind: SymbolKind::PROPERTY,
tags: Some(vec![SymbolTag::DEPRECATED]),
deprecated: None,
range: Range {
start: Position {
line: 0,
character: 7,
},
end: Position {
line: 0,
character: 24,
},
},
selection_range: Range {
start: Position {
line: 0,
character: 7,
},
end: Position {
line: 0,
character: 16,
},
},
children: None,
}]),
}],
);
}

0 comments on commit 63567c5

Please sign in to comment.