Skip to content

Commit 8be95b8

Browse files
authored
Feat/scoped-edits (#16)
2 parents 51e84fe + 7f58936 commit 8be95b8

2 files changed

Lines changed: 52 additions & 2 deletions

File tree

lua/jumpy/prompt.lua

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
local M = {}
22

33
local context_tools = require("jumpy.context-tools")
4+
local utils = require("jumpy.utils")
45

56
local state = {
67
win = nil,
@@ -75,9 +76,16 @@ function M.open()
7576
return
7677
end
7778

79+
local mode = vim.api.nvim_get_mode().mode
80+
local is_scoped = mode == "v" or mode == "V" or mode == "\22"
81+
7882
state.source_buf = vim.api.nvim_get_current_buf()
7983
state.reprompt_hunk_idx = nil
80-
state.buf, state.win = create_float(" jumpy ")
84+
85+
state.visual_selection = is_scoped and utils.get_visual_selection(state.source_buf) or nil
86+
local title = is_scoped and " jumpy (scoped) " or " jumpy "
87+
88+
state.buf, state.win = create_float(title)
8189

8290
M._set_submit_keymap()
8391
M._setup_completions(state.buf)
@@ -173,7 +181,10 @@ function M._submit()
173181
end
174182

175183
local source_buf = state.source_buf
176-
local source_lines = vim.api.nvim_buf_get_lines(source_buf, 0, -1, false)
184+
185+
local source_lines = state.visual_selection and vim.split(state.visual_selection.text, "\n", { plain = true })
186+
or vim.api.nvim_buf_get_lines(source_buf, 0, -1, false)
187+
177188
local filetype = vim.bo[source_buf].filetype
178189
local reprompt_idx = state.reprompt_hunk_idx
179190

@@ -242,6 +253,13 @@ function M._submit()
242253
end
243254

244255
local hunks = diff.compute(source_lines, proposed_lines)
256+
if state.visual_selection then
257+
local offset = state.visual_selection.start_line - 1
258+
259+
for _, hunk in ipairs(hunks) do
260+
hunk.old_start = hunk.old_start + offset
261+
end
262+
end
245263

246264
if #hunks == 0 then
247265
vim.notify("jumpy: no changes proposed", vim.log.levels.INFO)

lua/jumpy/utils.lua

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
local M = {}
2+
3+
function M.get_visual_selection(bufnr)
4+
bufnr = bufnr or 0
5+
6+
local mode = vim.api.nvim_get_mode().mode
7+
8+
local start_pos = vim.fn.getpos("v")
9+
local end_pos = vim.fn.getpos(".")
10+
11+
local start_line = start_pos[2]
12+
local end_line = end_pos[2]
13+
14+
if start_line > end_line or (start_line == end_line) then
15+
start_line, end_line = end_line, start_line
16+
end
17+
18+
local lines = vim.api.nvim_buf_get_lines(bufnr, start_line - 1, end_line, false)
19+
20+
if #lines == 0 then
21+
return nil
22+
end
23+
24+
return {
25+
text = table.concat(lines, "\n"),
26+
start_line = start_line,
27+
end_line = end_line,
28+
mode = mode,
29+
}
30+
end
31+
32+
return M

0 commit comments

Comments
 (0)