Skip to content

Commit

Permalink
took a lot of debugging to find this
Browse files Browse the repository at this point in the history
  • Loading branch information
urbit-pilled committed Mar 25, 2024
1 parent d5c1201 commit 197575d
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 21 deletions.
1 change: 1 addition & 0 deletions crates/core/src/pattern/dynamic_snippet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ impl DynamicSnippet {
let source_string = raw_source
.replace("\\n", "\n")
.replace("\\$", "$")
.replace("\\^", "^")
.replace("\\`", "`")
.replace("\\\"", "\"")
.replace("\\\\", "\\");
Expand Down
18 changes: 11 additions & 7 deletions crates/core/src/pattern/patterns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ use super::{
r#where::Where, rewrite::Rewrite, some::Some, string_constant::StringConstant,
variable::Variable, within::Within, Context, Node, State,
};
use crate::binding::node_text;
use crate::pattern::register_variable;
use crate::pattern::string_constant::AstLeafNode;
use anyhow::{anyhow, bail, Result};
Expand Down Expand Up @@ -481,7 +482,7 @@ impl Pattern {
}
if node_types[sort as usize].is_empty() {
let content = node.utf8_text(text)?;
if node.named_child_count() == 0
if (node.named_child_count() == 0 || node.named_child_count() == 1)
&& lang.replaced_metavariable_regex().is_match(&content)
{
let regex = implicit_metavariable_regex(
Expand Down Expand Up @@ -538,14 +539,17 @@ impl Pattern {
)
})
.collect::<Result<Vec<Pattern>>>()?;
// assumes that non multiple field has exactly one element

// TODO check if Pattern is Dots, and error at compile time,
// dots only makes sense in a list.
if !field.multiple() {
let lang = lang.get_ts_language();
let field_name = lang.field_name_for_id(field_id).unwrap();
let message = format!("field {} was empty!", field_name);
return Ok((field_id, false, nodes_list.pop().expect(&message)));
if !field.multiple() {
if nodes_list.len() == 1 {
return Ok((field_id, false, nodes_list.pop().unwrap()));
}
let field_node = node.child_by_field_id(field_id).unwrap();
return Ok((field_id, false, Pattern::AstLeafNode(AstLeafNode::new(
field_node.kind_id(), node_text(str::from_utf8(text).unwrap(), &field_node), lang,
)?)));
}
if nodes_list.len() == 1
&& matches!(
Expand Down
2 changes: 1 addition & 1 deletion crates/core/src/pattern/variable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ fn register_variable_optional_range(
}
return Ok(Variable::new(GLOBAL_VARS_SCOPE_INDEX, *i));
}
let (name_map, scope_index) = if name.starts_with("$GLOBAL_") {
let (name_map, scope_index) = if name.starts_with("$GLOBAL_") || name.starts_with("^GLOBAL_") {
(global_vars, GLOBAL_VARS_SCOPE_INDEX)
} else {
(vars, scope_index)
Expand Down
17 changes: 4 additions & 13 deletions crates/language/src/language.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ pub trait Language {
"..." => GritMetaValue::Dots,
_ => {
let mut s = s.to_owned();
s.insert_str(0, GRIT_METAVARIABLE_PREFIX);
s.insert_str(0, self.metavariable_prefix());
GritMetaValue::Variable(s)
}
})
Expand Down Expand Up @@ -414,11 +414,10 @@ impl SnippetTree {
}
// sanity check to avoid infinite loop
if snippet_root.id() == id {
if snippet_root
if !snippet_root
.utf8_text(self.context.as_bytes())
.unwrap()
.trim()
!= self.snippet.trim()
.trim().contains(self.snippet.trim())
{
return None;
}
Expand Down Expand Up @@ -493,7 +492,7 @@ pub fn fields_for_nodes(language: &TSLanguage, types: &str) -> Vec<Vec<Field>> {
#[cfg(test)]
mod tests {
use super::nodes_from_indices;
use crate::{language::Language, php::Php, tsx::Tsx};
use crate::{language::Language, tsx::Tsx};
use tree_sitter::Parser;
use trim_margin::MarginTrimmable;

Expand Down Expand Up @@ -536,14 +535,6 @@ mod tests {
assert_eq!(subbed, "µfoo$('µbar')");
}

#[test]
fn test_php_substitute_variable() {
let snippet = "$foo$('$bar')";
let lang = Php::new(None);
let subbed = lang.substitute_metavariable_prefix(snippet);
assert_eq!(subbed, "µ[foo]$('µ[bar]')");
}

#[test]
fn test_substitute_variable_template() {
let snippet = r#"
Expand Down

0 comments on commit 197575d

Please sign in to comment.