Skip to content

Commit

Permalink
feat(range): introduce motion1 and motion2 options
Browse files Browse the repository at this point in the history
Ref: #4
  • Loading branch information
gbprod committed Feb 2, 2022
1 parent a55ba9f commit 1af4d89
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 10 deletions.
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,28 @@ Default : `false`

Will require that there be word boundaries on each match (eg: `\<word\>` instead of `word`).

#### `range.motion1`

Default : `false`

This will use this motion for the first motion of range substitution.

eg. `lua require('substitute.range').operator({ motion1 = 'iW' })` will select
inner WORD as subject of substitution.

#### `range.motion2`

Default : `false`

This will use this motion for the second motion of range substitution.

eg. `lua require('substitute.range').operator({ motion2 = 'ap' })` will select
around paragraph as range of substitution.

You can combine `motion1` and `motion2` :
`lua require('substitute.range').operator({ motion1='iw', motion2 = 'ap' })`
will prepare substitution for inner word around paragraph.

### Integration

<details>
Expand Down
6 changes: 5 additions & 1 deletion lua/substitute/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ local function with_defaults(options)
prompt_current_text = options.range and options.range.prompt_current_text or false,
confirm = options.range and options.range.confirm or false,
complete_word = options.range and options.range.complete_word or false,
motion1 = options.range and options.range.motion1 or false,
motion2 = options.range and options.range.motion2 or false,
},
}
end
Expand All @@ -24,7 +26,9 @@ function config.get_range(overrides)
prefix = overrides.prefix or config.options.range.prefix,
prompt_current_text = overrides.prompt_current_text or config.options.range.prompt_current_text,
confirm = overrides.confirm or config.options.range.confirm,
complete_word = overrides.complete_word or config.options.complete_word,
complete_word = overrides.complete_word or config.options.range.complete_word,
motion1 = overrides.motion1 or config.options.range.motion1,
motion2 = overrides.motion2 or config.options.range.motion2,
}
end

Expand Down
21 changes: 12 additions & 9 deletions lua/substitute/range.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,22 @@ range.state = {
}

function range.operator(options)
range.state.overrides = options or {}
range.state.overrides = config.get_range(options or {})
vim.o.operatorfunc = "v:lua.require'substitute.range'.operator_callback"
vim.api.nvim_feedkeys("g@", "i", false)
vim.api.nvim_feedkeys(string.format("g@%s", range.state.overrides.motion1 or ""), "i", false)
end

function range.visual(options)
range.state.overrides = options or {}
vim.o.operatorfunc = "v:lua.require'substitute.range'.operator_callback"
vim.api.nvim_feedkeys("g@`>", "i", false)
options = config.get_range(options or {})
options.motion1 = "`>"
range.operator(options)
end

function range.word(options)
range.state.overrides = options or { complete_word = true }
vim.o.operatorfunc = "v:lua.require'substitute.range'.operator_callback"
vim.api.nvim_feedkeys("g@iw", "i", false)
options = config.get_range(options or {})
options.motion1 = "iw"
options.complete_word = true
range.operator(options)
end

local function create_match()
Expand Down Expand Up @@ -59,13 +60,15 @@ function range.operator_callback(vmode)
return
end

local c = config.get_range(range.state.overrides)

local line = vim.api.nvim_buf_get_lines(0, regions[1].start_row - 1, regions[1].end_row, true)
range.state.subject = string.sub(line[1], regions[1].start_col + 1, regions[1].end_col + 1)

create_match()

vim.o.operatorfunc = "v:lua.require'substitute.range'.selection_operator_callback"
vim.api.nvim_feedkeys("g@", "t", false)
vim.api.nvim_feedkeys(string.format("g@%s", c.motion2 or ""), "i", false)
end

local function create_replace_command()
Expand Down

0 comments on commit 1af4d89

Please sign in to comment.