-
-
Notifications
You must be signed in to change notification settings - Fork 473
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(grit): support Grit queries targeting CSS (#4444)
- Loading branch information
Showing
17 changed files
with
382 additions
and
58 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
use crate::{ | ||
grit_analysis_ext::GritAnalysisExt, grit_target_language::GritTargetParser, | ||
grit_tree::GritTargetTree, | ||
}; | ||
use biome_css_parser::{parse_css, CssParserOptions}; | ||
use biome_css_syntax::CssLanguage; | ||
use biome_parser::AnyParse; | ||
use grit_util::{AnalysisLogs, FileOrigin, Parser, SnippetTree}; | ||
use std::path::Path; | ||
|
||
pub struct GritCssParser; | ||
|
||
impl GritTargetParser for GritCssParser { | ||
fn from_cached_parse_result( | ||
&self, | ||
parse: &AnyParse, | ||
path: Option<&Path>, | ||
logs: &mut AnalysisLogs, | ||
) -> Option<GritTargetTree> { | ||
for diagnostic in parse.diagnostics() { | ||
logs.push(diagnostic.to_log(path)); | ||
} | ||
|
||
Some(GritTargetTree::new(parse.syntax::<CssLanguage>().into())) | ||
} | ||
|
||
fn parse_with_path(&self, source: &str, _path: &Path) -> AnyParse { | ||
parse_css(source, CssParserOptions::default()).into() | ||
} | ||
} | ||
|
||
impl Parser for GritCssParser { | ||
type Tree = GritTargetTree; | ||
|
||
fn parse_file( | ||
&mut self, | ||
body: &str, | ||
path: Option<&Path>, | ||
logs: &mut AnalysisLogs, | ||
_old_tree: FileOrigin<'_, GritTargetTree>, | ||
) -> Option<GritTargetTree> { | ||
let parse_result = parse_css(body, CssParserOptions::default().allow_metavariables()); | ||
|
||
for diagnostic in parse_result.diagnostics() { | ||
logs.push(diagnostic.to_log(path)); | ||
} | ||
|
||
Some(GritTargetTree::new(parse_result.syntax().into())) | ||
} | ||
|
||
fn parse_snippet( | ||
&mut self, | ||
prefix: &'static str, | ||
source: &str, | ||
postfix: &'static str, | ||
) -> SnippetTree<GritTargetTree> { | ||
let context = format!("{prefix}{source}{postfix}"); | ||
|
||
let len = if cfg!(target_arch = "wasm32") { | ||
|src: &str| src.chars().count() as u32 | ||
} else { | ||
|src: &str| src.len() as u32 | ||
}; | ||
|
||
let parse_result = parse_css(&context, CssParserOptions::default().allow_metavariables()); | ||
|
||
SnippetTree { | ||
tree: GritTargetTree::new(parse_result.syntax().into()), | ||
source: source.to_owned(), | ||
prefix, | ||
postfix, | ||
snippet_start: (len(prefix) + len(source) - len(source.trim_start())), | ||
snippet_end: (len(prefix) + len(source.trim_end())), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
120 changes: 120 additions & 0 deletions
120
crates/biome_grit_patterns/src/grit_target_language/css_target_language.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
mod constants; | ||
|
||
use super::{ | ||
normalize_quoted_string, DisregardedSlotCondition, GritTargetLanguageImpl, | ||
LeafEquivalenceClass, LeafNormalizer, | ||
}; | ||
use crate::{ | ||
grit_target_node::{GritTargetNode, GritTargetSyntaxKind}, | ||
CompileError, | ||
}; | ||
use biome_css_syntax::{CssLanguage, CssSyntaxKind}; | ||
use biome_rowan::{RawSyntaxKind, SyntaxKindSet}; | ||
use constants::DISREGARDED_SNIPPET_SLOTS; | ||
|
||
const COMMENT_KINDS: SyntaxKindSet<CssLanguage> = | ||
SyntaxKindSet::from_raw(RawSyntaxKind(CssSyntaxKind::COMMENT as u16)).union( | ||
SyntaxKindSet::from_raw(RawSyntaxKind(CssSyntaxKind::MULTILINE_COMMENT as u16)), | ||
); | ||
|
||
const EQUIVALENT_LEAF_NODES: &[&[LeafNormalizer]] = &[&[LeafNormalizer::new( | ||
GritTargetSyntaxKind::CssSyntaxKind(CssSyntaxKind::CSS_STRING_LITERAL), | ||
normalize_quoted_string, | ||
)]]; | ||
|
||
#[derive(Clone, Debug)] | ||
pub struct CssTargetLanguage; | ||
|
||
impl GritTargetLanguageImpl for CssTargetLanguage { | ||
type Kind = CssSyntaxKind; | ||
|
||
fn language_name(&self) -> &'static str { | ||
"CSS" | ||
} | ||
|
||
/// Returns the syntax kind for a node by name. | ||
/// | ||
/// For compatibility with existing Grit snippets (as well as the online | ||
/// Grit playground), node names should be aligned with TreeSitter's | ||
/// `ts_language_symbol_for_name()`. | ||
fn kind_by_name(&self, _node_name: &str) -> Option<CssSyntaxKind> { | ||
// TODO: See [super::JsTargetLanguage::kind_by_name()]. | ||
None | ||
} | ||
|
||
/// Returns the node name for a given syntax kind. | ||
/// | ||
/// For compatibility with existing Grit snippets (as well as the online | ||
/// Grit playground), node names should be aligned with TreeSitter's | ||
/// `ts_language_symbol_name()`. | ||
fn name_for_kind(&self, _kind: GritTargetSyntaxKind) -> &'static str { | ||
// TODO: See [super::JsTargetLanguage::name_for_kind()]. | ||
"(unknown node)" | ||
} | ||
|
||
/// Returns the slots with their names for the given node kind. | ||
/// | ||
/// For compatibility with existing Grit snippets (as well as the online | ||
/// Grit playground), node names should be aligned with TreeSitter's | ||
/// `ts_language_field_name_for_id()`. | ||
fn named_slots_for_kind(&self, _kind: GritTargetSyntaxKind) -> &'static [(&'static str, u32)] { | ||
// TODO: See [super::JsTargetLanguage::named_slots_for_kind()]. | ||
&[] | ||
} | ||
|
||
fn snippet_context_strings(&self) -> &[(&'static str, &'static str)] { | ||
&[ | ||
("", ""), | ||
("GRIT_BLOCK { ", " }"), | ||
("GRIT_BLOCK { GRIT_PROPERTY: ", " }"), | ||
] | ||
} | ||
|
||
fn is_comment_kind(kind: GritTargetSyntaxKind) -> bool { | ||
kind.as_css_kind() | ||
.map_or(false, |kind| COMMENT_KINDS.matches(kind)) | ||
} | ||
|
||
fn metavariable_kind() -> Self::Kind { | ||
CssSyntaxKind::CSS_METAVARIABLE | ||
} | ||
|
||
fn is_disregarded_snippet_field( | ||
&self, | ||
kind: GritTargetSyntaxKind, | ||
slot_index: u32, | ||
node: Option<GritTargetNode<'_>>, | ||
) -> bool { | ||
DISREGARDED_SNIPPET_SLOTS.iter().any( | ||
|(disregarded_kind, disregarded_slot_index, condition)| { | ||
if GritTargetSyntaxKind::from(*disregarded_kind) != kind | ||
|| *disregarded_slot_index != slot_index | ||
{ | ||
return false; | ||
} | ||
|
||
match condition { | ||
DisregardedSlotCondition::Always => true, | ||
DisregardedSlotCondition::OnlyIf(node_texts) => node_texts.iter().any(|text| { | ||
*text == node.as_ref().map(|node| node.text()).unwrap_or_default() | ||
}), | ||
} | ||
}, | ||
) | ||
} | ||
|
||
fn get_equivalence_class( | ||
&self, | ||
kind: GritTargetSyntaxKind, | ||
text: &str, | ||
) -> Result<Option<LeafEquivalenceClass>, CompileError> { | ||
if let Some(class) = EQUIVALENT_LEAF_NODES | ||
.iter() | ||
.find(|v| v.iter().any(|normalizer| normalizer.kind() == kind)) | ||
{ | ||
LeafEquivalenceClass::new(text, kind, class) | ||
} else { | ||
Ok(None) | ||
} | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
crates/biome_grit_patterns/src/grit_target_language/css_target_language/constants.rs
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.