Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add rewritten_text function #429

Merged
merged 3 commits into from
Jul 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"));
morgante marked this conversation as resolved.
Show resolved Hide resolved
};
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
Loading