diff --git a/languages/queries/json.scm b/languages/queries/json.scm index a4ef7ca8..93fcd4ce 100644 --- a/languages/queries/json.scm +++ b/languages/queries/json.scm @@ -22,7 +22,8 @@ ; Pairs without a trailing comma are last pair of an object. (object - ((pair) @append_hardline . "}") + (pair) @append_hardline + . ) ; Items in an array must have a newline after. See also the pairs above. @@ -31,16 +32,27 @@ ) (array - ((_) @append_hardline . "]") + (_) @append_hardline + . ) ; Children of arrays/objects should be indented -(array - _* @indented +(object + "{" @append_indent_start ) (object - _* @indented + (_) @append_indent_end + . +) + +(array + "[" @append_indent_start +) + +(array + (_) @append_indent_end + . ) ; By default our tool produces no spaces. We wish to add a space between the ":" and the following value. diff --git a/src/main.rs b/src/main.rs index 41d95e14..0a6ab8eb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,7 @@ use pretty::RcDoc; use std::collections::BTreeSet; use std::{error::Error, fs, io}; -use tree_sitter::{Node, Parser, Query, QueryCursor, QueryMatches}; +use tree_sitter::{Node, Parser, Query, QueryCursor, QueryMatch}; use tree_sitter_json::language; static TEST_FILE: &str = "tests/json.json"; @@ -156,7 +156,7 @@ fn atoms_to_doc<'a>(i: &mut usize, atoms: &'a Vec) -> RcDoc<'a, ()> { Atom::IndentEnd => unreachable!(), Atom::IndentStart => { *i = *i + 1; - atoms_to_doc(i, atoms).nest(1) + atoms_to_doc(i, atoms).nest(4) } Atom::Softline => RcDoc::softline(), Atom::Space => RcDoc::space(), @@ -171,11 +171,24 @@ fn resolve_capture(name: String, atoms: &mut Vec, node: Node) { match name.as_ref() { "append_hardline" => atoms_append(Atom::Hardline, node, atoms), "append_space" => atoms_append(Atom::Space, node, atoms), + "append_indent_start" => { + atoms_append(Atom::IndentStart, node, atoms) + } + "prepend_indent_end" => { + atoms_prepend(Atom::IndentEnd, node, atoms) + } + "prepend_indent_start" => { + atoms_prepend(Atom::IndentStart, node, atoms) + } + "append_indent_end" => { + atoms_append(Atom::IndentEnd, node, atoms) + } "indented" => { atoms_prepend(Atom::IndentStart, node, atoms); atoms_append(Atom::IndentEnd, node, atoms); } "append_comma" => atoms_append(Atom::Literal(",".to_string()), node, atoms), + // Skip over leafs _ => return, } }