Skip to content

Commit

Permalink
wipwipwip
Browse files Browse the repository at this point in the history
  • Loading branch information
nbacquey committed Dec 2, 2024
1 parent 8ea857b commit 7f582f5
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
15 changes: 8 additions & 7 deletions topiary-core/src/comments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ fn is_comment(node: &Node) -> bool {
fn find_comments<'a>(node: Node<'a>, input: &str, comments: & mut Vec<AnchoredComment<'a>>) -> FormatterResult<()> {
if is_comment(&node) {
let commented = find_anchor(&node, input)?;
comments.push(AnchoredComment{comment: node, commented});
comments.push(AnchoredComment{comment: node.clone(), commented});
Ok(())
} else {
let mut walker = node.walk();
Expand Down Expand Up @@ -201,9 +201,9 @@ fn find_anchor<'tree>(
}
} else {
if let Some(anchor) = previous_non_comment(node.clone()) {
return Ok(Commented::CommentedBefore(anchor.into()));
return Ok(Commented::CommentedBefore((&anchor).into()));
} else if let Some(anchor) = next_non_comment(node.clone()) {
return Ok(Commented::CommentedAfter(anchor.into()));
return Ok(Commented::CommentedAfter((&anchor).into()));
} else {
return Err(FormatterError::Internal(
format!("Could find no anchor for comment {node:?}",),
Expand All @@ -227,13 +227,14 @@ pub struct SeparatedInput<'a>{
// TODO: store comments instead of discarding them
pub fn extract_comments<'a>(
tree: Tree,
input: &str,
input: &'a str,
grammar: &Language,
) -> FormatterResult<(Tree, String, Vec<AnchoredComment<'a>>)> {
let mut anchors: Vec<AnchoredComment> = Vec::new();
let mut new_input: String = input.to_string();
let mut new_tree: Tree = tree;
find_comments(new_tree.root_node(), input, &mut anchors)?;
let mut new_tree: Tree = tree.clone();
let root = tree.root_node().clone();
find_comments(root, input, &mut anchors)?;
anchors.sort_by_key(|AnchoredComment { comment, .. }| comment.start_byte());
let mut edits: Vec<InputEdit> = Vec::new();
// for each (comment, anchor) pair in reverse order, we:
Expand All @@ -259,7 +260,7 @@ pub fn extract_comments<'a>(
edits.push(edit);
// 3)
for following_index in index..anchors.len() {
let AnchoredComment { commented: mut following_anchor, .. } = &anchors[following_index];
let AnchoredComment { commented: following_anchor, .. } = &anchors[following_index];
following_anchor.subtract(&comment.into());
}
}
Expand Down
2 changes: 1 addition & 1 deletion topiary-core/src/tree_sitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ pub fn apply_query(
let (tree, grammar) = parse(input_content, grammar, tolerate_parsing_errors)?;

// Remove comments in a separate stream before applying queries
let (tree, new_input) = extract_comments(tree, input_content, grammar)?;
let (tree, new_input, _comments) = extract_comments(tree, input_content, grammar)?;
let source = new_input.as_bytes();
let root = tree.root_node();

Expand Down

0 comments on commit 7f582f5

Please sign in to comment.