java.lua 3.5 KB

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