Skip to content

Commit

Permalink
Allow custom open quickfix commands for compile and build projects.
Browse files Browse the repository at this point in the history
  • Loading branch information
nsoufian committed Dec 28, 2024
1 parent ece818f commit a60e2bc
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 45 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"
{quickfix_open_cmd?} (string) Command to open quickfix list. Defaults to `copen`


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)
{quickfix_open_cmd?} (string) Command to open quickfix list. Defaults to `copen`


M.update_project_config() *jdtls.update_project_config*
Expand Down
96 changes: 51 additions & 45 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 quickfix_open_cmd? string Command to open quickfix list. Defaults to `copen`
local function on_build_result(quickfix_open_cmd)
quickfix_open_cmd = quickfix_open_cmd or 'copen'
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]))
vim.cmd(quickfix_open_cmd)
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"
---@param quickfix_open_cmd? string Command to open quickfix list. Defaults to `copen`
function M.compile(type)
request(0, 'java/buildWorkspace', type == 'full', on_build_result)
request(0, 'java/buildWorkspace', type == 'full', on_build_result(quickfix_open_cmd))
end


Expand Down Expand Up @@ -979,14 +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.quickfix_open_cmd))
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 quickfix_open_cmd? string Command to open quickfix list. Defaults to `copen`

--- Update the project configuration (from Gradle or Maven).
--- In a multi-module project this will only update the configuration of the
Expand Down

0 comments on commit a60e2bc

Please sign in to comment.