浏览代码

beiginning java support

Chris 4 年之前
父节点
当前提交
d16093b422
共有 8 个文件被更改,包括 98 次插入1 次删除
  1. 5 0
      init.vim
  2. 23 1
      keys/which-key.vim
  3. 10 0
      lua/lsp-wrapper.vim
  4. 8 0
      lua/lsp/java-ls.lua
  5. 17 0
      plug-config/lsp-config.vim
  6. 11 0
      spell/en.utf-8.add
  7. 二进制
      spell/en.utf-8.add.spl
  8. 24 0
      utils/bin/java-linux-ls

+ 5 - 0
init.vim

@@ -61,6 +61,7 @@ else
   luafile ~/.config/nvim/lua/lsp/lua-ls.lua
   luafile ~/.config/nvim/lua/lsp/python-ls.lua
   luafile ~/.config/nvim/lua/lsp/bash-ls.lua
+  autocmd FileType java luafile ~/.config/nvim/lua/lsp/java-ls.lua
   luafile ~/.config/nvim/lua/lsp/css-ls.lua
   luafile ~/.config/nvim/lua/lsp/docker-ls.lua
   luafile ~/.config/nvim/lua/lsp/graphql-ls.lua
@@ -84,3 +85,7 @@ endif
   " fix space and tab triggering completion all the time
   " Provide diagnostics for nvim tree 
   " port everything possible to lua
+  " augroup lsp
+  "   au!
+  "   au FileType java lua require('jdtls').start_or_attach({cmd = {'java-linux-ls'}})
+  " augroup end

+ 23 - 1
keys/which-key.vim

@@ -138,6 +138,29 @@ let g:which_key_map.f = {
       \ 'p' : [':Farr --source=rgnvim'     , 'project'],
       \ }
 
+" command! -buffer JdtCompile lua require('jdtls').compile()
+" command! -buffer JdtUpdateConfig lua require('jdtls').update_project_config()
+" command! -buffer JdtJol lua require('jdtls').jol()
+" command! -buffer JdtBytecode lua require('jdtls').javap()
+" command! -buffer JdtJshell lua require('jdtls').jshell()
+
+" j is for java
+let g:which_key_map.j = {
+      \ 'name' : '+java' ,
+      \ 'c' : [':AsyncTask file-compile'      , 'compile file'],
+      \ 'b' : [':AsyncTask project-build'     , 'build project'],
+      \ 'e' : [':AsyncTaskEdit'               , 'edit local tasks'],
+      \ 'f' : [':AsyncTaskFzf'                , 'find task'],
+      \ 'g' : [':AsyncTaskEdit!'              , 'edit global tasks'],
+      \ 'h' : [':AsyncTaskList!'              , 'list hidden tasks'],
+      \ 'l' : [':CocList tasks'               , 'list tasks'],
+      \ 'm' : [':AsyncTaskMacro'              , 'macro help'],
+      \ 'o' : [':copen'                       , 'open task view'],
+      \ 'r' : [':AsyncTask file-run'          , 'run file'],
+      \ 'p' : [':AsyncTask project-run'       , 'run project'],
+      \ 'x' : [':cclose'                      , 'close task view'],
+      \ }
+
 " k is for task
 let g:which_key_map.k = {
       \ 'name' : '+task' ,
@@ -147,7 +170,6 @@ let g:which_key_map.k = {
       \ 'f' : [':AsyncTaskFzf'                , 'find task'],
       \ 'g' : [':AsyncTaskEdit!'              , 'edit global tasks'],
       \ 'h' : [':AsyncTaskList!'              , 'list hidden tasks'],
-      \ 'l' : [':CocList tasks'               , 'list tasks'],
       \ 'm' : [':AsyncTaskMacro'              , 'macro help'],
       \ 'o' : [':copen'                       , 'open task view'],
       \ 'r' : [':AsyncTask file-run'          , 'run file'],

+ 10 - 0
lua/lsp-wrapper.vim

@@ -25,3 +25,13 @@ command! LspShowLineDiagnostics lua require 'lsp-wrapper'.show_line_diagnostics(
 " command! LspIncomingCalls lua require 'lsp-wrapper'.incoming_calls()
 " command! LspOutGoingCalls lua require 'lsp-wrapper'.outgoing_calls()
 " command! LspDocumentHighlight lua require 'lsp-wrapper'.document_highlight()
+
+" Java
+
+" command! FileType java LspCodeAction <Esc><Cmd>lua require('jdtls').code_action(true)<CR>
+" command! FileType java LspCodeAction <Esc><Cmd>lua require('jdtls').code_action(false, 'refactor')<CR>
+
+" nnoremap <A-o> <Cmd>lua require'jdtls'.organize_imports()<CR>
+" nnoremap crv <Cmd>lua require('jdtls').extract_variable()<CR>
+" vnoremap crv <Esc><Cmd>lua require('jdtls').extract_variable(true)<CR>
+" vnoremap crm <Esc><Cmd>lua require('jdtls').extract_method(true)<CR>

+ 8 - 0
lua/lsp/java-ls.lua

@@ -0,0 +1,8 @@
+-- In Vimscript
+-- augroup lsp
+--   au!
+--   au FileType java lua require('jdtls').start_or_attach({cmd = {'java-linux-ls'}})
+-- augroup end
+-- find_root looks for parent directories relative to the current buffer containing one of the given arguments.
+-- require'lspconfig'.jdtls.setup {cmd = {'java-linux-ls'}}
+require('jdtls').start_or_attach({cmd = {'java-linux-ls'}, root_dir = require('jdtls.setup').find_root({'gradle.build', 'pom.xml'})})

+ 17 - 0
plug-config/lsp-config.vim

@@ -17,3 +17,20 @@ autocmd BufWritePre *.jsx lua vim.lsp.buf.formatting_sync(nil, 100)
 autocmd BufWritePre *.lua lua vim.lsp.buf.formatting_sync(nil, 100)
 " autocmd BufWritePre *.py lua vim.lsp.buf.formatting_sync(nil, 100)
 
+" -- `code_action` is a superset of vim.lsp.buf.code_action and you'll be able to
+" -- use this mapping also with other language servers
+
+autocmd FileType java nnoremap ca <Cmd>lua require('jdtls').code_action()<CR>
+
+
+
+" -- If using nvim-dap
+" -- This requires java-debug and vscode-java-test bundles, see install steps in this README further below.
+" nnoremap <leader>df <Cmd>lua require'jdtls'.test_class()<CR>
+" nnoremap <leader>dn <Cmd>lua require'jdtls'.test_nearest_method()<CR>
+
+command! -buffer JdtCompile lua require('jdtls').compile()
+command! -buffer JdtUpdateConfig lua require('jdtls').update_project_config()
+command! -buffer JdtJol lua require('jdtls').jol()
+command! -buffer JdtBytecode lua require('jdtls').javap()
+command! -buffer JdtJshell lua require('jdtls').jshell()

+ 11 - 0
spell/en.utf-8.add

@@ -30,3 +30,14 @@ dir
 efm
 LuaRocks
 LuaFormatter
+SDKMAN
+sdkman
+Gradle
+JVM
+java
+gradle
+kotlin
+springboot
+jdtls
+plugin
+linux

二进制
spell/en.utf-8.add.spl


+ 24 - 0
utils/bin/java-linux-ls

@@ -0,0 +1,24 @@
+#!/usr/bin/env bash
+
+# NOTE:
+# This doesn't work as is on Windows. You'll need to create an equivalent `.bat` file instead
+#
+# NOTE:
+# If you're not using Linux you'll need to adjust the `-configuration` option
+# to point to the `config_mac' or `config_win` folders depending on your system.
+
+JAR="$HOME/.config/nvim/eclipse.jdt.ls/org.eclipse.jdt.ls.product/target/repository/plugins/org.eclipse.equinox.launcher_*.jar"
+GRADLE_HOME=$HOME/gradle $HOME/.sdkman/candidates/java/current/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/nvim/eclipse.jdt.ls/org.eclipse.jdt.ls.product/target/repository/config_linux" \
+  -data "${1:-$HOME/workspace}" \
+  --add-modules=ALL-SYSTEM \
+  --add-opens java.base/java.util=ALL-UNNAMED \
+  --add-opens java.base/java.lang=ALL-UNNAMED