mfussenegger / nvim-jdtls

Extensions for the built-in LSP support in Neovim for eclipse.jdt.ls
GNU General Public License v3.0
979 stars 62 forks source link

Extract to method missing while in visual selection #642

Closed asmodeus812 closed 3 months ago

asmodeus812 commented 3 months ago

LSP client configuration

                java = {
                    server = {
                        launchMode = "Standard",
                    },
                    trace = {
                        -- server = "message",
                        server = "off",
                    },
                    codeAction = {
                        sortMembers = {
                            avoidVolatileChanges = true,
                        },
                    },
                    cleanup = {
                        actionsOnSave = {
                            "invertEquals",
                            "addOverride",
                            "addDeprecated",
                            -- "addFinalModifier",
                            -- "qualifyMembers",
                            -- "qualifyStaticMembers",
                            -- "stringConcatToTextBlock",
                            -- "instanceofPatternMatch",
                            -- "lambdaExpression",
                            -- "switchExpression",
                        },
                    },
                    autobuild = {
                        enabled = false,
                    },
                    edit = {
                        validateAllOpenBuffersOnChanges = false,
                    },
                    references = {
                        includeDecompiledSources = true,
                    },
                    configuration = {
                        runtimes = lsp_runtime_locator(),
                        updateBuildConfiguration = "interactive",
                    },
                    contentProvider = {
                        preferred = "fernflower",
                    },
                    signatureHelp = {
                        enabled = true,
                        description = {
                            enabled = true,
                        },
                    },
                    referencesCodeLens = {
                        enabled = false,
                    },
                    implementationsCodeLens = {
                        enabled = false,
                    },
                    format = {
                        -- See https://raw.githubusercontent.com/google/styleguide/gh-pages/eclipse-java-google-style.xml for code style
                        enabled = true,
                        settings = lsp_format_settigns(),
                    },
                    saveActions = {
                        -- organizeImports = true,
                    },
                    sources = {
                        organizeImports = {
                            starThreshold = 9999,
                            staticStarThreshold = 9999,
                        },
                    },
                    filteredTypes = {
                        "java.awt.*",
                        "com.sun.*",
                        "jdk.*",
                        "sun.*",
                    },
                    project = {
                        resourceFilters = {
                            "build",
                            "node_modules",
                            "\\.git",
                            "\\.idea",
                            "\\.cache",
                            "\\.vscode",
                            "\\.settings",
                        },
                    },
                    completion = {
                        favoriteStaticMembers = {
                            "org.junit.jupiter.api.DynamicTest.*",
                            "org.junit.jupiter.api.Assertions.*",
                            "org.junit.jupiter.api.Assumptions.*",
                            "org.junit.jupiter.api.DynamicContainer.*",
                            "org.mockito.ArgumentMatchers.*",
                            "org.junit.Assert.*",
                            "org.junit.Assume.*",
                            "java.util.Objects.*",
                            "org.mockito.Mockito.*",
                            "org.mockito.Answers.*",
                        },
                        importOrder = {
                            "com",
                            "io",
                            "java",
                            "javax",
                            "org",
                        },
                    },
                    codeGeneration = {
                        tostring = {
                            skipNullValues = true,
                            listArrayContents = true,
                            template = "${object.className}{${member.name()}=${member.value}, ${otherMembers}}",
                        },
                        useBlocks = true,
                        hashCodeEquals = {
                            useInstanceof = true,
                            useJava7Objects = true,
                        },
                        generateComments = false,
                        insertLocation = true,
                    },
                    eclipse = {
                        downloadSources = true,
                    },
                    maven = {
                        downloadSources = true,
                        updateSnapshots = true,
                    },
                },

Eclipse.jdt.ls version

1.33

Steps to Reproduce

When executing vim.lsp.buf.code_actions()

public static void main(String[] args) {
        System.out.println(MESSAGE);
        System.out.println(MESSAGE);
        System.out.println(getMessage());
    }

Expected Result

Extract to method is visible in the actions

Actual Result

Extract to method is applicable for selected range. Optionally ask the user to enter the new method name

mfussenegger commented 3 months ago

Can't reproduce, works fine for me using a keymap like:

keymap.set({"n", "v"}, "<a-CR>", vim.lsp.buf.code_action, { buffer = args.buf })

recording

I think this wouldn't even require any of the client extensions of nvim-jdtls, but is possible with plain neovim core+jdtls.

asmodeus812 commented 3 months ago

Hm i see a completely different set of actions when i trigger this one with { context = { only = { "refactor" } }, apply = false } is that not a refactor or quickfix, why is it visible only with the non opts call version of code_action ?

mfussenegger commented 3 months ago

I also see the option with keymap.set("v", "<leader>r", "<Cmd>lua vim.lsp.buf.code_action { context = { only = {'refactor'}}}<CR>", { buffer = args.buf })

mfussenegger commented 3 months ago

One thing that came to mind: Maybe couble check the client capabilities. textDocument.codeAction.codeActionLiteralSupport.codeActionKind.valueSet should contain:

"",
"quickfix",
"refactor",
"refactor.extract",
"refactor.inline",
"refactor.rewrite",
"source",
"source.organizeImports",
"source.generate.toString",
"source.generate.hashCodeEquals"