Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pretty print tree-sitter-subtree expression #4295

Merged
Changes from 1 commit
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
54 changes: 52 additions & 2 deletions helix-term/src/commands/typed.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::ops::Deref;
use std::{fmt::Write, ops::Deref};

use super::*;

Expand Down Expand Up @@ -1473,7 +1473,57 @@ fn tree_sitter_subtree(
.root_node()
.descendant_for_byte_range(from, to)
{
let contents = format!("```tsq\n{}\n```", selected_node.to_sexp());
fn pretty_print_node(
node: Node<'_>,
sexpr: &mut String,
is_root: bool,
field_name: Option<&str>,
depth: usize,
) -> anyhow::Result<()> {
the-mikedavis marked this conversation as resolved.
Show resolved Hide resolved
fn is_visible(node: Node<'_>) -> bool {
node.is_missing()
|| (node.is_named() && node.language().node_kind_is_visible(node.kind_id()))
}

if is_visible(node) {
write!(sexpr, "{:depth$}", "")?;

if let Some(field_name) = field_name {
write!(sexpr, "{}: ", field_name)?;
}

write!(sexpr, "({}", node.kind())?;
} else if is_root {
write!(sexpr, "(\"{}\")", node.kind())?;
}

for child_idx in 0..node.child_count() {
if let Some(child) = node.child(child_idx) {
if is_visible(child) {
sexpr.push('\n');
}

pretty_print_node(
child,
sexpr,
false,
node.field_name_for_child(child_idx as u32),
depth + 2,
the-mikedavis marked this conversation as resolved.
Show resolved Hide resolved
)?;
}
}

if is_visible(node) {
write!(sexpr, ")")?;
}

Ok(())
}

let mut sexpr = String::new();
pretty_print_node(selected_node, &mut sexpr, true, None, 0)?;

let contents = format!("```tsq\n{}\n```", sexpr);
the-mikedavis marked this conversation as resolved.
Show resolved Hide resolved

let callback = async move {
let call: job::Callback =
Expand Down