-
-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make setup easier and outline eclipse.jdt.ls installation
- Loading branch information
1 parent
7b524f8
commit bc1e54f
Showing
4 changed files
with
220 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
local M = {} | ||
|
||
local is_windows = vim.loop.os_uname().version:match('Windows') | ||
|
||
M.sep = is_windows and '\\' or '/' | ||
|
||
if is_windows then | ||
M.is_fs_root = function(path) | ||
return path:match('^%a:$') | ||
end | ||
else | ||
M.is_fs_root = function(path) | ||
return path == '/' | ||
end | ||
end | ||
|
||
function M.join(...) | ||
local result = table.concat(vim.tbl_flatten {...}, M.sep):gsub(M.sep .. '+', M.sep) | ||
return result | ||
end | ||
|
||
return M |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
local api = vim.api | ||
local lsp = vim.lsp | ||
local uv = vim.loop | ||
local path = require('jdtls.path') | ||
|
||
local lsps = {} | ||
local status_callback = vim.schedule_wrap(function(_, _, result) | ||
api.nvim_command(string.format(':echohl Function | echo "%s" | echohl None', result.message)) | ||
end) | ||
|
||
local function attach_to_active_buf(bufnr, client_name) | ||
for _, buf in pairs(vim.fn.getbufinfo({bufloaded=true})) do | ||
if api.nvim_buf_get_option(buf.bufnr, 'filetype') == 'java' then | ||
local clients = lsp.buf_get_clients(buf.bufnr) | ||
for _, client in ipairs(clients) do | ||
if client.config.name == client_name then | ||
lsp.buf_attach_client(bufnr, client.id) | ||
return true | ||
end | ||
end | ||
end | ||
end | ||
print('No active LSP client found to use for jdt:// document') | ||
return false | ||
end | ||
|
||
local function find_root(bufname, markers) | ||
local dirname = vim.fn.fnamemodify(bufname, ':p:h') | ||
while not path.is_fs_root(dirname) do | ||
for _, marker in ipairs(markers) do | ||
if uv.fs_stat(path.join(dirname, marker)) then | ||
return dirname | ||
end | ||
end | ||
dirname = vim.fn.fnamemodify(dirname, ':h') | ||
end | ||
end | ||
|
||
local function start_or_attach(config) | ||
assert(config, 'config is required') | ||
assert( | ||
config.cmd and type(config.cmd) == 'table', | ||
'Config must have a `cmd` property and that must be a table. Got: ' | ||
.. table.concat(config.cmd, ' ') or 'nil' | ||
) | ||
assert( | ||
tonumber(vim.fn.executable(config.cmd[1])) == 1, | ||
'LSP cmd must be an executable: ' .. config.cmd[1] | ||
) | ||
config.name = config.name or 'jdt.ls' | ||
|
||
local bufnr = api.nvim_get_current_buf() | ||
local bufname = api.nvim_buf_get_name(bufnr) | ||
-- Won't be able to get the correct root path for jdt:// URIs | ||
-- So need to connect to an existing client | ||
if vim.startswith(bufname, 'jdt://') then | ||
if attach_to_active_buf(bufnr, config.name) then | ||
return | ||
end | ||
end | ||
|
||
config.root_dir = config.root_dir or find_root(bufname, {'.git', 'gradlew', 'mvnw'}) | ||
config.callbacks = config.callbacks or {} | ||
config.callbacks['language/status'] = config.callbacks['language/status'] or status_callback | ||
config.capabilities = config.capabilities or lsp.protocol.make_client_capabilities() | ||
config.capabilities.textDocument.codeAction = { | ||
dynamicRegistration = false; | ||
codeActionLiteralSupport = { | ||
codeActionKind = { | ||
valueSet = { | ||
"source.generate.toString", | ||
"source.generate.hashCodeEquals", | ||
"source.organizeImports", | ||
}; | ||
}; | ||
}; | ||
} | ||
local client_id = lsps[config.root_dir] | ||
if not client_id then | ||
client_id = lsp.start_client(config) | ||
lsps[config.root_dir] = client_id | ||
end | ||
lsp.buf_attach_client(bufnr, client_id) | ||
end | ||
|
||
local extendedClientCapabilities = { | ||
classFileContentsSupport = true; | ||
generateToStringPromptSupport = true; | ||
hashCodeEqualsPromptSupport = true; | ||
advancedExtractRefactoringSupport = true; | ||
advancedOrganizeImportsSupport = true; | ||
generateConstructorsPromptSupport = true; | ||
generateDelegateMethodsPromptSupport = true; | ||
}; | ||
|
||
|
||
local function add_commands() | ||
api.nvim_command [[command! -buffer -nargs=? JdtCompile lua require('jdtls').compile(<f-args>)]] | ||
api.nvim_command [[command! -buffer JdtUpdateConfig lua require('jdtls').update_project_config()]] | ||
api.nvim_command [[command! -buffer -nargs=* JdtJol lua require('jdtls').jol(<f-args>)]] | ||
api.nvim_command [[command! -buffer JdtBytecode lua require('jdtls').javap()]] | ||
api.nvim_command [[command! -buffer JdtJshell lua require('jdtls').jshell()]] | ||
end | ||
|
||
return { | ||
start_or_attach = start_or_attach; | ||
extendedClientCapabilities = extendedClientCapabilities; | ||
add_commands = add_commands; | ||
} |