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
Show file tree
Hide file tree
Changes from 2 commits
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
108 changes: 108 additions & 0 deletions helix-core/src/syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2024,6 +2024,57 @@ impl<I: Iterator<Item = HighlightEvent>> Iterator for Merge<I> {
}
}

pub fn pretty_print_tree<W: fmt::Write>(fmt: &mut W, node: Node<'_>) -> fmt::Result {
pretty_print_tree_impl(fmt, node, true, None, 0)
}

fn pretty_print_tree_impl<W: fmt::Write>(
fmt: &mut W,
node: Node<'_>,
is_root: bool,
field_name: Option<&str>,
depth: usize,
) -> fmt::Result {
fn is_visible(node: Node<'_>) -> bool {
the-mikedavis marked this conversation as resolved.
Show resolved Hide resolved
node.is_missing()
|| (node.is_named() && node.language().node_kind_is_visible(node.kind_id()))
}

if is_visible(node) {
let indentation_columns = depth * 2;
write!(fmt, "{:indentation_columns$}", "")?;

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

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

for child_idx in 0..node.child_count() {
fisherdarling marked this conversation as resolved.
Show resolved Hide resolved
if let Some(child) = node.child(child_idx) {
if is_visible(child) {
fmt.write_char('\n')?;
}

pretty_print_tree_impl(
fmt,
child,
false,
node.field_name_for_child(child_idx as u32),
depth + 1,
)?;
}
}

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

Ok(())
}
#[cfg(test)]
mod test {
use super::*;
Expand Down Expand Up @@ -2195,6 +2246,63 @@ mod test {
);
}

#[track_caller]
fn assert_pretty_print(source: &str, expected: &str, start: usize, end: usize) {
let source = Rope::from_str(source);

let loader = Loader::new(Configuration { language: vec![] });
let language = get_language("Rust").unwrap();

let config = HighlightConfiguration::new(language, "", "", "").unwrap();
let syntax = Syntax::new(&source, Arc::new(config), Arc::new(loader));

let root = syntax
.tree()
.root_node()
.descendant_for_byte_range(start, end)
.unwrap();

let mut output = String::new();
pretty_print_tree(&mut output, root).unwrap();

assert_eq!(expected, output);
}

#[test]
fn test_pretty_print() {
let source = r#"/// Hello"#;
assert_pretty_print(source, "(line_comment)", 0, source.len());

// A large tree should be indented with fields:
let source = r#"fn main() {
println!("Hello, World!");
}"#;
assert_pretty_print(
source,
concat!(
"(function_item\n",
" name: (identifier)\n",
" parameters: (parameters)\n",
" body: (block\n",
" (expression_statement\n",
" (macro_invocation\n",
" macro: (identifier)\n",
" (token_tree\n",
" (string_literal))))))",
),
0,
source.len(),
);

// Selecting a token should print just that token:
let source = r#"fn main() {}"#;
assert_pretty_print(source, r#"("fn")"#, 0, 1);

// Error nodes are printed as errors:
let source = r#"}{"#;
assert_pretty_print(source, "(ERROR)", 0, source.len());
}

#[test]
fn test_load_runtime_file() {
// Test to make sure we can load some data from the runtime directory.
Expand Down
5 changes: 4 additions & 1 deletion helix-term/src/commands/typed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::ops::Deref;

use super::*;

use helix_core::syntax::pretty_print_tree;
use helix_view::{
apply_transaction,
editor::{Action, CloseError, ConfigEvent},
Expand Down Expand Up @@ -1473,7 +1474,9 @@ fn tree_sitter_subtree(
.root_node()
.descendant_for_byte_range(from, to)
{
let contents = format!("```tsq\n{}\n```", selected_node.to_sexp());
let mut contents = String::from("```tsq\n");
pretty_print_tree(&mut contents, selected_node)?;
the-mikedavis marked this conversation as resolved.
Show resolved Hide resolved
contents.push_str("\n```");

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