Skip to content

Commit

Permalink
Merge branch 'mfussenegger:master' into fix_test_traces
Browse files Browse the repository at this point in the history
  • Loading branch information
michalchabowski authored Jan 9, 2025
2 parents 94d8350 + baae618 commit 1c75de0
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 60 deletions.
2 changes: 2 additions & 0 deletions doc/jdtls.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ M.compile({type}) *jdtls.compile*
Parameters: ~
{type} (string|nil) |"full"
|"incremental"
{on_compile_result?} (fun(result: table[]): nil) Callback to be called when the compile result is received.


M.build_projects({opts}) *jdtls.build_projects*
Expand All @@ -41,6 +42,7 @@ JdtBuildProjectOpts *JdtBuildProjectOpts*
Fields: ~
{select_mode?} (JdtProjectSelectMode) Show prompt to select projects or select all. Defaults to "prompt"
{full_build?} (boolean) full rebuild or incremental build. Defaults to true (full build)
{on_compile_result?} (fun(result: table[]): nil) Callback to be called when the compile result is received.


M.update_project_config() *jdtls.update_project_config*
Expand Down
99 changes: 52 additions & 47 deletions lua/jdtls.lua
Original file line number Diff line number Diff line change
Expand Up @@ -877,52 +877,56 @@ function M._complete_compile()
return 'full\nincremental'
end

local function on_build_result(err, result, ctx)
local CompileWorkspaceStatus = {
FAILED = 0,
SUCCEED = 1,
WITHERROR = 2,
CANCELLED = 3,
}
assert(not err, 'Error trying to build project(s): ' .. vim.inspect(err))
if result == CompileWorkspaceStatus.SUCCEED then
vim.fn.setqflist({}, 'r', { title = 'jdtls'; items = {} })
print('Compile successful')
else
local project_config_errors = {}
local compile_errors = {}
local ns = vim.lsp.diagnostic.get_namespace(ctx.client_id)
for _, d in pairs(vim.diagnostic.get(nil, { namespace = ns })) do
local fname = api.nvim_buf_get_name(d.bufnr)
local stat = vim.loop.fs_stat(fname)
local items
if (vim.endswith(fname, 'build.gradle')
or vim.endswith(fname, 'pom.xml')
or (stat and stat.type == 'directory')) then
items = project_config_errors
elseif vim.fn.fnamemodify(fname, ':e') == 'java' then
items = compile_errors
--- @param on_compile_result? fun(result: table[]): nil Callback to be called when the compile result is received.
local function on_build_result(on_compile_result)
on_compile_result = on_compile_result or function() vim.cmd('copen') end
return function(err, result, ctx)
local CompileWorkspaceStatus = {
FAILED = 0,
SUCCEED = 1,
WITHERROR = 2,
CANCELLED = 3,
}
assert(not err, 'Error trying to build project(s): ' .. vim.inspect(err))
if result == CompileWorkspaceStatus.SUCCEED then
vim.fn.setqflist({}, 'r', { title = 'jdtls'; items = {} })
print('Compile successful')
else
local project_config_errors = {}
local compile_errors = {}
local ns = vim.lsp.diagnostic.get_namespace(ctx.client_id)
for _, d in pairs(vim.diagnostic.get(nil, { namespace = ns })) do
local fname = api.nvim_buf_get_name(d.bufnr)
local stat = vim.loop.fs_stat(fname)
local items
if (vim.endswith(fname, 'build.gradle')
or vim.endswith(fname, 'pom.xml')
or (stat and stat.type == 'directory')) then
items = project_config_errors
elseif vim.fn.fnamemodify(fname, ':e') == 'java' then
items = compile_errors
end
if d.severity == vim.diagnostic.severity.ERROR and items then
table.insert(items, d)
end
end
if d.severity == vim.diagnostic.severity.ERROR and items then
table.insert(items, d)
local items = #project_config_errors > 0 and project_config_errors or compile_errors
vim.fn.setqflist({}, 'r', { title = 'jdtls'; items = vim.diagnostic.toqflist(items) })
if #items > 0 then
local reverse_status = {
[0] = "FAILED",
[1] = "SUCCEEDED",
[2] = "WITHERROR",
[3] = "CANCELLED",
}
print(string.format('Compile error. (%s)', reverse_status[result]))
on_compile_result(items)
else
print("Compile error, but no error diagnostics available."
.. " Save all pending changes and try running compile again."
.. " If you used incremental mode, try a full rebuild.")
end
end
local items = #project_config_errors > 0 and project_config_errors or compile_errors
vim.fn.setqflist({}, 'r', { title = 'jdtls'; items = vim.diagnostic.toqflist(items) })
if #items > 0 then
local reverse_status = {
[0] = "FAILED",
[1] = "SUCCEEDED",
[2] = "WITHERROR",
[3] = "CANCELLED",
}
print(string.format('Compile error. (%s)', reverse_status[result]))
vim.cmd('copen')
else
print("Compile error, but no error diagnostics available."
.. " Save all pending changes and try running compile again."
.. " If you used incremental mode, try a full rebuild.")
end
end
end

Expand All @@ -932,8 +936,9 @@ end
---@param type string|nil
---|"full"
---|"incremental"
function M.compile(type)
request(0, 'java/buildWorkspace', type == 'full', on_build_result)
---@param on_compile_result? fun(result: table[]): nil Callback to be called when the compile result is received.
function M.compile(type, on_compile_result)
request(0, 'java/buildWorkspace', type == 'full', on_build_result(on_compile_result))
end


Expand Down Expand Up @@ -979,15 +984,15 @@ function M.build_projects(opts)
identifiers = vim.tbl_map(function(project) return { uri = project } end, selection),
isFullBuild = opts.full_build == nil and true or opts.full_build
}
request(bufnr, 'java/buildProjects', params, on_build_result)
request(bufnr, 'java/buildProjects', params, on_build_result(opts.on_compile_result))
end
end)()
end

---@class JdtBuildProjectOpts
---@field select_mode? JdtProjectSelectMode Show prompt to select projects or select all. Defaults to "prompt"
---@field full_build? boolean full rebuild or incremental build. Defaults to true (full build)

---@field on_compile_result? fun(result: table[]): nil Callback to be called when the compile result is received.
--- Update the project configuration (from Gradle or Maven).
--- In a multi-module project this will only update the configuration of the
--- module of the current buffer.
Expand Down
47 changes: 34 additions & 13 deletions lua/jdtls/ui.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
local M = {}


function M.pick_one_async(items, prompt, label_fn, cb)
if vim.ui then
return vim.ui.select(items, {
Expand All @@ -21,7 +20,7 @@ end
function M.pick_one(items, prompt, label_fn)
local choices = {prompt}
for i, item in ipairs(items) do
table.insert(choices, string.format('%d: %s', i, label_fn(item)))
table.insert(choices, string.format("%d: %s", i, label_fn(item)))
end
local choice = vim.fn.inputlist(choices)
if choice < 1 or choice > #items then
Expand All @@ -41,6 +40,24 @@ local function index_of(xs, term)
end


---@param index integer
---@param choices string[]
---@param items table[]
---@param selected table[]
local function mark_selected(index, choices, items, selected)
local choice = choices[index]
local item = items[index]
if string.find(choice, "*") == nil then
table.insert(selected, item)
choices[index] = choice .. " *"
else
choices[index] = string.gsub(choice, " %*$", "")
local idx = index_of(selected, item)
table.remove(selected, idx)
end
end


function M.pick_many(items, prompt, label_f, opts)
if not items or #items == 0 then
return {}
Expand Down Expand Up @@ -71,18 +88,22 @@ function M.pick_many(items, prompt, label_f, opts)
if answer == "" then
break
end
local range_start, range_end = answer:match("(%d+)%s*%-%s*(%d*)")
if range_start then
range_start = math.max(1, tonumber(range_start))
range_end = math.min(#items, tonumber(range_end) or #items)

if range_start > range_end then
range_start, range_end = range_end, range_start
end

local index = tonumber(answer)
if index ~= nil then
local choice = choices[index]
local item = items[index]
if string.find(choice, "*") == nil then
table.insert(selected, item)
choices[index] = choice .. " *"
else
choices[index] = string.gsub(choice, " %*$", "")
local idx = index_of(selected, item)
table.remove(selected, idx)
for i = range_start, range_end do
mark_selected(i, choices, items, selected)
end
else
local idx = tonumber(answer)
if idx then
mark_selected(idx, choices, items, selected)
end
end
end
Expand Down

0 comments on commit 1c75de0

Please sign in to comment.