Skip to content

Commit 1924440

Browse files
committed
Added java-jdtls code actions shortcut <leader>ca
1 parent d1a6798 commit 1924440

2 files changed

Lines changed: 57 additions & 16 deletions

File tree

ftplugin/java.lua

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,53 @@
1-
-- print 'Loaded ftplugin/java.lua'
1+
---- print 'Loaded ftplugin/java.lua'
2+
--local jdtls = require 'jdtls'
3+
--
4+
--local root_dir = require('jdtls.setup').find_root { '.git', 'mvnw', 'gradlew', 'pom.xml', '.project' }
5+
--if root_dir == '' then
6+
-- root_dir = vim.fn.getcwd()
7+
--end
8+
--
9+
---- print('JDTLS root_dir = ' .. root_dir)
10+
--
11+
--local workspace_dir = vim.fn.stdpath 'data' .. '/jdtls-workspace/' .. vim.fn.fnamemodify(root_dir, ':p:h:t')
12+
--
13+
--local config = {
14+
-- cmd = { vim.fn.stdpath 'data' .. '/mason/packages/jdtls/bin/jdtls' },
15+
-- root_dir = root_dir,
16+
-- workspace_folder = workspace_dir,
17+
-- settings = {
18+
-- java = {
19+
-- project = {
20+
-- sourcePaths = { 'src' },
21+
-- },
22+
-- },
23+
-- },
24+
--}
25+
--
26+
--jdtls.start_or_attach(config)
227
local jdtls = require 'jdtls'
328

4-
local root_dir = require('jdtls.setup').find_root { '.git', 'mvnw', 'gradlew', 'pom.xml' }
29+
local root_dir = require('jdtls.setup').find_root { '.git', 'pom.xml', 'build.gradle' }
530
if root_dir == '' then
631
root_dir = vim.fn.getcwd()
732
end
833

9-
print('JDTLS root_dir = ' .. root_dir)
10-
1134
local workspace_dir = vim.fn.stdpath 'data' .. '/jdtls-workspace/' .. vim.fn.fnamemodify(root_dir, ':p:h:t')
1235

1336
local config = {
14-
cmd = { vim.fn.stdpath 'data' .. '/mason/packages/jdtls/bin/jdtls' }, -- 👈 absolute path
15-
root_dir = root_dir,
16-
workspace_folder = workspace_dir,
17-
settings = {
18-
java = {
19-
project = {
20-
sourcePaths = { 'src' }, -- 👈 tells jdtls where sources begin
21-
},
22-
},
37+
cmd = {
38+
vim.fn.stdpath 'data' .. '/mason/packages/jdtls/bin/jdtls',
39+
'-data',
40+
workspace_dir,
2341
},
42+
root_dir = root_dir,
2443
}
2544

2645
jdtls.start_or_attach(config)
46+
47+
-- LSP keymaps (for rename, code actions, etc.)
48+
local opts = { buffer = true }
49+
vim.keymap.set('n', '<leader>rn', vim.lsp.buf.rename, vim.tbl_extend('force', opts, { desc = 'LSP: Rename' }))
50+
vim.keymap.set('n', '<leader>ca', vim.lsp.buf.code_action, vim.tbl_extend('force', opts, { desc = 'LSP: Code Action' }))
51+
vim.keymap.set('n', 'gd', vim.lsp.buf.definition, vim.tbl_extend('force', opts, { desc = 'LSP: Go to Definition' }))
52+
vim.keymap.set('n', 'gr', vim.lsp.buf.references, vim.tbl_extend('force', opts, { desc = 'LSP: Find References' }))
53+
vim.keymap.set('n', 'K', vim.lsp.buf.hover, vim.tbl_extend('force', opts, { desc = 'LSP: Hover Doc' }))

init.lua

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,10 @@ P.S. You can delete this when you're done too. It's your config now! :)
8989
-- NOTE: Must happen before plugins are loaded (otherwise wrong leader will be used)
9090
vim.g.mapleader = ' '
9191
vim.g.maplocalleader = ' '
92-
92+
-- vim.cmd [[
93+
-- syntax enable
94+
-- filetype plugin indent on
95+
-- ]]
9396
-- Set to true if you have a Nerd Font installed and selected in the terminal
9497
vim.g.have_nerd_font = false
9598

@@ -256,7 +259,17 @@ require('lazy').setup({
256259
-- keys can be used to configure plugin behavior/loading/etc.
257260
--
258261
-- Use `opts = {}` to automatically pass options to a plugin's `setup()` function, forcing the plugin to be loaded.
259-
--
262+
{
263+
'fatih/vim-go',
264+
build = ':GoInstallBinaries',
265+
},
266+
{
267+
'hrsh7th/nvim-cmp',
268+
dependencies = {
269+
'hrsh7th/cmp-nvim-lsp',
270+
'neovim/nvim-lspconfig',
271+
},
272+
},
260273

261274
-- Alternatively, use `config = function() ... end` for full control over the configuration.
262275
-- If you prefer to call `setup` explicitly, use:
@@ -950,11 +963,12 @@ require('lazy').setup({
950963
main = 'nvim-treesitter.configs', -- Sets main module to use for opts
951964
-- [[ Configure Treesitter ]] See `:help nvim-treesitter`
952965
opts = {
953-
ensure_installed = { 'bash', 'c', 'diff', 'html', 'lua', 'luadoc', 'markdown', 'markdown_inline', 'query', 'vim', 'vimdoc' },
966+
ensure_installed = { 'bash', 'java', 'c', 'diff', 'html', 'lua', 'luadoc', 'markdown', 'markdown_inline', 'query', 'vim', 'vimdoc' },
954967
-- Autoinstall languages that are not installed
955968
auto_install = true,
956969
highlight = {
957970
enable = true,
971+
disable = { 'java' },
958972
-- Some languages depend on vim's regex highlighting system (such as Ruby) for indent rules.
959973
-- If you are experiencing weird indenting issues, add the language to
960974
-- the list of additional_vim_regex_highlighting and disabled languages for indent.

0 commit comments

Comments
 (0)