fix: windows jdtls support

This commit is contained in:
xuthus5 2023-05-03 20:21:17 +08:00
parent 27b6726aea
commit 3391a6d60a
Signed by: xuthus5
GPG Key ID: A23CF9620CBB55F9
2 changed files with 54 additions and 12 deletions

View File

@ -2,25 +2,38 @@ function IsDirectoryExist(dirname)
return os.rename(dirname, dirname) and true or false
end
function Path2Windows(path)
return (string.gsub(path, "/", "\\"))
end
local jdtls = require("jdtls")
local root_markers = { ".git", "mvnw", "gradlew", "pom.xml", "build.gradle" }
local root_dir = require("jdtls.setup").find_root(root_markers)
if root_dir == "" then
return
end
local project_name = vim.fn.fnamemodify(vim.fn.getcwd(), ":p:h:t")
local home = os.getenv("HOME")
local jdtls_home = home .. "/.jdtls"
local plugins_home = jdtls_home .. "/plugins"
local workspace = jdtls_home .. "/workspace/" .. project_name
local launcher = plugins_home .. "/org.eclipse.equinox.launcher_1.6.400.v20210924-0641.jar"
local configuration = jdtls_home .. "/config_linux"
if vim.loop.os_uname().sysname == "Windows" then
jdtls_home = Path2Windows(jdtls_home)
plugins_home = Path2Windows(plugins_home)
workspace = Path2Windows(workspace)
launcher = Path2Windows(launcher)
configuration = jdtls_home .. "\\config_win"
end
-- create when workspace not exist
if not IsDirectoryExist(workspace) then
os.execute("mkdir " .. workspace)
end
local launcher = plugins_home .. "/org.eclipse.equinox.launcher_1.6.400.v20210924-0641.jar"
local configuration = jdtls_home .. "/config_linux"
if vim.loop.os_uname().sysname == "Windows" then
configuration = jdtls_home .. "\\config_win"
end
@ -44,6 +57,7 @@ local config = {
"--add-opens",
"java.base/java.lang=ALL-UNNAMED",
"-javaagent:" .. jdtls_home .. "/bin/lombok.jar",
"-Xbootclasspath/a:" .. jdtls_home .. "/bin/lombok.jar",
"-jar",
launcher,
"-configuration",
@ -113,7 +127,6 @@ local config = {
},
},
},
init_options = {
bundles = {},
},

View File

@ -8,13 +8,36 @@ function IsFileExist(filename)
end
end
function IsWindows()
local delimeter = package.config.sub(1, 1)
if delimeter == "/" then
return false
end
return true
end
function Path2Windows(path)
return (string.gsub(path, "/", "\\"))
end
function IsDirectoryExist(dirname)
return os.rename(dirname, dirname) and true or false
end
local jdtlspath = os.getenv("HOME") .. "/.jdtls"
if vim.loop.os_uname().sysname == "Windows" then
jdtlspath = os.getenv("HOME") .. "\\.jdtls"
local homepage = os.getenv("HOME")
if IsWindows() then
homepage = os.getenv("USERPROFILE")
end
local jdtlspath = homepage .. "/.jdtls"
local jdtls_bin = jdtlspath .. "/bin/jdtls"
local lombok_jar = jdtlspath .. "/bin/lombok.jar"
local temp_download = os.getenv("TEMP")
if IsWindows() then
jdtlspath = Path2Windows(jdtlspath)
jdtls_bin = Path2Windows(jdtls_bin)
lombok_jar = Path2Windows(lombok_jar)
end
-- 创建目录
@ -23,29 +46,35 @@ if not IsFileExist(jdtlspath) then
end
-- 检测文件存在否
if not IsDirectoryExist(jdtlspath .. "/bin/jdtls") then
if not IsDirectoryExist(jdtls_bin) then
print("download jdtls binary")
local download_ret = os.execute(
"wget https://download.eclipse.org/jdtls/milestones/1.23.0/jdt-language-server-1.23.0-202304271346.tar.gz -P /tmp"
"curl --create-dirs -O --output-dir "
.. temp_download
.. " https://download.eclipse.org/jdtls/milestones/1.23.0/jdt-language-server-1.23.0-202304271346.tar.gz"
)
if download_ret ~= 0 then
vim.notify("download jdtls binary failed")
return
end
vim.notify("download jdtls success")
local tar_ret = os.execute("tar xvzf /tmp/jdt-language-server-1.23.0-202304271346.tar.gz -C " .. jdtlspath)
local tar_ret =
os.execute("tar xvzf " .. temp_download .. "/jdt-language-server-1.23.0-202304271346.tar.gz -C " .. jdtlspath)
if tar_ret ~= 0 then
vim.notify("extract jdtls file failed")
return
end
vim.notify("install jdtls success")
end
local lombok_path = jdtlspath .. "/bin/lombok.jar"
-- 下载lombok
if not IsFileExist(jdtlspath .. "/bin/lombok.jar") then
if not IsFileExist(lombok_jar) then
vim.notify("download lombok.jar")
local download_ret = os.execute("wget https://projectlombok.org/downloads/lombok.jar -P " .. jdtlspath .. "/bin")
local download_ret = os.execute(
"curl --create-dirs -O --output-dir " .. jdtlspath .. "/bin https://projectlombok.org/downloads/lombok.jar"
)
if download_ret ~= 0 then
vim.notify("download lombok failed")
end
vim.notify("download lombok.jar success")
end