Skip to content

Commit

Permalink
chore: move alternate_metavariable_kinds around (#314)
Browse files Browse the repository at this point in the history
  • Loading branch information
arendjr authored May 6, 2024
1 parent 306d7ca commit 3772e3c
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 37 deletions.
4 changes: 0 additions & 4 deletions crates/grit-util/src/language.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@ pub trait Language: Sized {

fn snippet_context_strings(&self) -> &[(&'static str, &'static str)];

fn alternate_metavariable_kinds(&self) -> &[&'static str] {
&[]
}

fn metavariable_prefix(&self) -> &'static str {
"$"
}
Expand Down
8 changes: 2 additions & 6 deletions crates/language/src/javascript.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
js_like::{
js_disregarded_field_values, js_like_get_statement_sorts, js_like_is_comment,
jslike_check_replacements, MarzanoJsLikeParser,
js_like_is_metavariable, jslike_check_replacements, MarzanoJsLikeParser,
},
language::{
check_disregarded_field_map, fields_for_nodes, kind_and_field_id_for_field_map, Field,
Expand Down Expand Up @@ -110,17 +110,13 @@ impl Language for JavaScript {
}

fn is_metavariable(&self, node: &NodeWithSource) -> bool {
MarzanoLanguage::is_metavariable_node(self, node)
js_like_is_metavariable(node, self, &["template_content"])
}

fn is_statement(&self, node: &NodeWithSource) -> bool {
self.statement_sorts.contains(&node.node.kind_id())
}

fn alternate_metavariable_kinds(&self) -> &[&'static str] {
&["template_content"]
}

// assumes trim doesn't do anything otherwise range is off
fn comment_text_range(&self, node: &NodeWithSource) -> Option<ByteRange> {
let content_text = node.text().ok()?;
Expand Down
13 changes: 13 additions & 0 deletions crates/language/src/js_like.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,19 @@ pub(crate) fn js_like_disregarded_field_values(
res
}

pub(crate) fn js_like_is_metavariable<'a>(
node: &NodeWithSource,
lang: &impl MarzanoLanguage<'a>,
alternate_metavariable_kinds: &[&str],
) -> bool {
node.node.is_named()
&& (node.node.kind_id() == lang.metavariable_sort()
|| (alternate_metavariable_kinds.contains(&node.node.kind().as_ref())
&& node
.text()
.is_ok_and(|t| lang.exact_replaced_variable_regex().is_match(&t))))
}

pub(crate) struct MarzanoJsLikeParser(MarzanoParser);

impl MarzanoJsLikeParser {
Expand Down
9 changes: 1 addition & 8 deletions crates/language/src/language.rs
Original file line number Diff line number Diff line change
Expand Up @@ -342,14 +342,7 @@ pub trait MarzanoLanguage<'a>: Language<Node<'a> = NodeWithSource<'a>> + NodeTyp
fn metavariable_sort(&self) -> SortId;

fn is_metavariable_node(&self, node: &NodeWithSource<'_>) -> bool {
node.node.is_named()
&& (node.node.kind_id() == self.metavariable_sort()
|| (self
.alternate_metavariable_kinds()
.contains(&node.node.kind().as_ref())
&& node
.text()
.is_ok_and(|t| self.exact_replaced_variable_regex().is_match(&t))))
node.node.is_named() && node.node.kind_id() == self.metavariable_sort()
}

fn get_equivalence_class(
Expand Down
6 changes: 0 additions & 6 deletions crates/language/src/target_language.rs
Original file line number Diff line number Diff line change
Expand Up @@ -400,12 +400,6 @@ macro_rules! generate_target_language {
}
}

fn alternate_metavariable_kinds(&self) -> &[&'static str] {
match self {
$(Self::$language(lang) => Language::alternate_metavariable_kinds(lang)),+
}
}

fn metavariable_prefix(&self) -> &'static str {
match self {
$(Self::$language(lang) => Language::metavariable_prefix(lang)),+
Expand Down
12 changes: 6 additions & 6 deletions crates/language/src/tsx.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
js_like::{
js_like_disregarded_field_values, js_like_get_statement_sorts, js_like_is_comment,
jslike_check_replacements, MarzanoJsLikeParser,
js_like_is_metavariable, jslike_check_replacements, MarzanoJsLikeParser,
},
language::{
check_disregarded_field_map, fields_for_nodes, kind_and_field_id_for_field_map, Field,
Expand Down Expand Up @@ -82,10 +82,6 @@ impl Language for Tsx {
"TSX"
}

fn alternate_metavariable_kinds(&self) -> &[&'static str] {
&["template_content", "template_literal_type_content"]
}

fn snippet_context_strings(&self) -> &[(&'static str, &'static str)] {
&[
("", ""),
Expand Down Expand Up @@ -114,7 +110,11 @@ impl Language for Tsx {
}

fn is_metavariable(&self, node: &NodeWithSource) -> bool {
MarzanoLanguage::is_metavariable_node(self, node)
js_like_is_metavariable(
node,
self,
&["template_content", "template_literal_type_content"],
)
}

fn is_statement(&self, node: &NodeWithSource) -> bool {
Expand Down
14 changes: 7 additions & 7 deletions crates/language/src/typescript.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::js_like::{
js_like_disregarded_field_values, js_like_get_statement_sorts, jslike_check_replacements,
MarzanoJsLikeParser,
js_like_disregarded_field_values, js_like_get_statement_sorts, js_like_is_metavariable,
jslike_check_replacements, MarzanoJsLikeParser,
};
use crate::language::{
check_disregarded_field_map, fields_for_nodes, kind_and_field_id_for_field_map, Field,
Expand Down Expand Up @@ -78,10 +78,6 @@ impl Language for TypeScript {
"TypeScript"
}

fn alternate_metavariable_kinds(&self) -> &[&'static str] {
&["template_content", "template_literal_type_content"]
}

fn snippet_context_strings(&self) -> &[(&'static str, &'static str)] {
&[
("", ""),
Expand Down Expand Up @@ -110,7 +106,11 @@ impl Language for TypeScript {
}

fn is_metavariable(&self, node: &NodeWithSource) -> bool {
MarzanoLanguage::is_metavariable_node(self, node)
js_like_is_metavariable(
node,
self,
&["template_content", "template_literal_type_content"],
)
}

fn is_statement(&self, node: &NodeWithSource) -> bool {
Expand Down

0 comments on commit 3772e3c

Please sign in to comment.