This function is the heart of ts. To edit a tree-sitter tree, you first need to select the parts you want to delete or update.
Available tree-sitter parsers
This is the manual page of the ts_tree_select() 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_select(<ts_tree_tsjsonc>) |
| tstoml | 0.0.0.9000 | Edit TOML files. | ts_tree_select(<ts_tree_tstoml>) |
Arguments
- tree
A
ts_treeobject as returned byts_tree_new().- ...
Selection expressions, see details.
- refine
Logical, whether to refine the current selection or start a new selection.
Details
The selection process is iterative. Selection expressions (selectors) are applied one by one, and each selector selects nodes from the currently selected nodes. For each selector, it is applied individually to each currently selected node, and the results are concatenated.
The selection process starts from the root of the DOM tree, the document
node (see ts_tree_dom()), unless
refine = TRUE is set, in which case it starts from the current
selection.
See the various types of selection expressions below.
Selectors
All elements: TRUE
Selects all child nodes of the current nodes.
json <- tsjsonc::ts_parse_jsonc(
'{ "a": 1, "b": [10, 20, 30], "c": { "c1": true, "c2": null } }'
)
json |> ts_tree_select(c("b", "c"), TRUE)#> # jsonc (1 line, 5 selected elements) #> > 1 | { "a": 1, "b": [10, 20, 30], "c": { "c1": true, "c2": null } }
Specific keys: character vector
Selects child nodes with the given names from nodes with named children. If a node has no named children, it selects nothing from that node.
json <- tsjsonc::ts_parse_jsonc(
'{ "a": 1, "b": [10, 20, 30], "c": { "c1": true, "c2": null } }'
)
json |> ts_tree_select(c("a", "c"), c("c1"))#> # jsonc (1 line, 1 selected element) #> > 1 | { "a": 1, "b": [10, 20, 30], "c": { "c1": true, "c2": null } }
By position: integer vector
Selects child nodes by position. Positive indices count from the start, negative indices count from the end. Zero indices are not allowed.
For JSONC positional indices can be used both for arrays and objects. For other nodes nothing is selected.
json <- tsjsonc::ts_parse_jsonc(
'{ "a": 1, "b": [10, 20, 30], "c": { "c1": true, "c2": null } }'
)
json |> ts_tree_select(c("b", "c"), -1)#> # jsonc (1 line, 2 selected elements) #> > 1 | { "a": 1, "b": [10, 20, 30], "c": { "c1": true, "c2": null } }
Matching keys: regular expression
A character scalar named regex can be used to select child nodes
whose names match the given regular expression, from nodes with named
children. If a node has no named children, it selects nothing from that
node.
json <- tsjsonc::ts_parse_jsonc(
'{ "apple": 1, "almond": 2, "banana": 3, "cherry": 4 }'
)
json |> ts_tree_select(regex = "^a")#> # jsonc (1 line, 2 selected elements) #> > 1 | { "apple": 1, "almond": 2, "banana": 3, "cherry": 4 }
Tree sitter query matches
A character scalar named query can be used to select nodes matching
a tree-sitter query. See ts_tree_query()
for details on tree-sitter queries.
Instead of a character scalar this can also be a two-element list, where the first element is the query string and the second element is a character vector of capture names to select. In this case only nodes matching the given capture names will be selected.
ts_language_jsonc() for
details on the JSONC grammar.
This example selects all numbers in the JSON document.
json <- tsjsonc::ts_parse_jsonc(
'{ "a": 1, "b": [10, 20, 30], "c": { "c1": true, "c2": 100 } }'
)
json |> ts_tree_select(query = "(number) @number")#> # jsonc (1 line, 5 selected elements) #> > 1 | { "a": 1, "b": [10, 20, 30], "c": { "c1": true, "c2": 100 } }
Explicit node ids
You can use I(c(...)) to select nodes by their ids directly. This is
for advanced use cases only.
json <- tsjsonc::ts_parse_jsonc(
'{ "a": 1, "b": [10, 20, 30], "c": { "c1": true, "c2": null } }'
)
ts_tree_dom(json)#> document (1) #> └─object (2) #> ├─number (10) # a #> ├─array (18) # b #> │ ├─number (20) #> │ ├─number (22) #> │ └─number (24) #> └─object (33) # c #> ├─true (41) # c1 #> └─null (49) # c2
json |> ts_tree_select(I(18))#> # jsonc (1 line, 1 selected element) #> > 1 | { "a": 1, "b": [10, 20, 30], "c": { "c1": true, "c2": null } }
Refining selections
If the refine argument of
ts_tree_select() is TRUE, then
the selection starts from the already selected elements (all of them
simultanously), instead of starting from the document element.
json <- tsjsonc::ts_parse_jsonc(
'{ "a": 1, "b": [10, 20, 30], "c": { "c1": true, "c2": null } }'
)
json <- json |> ts_tree_select(c("b", "c"))json |> ts_tree_select(1:2)#> # jsonc (1 line, 2 selected elements) #> > 1 | { "a": 1, "b": [10, 20, 30], "c": { "c1": true, "c2": null } }
json |> ts_tree_select(1:2, refine = TRUE)#> # jsonc (1 line, 4 selected elements) #> > 1 | { "a": 1, "b": [10, 20, 30], "c": { "c1": true, "c2": null } }
The ts_tree_select<-() replacement function
The ts_tree_select<-() replacement
function works similarly to the combination of
ts_tree_select() and
ts_tree_update(), but it might be more
readable.
json <- tsjsonc::ts_parse_jsonc(
'{ "a": 1, "b": [10, 20, 30], "c": { "c1": true, "c2": null } }'
)
json#> # jsonc (1 line) #> 1 | { "a": 1, "b": [10, 20, 30], "c": { "c1": true, "c2": null } }
json |> ts_tree_select("b", 1)#> # jsonc (1 line, 1 selected element) #> > 1 | { "a": 1, "b": [10, 20, 30], "c": { "c1": true, "c2": null } }
ts_tree_select(json, "b", 1) <- 100
json#> # jsonc (1 line) #> 1 | { "a": 1, "b": [100, 20, 30], "c": { "c1": true, "c2": null } }
The [[ and [[<- operators
The [[ operator works similarly to the combination of
ts_tree_select() and
ts_tree_unserialize(), but it
might be more readable.
json <- tsjsonc::ts_parse_jsonc(
'{ "a": 1, "b": [10, 20, 30], "c": { "c1": true, "c2": null } }'
)
json |> ts_tree_select("b", 1)#> # jsonc (1 line, 1 selected element) #> > 1 | { "a": 1, "b": [10, 20, 30], "c": { "c1": true, "c2": null } }
json[[list("b", 1)]]#> [[1]] #> [1] 10 #>
The [[<- operator works similarly to the combination of
ts_tree_select() and
ts_tree_update(), (and also to the
replacement function ts_tree_select<-()),
but it might be more readable.
json <- tsjsonc::ts_parse_jsonc(
'{ "a": 1, "b": [10, 20, 30], "c": { "c1": true, "c2": null } }'
)
json#> # jsonc (1 line) #> 1 | { "a": 1, "b": [10, 20, 30], "c": { "c1": true, "c2": null } }
json |> ts_tree_select("b", 1)#> # jsonc (1 line, 1 selected element) #> > 1 | { "a": 1, "b": [10, 20, 30], "c": { "c1": true, "c2": null } }
json[[list("b", 1)]] <- 100
json#> # jsonc (1 line) #> 1 | { "a": 1, "b": [100, 20, 30], "c": { "c1": true, "c2": null } }
See also
Methods in installed packages: ts_tree_select(<ts_tree_tsjsonc>) and ts_tree_select(<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_format(),
ts_tree_insert(),
ts_tree_new(),
ts_tree_query(),
ts_tree_sexpr(),
ts_tree_unserialize(),
ts_tree_update(),
ts_tree_write()
Examples
# ----------------------------------------------------------------------
# Create a JSONC tree, needs the tsjsonc package
json <- ts_tree_new(
tsjsonc::ts_language_jsonc(),
text = '{ "a": 1, "b": 2, "c": { "d": 3, "e": 4 } }'
)
json |> ts_tree_select("c", "d")
#> # jsonc (1 line, 1 selected element)
#> > 1 | { "a": 1, "b": 2, "c": { "d": 3, "e": 4 } }
# ----------------------------------------------------------------------
# Create a TOML tree, needs the tstoml package
toml <- ts_tree_new(
tstoml::ts_language_toml(),
text = tstoml::toml_example_text()
)
toml |> ts_tree_select("servers", TRUE, "ip")
#> # toml (23 lines, 2 selected elements)
#> ...
#> 15 | [servers]
#> 16 |
#> 17 | [servers.alpha]
#> > 18 | ip = "10.0.0.1"
#> 19 | role = "frontend"
#> 20 |
#> 21 | [servers.beta]
#> > 22 | ip = "10.0.0.2"
#> 23 | role = "backend"