Skip to content
Open
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
3 changes: 3 additions & 0 deletions .github/workflows/pr-lint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ on:
pull_request:
types:
- opened
```
<required_end>
- opened
- edited
- synchronize

Expand Down
3 changes: 3 additions & 0 deletions crates/cli/src/commands/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ use crate::{
#[derive(Args, Debug, Serialize)]
pub struct ListArgs {
/// List only at or above an enforcement level.
/// List only items targeting a specific language.
#[clap(long = "language", alias = "lang")]
pub lang: Option<String>,
#[clap(long = "level")]
pub level: Option<EnforcementLevel>,
/// List items from a specific source.
Expand Down
10 changes: 7 additions & 3 deletions crates/cli/src/commands/patterns_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,13 @@ impl Listable for ResolvedGritDefinition {
normal_tags.extend(more_tags);
normal_tags
}
}

pub(crate) async fn run_patterns_list(arg: ListArgs, parent: GlobalFormatFlags) -> Result<()> {
let (resolved, curr_repo) = resolve_from_flags_or_cwd(&parent, &arg.source).await?;
list_applyables(false, false, resolved, arg.level, &parent, curr_repo).await
}
let filtered_resolved: Vec<_> = if let Some(ref lang) = arg.lang {
resolved.into_iter().filter(|pattern| pattern.language().map_or(false, |l| l == lang)).collect()
} else {
resolved
};
list_applyables(false, false, filtered_resolved, arg.level, &parent, curr_repo).await
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,27 @@ import os
import openai
from flask import Flask, redirect, render_template, request, url_for

import pytest

def test_language_option_inline_pattern_apply():
# Set up the test environment and arguments
args = ["grit", "patterns", "list", "--lang", "python"]
expected_patterns = ["pattern1", "pattern2"] # Example expected patterns for Python

# Run the command with the language argument
result = run_command(args)

# Assert that the output contains the expected patterns for the specified language
for pattern in expected_patterns:
assert pattern in result.output

app = Flask(__name__)
openai.api_key = dotenv.fetch("OPENAI_API_KEY")
```

<required_end>

```
</required_end>
app = Flask(__name__)
openai.api_key = dotenv.fetch("OPENAI_API_KEY")
8 changes: 6 additions & 2 deletions crates/core/src/pattern_compiler/within_compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ impl NodeCompiler for WithinCompiler {
.child_by_field_name("pattern")
.ok_or_else(|| anyhow!("missing pattern of pattern within"))?;
let within = PatternCompiler::from_node(&within, context)?;
Ok(Within::new(within))
let until = node
.child_by_field_name("until")
.map(|n| PatternCompiler::from_node(&n, context))
.transpose()?;
Ok(Within::new(within, until))
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub trait ResolvedPattern<'a, Q: QueryContext>: Clone + Debug + PartialEq {

fn from_list_parts(parts: impl Iterator<Item = Self>) -> Self;

fn from_node_binding(node: Q::Node<'a>) -> Self {
fn from_node_binding(node: &Q::Node<'a>) -> Self {
Self::from_binding(Binding::from_node(node))
}

Expand Down
20 changes: 16 additions & 4 deletions crates/grit-pattern-matcher/src/pattern/within.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ use grit_util::{AnalysisLogs, AstNode};
#[derive(Debug, Clone)]
pub struct Within<Q: QueryContext> {
pub pattern: Pattern<Q>,
pub until: Option<Pattern<Q>>,
}

impl<Q: QueryContext> Within<Q> {
pub fn new(pattern: Pattern<Q>) -> Self {
Self { pattern }
pub fn new(pattern: Pattern<Q>, until: Option<Pattern<Q>>) -> Self {
Self { pattern, until }
}
}

Expand Down Expand Up @@ -52,7 +53,7 @@ impl<Q: QueryContext> Matcher<Q> for Within<Q> {
for n in node.ancestors() {
let state = cur_state.clone();
if self.pattern.execute(
&ResolvedPattern::from_node_binding(n),
&ResolvedPattern::from_node_binding(&n),
&mut cur_state,
context,
logs,
Expand All @@ -61,6 +62,17 @@ impl<Q: QueryContext> Matcher<Q> for Within<Q> {
} else {
cur_state = state;
}

if let Some(until) = &self.until {
if until.execute(
&ResolvedPattern::from_node_binding(&n),
&mut cur_state,
context,
logs,
)? {
break;
}
}
}
if did_match {
*init_state = cur_state;
Expand All @@ -69,4 +81,4 @@ impl<Q: QueryContext> Matcher<Q> for Within<Q> {
Ok(false)
}
}
}
}