Format the selected elements of a tree sitter tree for printing
Source:R/ts-tree-format.R
ts_tree_format.Rd(Re)format the selected elements of the document represented by a tree-sitter tree, if the tree-sitter parser supports formatting.
Available tree-sitter parsers
This is the manual page of the ts_tree_format() S3 generic function.
Methods in parser packages may override this generic.
For the ones that do see the links to their manual pages in the table.
| Package | Version | Title | Method |
| tsjsonc | 0.0.0.9000 | Edit JSON Files. | ts_tree_format(<ts_tree_tsjsonc>) |
| tstoml | 0.0.0.9000 | Edit TOML files. | ts_tree_format(<ts_tree_tstoml>) |
Details
If tree does not have a selection, then the whole document is
formatted.
jsonc <- tsjsonc::ts_parse_jsonc("{ \"a\": [1,2,3] }")
jsonc |> ts_tree_format()#> # jsonc (7 lines) #> 1 | { #> 2 | "a": [ #> 3 | 1, #> 4 | 2, #> 5 | 3 #> 6 | ] #> 7 | }
tree has an empty selection, then it is returned unchanged.jsonc <- tsjsonc::ts_parse_jsonc("{ \"a\": [1,2,3] }")
jsonc |> ts_tree_select("c") |> ts_tree_format()#> # jsonc (1 line) #> 1 | { "a": [1,2,3] }
jsonc <- tsjsonc::ts_parse_jsonc("{ \"a\": [1,2,3] }") |>
ts_tree_format()jsonc |> ts_tree_select(TRUE) |>
ts_tree_format(options = list(format = "oneline"))#> # jsonc (3 lines) #> 1 | { #> 2 | "a": [ 1, 2, 3 ] #> 3 | }
See also
Methods in installed packages: ts_tree_format(<ts_tree_tsjsonc>) and ts_tree_format(<ts_tree_tstoml>).
Other ts_tree generics:
[[.ts_tree(),
[[<-.ts_tree(),
format.ts_tree(),
print.ts_tree(),
select-set,
ts_tree_ast(),
ts_tree_delete(),
ts_tree_dom(),
ts_tree_insert(),
ts_tree_new(),
ts_tree_query(),
ts_tree_select(),
ts_tree_sexpr(),
ts_tree_unserialize(),
ts_tree_update(),
ts_tree_write()
Examples
# Create a parse tree with tsjsonc -------------------------------------
tree <- tsjsonc::ts_parse_jsonc('{ "a":true, "b": [1,2,3] }')
tree
#> # jsonc (1 line)
#> 1 | { "a":true, "b": [1,2,3] }
# Format whole document
tree |> ts_tree_format()
#> # jsonc (8 lines)
#> 1 | {
#> 2 | "a": true,
#> 3 | "b": [
#> 4 | 1,
#> 5 | 2,
#> 6 | 3
#> 7 | ]
#> 8 | }
# Format each top element under the document node in one line
tree |> ts_tree_format() |>
ts_tree_select(TRUE) |>
ts_tree_format(options = list(format = "oneline"))
#> # jsonc (4 lines)
#> 1 | {
#> 2 | "a": true,
#> 3 | "b": [ 1, 2, 3 ]
#> 4 | }
# Create a parse tree with tstoml --------------------------------------
tree <- tstoml::ts_parse_toml(r"(
[servers]
alpha = { ip = "127.0.0.1", dc = "eqdc10" }
beta = { ip = "127.0.0.2", dc = "eqdc20" }
)")
tree
#> # toml (4 lines)
#> 1 |
#> 2 | [servers]
#> 3 | alpha = { ip = "127.0.0.1", dc = "eqdc10" }
#> 4 | beta = { ip = "127.0.0.2", dc = "eqdc20" }
tree |> ts_tree_format()
#> # toml (3 lines)
#> 1 | [servers]
#> 2 | alpha = { ip = "127.0.0.1", dc = "eqdc10" }
#> 3 | beta = { ip = "127.0.0.2", dc = "eqdc20" }