Skip to content

Commit

Permalink
Create append/prepend_hardline
Browse files Browse the repository at this point in the history
  • Loading branch information
ErinvanderVeen committed Jun 14, 2022
1 parent cd118e9 commit c341d31
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
22 changes: 17 additions & 5 deletions languages/queries/json.scm
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand Down
17 changes: 15 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -156,7 +156,7 @@ fn atoms_to_doc<'a>(i: &mut usize, atoms: &'a Vec<Atom>) -> 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(),
Expand All @@ -171,11 +171,24 @@ fn resolve_capture(name: String, atoms: &mut Vec<Atom>, 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,
}
}
Expand Down

0 comments on commit c341d31

Please sign in to comment.