summaryrefslogtreecommitdiff
path: root/.config/nvim/lua/plugins/nvim-jdtls.lua
blob: 82713df17994dd4a13d31310dbc54d432b5d4fc6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
local java_packages_order = {
    'codes%.kyren',
    'org%.bukkit',
    { 'io%.papermc', 100 },
    'org%.jspecify',
    { 'org%.jetbrains', 50 },
    { 'net%.minecraft', 10 },
}

local function process_weights(list)
    local result = {}
    local current_weight = 0

    -- Process in reverse: from bottom to top
    for i = #list, 1, -1 do
        local entry = list[i]
        local pattern, weight

        -- Extract pattern and potential explicit weight
        if type(entry) == 'table' then
            pattern = entry[1]
            weight = entry[2]
        else
            pattern = entry
            weight = nil
        end

        -- Determine the weight to assign
        local final_weight
        if weight then
            if weight >= current_weight then
                final_weight = weight
                current_weight = weight + 1 -- Update counter for next iteration
            else
                local message = string.format(
                    'Weight %d for %s is smaller than previous, using current counter %d',
                    weight,
                    pattern,
                    current_weight
                )
                vim.defer_fn(function()
                    vim.notify(message, vim.log.levels.WARN)
                end, 5 * 1000)
                final_weight = weight
                current_weight = current_weight + 1 -- Increment counter normally
            end
        else
            final_weight = current_weight
            current_weight = current_weight + 1
        end

        result[pattern] = final_weight
    end

    return result
end

local java_package_weights = process_weights(java_packages_order)

return {
    'mfussenegger/nvim-jdtls',
    ft = { 'java' },
    dependencies = { 'Saghen/blink.cmp' },
    config = function()
        local setup_jdtls = function()
            local current_buf = vim.api.nvim_buf_get_name(0)
            local root_file = vim.fs.find({
                '.git',
                'build.gradle',
                'build.gradle.kts',
                'build.xml',
                'pom.xml',
                'settings.gradle',
                'settings.gradle.kts',
            }, { upward = true, path = current_buf })[1]

            local root_dir = root_file and vim.fs.dirname(root_file) or vim.fn.getcwd()
            local project_name = vim.fn.fnamemodify(root_dir, ':p:h:t')
            local workspace_dir = vim.fn.expand('~/.cache/jdtls-workspace/') .. project_name

            -- Safely mix standard blink capabilities with internal jdtls mappings
            local lsp_capabilities = require('blink.cmp').get_lsp_capabilities()

            local function get_action_weight(title)
                for pattern, weight in pairs(java_package_weights) do
                    if title:match(pattern) then
                        return weight
                    end
                end
                return 0 -- Neutral weight for untracked packages
            end

            local opts = {
                cmd = { 'jdtls', '-data', workspace_dir },
                root_dir = root_dir,
                capabilities = lsp_capabilities,

                -- Extended capabilities must live here, NOT in the root capabilities key
                init_options = {
                    extendedClientCapabilities = require('jdtls').extendedClientCapabilities,
                },

                settings = {
                    java = {
                        signatureHelp = { enabled = true },
                        compile = {
                            nullAnalysis = {
                                mode = 'enabled',
                                nullable = { 'org.jspecify.annotations.Nullable' },
                                nonnull = { 'org.jspecify.annotations.NonNull' },
                                nonnullbydefault = { 'org.jspecify.annotations.NullMarked' },
                            },
                        },
                        format = {
                            enabled = true,
                            settings = {
                                url = '/home/kyren/.config/nvim/eclipse-formatter.xml',
                                profile = 'Custom_Style',
                            },
                        },
                        errors = {
                            incompleteClasspath = 'warning',
                        },
                        completion = {
                            -- Stops jdtls from injecting variable placeholders when autocompleting
                            guessMethodArguments = 'off',

                            favoriteStaticMembers = {
                                'codes.kyren.kapi.Kapi.expect',
                                'codes.kyren.kapi.Kapi.panic',
                                'codes.kyren.kapi.Kapi.unreachable',
                            },

                            filteredTypes = {
                                'edu.umd.cs.findbugs.*',
                                'javax.annotation.*',
                                'org.checkerframework.*',
                                'org.eclipse.sisu.*',
                                -- 'org.jetbrains.annotations.*',
                                'org.sonatype.inject.*',
                                'java.awt.*',
                                'com.sun.*',
                                'sun.*',
                                'jdk.*',
                            },
                        },
                        jdt = {
                            ls = {
                                kotlinSupport = {
                                    enabled = true,
                                },
                            },
                        },
                    },
                },

                on_attach = function(client, bufnr)
                    if client._code_action_sorted then
                        return
                    end
                    client._code_action_sorted = true

                    -- Hook into the absolute lowest-level RPC pipe.
                    -- This catches 100% of traffic, bypassing any plugin API overrides.
                    local orig_rpc_request = client.rpc.request

                    client.rpc.request = function(method, params, handler, ...)
                        if method == 'textDocument/codeAction' then
                            local wrapped_handler = function(err, result, ctx, config)
                                if not err and result and type(result) == 'table' then
                                    -- Sort the raw array (Respected by native vim.ui.select)
                                    table.sort(result, function(a, b)
                                        local title_a = a.title or ''
                                        local title_b = b.title or ''
                                        local weight_a = get_action_weight(title_a)
                                        local weight_b = get_action_weight(title_b)

                                        if weight_a ~= weight_b then
                                            return weight_a > weight_b
                                        end
                                        return title_a < title_b
                                    end)
                                end

                                if handler then
                                    return handler(err, result, ctx, config)
                                end
                            end

                            return orig_rpc_request(method, params, wrapped_handler, ...)
                        end

                        return orig_rpc_request(method, params, handler, ...)
                    end
                end,
            }

            require('jdtls').start_or_attach(opts)
        end

        setup_jdtls()

        vim.api.nvim_create_autocmd('FileType', {
            pattern = 'java',
            callback = setup_jdtls,
        })
    end,
    java_package_weights = java_package_weights,
}