Added java-jdtls code actions shortcut <leader>ca

This commit is contained in:
Knoppiix 2025-10-21 11:30:59 -04:00
parent d1a6798981
commit 19244408c1
2 changed files with 57 additions and 16 deletions

View File

@ -1,26 +1,53 @@
-- print 'Loaded ftplugin/java.lua' ---- print 'Loaded ftplugin/java.lua'
--local jdtls = require 'jdtls'
--
--local root_dir = require('jdtls.setup').find_root { '.git', 'mvnw', 'gradlew', 'pom.xml', '.project' }
--if root_dir == '' then
-- root_dir = vim.fn.getcwd()
--end
--
---- print('JDTLS root_dir = ' .. root_dir)
--
--local workspace_dir = vim.fn.stdpath 'data' .. '/jdtls-workspace/' .. vim.fn.fnamemodify(root_dir, ':p:h:t')
--
--local config = {
-- cmd = { vim.fn.stdpath 'data' .. '/mason/packages/jdtls/bin/jdtls' },
-- root_dir = root_dir,
-- workspace_folder = workspace_dir,
-- settings = {
-- java = {
-- project = {
-- sourcePaths = { 'src' },
-- },
-- },
-- },
--}
--
--jdtls.start_or_attach(config)
local jdtls = require 'jdtls' local jdtls = require 'jdtls'
local root_dir = require('jdtls.setup').find_root { '.git', 'mvnw', 'gradlew', 'pom.xml' } local root_dir = require('jdtls.setup').find_root { '.git', 'pom.xml', 'build.gradle' }
if root_dir == '' then if root_dir == '' then
root_dir = vim.fn.getcwd() root_dir = vim.fn.getcwd()
end end
print('JDTLS root_dir = ' .. root_dir)
local workspace_dir = vim.fn.stdpath 'data' .. '/jdtls-workspace/' .. vim.fn.fnamemodify(root_dir, ':p:h:t') local workspace_dir = vim.fn.stdpath 'data' .. '/jdtls-workspace/' .. vim.fn.fnamemodify(root_dir, ':p:h:t')
local config = { local config = {
cmd = { vim.fn.stdpath 'data' .. '/mason/packages/jdtls/bin/jdtls' }, -- 👈 absolute path cmd = {
root_dir = root_dir, vim.fn.stdpath 'data' .. '/mason/packages/jdtls/bin/jdtls',
workspace_folder = workspace_dir, '-data',
settings = { workspace_dir,
java = {
project = {
sourcePaths = { 'src' }, -- 👈 tells jdtls where sources begin
},
},
}, },
root_dir = root_dir,
} }
jdtls.start_or_attach(config) jdtls.start_or_attach(config)
-- LSP keymaps (for rename, code actions, etc.)
local opts = { buffer = true }
vim.keymap.set('n', '<leader>rn', vim.lsp.buf.rename, vim.tbl_extend('force', opts, { desc = 'LSP: Rename' }))
vim.keymap.set('n', '<leader>ca', vim.lsp.buf.code_action, vim.tbl_extend('force', opts, { desc = 'LSP: Code Action' }))
vim.keymap.set('n', 'gd', vim.lsp.buf.definition, vim.tbl_extend('force', opts, { desc = 'LSP: Go to Definition' }))
vim.keymap.set('n', 'gr', vim.lsp.buf.references, vim.tbl_extend('force', opts, { desc = 'LSP: Find References' }))
vim.keymap.set('n', 'K', vim.lsp.buf.hover, vim.tbl_extend('force', opts, { desc = 'LSP: Hover Doc' }))

View File

@ -89,7 +89,10 @@ P.S. You can delete this when you're done too. It's your config now! :)
-- NOTE: Must happen before plugins are loaded (otherwise wrong leader will be used) -- NOTE: Must happen before plugins are loaded (otherwise wrong leader will be used)
vim.g.mapleader = ' ' vim.g.mapleader = ' '
vim.g.maplocalleader = ' ' vim.g.maplocalleader = ' '
-- vim.cmd [[
-- syntax enable
-- filetype plugin indent on
-- ]]
-- Set to true if you have a Nerd Font installed and selected in the terminal -- Set to true if you have a Nerd Font installed and selected in the terminal
vim.g.have_nerd_font = false vim.g.have_nerd_font = false
@ -256,7 +259,17 @@ require('lazy').setup({
-- keys can be used to configure plugin behavior/loading/etc. -- keys can be used to configure plugin behavior/loading/etc.
-- --
-- Use `opts = {}` to automatically pass options to a plugin's `setup()` function, forcing the plugin to be loaded. -- Use `opts = {}` to automatically pass options to a plugin's `setup()` function, forcing the plugin to be loaded.
-- {
'fatih/vim-go',
build = ':GoInstallBinaries',
},
{
'hrsh7th/nvim-cmp',
dependencies = {
'hrsh7th/cmp-nvim-lsp',
'neovim/nvim-lspconfig',
},
},
-- Alternatively, use `config = function() ... end` for full control over the configuration. -- Alternatively, use `config = function() ... end` for full control over the configuration.
-- If you prefer to call `setup` explicitly, use: -- If you prefer to call `setup` explicitly, use:
@ -950,11 +963,12 @@ require('lazy').setup({
main = 'nvim-treesitter.configs', -- Sets main module to use for opts main = 'nvim-treesitter.configs', -- Sets main module to use for opts
-- [[ Configure Treesitter ]] See `:help nvim-treesitter` -- [[ Configure Treesitter ]] See `:help nvim-treesitter`
opts = { opts = {
ensure_installed = { 'bash', 'c', 'diff', 'html', 'lua', 'luadoc', 'markdown', 'markdown_inline', 'query', 'vim', 'vimdoc' }, ensure_installed = { 'bash', 'java', 'c', 'diff', 'html', 'lua', 'luadoc', 'markdown', 'markdown_inline', 'query', 'vim', 'vimdoc' },
-- Autoinstall languages that are not installed -- Autoinstall languages that are not installed
auto_install = true, auto_install = true,
highlight = { highlight = {
enable = true, enable = true,
disable = { 'java' },
-- Some languages depend on vim's regex highlighting system (such as Ruby) for indent rules. -- Some languages depend on vim's regex highlighting system (such as Ruby) for indent rules.
-- If you are experiencing weird indenting issues, add the language to -- If you are experiencing weird indenting issues, add the language to
-- the list of additional_vim_regex_highlighting and disabled languages for indent. -- the list of additional_vim_regex_highlighting and disabled languages for indent.