Skip to content

Commit

Permalink
Move binding_is_suppressed() unto Binding
Browse files Browse the repository at this point in the history
  • Loading branch information
arendjr committed Mar 28, 2024
1 parent 7509491 commit 979c8b7
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 35 deletions.
4 changes: 2 additions & 2 deletions crates/core/src/pattern/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use std::ops::Range as StdRange;
use super::compiler::MATCH_VAR;
use super::FileOwner;
use crate::intervals::{earliest_deadline_sort, get_top_level_intervals_in_range, Interval};
use crate::suppress::is_binding_suppressed;
use anyhow::{anyhow, Result};
use anyhow::{bail, Ok};
use im::{vector, Vector};
Expand Down Expand Up @@ -246,7 +245,8 @@ impl<'a> State<'a> {
if let ResolvedPattern::Binding(bindings) = value {
for binding in bindings.iter() {
bindings_count += 1;
if is_binding_suppressed(binding, lang, current_name)
if binding
.is_suppressed(lang, current_name)
.unwrap_or_default()
{
suppressed_count += 1;
Expand Down
62 changes: 32 additions & 30 deletions crates/core/src/suppress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,39 +8,41 @@ use crate::binding::Binding;
use crate::resolve;
use anyhow::Result;

pub(crate) fn is_binding_suppressed(
binding: &Binding,
lang: &impl Language,
current_name: Option<&str>,
) -> Result<bool> {
let (src, node) = match binding {
Binding::Node(src, node) => (src, node),
Binding::String(_, _) => return Ok(false),
Binding::List(src, node, _) => (src, node),
Binding::Empty(src, node, _) => (src, node),
Binding::FileName(_) => return Ok(false),
Binding::ConstantRef(_) => return Ok(false),
};
let target_range = node.range();
for n in
node.children(&mut node.walk())
.chain(ParentTraverse::new(TreeSitterParentCursor::new(
node.clone(),
)))
{
let mut cursor = n.walk();
let children = n.children(&mut cursor);
for c in children {
if !(lang.is_comment(c.kind_id()) || lang.is_comment_wrapper(&c)) {
continue;
}
if is_suppress_comment(&c, src, &target_range, current_name, lang)? {
return Ok(true);
impl<'a> Binding<'a> {
pub(crate) fn is_suppressed(
&self,
lang: &impl Language,
current_name: Option<&str>,
) -> Result<bool> {
let (src, node) = match self {
Self::Node(src, node) => (src, node),
Self::String(_, _) => return Ok(false),
Self::List(src, node, _) => (src, node),
Self::Empty(src, node, _) => (src, node),
Self::FileName(_) => return Ok(false),
Self::ConstantRef(_) => return Ok(false),
};
let target_range = node.range();
for n in
node.children(&mut node.walk())
.chain(ParentTraverse::new(TreeSitterParentCursor::new(
node.clone(),
)))
{
let mut cursor = n.walk();
let children = n.children(&mut cursor);
for c in children {
if !(lang.is_comment(c.kind_id()) || lang.is_comment_wrapper(&c)) {
continue;
}
if is_suppress_comment(&c, src, &target_range, current_name, lang)? {
return Ok(true);
}
}
}
}

Ok(false)
Ok(false)
}
}

fn is_suppress_comment(
Expand Down
5 changes: 2 additions & 3 deletions crates/core/src/text_unparser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use crate::binding::{linearize_binding, Binding};
use crate::pattern::resolved_pattern::CodeRange;
use crate::pattern::state::FileRegistry;
use crate::pattern::Effect;
use crate::suppress::is_binding_suppressed;
use anyhow::Result;
use im::Vector;
use marzano_language::target_language::TargetLanguage;
Expand All @@ -28,9 +27,9 @@ pub(crate) fn apply_effects<'a>(
current_name: Option<&str>,
logs: &mut AnalysisLogs,
) -> Result<(String, Option<Vec<Range<usize>>>)> {
let mut our_effects = Vec::new();
let mut our_effects = Vec::with_capacity(effects.len());
for effect in effects {
let disabled = is_binding_suppressed(&effect.binding, language, current_name)?;
let disabled = effect.binding.is_suppressed(language, current_name)?;
if !disabled {
our_effects.push(effect);
}
Expand Down

0 comments on commit 979c8b7

Please sign in to comment.