Skip to content

Instantly share code, notes, and snippets.

@rnkoaa
Last active October 28, 2021 03:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rnkoaa/8dc46a9cf7534edcd0405cda6e3c2970 to your computer and use it in GitHub Desktop.
Save rnkoaa/8dc46a9cf7534edcd0405cda6e3c2970 to your computer and use it in GitHub Desktop.
simple lua config for neovim with lsp

Setup environment

  1. clone from eclipse jdtls
  2. compile with cd eclipse.jdt.ls && ./mvnw clean install -DskipTests
  3. download google java format
  4. create launch-jdtls.sh
#!/bin/sh

JAR="$HOME/.config/eclipse.jdt.ls/org.eclipse.jdt.ls.product/target/repository/plugins/org.eclipse.equinox.launcher_*.jar"
JAVA_HOME="$HOME/.sdkman/candidates/java/17.0.1-open"
GRADLE_HOME=$HOME/.sdkman/candidates/gradle $JAVA_HOME/bin/java \
-Declipse.application=org.eclipse.jdt.ls.core.id1 \
-Dosgi.bundles.defaultStartLevel=4 \
-Declipse.product=org.eclipse.jdt.ls.core.product \
-Dlog.protocol=true \
-Dlog.level=ALL \
-Xms1g \
-Xmx2G \
-jar $(echo "$JAR") \
-configuration "$HOME/.config/eclipse.jdt.ls/org.eclipse.jdt.ls.product/target/repository/config_mac" \
-data "${1:-$HOME/eclipse-workspace}" \
--add-modules=ALL-SYSTEM \
--add-opens java.base/java.util=ALL-UNNAMED \
  1. ensure java is installed
  2. arch -arm64 brew install fzf
  3. arch -arm64 brew install bat
  4. arch -arm64 brew install lua-language-server
  5. brew install ninja
  6. npm install -g lua-fmt
  7. npm install -g bash-language-server
  8. npm install -g typescript typescript-language-server
  9. npm install -g vim-language-server

Vim Setup

  • Create config directory mkdir -p ~/.config/nvim
  • Create init.lua -> touch ~/.config/nvim/init.lua
  • Create java.lua -> mkdir -p ~/.config/nvim/ftplugin && touch ~/.confing/nvim/ftplugin/java.lua
  • Copy the contents of each file to the necessary location
  • Run packer install in vim -> :PackerInstall

Links for inspiration

makeprg
ERRORFORMAT
%file: line %linenum: %errormessage
https://unix.stackexchange.com/questions/119644/set-makeprg-to-gradlew
:e /.vim/compiler/shell.vim
:e ~/.config/nvim/compiler/gradle.vim
let s:gradlew = escape(findfile('gradlew', '.;') . " -b " . findfile('build.gradle', '.;'), ' \')
if exists("current_compiler")
finish
endif
if exists(":CompilerSet") != 2 " older Vim always used :setlocal
command -nargs=* CompilerSet setlocal <args>
endif
let current_compiler = s:gradlew
execute "CompilerSet makeprg=" . s:gradlew
" copied from javac.vim + added the :compileJava bits
CompilerSet errorformat=%E:compileJava%f:%l:\ %m,%E%f:%l:\ %m,%-Z%p^,%-C%.%#,%-G%.%#
set compiler shell
:e $VIMRUNTIME/compiler
--
ftplugin
local utils = {}
local scopes = {o = vim.o, b = vim.bo, w = vim.wo}
function utils.opt(scope, key, value)
scopes[scope][key] = value
if scope ~= "o" then
scopes["o"][key] = value
end
end
function utils.map(mode, lhs, rhs, opts)
local options = {noremap = true}
if opts then
options = vim.tbl_extend("force", options, opts)
end
vim.api.nvim_set_keymap(mode, lhs, rhs, options)
end
-- Install packer
local install_path = vim.fn.stdpath "data" .. "/site/pack/packer/start/packer.nvim"
if vim.fn.empty(vim.fn.glob(install_path)) > 0 then
vim.fn.execute("!git clone https://github.com/wbthomason/packer.nvim " .. install_path)
end
vim.api.nvim_exec(
[[
augroup Packer
autocmd!
autocmd BufWritePost init.lua PackerCompile
augroup end
]],
false
)
local use = require("packer").use
require("packer").startup(
function()
-- For Packer
use "wbthomason/packer.nvim"
-- Explorer
use {
"kyazdani42/nvim-tree.lua",
requires = "kyazdani42/nvim-web-devicons",
config = function()
require "nvim-tree".setup {}
end
}
use "mhartington/formatter.nvim"
use "neovim/nvim-lspconfig"
use "glepnir/lspsaga.nvim"
use "onsails/lspkind-nvim"
use "hrsh7th/cmp-nvim-lsp"
use "hrsh7th/cmp-buffer"
use "EdenEast/nightfox.nvim"
use "hrsh7th/nvim-cmp"
use "hrsh7th/cmp-path"
use "saadparwaiz1/cmp_luasnip"
use "L3MON4D3/LuaSnip" -- Snippets plugin
use "windwp/nvim-autopairs"
use "maxmellon/vim-jsx-pretty"
-- Highlight, edit, and navigate code using a fast incremental parsing library
use "nvim-treesitter/nvim-treesitter"
use "nvim-lualine/lualine.nvim"
-- Additional textobjects for treesitter
use "nvim-treesitter/nvim-treesitter-textobjects"
use "tpope/vim-commentary" -- "gc" to comment visual regions/lines
-- Add indentation guides even on blank lines
use "lukas-reineke/indent-blankline.nvim"
use {
"nvim-telescope/telescope.nvim",
requires = {{"nvim-lua/plenary.nvim"}}
}
use "mfussenegger/nvim-jdtls"
end
)
require("nvim-autopairs").setup()
require("lspkind").init()
require("nightfox").load("nordfox")
require("lualine").setup(
{
theme = "nightfox"
}
)
--Incremental live completion (note: this is now a default on master)
vim.o.inccommand = "nosplit"
--Set highlight on search
vim.o.hlsearch = false
--Make line numbers default
vim.wo.number = true
--Do not save when switching buffers (note: this is now a default on master)
vim.o.hidden = true
--Enable mouse mode
vim.o.mouse = "a"
--Enable break indent
vim.o.breakindent = true
--Save undo history
vim.opt.undofile = true
--Case insensitive searching UNLESS /C or capital in search
vim.o.ignorecase = true
vim.o.smartcase = true
--Decrease update time
vim.o.updatetime = 250
vim.wo.signcolumn = "yes"
--Set colorscheme (order is important here)
vim.o.termguicolors = true
-- vim.g.onedark_terminal_italics = 2
--Set statusbar
-- vim.g.lightline = {
-- colorscheme = 'onedark',
-- active = { left = { { 'mode', 'paste' }, { 'gitbranch', 'readonly', 'filename', 'modified' } } },
-- component_function = { gitbranch = 'fugitive#head' },
-- }
--Remap space as leader key
vim.api.nvim_set_keymap("", "<Space>", "<Nop>", {noremap = true, silent = true})
vim.g.mapleader = " "
vim.g.maplocalleader = " "
--Remap for dealing with word wrap
local opts = {noremap = true, expr = true, silent = true}
vim.api.nvim_set_keymap("n", "k", "v:count == 0 ? 'gk' : 'k'", opts)
vim.api.nvim_set_keymap("n", "j", "v:count == 0 ? 'gj' : 'j'", opts)
-- vim.api.nvim_set_keymap('i', 'jk', "<ESC>", opts)
-- vim.api.nvim_set_keymap('i', 'kj', "<ESC>", opts)
-- Highlight on yank
vim.api.nvim_exec(
[[
augroup YankHighlight
autocmd!
autocmd TextYankPost * silent! lua vim.highlight.on_yank()
augroup end
]],
false
)
-- Y yank until the end of line (note: this is now a default on master)
vim.api.nvim_set_keymap("n", "Y", "y$", {noremap = true})
--Map blankline
vim.g.indent_blankline_char = "┊"
vim.g.indent_blankline_filetype_exclude = {"help", "packer"}
vim.g.indent_blankline_buftype_exclude = {"terminal", "nofile"}
vim.g.indent_blankline_char_highlight = "LineNr"
vim.g.indent_blankline_show_trailing_blankline_indent = false
-- Telescope
require("telescope").setup {
defaults = {
mappings = {
i = {
["<C-u>"] = false,
["<C-d>"] = false
}
}
}
}
--Add leader shortcuts
utils.map('n', '<leader>ff', [[<cmd>lua require('telescope.builtin').find_files()<cr>]], opts )
utils.map('n', '<C-p>', [[<cmd>lua require('telescope.builtin').find_files()<cr>]], opts)
utils.map('n', '<leader>fg', [[<cmd>lua require('telescope.builtin').live_grep()<cr>]], opts)
utils.map('n', '<leader>fb', [[<cmd>lua require('telescope.builtin').buffers()<cr>]], opts)
utils.map('n', '<leader>fh', [[<cmd>lua require('telescope.builtin').help_tags()<cr>]], opts)
-- Treesitter configuration
-- Parsers must be installed manually via :TSInstall
require("nvim-treesitter.configs").setup {
highlight = {
enable = true -- false will disable the whole extension
},
incremental_selection = {
enable = true,
keymaps = {
init_selection = "gnn",
node_incremental = "grn",
scope_incremental = "grc",
node_decremental = "grm"
}
},
indent = {
enable = true
},
textobjects = {
select = {
enable = true,
lookahead = true, -- Automatically jump forward to textobj, similar to targets.vim
keymaps = {
-- You can use the capture groups defined in textobjects.scm
["af"] = "@function.outer",
["if"] = "@function.inner",
["ac"] = "@class.outer",
["ic"] = "@class.inner"
}
},
move = {
enable = true,
set_jumps = true, -- whether to set jumps in the jumplist
goto_next_start = {
["]m"] = "@function.outer",
["]]"] = "@class.outer"
},
goto_next_end = {
["]M"] = "@function.outer",
["]["] = "@class.outer"
},
goto_previous_start = {
["[m"] = "@function.outer",
["[["] = "@class.outer"
},
goto_previous_end = {
["[M"] = "@function.outer",
["[]"] = "@class.outer"
}
}
}
}
-- LSP settings
local nvim_lsp = require "lspconfig"
local on_attach = function(_, bufnr)
vim.api.nvim_buf_set_option(bufnr, "omnifunc", "v:lua.vim.lsp.omnifunc")
local opts = {noremap = true, silent = true}
vim.api.nvim_buf_set_keymap(bufnr, "n", "gD", "<cmd>lua vim.lsp.buf.declaration()<CR>", opts)
vim.api.nvim_buf_set_keymap(bufnr, "n", "gd", "<cmd>lua vim.lsp.buf.definition()<CR>", opts)
vim.api.nvim_buf_set_keymap(bufnr, "n", "K", "<cmd>lua vim.lsp.buf.hover()<CR>", opts)
vim.api.nvim_buf_set_keymap(bufnr, "n", "gi", "<cmd>lua vim.lsp.buf.implementation()<CR>", opts)
vim.api.nvim_buf_set_keymap(bufnr, "n", "<C-k>", "<cmd>lua vim.lsp.buf.signature_help()<CR>", opts)
vim.api.nvim_buf_set_keymap(bufnr, "n", "<leader>wa", "<cmd>lua vim.lsp.buf.add_workspace_folder()<CR>", opts)
vim.api.nvim_buf_set_keymap(bufnr, "n", "<leader>wr", "<cmd>lua vim.lsp.buf.remove_workspace_folder()<CR>", opts)
vim.api.nvim_buf_set_keymap(
bufnr,
"n",
"<leader>wl",
"<cmd>lua print(vim.inspect(vim.lsp.buf.list_workspace_folders()))<CR>",
opts
)
vim.api.nvim_buf_set_keymap(bufnr, "n", "<leader>D", "<cmd>lua vim.lsp.buf.type_definition()<CR>", opts)
vim.api.nvim_buf_set_keymap(bufnr, "n", "<leader>rn", "<cmd>lua vim.lsp.buf.rename()<CR>", opts)
vim.api.nvim_buf_set_keymap(bufnr, "n", "gr", "<cmd>lua vim.lsp.buf.references()<CR>", opts)
vim.api.nvim_buf_set_keymap(bufnr, "n", "<leader>ca", "<cmd>lua vim.lsp.buf.code_action()<CR>", opts)
-- vim.api.nvim_buf_set_keymap(bufnr, 'v', '<leader>ca', '<cmd>lua vim.lsp.buf.range_code_action()<CR>', opts)
vim.api.nvim_buf_set_keymap(bufnr, "n", "<leader>e", "<cmd>lua vim.lsp.diagnostic.show_line_diagnostics()<CR>", opts)
vim.api.nvim_buf_set_keymap(bufnr, "n", "[d", "<cmd>lua vim.lsp.diagnostic.goto_prev()<CR>", opts)
vim.api.nvim_buf_set_keymap(bufnr, "n", "]d", "<cmd>lua vim.lsp.diagnostic.goto_next()<CR>", opts)
vim.api.nvim_buf_set_keymap(bufnr, "n", "<leader>q", "<cmd>lua vim.lsp.diagnostic.set_loclist()<CR>", opts)
vim.api.nvim_buf_set_keymap(
bufnr,
"n",
"<leader>so",
[[<cmd>lua require('telescope.builtin').lsp_document_symbols()<CR>]],
opts
)
vim.cmd [[ command! Format execute 'lua vim.lsp.buf.formatting()' ]]
end
-- nvim-cmp supports additional completion capabilities
local capabilities = vim.lsp.protocol.make_client_capabilities()
capabilities = require("cmp_nvim_lsp").update_capabilities(capabilities)
-- Enable the following language servers
local servers = {
"bashls",
"cssls",
"dockerls",
"jsonls",
"sqls",
"tsserver",
"vimls",
"yamlls"
}
for _, lsp in ipairs(servers) do
nvim_lsp[lsp].setup {
on_attach = on_attach,
capabilities = capabilities
}
end
-- Set completeopt to have a better completion experience
vim.o.completeopt = "menuone,noselect"
-- luasnip setup
local luasnip = require "luasnip"
-- nvim-cmp setup
local lspkind = require("lspkind")
-- formatting = {
-- format = lspkind.cmp_format({with_text = false, maxwidth = 50})
-- }
local cmp = require "cmp"
cmp.setup {
formatting = {
format = lspkind.cmp_format({with_text = false, maxwidth = 50})
},
snippet = {
expand = function(args)
luasnip.lsp_expand(args.body)
end
},
mapping = {
["<C-p>"] = cmp.mapping.select_prev_item(),
["<C-n>"] = cmp.mapping.select_next_item(),
["<C-d>"] = cmp.mapping.scroll_docs(-4),
["<C-f>"] = cmp.mapping.scroll_docs(4),
["<C-Space>"] = cmp.mapping.complete(),
["<C-e>"] = cmp.mapping.close(),
["<CR>"] = cmp.mapping.confirm {
behavior = cmp.ConfirmBehavior.Replace,
select = true
},
["<Tab>"] = function(fallback)
if cmp.visible() then
cmp.select_next_item()
elseif luasnip.expand_or_jumpable() then
luasnip.expand_or_jump()
else
fallback()
end
end,
["<S-Tab>"] = function(fallback)
if cmp.visible() then
cmp.select_prev_item()
elseif luasnip.jumpable(-1) then
luasnip.jump(-1)
else
fallback()
end
end
},
sources = {
{name = "nvim_lsp"},
{name = "luasnip"},
{name = "buffer"},
{name = "path"}
}
}
require("nvim-autopairs.completion.cmp").setup {
map_cr = true,
map_complete = true,
auto_select = true
}
-- Setup lspconfig.
require("lspconfig").tsserver.setup {
capabilities = require("cmp_nvim_lsp").update_capabilities(vim.lsp.protocol.make_client_capabilities())
}
-- explorer
utils.map('n', '<Leader>e', ':NvimTreeToggle<CR>', {noremap = true, silent = true})
utils.map('n', '-', ':RnvimrToggle<CR>', {noremap = true, silent = true})
-- better window movement
utils.map('n', '<C-h>', '<C-w>h', {silent = true})
utils.map('n', '<C-j>', '<C-w>j', {silent = true})
utils.map('n', '<C-k>', '<C-w>k', {silent = true})
utils.map('n', '<C-l>', '<C-w>l', {silent = true})
utils.map("i", "jk", "<ESC>")
utils.map("i", "kj", "<ESC>")
utils.map("i", "jj", "<ESC>")
utils.map("n", "<leader>p", '"_dp')
-- Y yank until the end of line
utils.map("n", "Y", "y$")
-- map('n', '<leader>/', '<cmd>noh<CR>') -- Clear highlights
utils.map("n", "<A-/>", "<cmd>noh<CR>") -- Clear highlights
utils.map("i", "jk", "<Esc>")
utils.map("", "<leader>c", '"+y') --Copy to clipboard in normal, visual, select and operator modes
utils.map("i", "<C-u>", "<C-g>u<C-u>") -- Make <C-u> undo-friendly
utils.map("i", "<C-w>", "<C-g>u<C-w>") -- Make <C-w> undo-friendly
-- Formatters
vim.cmd([[nnoremap <silent> <leader>f :Format<CR>]])
local home = os.getenv("HOME")
require("formatter").setup(
{
filetype = {
typescript = {
function()
return {
exe = "prettier",
args = {"--stdin-filepath", vim.fn.fnameescape(vim.api.nvim_buf_get_name(0)), "--single-quote"},
stdin = true
}
end
},
typescriptreact = {
function()
return {
exe = "prettier",
args = {"--stdin-filepath", vim.fn.fnameescape(vim.api.nvim_buf_get_name(0)), "--single-quote"},
stdin = true
}
end
},
java = {
function()
return {
exe = "java",
-- Formatter uses '-' as stdin
args = {
"--add-exports jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED --add-exports jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED --add-exports jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED --add-exports jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED --add-exports jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED",
"-jar",
home .. "/.config/formatters/google-java-format-1.11.0-all-deps.jar",
"--aosp", -- 4 spaces
vim.api.nvim_buf_get_name(0)
},
-- "-" },
stdin = true
}
end
},
javascript = {
function()
return {
exe = "prettier",
args = {"--stdin-filepath", vim.fn.fnameescape(vim.api.nvim_buf_get_name(0)), "--single-quote"},
stdin = true
}
end
},
javascriptreact = {
function()
return {
exe = "prettier",
args = {"--stdin-filepath", vim.fn.fnameescape(vim.api.nvim_buf_get_name(0)), "--single-quote"},
stdin = true
}
end
},
json = {
function()
return {
exe = "prettier",
args = {"--stdin-filepath", vim.fn.fnameescape(vim.api.nvim_buf_get_name(0)), "--single-quote"},
stdin = true
}
end
},
lua = {
-- luafmt
function()
return {
exe = "luafmt",
args = {"--indent-count", 2, "--stdin"},
stdin = true
}
end
}
}
}
)
-- following options are the default
require'nvim-tree'.setup {
-- disables netrw completely
disable_netrw = true,
-- hijack netrw window on startup
hijack_netrw = true,
-- open the tree when running this setup function
open_on_setup = false,
-- will not open on setup if the filetype is in this list
ignore_ft_on_setup = {},
-- closes neovim automatically when the tree is the last **WINDOW** in the view
auto_close = true,
-- opens the tree when changing/opening a new tab if the tree wasn't previously opened
open_on_tab = false,
-- hijacks new directory buffers when they are opened.
update_to_buf_dir = {
-- enable the feature
enable = true,
-- allow to open the tree if it was previously closed
auto_open = true,
},
-- hijack the cursor in the tree to put it at the start of the filename
hijack_cursor = false,
-- updates the root directory of the tree on `DirChanged` (when you run `:cd` usually)
update_cwd = false,
-- show lsp diagnostics in the signcolumn
diagnostics = {
enable = false,
icons = {
hint = "",
info = "",
warning = "",
error = "",
}
},
-- update the focused file on `BufEnter`, un-collapses the folders recursively until it finds the file
update_focused_file = {
-- enables the feature
enable = false,
-- update the root directory of the tree to the one of the folder containing the file if the file is not under the current root directory
-- only relevant when `update_focused_file.enable` is true
update_cwd = false,
-- list of buffer names / filetypes that will not update the cwd if the file isn't found under the current root directory
-- only relevant when `update_focused_file.update_cwd` is true and `update_focused_file.enable` is true
ignore_list = {}
},
-- configuration options for the system open command (`s` in the tree by default)
system_open = {
-- the command to run this, leaving nil should work in most cases
cmd = nil,
-- the command arguments as a list
args = {}
},
view = {
-- width of the window, can be either a number (columns) or a string in `%`, for left or right side placement
width = 20,
-- height of the window, can be either a number (columns) or a string in `%`, for top or bottom side placement
height = 30,
-- Hide the root path of the current folder on top of the tree
hide_root_folder = false,
-- side of the tree, can be one of 'left' | 'right' | 'top' | 'bottom'
side = 'left',
-- if true the tree will resize itself after opening a file
auto_resize = true,
mappings = {
-- custom only false will merge the list with the default mappings
-- if true, it will only use your list to set the mappings
custom_only = false,
-- list of mappings to set on the tree manually
list = {}
}
}
}
-- https://github.com/Nguyen-Hoang-Nam/nvim-dotfiles-kitty/blob/main/lua/lsp/jdtls.lua
--https://github.com/ChrisAmelia/dotfiles/blob/master/nvim/lua/lsp.lua
--
local workspace_dir = vim.fn.fnamemodify(vim.fn.getcwd(), ":p:h:t")
local api = vim.api
local root_markers = {"build.gradle", "gradlew", "pom.xml"}
local root_dir = require("jdtls.setup").find_root(root_markers)
local home = os.getenv("HOME")
local workspace_folder = home .. "/.workspace" .. vim.fn.fnamemodify(root_dir, ":p:h:t")
local function register_buffer(bufnr, client_id)
if not clients[bufnr] then
print("adding client id " .. client_id)
clients[bufnr] = {client_id}
else
print("client id exists")
table.insert(clients[bufnr], client_id)
end
end
local opts = {noremap = true, silent = true}
-- mappings
local map = function(type, key, value)
vim.api.nvim_set_keymap(type, key, value, opts)
-- api.nvim_buf_set_keymap(bufnr, type, key, value, {noremap = true, silent = true})
end
local function on_attach(client, bufnr)
bufnr = bufnr or api.nvim_get_current_buf()
--vim.notify("Attaching LSP client "..client.id.." to buffer "..bufnr)
-- register client/buffer relation
register_buffer(bufnr, client.id)
print("JDTLS Lsp Attached")
require("jdtls.setup").add_commands()
require "lsp-status".register_progress()
-- mappings
-- local map = function(type, key, value)
-- api.nvim_buf_set_keymap(bufnr, type, key, value, {noremap = true, silent = true})
-- end
-- TODO: Can we make these look like the saga ones?
map("n", "gca", '<cmd>lua require("jdtls").code_action()<CR>')
map("v", "gcb", ':<cmd>lua lua require("jdtls").code_action(true)<CR>')
-- api.nvim_buf_set_keymap(bufnr, "n", "<leader>o", "<Cmd>lua require'jdtls'.organize_imports()<CR>", opts)
-- api.nvim_buf_set_keymap(bufnr, "n", "<leader>a", "<Cmd>lua require'jdtls'.code_action()<CR>", opts)
-- api.nvim_buf_set_option(bufnr, "omnifunc", "v:lua.vim.lsp.omnifunc")
-- local opts = {noremap = true, silent = true}
-- local function buf_set_keymap(mode, mapping, command)
-- api.nvim_buf_set_keymap(bufnr, mode, mapping, command, opts)
-- end
-- api.nvim_buf_set_option(bufnr, "omnifunc", "v:lua.vim.lsp.omnifunc")
-- buf_set_keymap("n", "<Leader>d", "<Cmd>lua vim.lsp.buf.definition()<CR>")
-- buf_set_keymap("n", "<Leader>a", [[<Cmd>lua require'jdtls'.code_action()<CR>]])
-- buf_set_keymap("n", "<Leader><Leader>", "<Cmd>lua vim.lsp.buf.hover()<CR>")
-- buf_set_keymap("n", "<Leader>r", "<cmd>lua vim.lsp.buf.rename()<CR>")
-- buf_set_keymap("n", "[d", "<cmd>lua vim.lsp.diagnostic.goto_prev()<CR>")
-- buf_set_keymap("n", "]d", "<cmd>lua vim.lsp.diagnostic.goto_next()<CR>")
-- buf_set_keymap("n", "gdi", "<Cmd>lua require'jdtls'.organize_imports()<CR>")
-- buf_set_keymap("n", "gdt", "<Cmd>lua require'jdtls'.test_class()<CR>")
-- buf_set_keymap("n", "gdn", "<Cmd>lua require'jdtls'.test_nearest_method()<CR>")
-- buf_set_keymap("v", "gde", "<Esc><Cmd>lua require('jdtls').extract_variable(true)<CR>")
-- buf_set_keymap("n", "gde", "<Cmd>lua require('jdtls').extract_variable()<CR>")
-- buf_set_keymap("v", "gdm", "<Esc><Cmd>lua require('jdtls').extract_method(true)<CR>")
end
local config = {
flags = {
debounce_text_changes = 150,
allow_incremental_sync = true,
server_side_fuzzy_completion = true
},
capabilities = {
workspace = {
configuration = true
},
textDocument = {
completion = {
completionItem = {
snippetSupport = true
}
}
}
},
cmd = {"launch-jdtls.sh", workspace_folder},
root_dir = require("jdtls.setup").find_root(root_markers)
}
config.on_init = function(client, _)
client.notify("workspace/didChangeConfiguration", {settings = config.settings})
end
local extendedClientCapabilities = require("jdtls").extendedClientCapabilities
extendedClientCapabilities.resolveAdditionalTextEditsSupport = true
config.init_options = {
extendedClientCapabilities = extendedClientCapabilities
}
config.settings = {
-- ['java.format.settings.url'] = home .. '/.config/nvim/language-servers/java-google-formatter.xml',
-- ['java.format.settings.profile'] = 'GoogleStyle',
java = {
signatureHelp = {enabled = true},
contentProvider = {preferred = "fernflower"},
completion = {
favoriteStaticMembers = {}
},
sources = {
organizeImports = {
starThreshold = 9999,
staticStarThreshold = 9999
}
},
codeGeneration = {
generateComments = true
-- toString = {
-- template = '${object.className}{${member.name()}=${member.value}, ${otherMembers}}',
-- },
},
configuration = {
runtimes = {
{
name = "JavaSE-17",
path = home .. "/.sdkman/candidates/java/17.0.1-open"
},
{
name = "JavaSE-11",
path = home .. "/.sdkman/candidates/java/11.0.12-zulu"
}
}
}
}
}
config.on_attach = on_attach
local capabilities = require("cmp_nvim_lsp").update_capabilities(vim.lsp.protocol.make_client_capabilities())
require("lspconfig")["jdtls"].setup {
capabilities = capabilities
}
local finders = require "telescope.finders"
local sorters = require "telescope.sorters"
local actions = require "telescope.actions"
local pickers = require "telescope.pickers"
require("jdtls.ui").pick_one_async = function(items, prompt, label_fn, cb)
local opts = {}
pickers.new(
opts,
{
prompt_title = prompt,
finder = finders.new_table {
results = items,
entry_maker = function(entry)
return {
value = entry,
display = label_fn(entry),
ordinal = label_fn(entry)
}
end
},
sorter = sorters.get_generic_fuzzy_sorter(),
attach_mappings = function(prompt_bufnr)
actions.select_default:replace(
function()
local selection = actions.get_selected_entry(prompt_bufnr)
actions.close(prompt_bufnr)
cb(selection.value)
end
)
return true
end
}
):find()
end
require("jdtls").start_or_attach(config)
map("n", "gca", "<Cmd>lua require('jdtls').code_action()<CR>", opts)
-- map("v", "gca", "<Cmd>lua require('jdtls').code_action(true)<CR>", opts)
map("n", "gco", "<Cmd>lua require'jdtls'.organize_imports()<CR>", opts)
map("n", "gd", "<Cmd>lua vim.lsp.buf.definition()<CR>")
map("n", "K", "<Cmd>lua vim.lsp.buf.hover()<CR>")
map("n", "gcr", "<cmd>lua vim.lsp.buf.rename()<CR>")
map("n", "[d", "<cmd>lua vim.lsp.diagnostic.goto_prev()<CR>")
map("n", "]d", "<cmd>lua vim.lsp.diagnostic.goto_next()<CR>")
map("n", "gdt", "<Cmd>lua require'jdtls'.test_class()<CR>")
map("n", "gdn", "<Cmd>lua require'jdtls'.test_nearest_method()<CR>")
map("v", "gde", "<Esc><Cmd>lua require('jdtls').extract_variable(true)<CR>")
map("n", "gde", "<Cmd>lua require('jdtls').extract_variable()<CR>")
map("v", "gdm", "<Esc><Cmd>lua require('jdtls').extract_method(true)<CR>")
#!/bin/sh
JAR="$HOME/.config/eclipse.jdt.ls/org.eclipse.jdt.ls.product/target/repository/plugins/org.eclipse.equinox.launcher_*.jar"
JAVA_HOME="$HOME/.sdkman/candidates/java/17.0.1-open"
GRADLE_HOME=$HOME/.sdkman/candidates/gradle $JAVA_HOME/bin/java \
-Declipse.application=org.eclipse.jdt.ls.core.id1 \
-Dosgi.bundles.defaultStartLevel=4 \
-Declipse.product=org.eclipse.jdt.ls.core.product \
-Dlog.protocol=true \
-Dlog.level=ALL \
-Xms1g \
-Xmx2G \
-jar $(echo "$JAR") \
-configuration "$HOME/.config/eclipse.jdt.ls/org.eclipse.jdt.ls.product/target/repository/config_mac" \
-data "${1:-$HOME/eclipse-workspace}" \
--add-modules=ALL-SYSTEM \
--add-opens java.base/java.util=ALL-UNNAMED \
--add-opens java.base/java.lang=ALL-UNNAMED
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment