Skip to content

Insert a new element into each selected element.

Available tree-sitter parsers

This is the manual page of the ts_tree_insert() 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.

PackageVersionTitleMethod
tsjsonc0.0.0.9000Edit JSON Files.ts_tree_insert(<ts_tree_tsjsonc>)
tstoml0.0.0.9000Edit TOML files.

Usage

ts_tree_insert(tree, new, key, at, options, ...)

Arguments

tree

A ts_tree object.

new

The new element to insert.

The type of new depends on the parser and the method that implements the insertion. See details in the manual of the specific parser.

key

The key of the new element, if inserting into a keyed element.

For example a JSON(C) object or a TOML table are keyed elements.

at

The position to insert the new element at.

The interpretation of this argument depends on the method that implements the insertion. Typically the followings are supported:

  • 0 inserts at the beginning.

  • Inf inserts at the end.

  • A positive integer n inserts after the n-th element.

  • A character scalar inserts after the element with the given key, in keyed elements.

See the details in the manual of the specific parser.

options

A list of options for the insertion.

See details in the manual of the specific parser.

...

Extra arguments for methods.

Value

A ts_tree object representing the modified parse tree.

Details

It is not always possible to insert a new element into a selected element. For example in a JSONC document you can only insert a new element into an array or an object, but not into scalar elements. If the insertion is not possible, an error is raised.

 

json <- tsjsonc::ts_parse_jsonc("{ \"a\": true, \"b\": [1, 2, 3] }")
json |> ts_tree_select("a") |> ts_tree_insert("foo")
#>   Cannot insert into a 'true' JSON element. Can only insert into 'array' and 'ob
#> ject' elements and empty JSON documents.
If tree does not have a selection, the new element is inserted into at the top level.

 

json <- tsjsonc::ts_parse_jsonc("{ \"a\": true, \"b\": [1, 2, 3] }")
json |> ts_tree_insert(key = "c", new = "foo")
#> # jsonc (9 lines)
#> 1 | {
#> 2 |     "a": true,
#> 3 |     "b": [
#> 4 |         1,
#> 5 |         2,
#> 6 |         3
#> 7 |     ],
#> 8 |     "c": "foo"
#> 9 | }
If tree has an empty selection, then it is returned unchanged, i.e. no new element is inserted.

 

json <- tsjsonc::ts_parse_jsonc("{ \"a\": true, \"b\": [1, 2, 3] }")
json |> ts_tree_select("nonexistent") |> ts_tree_insert("foo")
#> # jsonc (1 line, 0 selected elements)
#> 1 | { "a": true, "b": [1, 2, 3] }

Examples

# Create a parse tree with tsjsonc -------------------------------------
tree <- tsjsonc::ts_parse_jsonc('{ "a": true, "b": [1, 2, 3] }')

tree |> ts_tree_select("b") |> ts_tree_insert(4, at = Inf)
#> # jsonc (6 lines)
#> 1 | { "a": true, "b": [
#> 2 |     1,
#> 3 |     2,
#> 4 |     3,
#> 5 |     4
#> 6 | ] }

# 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 |>
  ts_tree_select("servers", TRUE) |>
  ts_tree_insert(key = "active", TRUE)
#> # toml (3 lines)
#> 1 | [servers]
#> 2 |   alpha = { ip = "127.0.0.1", dc = "eqdc10", active = true }
#> 3 |   beta = { ip = "127.0.0.2", dc = "eqdc20", active = true }