Skip to content

Commit

Permalink
feat: open with last pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
llllvvuu committed Jul 18, 2023
1 parent e576851 commit cd83f3e
Showing 1 changed file with 26 additions and 5 deletions.
31 changes: 26 additions & 5 deletions lua/ssr.lua
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ local help_msg = " (Press ? for help)"
---@field status_extmark number
---@field search_extmark number
---@field replace_extmark number
---@field last_parser Parser
---@field last_node TSNode
---@field last_pattern string
---@field matches Match[]
local Ui = {}
Expand All @@ -56,23 +58,33 @@ function Ui:new()
return setmetatable({}, self)
end

function Ui:open()
function Ui:open(use_last_pattern)
if use_last_pattern and not (M._last_node and M._last_pattern) then
error("Error opening last ssr.nvim window: no last window")
end
self.origin_win = api.nvim_get_current_win()
self.origin_buf = api.nvim_win_get_buf(self.origin_win)
local lang = parsers.get_buf_lang(self.origin_buf)
if not parsers.has_parser(lang) then
return utils.notify("Treesitter parser not found, please try to install it with :TSInstall " .. lang)
end
local origin_node = utils.node_for_range(self.origin_buf, utils.get_selection(self.origin_win))
local parser = Parser:new(self.origin_buf, origin_node)
local origin_node = use_last_pattern and M._last_node
or utils.node_for_range(self.origin_buf, utils.get_selection(self.origin_win))
local parser = use_last_pattern and M._last_parser
or Parser:new(self.origin_buf, origin_node)
if not parser then
return
end
self.parser = parser
local placeholder = ts.get_node_text(origin_node, self.origin_buf)
M._last_node = origin_node
M._last_parser = parser
local placeholder = use_last_pattern and M._last_pattern
or ts.get_node_text(origin_node, self.origin_buf)
placeholder = "\n\n" .. placeholder .. "\n\n"
placeholder = vim.split(placeholder, "\n")
utils.remove_indent(placeholder, utils.get_indent(self.origin_buf, origin_node:start()))
if not use_last_pattern then
utils.remove_indent(placeholder, utils.get_indent(self.origin_buf, origin_node:start()))
end
self.ui_buf = api.nvim_create_buf(false, true)
api.nvim_buf_set_lines(self.ui_buf, 0, -1, true, placeholder)
self.ns = api.nvim_create_namespace ""
Expand All @@ -85,6 +97,9 @@ function Ui:open()
self.status_extmark = set_extmark(0, { { "[SSR]", "Comment" }, { help_msg, "Comment" } })
self.search_extmark = set_extmark(1, { { "SEARCH:", "String" } })
self.replace_extmark = set_extmark(#placeholder - 2, { { "REPLACE:", "String" } })
if use_last_pattern then
api.nvim_buf_set_lines(self.ui_buf, #placeholder - 1, -1, true, vim.split(M._last_template, "\n"))
end

local function map(key, func)
keymap.set("n", key, function()
Expand Down Expand Up @@ -352,6 +367,8 @@ function Ui:get_input()
local template_pos = api.nvim_buf_get_extmark_by_id(self.ui_buf, self.ns, self.replace_extmark, {})[1]
local pattern = vim.trim(table.concat(lines, "\n", pattern_pos + 2, template_pos))
local template = vim.trim(table.concat(lines, "\n", template_pos + 1, #lines))
M._last_pattern = pattern
M._last_template = template
return pattern, template
end

Expand All @@ -371,4 +388,8 @@ function M.open()
Ui:new():open()
end

function M.reopen()
Ui:new():open(true)
end

return M

0 comments on commit cd83f3e

Please sign in to comment.