dial.nvim
dial.nvim copied to clipboard
How to create switch between ES6-style arrow and normal functions?
Hi,
I want to migrate from switch.vim and vim-speeddating into this plugin but it's missing my favourite feature of switching between ES6-style arrow and normal functions like described here:
https://github.com/AndrewRadev/switch.vim#javascript
Is it possible to implement it via this plugin?
Conditionally possible. If the replacement range is a single line, it can be implemented using augend.user.
Simple implementation example:
local augend = require "dial.augend"
local common = require "dial.augend.common"
local js_func = {}
function js_func.find(line, cursor)
local range_func = common.find_pattern([[function%s*%b()%s*%b{}]])(line, cursor)
local range_arrow = common.find_pattern([[%b()%s*=>%s*%b{}]])(line, cursor)
if range_func == nil then
return range_arrow
end
if range_arrow == nil then
return range_func
end
if range_func.from < range_arrow.from then
return range_func
end
return range_arrow
end
function js_func.add(text, addend, cursor)
local _, _, params_func, body_func = text:find([[function%s*(%b())%s*(%b{})]])
if params_func ~= nil then
return { text = params_func .. " => " .. body_func, cursor = 1 }
end
local _, _, params_arrow, body_arrow = text:find([[(%b())%s*=>%s*(%b{})]])
if params_arrow ~= nil then
return { text = "function " .. params_arrow .. " " .. body_arrow, cursor = 1 }
end
end
local augend_js_func = augend.user.new(js_func)
require("dial.config").augends:register_group {
default = {
augend_js_func,
-- other augend configs,
}
}
The problem is regarded as resolved and closed.