Skip to content

Commit

Permalink
feat: add rewritten_text function (#429)
Browse files Browse the repository at this point in the history
  • Loading branch information
morgante committed Jul 20, 2024
1 parent 86b88bf commit 4141c07
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 6 deletions.
29 changes: 23 additions & 6 deletions crates/core/src/built_in_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ use grit_pattern_matcher::{
ResolvedSnippet, State,
},
};
use grit_util::{AnalysisLogs, Language};
use grit_util::{AnalysisLogs, CodeRange, Language};
use im::Vector;
use itertools::Itertools;
use rand::prelude::SliceRandom;
use rand::Rng;
use std::collections::BTreeMap;
use std::collections::{BTreeMap, HashMap};

// todo we can probably use a macro to generate a function that takes a vec and
// and calls the input function with the vec args unpacked.
Expand Down Expand Up @@ -144,7 +144,7 @@ impl BuiltIns {
BuiltInFunction::new("capitalize", vec!["string"], Box::new(capitalize_fn)),
BuiltInFunction::new("lowercase", vec!["string"], Box::new(lowercase_fn)),
BuiltInFunction::new("uppercase", vec!["string"], Box::new(uppercase_fn)),
BuiltInFunction::new("text", vec!["string"], Box::new(text_fn)),
BuiltInFunction::new("text", vec!["string", "linearize"], Box::new(text_fn)),
BuiltInFunction::new("trim", vec!["string", "trim_chars"], Box::new(trim_fn)),
BuiltInFunction::new("join", vec!["list", "separator"], Box::new(join_fn)),
BuiltInFunction::new("distinct", vec!["list"], Box::new(distinct_fn)),
Expand Down Expand Up @@ -245,10 +245,27 @@ fn text_fn<'a>(
) -> Result<MarzanoResolvedPattern<'a>> {
let args = MarzanoResolvedPattern::from_patterns(args, state, context, logs)?;

let s = match args.first() {
Some(Some(resolved_pattern)) => resolved_pattern.text(&state.files, context.language())?,
_ => return Err(anyhow!("text takes 1 argument")),
let Some(Some(resolved_pattern)) = args.first() else {
return Err(anyhow!("text takes 1 argument"));
};
let should_linearize = match args.get(1) {
Some(Some(resolved_pattern)) => resolved_pattern.is_truthy(state, context.language())?,
_ => false,
};
if !should_linearize {
let text = resolved_pattern.text(&state.files, context.language())?;
return Ok(ResolvedPattern::from_string(text.to_string()));
}
let mut memo: HashMap<CodeRange, Option<String>> = HashMap::new();
let effects: Vec<_> = state.effects.clone().into_iter().collect();
let s = resolved_pattern.linearized_text(
context.language(),
&effects,
&state.files,
&mut memo,
false,
logs,
)?;
Ok(ResolvedPattern::from_string(s.to_string()))
}

Expand Down
37 changes: 37 additions & 0 deletions crates/core/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6540,6 +6540,43 @@ fn even_more_linearized_test() {
.unwrap();
}

#[test]
fn linearized_text_fn() {
run_test_expected({
TestArgExpected {
pattern: r#"
|language js
|
|function go_to_the_zoo($a) js {
| return $a.text.replaceAll("baz", "bars").replaceAll("zoo", "alexandria")
|}
|
|program() where {
| $program <: contains bubble `foo($a)` => `zoo($a)`,
| $current_text = text($program, true),
| $final_text = go_to_the_zoo($current_text),
| $program => $final_text
|}
|"#
.trim_margin()
.unwrap(),
source: r#"
|foo(bar(baz))
|foo(blo, fly)
|"#
.trim_margin()
.unwrap(),
expected: r#"
|alexandria(bar(bars))
|alexandria(blo, fly)
|"#
.trim_margin()
.unwrap(),
}
})
.unwrap();
}

#[test]
fn matching_var_snippet_work() {
run_test_expected({
Expand Down

0 comments on commit 4141c07

Please sign in to comment.