java.lua 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. vim.cmd "let proj = FindRootDirectory()"
  2. local root_dir = vim.api.nvim_get_var "proj"
  3. -- use the global prettier if you didn't find the local one
  4. local prettier_instance = root_dir .. "/node_modules/.bin/prettier"
  5. if vim.fn.executable(prettier_instance) ~= 1 then
  6. prettier_instance = O.lang.tsserver.formatter.exe
  7. end
  8. O.formatters.filetype["java"] = {
  9. function()
  10. return {
  11. exe = prettier_instance,
  12. -- TODO: allow user to override this
  13. args = { "--stdin-filepath", vim.api.nvim_buf_get_name(0) },
  14. stdin = true,
  15. }
  16. end,
  17. }
  18. require("formatter.config").set_defaults {
  19. logging = false,
  20. filetype = O.formatters.filetype,
  21. }
  22. if require("lv-utils").check_lsp_client_active "jdtls" then
  23. return
  24. end
  25. if O.lang.java.java_tools.active then
  26. -- find_root looks for parent directories relative to the current buffer containing one of the given arguments.
  27. if vim.fn.has "mac" == 1 then
  28. WORKSPACE_PATH = "/Users/" .. USER .. "/workspace/"
  29. elseif vim.fn.has "unix" == 1 then
  30. WORKSPACE_PATH = "/home/" .. USER .. "/workspace/"
  31. else
  32. print "Unsupported system"
  33. end
  34. JAVA_LS_EXECUTABLE = CONFIG_PATH .. "/utils/bin/jdtls"
  35. require("jdtls").start_or_attach {
  36. on_attach = require("lsp").common_on_attach,
  37. cmd = { JAVA_LS_EXECUTABLE, WORKSPACE_PATH .. vim.fn.fnamemodify(vim.fn.getcwd(), ":p:h:t") },
  38. }
  39. vim.api.nvim_set_keymap(
  40. "n",
  41. "<leader>la",
  42. ":lua require('jdtls').code_action()<CR>",
  43. { noremap = true, silent = true }
  44. )
  45. vim.api.nvim_set_keymap(
  46. "n",
  47. "<leader>lR",
  48. ":lua require('jdtls').code_action(false, 'refactor')<CR>",
  49. { noremap = true, silent = true }
  50. )
  51. vim.cmd "command! -buffer JdtCompile lua require('jdtls').compile()"
  52. vim.cmd "command! -buffer JdtUpdateConfig lua require('jdtls').update_project_config()"
  53. -- vim.cmd "command! -buffer JdtJol lua require('jdtls').jol()"
  54. vim.cmd "command! -buffer JdtBytecode lua require('jdtls').javap()"
  55. -- vim.cmd "command! -buffer JdtJshell lua require('jdtls').jshell()"
  56. else
  57. local util = require "lspconfig/util"
  58. require("lspconfig").jdtls.setup {
  59. on_attach = require("lsp").common_on_attach,
  60. cmd = { DATA_PATH .. "/lspinstall/java/jdtls.sh" },
  61. filetypes = { "java" },
  62. root_dir = util.root_pattern { ".git", "build.gradle", "pom.xml" },
  63. -- init_options = {bundles = bundles}
  64. -- on_attach = require'lsp'.common_on_attach
  65. }
  66. end
  67. -- local bundles = {
  68. -- vim.fn.glob(
  69. -- CONFIG_PATH.."/.debuggers/java-debug/com.microsoft.java.debug.plugin/target/com.microsoft.java.debug.plugin-*.jar")
  70. -- };
  71. -- require('jdtls').start_or_attach({
  72. -- on_attach = on_attach,
  73. -- cmd = {DATA_PATH .. "/lspinstall/java/jdtls.sh"},
  74. -- root_dir = require('jdtls.setup').find_root({'build.gradle', 'pom.xml', '.git'}),
  75. -- init_options = {bundles = bundles}
  76. -- })
  77. -- TODO: setup autoformat stuff later
  78. -- _java = {
  79. -- -- {'FileType', 'java', 'luafile '..CONFIG_PATH..'/lua/lsp/java-ls.lua'},
  80. -- {
  81. -- 'FileType', 'java',
  82. -- 'nnoremap ca <Cmd>lua require(\'jdtls\').code_action()<CR>'
  83. -- }
  84. -- }