diff --git a/README.md b/README.md index 43e82e6..1c0a29a 100644 --- a/README.md +++ b/README.md @@ -180,6 +180,73 @@ paredit.api.slurp_forwards() - **`move_to_next_element`** - **`move_to_prev_element`** +Form/element wrap api is in `paredit.wrap` module: + +- **`wrap_element_under_cursor`** - accepts prefix and suffix, returns wrapped `TSNode` +- **`wrap_enclosing_form_under_cursor`** - accepts prefix and suffix, returns wrapped `TSNode` + +Cursor api `paredit.cursor` + +- **`place_cursor`** - accepts `TSNode`, and following options: + - `placement` - enumeration `left_edge`,`inner_start`,`inner_end`,`right_edge` + - `mode` - currently only `insert` is supported, defaults to `normal` + +## API usage recipes + +### `vim-sexp` wrap form (head/tail) replication + +Require api module: +```lua +local paredit = require("nvim-paredit.api") +``` +Add following keybindings to config: +```lua +["w"] = { + function() + -- place cursor and set mode to `insert` + paredit.cursor.place_cursor( + -- wrap element under cursor with `( ` and `)` + paredit.wrap.wrap_element_under_cursor("( ", ")"), + -- cursor placement opts + { placement = "inner_start", mode = "insert" } + ) + end, + "Wrap element insert head", +}, + +["W"] = { + function() + paredit.cursor.place_cursor( + paredit.wrap.wrap_element_under_cursor("(", ")"), + { placement = "inner_end", mode = "insert" } + ) + end, + "Wrap element insert tail", +}, + +-- same as above but for enclosing form +["i"] = { + function() + paredit.cursor.place_cursor( + paredit.wrap.wrap_enclosing_form_under_cursor("( ", ")"), + { placement = "inner_start", mode = "insert" } + ) + end, + "Wrap form insert head", +}, + +["I"] = { + function() + paredit.cursor.place_cursor( + paredit.wrap.wrap_enclosing_form_under_cursor("(", ")"), + { placement = "inner_end", mode = "insert" } + ) + end, + "Wrap form insert tail", +} +``` +Same approach can be used for other `vim-sexp` keybindings (e.g. `e[`) with cursor placement or without. + ## Prior Art ### [vim-sexp](https://github.com/guns/vim-sexp)