Skip to content

Commit

Permalink
feat(range): introduce the cursor position config
Browse files Browse the repository at this point in the history
Fixes: #69
  • Loading branch information
gbprod committed Nov 16, 2023
1 parent 2576e34 commit a2e7453
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,14 @@ Default : `false`

Will automatically apply the changes on the buffer. You will not have to validate the command line.

#### `range.cursor_position`

Default : `end`

This will set the cursor position in the command line at the end or at the beginning of the replacement text. Possible values are `end` and `start`.

eg. With `range.cursor_position = 'start'`, the cursor will be set here: `s/foo/|bar/g` but with `range.cursor_position = 'end'` it will be set to `s/foo/bar|/g`.

### 🤝 Integration

<details>
Expand Down
1 change: 1 addition & 0 deletions lua/substitute/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ function config.setup(options)
group_substituted_text = false,
suffix = "",
auto_apply = false,
cursor_position = "end",
},
exchange = {
motion = nil,
Expand Down
12 changes: 10 additions & 2 deletions lua/substitute/range.lua
Original file line number Diff line number Diff line change
Expand Up @@ -192,16 +192,24 @@ function range.create_replace_command()
local left = vim.api.nvim_replace_termcodes("<left>", true, false, true)
local cr = vim.api.nvim_replace_termcodes("<cr>", true, false, true)

local escaped_replacement = get_escaped_replacement(c)

local cmd = string.format(
-- vim.api.nvim_replace_termcodes(":%s%s/%s/%s/g%s%s%s", true, false, true),
":%s%s/%s/%s/g%s%s%s%s",
range.state.range,
c.prefix,
get_escaped_subject(c),
get_escaped_replacement(c),
escaped_replacement,
c.confirm and "c" or "",
c.suffix,
string.rep(left, 2 + (c.confirm and c.suffix:len() + 1 or c.suffix:len()), ""),
string.rep(
left,
2
+ (c.confirm and c.suffix:len() + 1 or c.suffix:len())
+ (c.cursor_position == "start" and escaped_replacement:len() or 0),
""
),
c.auto_apply and cr or ""
)

Expand Down

0 comments on commit a2e7453

Please sign in to comment.