Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to create switch between ES6-style arrow and normal functions? #29

Closed
ecosse3 opened this issue Aug 17, 2022 · 2 comments
Closed

How to create switch between ES6-style arrow and normal functions? #29

ecosse3 opened this issue Aug 17, 2022 · 2 comments
Labels
question Further information is requested

Comments

@ecosse3
Copy link

ecosse3 commented Aug 17, 2022

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?

@monaqa
Copy link
Owner

monaqa commented Aug 20, 2022

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,
    }
}

@monaqa monaqa added the question Further information is requested label Aug 28, 2022
@monaqa
Copy link
Owner

monaqa commented Oct 18, 2022

The problem is regarded as resolved and closed.

@monaqa monaqa closed this as completed Oct 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants