Skip to content

Commit

Permalink
CSS variables — why this don't work ?
Browse files Browse the repository at this point in the history
  • Loading branch information
goulvenclech committed Dec 17, 2024
1 parent 63567c5 commit 0c2d203
Show file tree
Hide file tree
Showing 4 changed files with 179 additions and 24 deletions.
34 changes: 24 additions & 10 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/csslsrs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ biome_css_syntax = "0.5.7"
biome_rowan = "0.5.7"
rustc-hash = "2.0.0"
palette = { git = "https://github.com/Ogeon/palette/", rev = "234309cdd2f96ac04f034f963c018b98065dcfd1", version = "0.7.6" }
pretty_assertions = "1.4.1"

[dev-dependencies]
criterion = { package = "codspeed-criterion-compat", version = "*" }
Expand Down
45 changes: 31 additions & 14 deletions crates/csslsrs/src/features/document_symbols.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,37 @@ fn extract_document_symbols(
)
})
}),
// Handle CSS properties, e.g. `color`, `font-size`, etc.
CssSyntaxKind::CSS_GENERIC_PROPERTY => child
.children()
.find(|c| c.kind() == CssSyntaxKind::CSS_IDENTIFIER)
.and_then(|c| c.first_token())
.map(|token| {
create_symbol(
token.text_trimmed().to_string(),
SymbolKind::PROPERTY,
range(line_index, child.text_trimmed_range(), encoding).unwrap(),
range(line_index, token.text_trimmed_range(), encoding).unwrap(),
is_property_deprecated(token.text_trimmed(), custom_data),
)
}),
CssSyntaxKind::CSS_GENERIC_PROPERTY => child.children().find_map(|c| {
eprintln!("WHY: {:?}", child.first_child_or_token());
eprintln!("WHY2: {:?}", c.kind());
match c.kind() {
// Handle CSS variables, e.g. `--foo`, `--bar`, etc.
CssSyntaxKind::CSS_DASHED_IDENTIFIER => c.first_token().map(|property_node| {
eprintln!("variable_node: {:?}", property_node.text_trimmed());
create_symbol(
property_node.text_trimmed().to_string(),
SymbolKind::VARIABLE,
range(line_index, child.text_trimmed_range(), encoding).unwrap(),
range(line_index, property_node.text_trimmed_range(), encoding)
.unwrap(),
false,
)
}),
// Handle CSS properties, e.g. `color`, `font-size`, etc.
CssSyntaxKind::CSS_IDENTIFIER => c.first_token().map(|property_node| {
eprintln!("property_node: {:?}", property_node.text_trimmed());
create_symbol(
property_node.text_trimmed().to_string(),
SymbolKind::PROPERTY,
range(line_index, child.text_trimmed_range(), encoding).unwrap(),
range(line_index, property_node.text_trimmed_range(), encoding)
.unwrap(),
is_property_deprecated(property_node.text_trimmed(), custom_data),
)
}),
_ => None,
}
}),
// Handle CSS selectors, e.g. `.foo`, `#bar`, `div > span`, etc. Even when nested.
CssSyntaxKind::CSS_QUALIFIED_RULE | CssSyntaxKind::CSS_NESTED_QUALIFIED_RULE => child
.children()
Expand Down
123 changes: 123 additions & 0 deletions crates/csslsrs/tests/document_symbols.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// 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, SymbolTag, TextDocumentItem, Uri};
use pretty_assertions::assert_eq;
use std::str::FromStr;

fn assert_document_symbols(
Expand Down Expand Up @@ -483,3 +484,125 @@ fn test_deprecated_property() {
}],
);
}

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

assert_document_symbols(
&mut ls,
":root {--color-primary: #333;}h1 {color: var(--color-primary);}",
vec![
DocumentSymbol {
name: ":root".to_string(),
detail: None,
kind: SymbolKind::CLASS,
tags: None,
deprecated: None,
range: Range {
start: Position {
line: 0,
character: 0,
},
end: Position {
line: 0,
character: 30,
},
},
selection_range: Range {
start: Position {
line: 0,
character: 0,
},
end: Position {
line: 0,
character: 5,
},
},
children: Some(vec![DocumentSymbol {
name: "--color-primary".to_string(),
detail: None,
kind: SymbolKind::VARIABLE,
tags: None,
deprecated: None,
range: Range {
start: Position {
line: 0,
character: 7,
},
end: Position {
line: 0,
character: 28,
},
},
selection_range: Range {
start: Position {
line: 0,
character: 7,
},
end: Position {
line: 0,
character: 22,
},
},
children: None,
}]),
},
DocumentSymbol {
name: "h1".to_string(),
detail: None,
kind: SymbolKind::CLASS,
tags: None,
deprecated: None,
range: Range {
start: Position {
line: 4,
character: 0,
},
end: Position {
line: 6,
character: 1,
},
},
selection_range: Range {
start: Position {
line: 4,
character: 0,
},
end: Position {
line: 4,
character: 2,
},
},
children: Some(vec![DocumentSymbol {
name: "color".to_string(),
detail: None,
kind: SymbolKind::PROPERTY,
tags: None,
deprecated: None,
range: Range {
start: Position {
line: 5,
character: 2,
},
end: Position {
line: 5,
character: 31,
},
},
selection_range: Range {
start: Position {
line: 5,
character: 2,
},
end: Position {
line: 5,
character: 7,
},
},
children: None,
}]),
},
],
);
}

0 comments on commit 0c2d203

Please sign in to comment.