Skip to content

Commit

Permalink
Expose java/buildProjects via jdtls.build_projects
Browse files Browse the repository at this point in the history
  • Loading branch information
mfussenegger committed Jun 30, 2022
1 parent 03f64f1 commit 703268d
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 62 deletions.
37 changes: 33 additions & 4 deletions doc/jdtls.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,51 @@ M.compile({type}) *jdtls.compile*
{type} (nil|"full"|"incremental")


M.build_projects({opts}) *jdtls.build_projects*
Trigger a rebuild of one or more projects.



Parameters: ~
{opts} (JdtBuildProjectOpts|nil) optional configuration options


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)


M.update_project_config() *jdtls.update_project_config*
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.


M.update_projects_config({mode}) *jdtls.update_projects_config*
M.update_projects_config({opts}) *jdtls.update_projects_config*
Process changes made to the Gradle or Maven configuration of one or more projects.
Requires eclipse.jdt.ls >= 1.13.0



Parameters: ~
{mode} (nil|"prompt"|"all") Whether to prompt for projects to update or update all. Defaults to "prompt"
{opts} (JdtUpdateProjectsOpts|nil) configuration options


JdtUpdateProjectsOpts *JdtUpdateProjectsOpts*


Fields: ~
{select_mode} (JdtProjectSelectMode|nil) show prompt to select projects or select all. Defaults to "prompt"


JdtProjectSelectMode *JdtProjectSelectMode*


Type: ~
"all"|"prompt"|nil


M.javap() *jdtls.javap*
Expand Down Expand Up @@ -72,8 +103,6 @@ M.jol({mode}, {classname}) *jdtls.jol*
lua require('jdtls').jol(nil, "java.util.ImmutableCollections$List12")
```



Parameters: ~
{mode} (nil|"estimates"|"footprint"|"externals"|"internals")
{classname} (string|nil) fully qualified class name. Defaults to the current class.
Expand Down
150 changes: 92 additions & 58 deletions lua/jdtls.lua
Original file line number Diff line number Diff line change
Expand Up @@ -643,77 +643,63 @@ function M._complete_compile()
return 'full\nincremental'
end


--- Compile/build the Java workspace.
--- If there are compile errors they'll be shown in the quickfix list.
---
---@param type nil|"full"|"incremental"
function M.compile(type)
local function on_build_result(err, result)
local CompileWorkspaceStatus = {
FAILED = 0,
SUCCEED = 1,
WITHERROR = 2,
CANCELLED = 3,
}
request(0, 'java/buildWorkspace', type == 'full', function(err, result)
assert(not err, 'Error on `java/buildWorkspace`: ' .. vim.inspect(err))
if result == CompileWorkspaceStatus.SUCCEED then
vim.fn.setqflist({}, 'r', { title = 'jdtls'; items = {} })
print('Compile successfull')
else
vim.tbl_add_reverse_lookup(CompileWorkspaceStatus)
local project_config_errors = {}
local compile_errors = {}
for _, d in pairs(vim.diagnostic.get(nil)) 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
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 successfull')
else
vim.tbl_add_reverse_lookup(CompileWorkspaceStatus)
local project_config_errors = {}
local compile_errors = {}
for _, d in pairs(vim.diagnostic.get(nil)) 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
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
print(string.format('Compile error. (%s)', CompileWorkspaceStatus[result]))
vim.cmd('copen')
else
print('Compile error, but no error diagnostics available. Try running compile again.')
if d.severity == vim.diagnostic.severity.ERROR and items then
table.insert(items, d)
end
end
end)
end

--- 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.
function M.update_project_config()
local params = { uri = vim.uri_from_bufnr(0) }
request(0, 'java/projectConfigurationUpdate', params, function(err)
if err then
print('Could not update project configuration: ' .. err.message)
return
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
print(string.format('Compile error. (%s)', CompileWorkspaceStatus[result]))
vim.cmd('copen')
else
print('Compile error, but no error diagnostics available. Try running compile again.')
end
end)
end
end

--- Process changes made to the Gradle or Maven configuration of one or more projects.
--- Requires eclipse.jdt.ls >= 1.13.0

--- Compile/build the Java workspace.
--- If there are compile errors they'll be shown in the quickfix list.
---
---@param mode nil|"prompt"|"all" Whether to prompt for projects to update or update all. Defaults to "prompt"
function M.update_projects_config(mode)
mode = mode or "pick"
local bufnr = api.nvim_get_current_buf()
---@param type nil|"full"|"incremental"
function M.compile(type)
request(0, 'java/buildWorkspace', type == 'full', on_build_result)
end


---@param mode nil|"prompt"|"all"
local function pick_projects(mode, on_projects)
local command = {
command = 'java.project.getAll',
}
local bufnr = api.nvim_get_current_buf()
util.execute_command(command, function(err, projects)
if err then
error(err.message or vim.inspect(err))
Expand All @@ -732,15 +718,65 @@ function M.update_projects_config(mode)
end
)
end
on_projects(selection)
end, bufnr)
end


--- Trigger a rebuild of one or more projects.
---
---@param opts JdtBuildProjectOpts|nil optional configuration options
function M.build_projects(opts)
opts = opts or {}
local bufnr = api.nvim_get_current_buf()
pick_projects(opts.select_mode or "prompt", function(selection)
if selection and next(selection) then
local params = {
identifiers = vim.tbl_map(function(project) return { uri = project } end, selection),
isFullBuild = opts.full_build == nil and true or opts.full_build
}
vim.lsp.buf_request(bufnr, 'java/buildProjects', params, on_build_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)

--- 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.
function M.update_project_config()
local params = { uri = vim.uri_from_bufnr(0) }
request(0, 'java/projectConfigurationUpdate', params, function(err)
if err then
print('Could not update project configuration: ' .. err.message)
return
end
end)
end

--- Process changes made to the Gradle or Maven configuration of one or more projects.
--- Requires eclipse.jdt.ls >= 1.13.0
---
---@param opts JdtUpdateProjectsOpts|nil configuration options
function M.update_projects_config(opts)
opts = opts or {}
local bufnr = api.nvim_get_current_buf()
pick_projects(opts.select_mode or "prompt", function(selection)
if selection and next(selection) then
local params = {
identifiers = vim.tbl_map(function(project) return { uri = project } end, selection)
}
vim.lsp.buf_notify(bufnr, 'java/projectConfigurationsUpdate', params)
end
end, bufnr)
end)
end
---@class JdtUpdateProjectsOpts
---@field select_mode JdtProjectSelectMode|nil show prompt to select projects or select all. Defaults to "prompt"

---@alias JdtProjectSelectMode "all"|"prompt"|nil
--

local function mk_extract(entity)
return function(from_selection)
Expand Down Expand Up @@ -825,15 +861,13 @@ end
--- Must be called from a regular java source file.
---
--- Examples:
--
--- ```
--- lua require('jdtls').jol()
--- ```
---
--- ```
--- lua require('jdtls').jol(nil, "java.util.ImmutableCollections$List12")
--- ```
---
---@param mode nil|"estimates"|"footprint"|"externals"|"internals"
---@param classname string|nil fully qualified class name. Defaults to the current class.
function M.jol(mode, classname)
Expand Down

0 comments on commit 703268d

Please sign in to comment.