Skip to content

Commit

Permalink
custom node formatting rather than
Browse files Browse the repository at this point in the history
  • Loading branch information
fisherdarling committed Oct 15, 2022
1 parent a3ed916 commit 97dd38e
Showing 1 changed file with 51 additions and 2 deletions.
53 changes: 51 additions & 2 deletions helix-term/src/commands/typed.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use std::ops::Deref;
use std::{fmt::Write, ops::Deref};

use super::*;

use helix_core::tree_sitter::TreeCursor;
use helix_view::{
apply_transaction,
editor::{Action, CloseError, ConfigEvent},
Expand Down Expand Up @@ -1473,7 +1474,55 @@ 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,
) {
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,
);
}
}

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

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

let contents = format!("```tsq\n{}\n```", sexpr);

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

0 comments on commit 97dd38e

Please sign in to comment.