Skip to content

Commit

Permalink
minimal refactoring to use with LazyVim
Browse files Browse the repository at this point in the history
  • Loading branch information
artur-shaik committed Nov 8, 2024
1 parent 66b18ce commit e9258bc
Show file tree
Hide file tree
Showing 8 changed files with 279 additions and 134 deletions.
80 changes: 66 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,78 @@ In addition to autocompletion it can:

## Installation

Minimal setup using `vim-plug`:
Minimal setup using `LazyVim`:

```
call plug#begin('~/.vim/plugged')
Plug 'neovim/nvim-lspconfig'
Plug 'hrsh7th/nvim-cmp'
Plug 'hrsh7th/cmp-nvim-lsp'
Plug 'williamboman/nvim-lsp-installer'
Plug 'puremourning/vimspector'
Plug 'mfussenegger/nvim-jdtls'
Plug 'artur-shaik/jc.nvim'
return {
{
"mfussenegger/nvim-jdtls",
config = function() end,
},
{
"puremourning/vimspector",
keys = {
{
"<leader>vr",
"<Cmd>VimspectorReset<cr>",
},
{
"<leader>vb",
"<Cmd>VimspectorBreakpoints<cr>",
},
},
init = function()
vim.g.vimspector_enable_mappings = "HUMAN"
end,
},
{
dir = "artur-shaik/jc.nvim",
name = "jc.nvim",
dependencies = {
"puremourning/vimspector",
"mfussenegger/nvim-jdtls",
"williamboman/mason.nvim",
},
ft = { "java" },
opts = {
java_exec = "/home/ash/.sdkman/candidates/java/17.0.7-oracle/bin/java",
keys_prefix = "'j",
settings = {
java = {
configuration = {
runtimes = {
{
name = "JavaSE-11",
path = "/home/ash/.sdkman/candidates/java/11.0.12-open/",
default = true,
},
},
},
},
},
},
},
}
```

call plug#end()
lspconfig:

lua require('jc').setup{}
```lua
return {
{
"neovim/nvim-lspconfig",
opts = {
servers = {
jdtls = {},
},
setup = {
jdtls = function()
return true
end,
},
},
```

Execute `:PlugInstall` and restart neovim.

For triggering autocompletion automatically consider [configure nvim-cmp](https://github.com/hrsh7th/nvim-cmp/#recommended-configuration).

## Configurations
Expand Down
2 changes: 1 addition & 1 deletion autoload/jc.vim
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
function! s:OnSave()
if get(g:, 'jc_autoformat_on_save', 1)
if get(g:, 'jc_autoformat_on_save', 0)
if has('nvim-0.8.0')
lua vim.lsp.buf.format({ async = false })
endif
Expand Down
11 changes: 7 additions & 4 deletions lua/jc.lua
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
local lsp = require("jc.lsp")
local server = require("jc.server")
local jdtls = require("jc.jdtls")

M = {}
local user_on_attach = function(_, _) end

local config = {
java_exec = "java",
jc_on_attach = function(client, bufnr)
lsp.on_attach(client, bufnr)
user_on_attach(client, bufnr)
jc_on_attach = function(args)
if args.data then
local client = vim.lsp.get_client_by_id(args.data.client_id)
lsp.on_attach(M.config, client, args.buf)
user_on_attach(client, args.buf)
end
end,
keys_prefix = "<leader>j",
}

M.setup = function(args)
Expand Down
4 changes: 2 additions & 2 deletions lua/jc/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ local mappings = require("jc.config.mappings")

local M = {}

function M.initialize_configuration(bufnr)
function M.initialize_configuration(conf, bufnr)
if vim.g.jc_default_mappings then
mappings.install_mappings(bufnr)
mappings.install_mappings(conf, bufnr)
end
end

Expand Down
49 changes: 34 additions & 15 deletions lua/jc/config/mappings.lua
Original file line number Diff line number Diff line change
@@ -1,31 +1,50 @@
local M = {}

function M.install_mappings(bufnr)
function M.install_mappings(conf, bufnr)
local opts = { noremap = true, silent = true }
local prefix = conf.keys_prefix

vim.api.nvim_buf_set_keymap(bufnr, "n", "<leader>jda", "<cmd>lua require('jc.vimspector').debug_attach()<CR>", opts)
vim.api.nvim_set_keymap("n", prefix .. "da", "<cmd>lua require('jc.vimspector').debug_attach()<CR>", opts)

vim.api.nvim_buf_set_keymap(bufnr, "n", "<leader>ji", "<cmd>lua require('jc.jdtls').organize_imports(true)<CR>", opts)
vim.api.nvim_buf_set_keymap(
bufnr,
"n",
"<leader>jI",
prefix .. "i",
"<cmd>lua require('jc.jdtls').organize_imports(true)<CR>",
opts
)
vim.api.nvim_buf_set_keymap(
bufnr,
"n",
prefix .. "I",
"<cmd>lua require('jc.jdtls').organize_imports(false)<CR>",
opts
)
vim.api.nvim_buf_set_keymap(bufnr, "i", "<C-j>i", "<cmd>lua require('jc.jdtls').organize_imports()<CR>", opts)
vim.api.nvim_buf_set_keymap(bufnr, "n", "<leader>jts", "<cmd>lua require('jc.jdtls').generate_toString()<CR>", opts)
vim.api.nvim_buf_set_keymap(bufnr, "n", prefix .. "ts", "<cmd>lua require('jc.jdtls').generate_toString()<CR>", opts)
vim.api.nvim_buf_set_keymap(
bufnr,
"n",
"<leader>jeq",
prefix .. "eq",
"<cmd>lua require('jc.jdtls').generate_hashCodeAndEquals()<CR>",
opts
)

vim.api.nvim_buf_set_keymap(bufnr, "n", "<leader>jA", "<cmd>lua require('jc.jdtls').generate_accessors()<CR>", opts)
vim.api.nvim_buf_set_keymap(bufnr, "n", "<leader>js", "<cmd>lua require('jc.jdtls').generate_accessor('s')<CR>", opts)
vim.api.nvim_buf_set_keymap(bufnr, "n", "<leader>jg", "<cmd>lua require('jc.jdtls').generate_accessor('g')<CR>", opts)
vim.api.nvim_buf_set_keymap(bufnr, "n", prefix .. "A", "<cmd>lua require('jc.jdtls').generate_accessors()<CR>", opts)
vim.api.nvim_buf_set_keymap(
bufnr,
"n",
prefix .. "s",
"<cmd>lua require('jc.jdtls').generate_accessor('s')<CR>",
opts
)
vim.api.nvim_buf_set_keymap(
bufnr,
"n",
prefix .. "g",
"<cmd>lua require('jc.jdtls').generate_accessor('g')<CR>",
opts
)
vim.api.nvim_buf_set_keymap(
bufnr,
"n",
Expand All @@ -40,30 +59,30 @@ function M.install_mappings(bufnr)
vim.api.nvim_buf_set_keymap(
bufnr,
"n",
"<leader>jc",
prefix .. "c",
"<cmd>lua require('jc.jdtls').generate_constructor(nil, nil, {default = false})<CR>",
opts
)
vim.api.nvim_buf_set_keymap(
bufnr,
"n",
"<leader>jcc",
prefix .. "cc",
"<cmd>lua require('jc.jdtls').generate_constructor(nil, nil, {default = true})<CR>",
opts
)

vim.api.nvim_buf_set_keymap(
bufnr,
"n",
"<leader>jm",
prefix .. "m",
"<cmd>lua require('jc.jdtls').generate_abstractMethods()<CR>",
opts
)
vim.api.nvim_buf_set_keymap(bufnr, "i", "<C-j>m", "<cmd>lua require('jc.jdtls').generate_abstractMethods()<CR>", opts)
vim.api.nvim_buf_set_keymap(
bufnr,
"n",
"<Leader>jn",
prefix .. "n",
"<cmd>lua require('jc.class_generator').generate_class()<CR>",
opts
)
Expand All @@ -72,15 +91,15 @@ function M.install_mappings(bufnr)
vim.api.nvim_buf_set_keymap(
bufnr,
"v",
"<leader>jre",
prefix .. "re",
"<Esc><Cmd>lua require('jdtls').extract_variable(true)<CR>",
opts
)
vim.api.nvim_buf_set_keymap(bufnr, "n", "<leader>jre", "<Cmd>lua require('jdtls').extract_variable()<CR>", opts)
vim.api.nvim_buf_set_keymap(
bufnr,
"v",
"<leader>jrm",
prefix .. "rm",
"<Esc><Cmd>lua require('jdtls').extract_method(true)<CR>",
opts
)
Expand Down
46 changes: 34 additions & 12 deletions lua/jc/jdtls.lua
Original file line number Diff line number Diff line change
Expand Up @@ -100,28 +100,49 @@ function M.generate_accessors(fields)
end

function M.generate_abstractMethods()
local curbuf = vim.api.nvim_get_current_buf()
local diagnostics = {}
local line = 0
for _, diagnostic in ipairs(vim.diagnostic.get(0)) do
if diagnostic.code == "67109264" then
diagnostic.range = {
start = {
character = diagnostic.col,
line = diagnostic.lnum,
},
["end"] = {
character = diagnostic.end_col,
line = diagnostic.end_lnum,
if line == 0 then
line = diagnostic.lnum
end
table.insert(diagnostics, {
code = diagnostic.code,
message = diagnostic.message,
severity = 1,
source = "Java",
range = {
start = {
character = diagnostic.col,
line = diagnostic.lnum,
},
["end"] = {
character = diagnostic.end_col,
line = diagnostic.end_lnum,
},
},
}
table.insert(diagnostics, diagnostic)
})
end
end
if diagnostics then
local params = vim.lsp.util.make_range_params()
params.context = {
diagnostics = diagnostics,
}
vim.lsp.buf_request(0, "textDocument/codeAction", params, function(err, actions)
params.range = {
start = {
character = 0,
line = line,
},
["end"] = {
character = 0,
line = line,
},
}
vim.notify(vim.print(params), vim.log.levels.DEBUG)
vim.lsp.buf_request(curbuf, "textDocument/codeAction", params, function(err, actions)
if actions then
local add_method_action = nil
for _, action in ipairs(actions) do
Expand All @@ -131,7 +152,8 @@ function M.generate_abstractMethods()
end
end
if add_method_action then
vim.lsp.buf_request(0, "codeAction/resolve", add_method_action, apply_edit)
vim.notify(vim.print(add_method_action), vim.log.levels.DEBUG)
vim.lsp.buf_request(curbuf, "codeAction/resolve", add_method_action, apply_edit)
else
vim.notify("No action found", vim.log.levels.INFO)
end
Expand Down
8 changes: 4 additions & 4 deletions lua/jc/lsp.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ local chains = require("jc.chains")()

local M = {}

function M.on_attach(_, bufnr)
config.initialize_configuration(bufnr)
function M.on_attach(conf, _, bufnr)
config.initialize_configuration(conf, bufnr)
end

function M.executeCommand(command, callback, on_failure)
local clients = vim.lsp.buf_get_clients()
local clients = vim.lsp.get_clients()
local capableClient = nil

for _, client in pairs(clients) do
Expand Down Expand Up @@ -41,7 +41,7 @@ function M.executeCommand(command, callback, on_failure)
end

function M.get_jdtls_client()
local clients = vim.lsp.get_active_clients()
local clients = vim.lsp.get_clients()
for _, client in ipairs(clients) do
if client.name == "jdtls" then
return client
Expand Down
Loading

0 comments on commit e9258bc

Please sign in to comment.