Skip to content

Commit

Permalink
add NodeExt trait and use .display_one_based() in some logs
Browse files Browse the repository at this point in the history
  • Loading branch information
evertedsphere committed Dec 20, 2023
1 parent 8cc9aa4 commit cee7be6
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 14 deletions.
60 changes: 46 additions & 14 deletions topiary/src/atom_collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,26 @@ use std::{

use tree_sitter_facade::Node;

use crate::{Atom, FormatterError, FormatterResult, ScopeCondition, ScopeInformation};
use crate::{
tree_sitter::Position, Atom, FormatterError, FormatterResult, ScopeCondition, ScopeInformation,
};

/// Extension trait for [`Node`] to allow for 1-based display in logs.
pub trait NodeExt {
/// Produce a textual representation with 1-based row/column indexes.
fn display_one_based(&self) -> String;
}

impl<'a> NodeExt for Node<'a> {
fn display_one_based(&self) -> String {
format!(
"{{Node {} {} - {}}}",
self.kind(),
Position::from(self.start_position()),
Position::from(self.end_position()),
)
}
}

/// A struct that holds sets of node IDs that have line breaks before or after them.
///
Expand Down Expand Up @@ -205,7 +224,10 @@ impl AtomCollection {
}
if let Some(parent_id) = self.parent_leaf_nodes.get(&node.id()) {
if *parent_id != node.id() {
log::warn!("Skipping because the match occurred below a leaf node: {node:?}");
log::warn!(
"Skipping because the match occurred below a leaf node: {}",
node.display_one_based()
);
return Ok(());
}
}
Expand Down Expand Up @@ -464,14 +486,14 @@ impl AtomCollection {
let parent_ids = [parent_ids, &[id]].concat();

log::debug!(
"CST node: {}{:?} - Named: {}",
"CST node: {}{} - Named: {}",
" ".repeat(level),
node,
node.display_one_based(),
node.is_named()
);

if node.end_byte() == node.start_byte() {
log::debug!("Skipping zero-byte node: {node:?}");
log::debug!("Skipping zero-byte node: {}", node.display_one_based());
} else if node.child_count() == 0
|| self.specified_leaf_nodes.contains(&node.id())
// We treat error nodes as leafs when `tolerate_parsing_errors` is set to true.
Expand Down Expand Up @@ -511,7 +533,10 @@ impl AtomCollection {
// TODO: Pre-populate these
let target_node = self.first_leaf(node);

log::debug!("Prepending {atom:?} to node {:?}", target_node,);
log::debug!(
"Prepending {atom:?} to node {}",
target_node.display_one_based()
);

self.prepend.entry(target_node.id()).or_default().push(atom);
}
Expand All @@ -528,7 +553,10 @@ impl AtomCollection {
let atom = self.wrap(atom, predicates);
let target_node = self.last_leaf(node);

log::debug!("Appending {atom:?} to node {:?}", target_node,);
log::debug!(
"Appending {atom:?} to node {}",
target_node.display_one_based()
);

self.append.entry(target_node.id()).or_default().push(atom);
}
Expand Down Expand Up @@ -560,18 +588,18 @@ impl AtomCollection {

if self.multi_line_nodes.contains(&parent_id) {
log::debug!(
"Expanding softline to hardline in node {:?} with parent {}: {:?}",
node,
"Expanding softline to hardline in node {} with parent {}: {}",
node.display_one_based(),
parent_id,
parent
parent.display_one_based()
);
Atom::Hardline
} else if spaced {
log::debug!(
"Expanding softline to space in node {:?} with parent {}: {:?}",
node,
"Expanding softline to space in node {} with parent {}: {}",
node.display_one_based(),
parent_id,
parent
parent.display_one_based()
);
Atom::Space
} else {
Expand Down Expand Up @@ -1030,7 +1058,11 @@ fn detect_multi_line_nodes(dfs_nodes: &[Node]) -> HashSet<usize> {
let end_line = node.end_position().row();

if end_line > start_line {
log::debug!("Multi-line node {}: {:?}", node.id(), node,);
log::debug!(
"Multi-line node {}: {}",
node.id(),
node.display_one_based()
);
return Some(node.id());
}

Expand Down
6 changes: 6 additions & 0 deletions topiary/src/tree_sitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ pub struct Position {
pub column: u32,
}

impl std::fmt::Display for Position {
fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
write!(f, "({},{})", self.row, self.column)
}
}

/// Topiary often needs both the tree-sitter `Query` and the original content
/// beloging to the file from which the query was parsed. This struct is a simple
/// convenience wrapper that combines the `Query` with its original string.
Expand Down

0 comments on commit cee7be6

Please sign in to comment.