summaryrefslogtreecommitdiff
path: root/lua/lang/java.lua
blob: d84c000095e998293bd86eb85d5d735f788438ff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
local M = {}

M.config = function()
  O.lang.java = {
    java_tools = {
      active = false,
    },
    formatter = {
      exe = "prettier",
      args = { "--stdin-filepath", vim.api.nvim_buf_get_name(0), "--single-quote" },
    },
  }
end

M.format = function()
  vim.cmd "let proj = FindRootDirectory()"
  local root_dir = vim.api.nvim_get_var "proj"

  -- use the global prettier if you didn't find the local one
  local prettier_instance = root_dir .. "/node_modules/.bin/prettier"
  if vim.fn.executable(prettier_instance) ~= 1 then
    prettier_instance = O.lang.tsserver.formatter.exe
  end

  O.formatters.filetype["java"] = {
    function()
      return {
        exe = prettier_instance,
        -- TODO: allow user to override this
        args = { "--stdin-filepath", vim.api.nvim_buf_get_name(0) },
        stdin = true,
      }
    end,
  }

  require("formatter.config").set_defaults {
    logging = false,
    filetype = O.formatters.filetype,
  }
end

M.lint = function()
  -- TODO: implement linters (if applicable)
  return "No linters configured!"
end

M.lsp = function()
  if require("lv-utils").check_lsp_client_active "jdtls" then
    return
  end

  if O.lang.java.java_tools.active then
    -- find_root looks for parent directories relative to the current buffer containing one of the given arguments.
    if vim.fn.has "mac" == 1 then
      WORKSPACE_PATH = "/Users/" .. USER .. "/workspace/"
    elseif vim.fn.has "unix" == 1 then
      WORKSPACE_PATH = "/home/" .. USER .. "/workspace/"
    else
      print "Unsupported system"
    end

    JAVA_LS_EXECUTABLE = os.getenv "HOME" .. "/.local/share/lunarvim/lvim/utils/bin/jdtls"

    require("jdtls").start_or_attach {
      on_attach = require("lsp").common_on_attach,
      cmd = { JAVA_LS_EXECUTABLE, WORKSPACE_PATH .. vim.fn.fnamemodify(vim.fn.getcwd(), ":p:h:t") },
    }

    vim.api.nvim_set_keymap(
      "n",
      "<leader>la",
      ":lua require('jdtls').code_action()<CR>",
      { noremap = true, silent = true }
    )
    vim.api.nvim_set_keymap(
      "n",
      "<leader>lR",
      ":lua require('jdtls').code_action(false, 'refactor')<CR>",
      { noremap = true, silent = true }
    )

    vim.cmd "command! -buffer JdtCompile lua require('jdtls').compile()"
    vim.cmd "command! -buffer JdtUpdateConfig lua require('jdtls').update_project_config()"
    -- vim.cmd "command! -buffer JdtJol lua require('jdtls').jol()"
    vim.cmd "command! -buffer JdtBytecode lua require('jdtls').javap()"
    -- vim.cmd "command! -buffer JdtJshell lua require('jdtls').jshell()"
  else
    local util = require "lspconfig/util"

    require("lspconfig").jdtls.setup {
      on_attach = require("lsp").common_on_attach,
      cmd = { DATA_PATH .. "/lspinstall/java/jdtls.sh" },
      filetypes = { "java" },
      root_dir = util.root_pattern { ".git", "build.gradle", "pom.xml" },
      -- init_options = {bundles = bundles}
      -- on_attach = require'lsp'.common_on_attach
    }
  end
end

M.dap = function()
  -- TODO: implement dap
  return "No DAP configured!"
end

-- local bundles = {
--     vim.fn.glob(
--         CONFIG_PATH.."/.debuggers/java-debug/com.microsoft.java.debug.plugin/target/com.microsoft.java.debug.plugin-*.jar")
-- };

-- require('jdtls').start_or_attach({
--     on_attach = on_attach,
--     cmd = {DATA_PATH .. "/lspinstall/java/jdtls.sh"},
--     root_dir = require('jdtls.setup').find_root({'build.gradle', 'pom.xml', '.git'}),
--     init_options = {bundles = bundles}
-- })

-- TODO: setup autoformat stuff later
-- _java = {
--     -- {'FileType', 'java', 'luafile '..CONFIG_PATH..'/lua/lsp/java-ls.lua'},
--     {
--         'FileType', 'java',
--         'nnoremap ca <Cmd>lua require(\'jdtls\').code_action()<CR>'
--     }
-- }

return M