java.lua 3.7 KB

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