Skip to content

Commit

Permalink
chore: refactor normalize_insert() (#87)
Browse files Browse the repository at this point in the history
  • Loading branch information
arendjr authored Mar 27, 2024
1 parent ce8e8f9 commit 00a1b2a
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 61 deletions.
3 changes: 1 addition & 2 deletions crates/core/src/pattern/accumulate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ use super::{
};
use super::{Effect, EffectKind};
use crate::context::Context;
use crate::smart_insert::normalize_insert;
use tree_sitter::Node;

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -203,7 +202,7 @@ impl Matcher for Accumulate {
.iter()
.map(|b| {
let is_first = !state.effects.iter().any(|e| e.binding == *b);
normalize_insert(b, &mut replacement, is_first, context.language())?;
replacement.normalize_insert(b, is_first, context.language())?;
Ok(Effect {
binding: b.clone(),
pattern: replacement.clone(),
Expand Down
23 changes: 21 additions & 2 deletions crates/core/src/pattern/resolved_pattern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ use crate::{
binding::{Binding, Constant},
context::Context,
pattern::{container::PatternOrResolved, patterns::Name},
smart_insert::normalize_insert,
};
use anyhow::{anyhow, bail, Result};
use im::{vector, Vector};
Expand Down Expand Up @@ -374,7 +373,7 @@ impl<'a> ResolvedPattern<'a> {
.iter()
.map(|b| {
let is_first = !effects.iter().any(|e| e.binding == *b);
normalize_insert(b, &mut with, is_first, language)?;
with.normalize_insert(b, is_first, language)?;
Ok(Effect {
binding: b.clone(),
pattern: with.clone(),
Expand Down Expand Up @@ -977,6 +976,26 @@ impl<'a> ResolvedPattern<'a> {
ResolvedPattern::Constant(constant) => Ok(constant.to_string().into()),
}
}

pub(crate) fn normalize_insert(
&mut self,
binding: &Binding<'a>,
is_first: bool,
language: &TargetLanguage,
) -> Result<()> {
let ResolvedPattern::Snippets(ref mut snippets) = self else {
return Ok(());
};
let Some(ResolvedSnippet::Text(text)) = snippets.front() else {
return Ok(());
};
if let Some(padding) = binding.get_insertion_padding(&text, is_first, language) {

Check failure on line 992 in crates/core/src/pattern/resolved_pattern.rs

View workflow job for this annotation

GitHub Actions / clippy

this expression creates a reference which is immediately dereferenced by the compiler

error: this expression creates a reference which is immediately dereferenced by the compiler --> crates/core/src/pattern/resolved_pattern.rs:992:62 | 992 | if let Some(padding) = binding.get_insertion_padding(&text, is_first, language) { | ^^^^^ help: change this to: `text` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow = note: `-D clippy::needless-borrow` implied by `-D warnings` = help: to override `-D warnings` add `#[allow(clippy::needless_borrow)]`
if padding.chars().next() != binding.text().chars().last() {
snippets.push_front(ResolvedSnippet::Text(padding.into()));
}
}
Ok(())
}
}

pub(crate) fn pattern_to_binding<'a>(
Expand Down
98 changes: 41 additions & 57 deletions crates/core/src/smart_insert.rs
Original file line number Diff line number Diff line change
@@ -1,69 +1,53 @@
use crate::{
binding::Binding,
pattern::resolved_pattern::{ResolvedPattern, ResolvedSnippet},
};
use anyhow::Result;
use crate::binding::Binding;
use itertools::Itertools;
use marzano_language::{language::Language, target_language::TargetLanguage};
use tree_sitter::Node;

pub(crate) fn normalize_insert<'a>(
binding: &Binding<'a>,
with: &mut ResolvedPattern<'a>,
is_first: bool,
language: &TargetLanguage,
) -> Result<()> {
let ResolvedPattern::Snippets(ref mut snippets) = with else {
return Ok(());
};
let Some(ResolvedSnippet::Text(text)) = snippets.front() else {
return Ok(());
};
let insert_padding = match binding {
Binding::List(src, node, field_id) => {
let mut cursor = node.walk();
let children = node
.children_by_field_id(*field_id, &mut cursor)
.collect_vec();
if children.is_empty() {
return Ok(());
}
calculate_padding(src, &children, text, is_first, language).or_else(|| {
if children.len() == 1 {
let child = children.first().unwrap();
let child_text = child.utf8_text(src.as_bytes()).ok()?;
if child.end_position().row() > child.start_position().row()
&& !child_text.ends_with('\n')
&& !text.starts_with('\n')
{
return Some("\n".to_string());
impl<'a> Binding<'a> {
/// Returns the padding to use for inserting the given text.
pub(crate) fn get_insertion_padding(
&self,
text: &str,
is_first: bool,
language: &TargetLanguage,
) -> Option<String> {
match self {
Self::List(src, node, field_id) => {
let mut cursor = node.walk();
let children = node
.children_by_field_id(*field_id, &mut cursor)
.collect_vec();
if children.is_empty() {
return None;
}
calculate_padding(src, &children, text, is_first, language).or_else(|| {
if children.len() == 1 {
let child = children.first().unwrap();
let child_text = child.utf8_text(src.as_bytes()).ok()?;
if child.end_position().row() > child.start_position().row()
&& !child_text.ends_with('\n')
&& !text.starts_with('\n')
{
return Some("\n".to_string());
}
}
None
})
}
Self::Node(src, node) => {
let node_text = node.utf8_text(src.as_bytes()).ok()?;
if language.is_statement(node.kind_id())
&& !node_text.ends_with('\n')
&& !text.starts_with('\n')
{
Some("\n".to_string())
} else {
None
}
None
})
}
Binding::Node(src, node) => {
let node_text = node.utf8_text(src.as_bytes())?;
if language.is_statement(node.kind_id())
&& !node_text.ends_with('\n')
&& !text.starts_with('\n')
{
Some("\n".to_string())
} else {
None
}
}
Binding::String(..)
| Binding::FileName(_)
| Binding::Empty(..)
| Binding::ConstantRef(_) => None,
};
if let Some(padding) = insert_padding {
if padding.chars().next() != binding.text().chars().last() {
snippets.push_front(ResolvedSnippet::Text(padding.into()));
Self::String(..) | Self::FileName(_) | Self::Empty(..) | Self::ConstantRef(_) => None,
}
}
Ok(())
}

fn calculate_padding(
Expand Down

0 comments on commit 00a1b2a

Please sign in to comment.