LunarVim icon indicating copy to clipboard operation
LunarVim copied to clipboard

Add auto update imports on file rename

Open benediktms opened this issue 3 years ago • 3 comments

Feature Description

Modern IDEs/Text Editors like VSCode have the ability to automatically update import statements where the language server supports this (e.g. for TypeScript and Rust). For large code bases where a import may be used dozens if not hundreds of times this is a real life saver. It would be great to have similar funcitonality in LunarVim where appropriate.

Describe the alternatives you have considered

Going into a seperate editor like VSCode to facilitate the refactor

Support information

No response

benediktms avatar Oct 06 '22 14:10 benediktms

we should already have this ( for example in rust) though we might have better support for them (after these PRs get merged)

for example, hitting in the following picture, selecting Error, and hitting <CR> would auto import the proper thing

Screenshot 2022-10-06 at 6 44 03 PM (2) Screenshot 2022-10-06 at 6 44 19 PM (2)

abzcoding avatar Oct 06 '22 15:10 abzcoding

Yes the auto import works, but I was thinking more about LunarVim detecting changes to the file name itself (following the Rust example, if a module/folder changes name). See pictures image

If I change the name of the routes folder I would like the module to detect these changes and update accordingly:

image

benediktms avatar Oct 06 '22 15:10 benediktms

That might be tricky since you are changing sth inside nvimtree plugin( and eventually the filesystem) and expect the language server to pick that up and do the right thing.

So I'm not sure if we should do that inside lunarvim or if we should use/create a new plugin to provide functionality

cc: @kylo252 @ChristianChiarulli

abzcoding avatar Oct 06 '22 15:10 abzcoding

This issue is stale because it has been open for 30 days with no activity.

github-actions[bot] avatar Nov 06 '22 02:11 github-actions[bot]

This issue was closed because it has been inactive for 14 days since being marked as stale.

github-actions[bot] avatar Nov 21 '22 02:11 github-actions[bot]

@benediktms Did you end up finding a workaround?

I wonder if it would it be possible to implement something like this in lunarvim? https://github.com/neovim/neovim/issues/20784#issuecomment-1288085253

Not as good as the suggestion above but at least allows a way to run a command to get the desired result

crushingCodes avatar Aug 17 '23 00:08 crushingCodes

I believe if you create this file file lua/lvim/lsp/providers/tsserver.lua with this content, you should be able to use :RenameFile command.


local function rename_file()
    local source_file, target_file

    vim.ui.input({
        prompt = "Source : ",
        completion = "file",
        default = vim.api.nvim_buf_get_name(0)
    },
        function(input)
            source_file = input
        end
    )
    vim.ui.input({
        prompt = "Target : ",
        completion = "file",
        default = source_file
    },
        function(input)
            target_file = input
        end
    )

    local params = {
        command = "_typescript.applyRenameFile",
        arguments = {
            {
                sourceUri = source_file,
                targetUri = target_file,
            },
        },
        title = ""
    }

    vim.lsp.util.rename(source_file, target_file)
    vim.lsp.buf.execute_command(params)
end


return {
    commands = {
        RenameFile = {
            rename_file,
            description = "Rename File"
        },
    }
}

Credit for the function: https://github.com/neovim/neovim/issues/20784#issuecomment-1288085253

olalonde avatar Sep 11 '23 06:09 olalonde

@olalonde thanks link led me to find antosha417/nvim-lsp-file-operations which works for this use case

crushingCodes avatar Sep 29 '23 06:09 crushingCodes