Skip to content

Commit 87e25ad

Browse files
Use VSCode Spring Boot Tools from mason-registry (#14)
* feat: added mason-registry for searching ls and some refactorings --------- Co-authored-by: luokai <kaiv2@outlook.com>
1 parent db7fe5c commit 87e25ad

File tree

6 files changed

+209
-175
lines changed

6 files changed

+209
-175
lines changed

README.md

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,36 +12,30 @@
1212

1313
## 安装
1414

15-
- `lazy.nvim`
15+
- `lazy.nvim` 使用 ``
1616
```lua
17+
-- 使用 `autocmd` 方式启动(默认)
18+
-- 默认使用 mason 或 ~/.vscode/extensions/vmware.vscode-spring-boot-x.xx.x 中的 jar
1719
{
1820
"JavaHello/spring-boot.nvim",
19-
ft = "java",
21+
ft = {"java", "yaml", "jproperties"},
2022
dependencies = {
2123
"mfussenegger/nvim-jdtls", -- or nvim-java, nvim-lspconfig
2224
"ibhagwan/fzf-lua", -- 可选
2325
},
24-
init = function()
25-
vim.g.spring_boot = {
26-
jdt_extensions_path = nil, -- 默认使用 ~/.vscode/extensions/vmware.vscode-spring-boot-x.xx.x
27-
jdt_extensions_jars = {
28-
"io.projectreactor.reactor-core.jar",
29-
"org.reactivestreams.reactive-streams.jar",
30-
"jdt-ls-commons.jar",
31-
"jdt-ls-extension.jar",
32-
"sts-gradle-tooling.jar",
33-
},
34-
}
35-
end,
36-
config = function()
37-
require("spring_boot").setup {
38-
ls_path = nil, -- 默认使用 ~/.vscode/extensions/vmware.vscode-spring-boot-x.xx.x
39-
jdtls_name = "jdtls",
40-
log_file = nil,
41-
java_cmd = nil,
42-
}
43-
end,
26+
---@type bootls.Config
27+
opts = {}
4428
},
29+
30+
-- 使用 `ftplugin` 或自定义 方式启动
31+
{
32+
"JavaHello/spring-boot.nvim",
33+
lazy = true,
34+
dependencies = {
35+
"mfussenegger/nvim-jdtls", -- or nvim-java, nvim-lspconfig
36+
},
37+
config = false
38+
}
4539
```
4640
- [Visual Studio Code](https://code.visualstudio.com/) 中安装[VScode Spring Boot](https://marketplace.visualstudio.com/items?itemName=vmware.vscode-spring-boot)(可选的)
4741

lua/spring_boot.lua

Lines changed: 94 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
1-
local jdt_ext_jars = {
1+
local jdt_extensions_jars = {
22
"io.projectreactor.reactor-core.jar",
33
"org.reactivestreams.reactive-streams.jar",
44
"jdt-ls-commons.jar",
55
"jdt-ls-extension.jar",
66
"sts-gradle-tooling.jar",
77
}
8-
vim.g.spring_boot = {
9-
autocmd = true,
8+
9+
local spring_boot = {
1010
jdt_extensions_path = nil,
1111
-- https://github.com/spring-projects/sts4/blob/7d3d91ecfa6087ae2d0e0f595da61ce8f52fed96/vscode-extensions/vscode-spring-boot/package.json#L33
12-
jdt_extensions_jars = jdt_ext_jars,
12+
jdt_expanded_extensions_jars = {},
13+
is_bundle_jar = function(path)
14+
for _, jar in ipairs(jdt_extensions_jars) do
15+
if vim.endswith(path, jar) then
16+
return true
17+
end
18+
end
19+
end,
1320
}
1421

1522
local M = {}
@@ -43,30 +50,103 @@ M.init_lsp_commands = function()
4350
end
4451
end
4552

53+
M.get_ls_from_mason = function()
54+
local result = M.get_from_mason_registry("vscode-spring-boot-tools", "vscode-spring-boot-tools/language-server.jar")
55+
if #result > 0 then
56+
return result[1]
57+
end
58+
return nil
59+
end
60+
61+
M.get_from_mason_registry = function(package_name, key_prefix)
62+
local success, mason_registry = pcall(require, "mason-registry")
63+
local result = {}
64+
if success then
65+
mason_registry.refresh()
66+
local mason_package = mason_registry.get_package(package_name)
67+
if mason_package == nil then
68+
return result
69+
end
70+
if mason_package:is_installed() then
71+
local install_path = mason_package:get_install_path()
72+
mason_package:get_receipt():if_present(function(recipe)
73+
for key, value in pairs(recipe.links.share) do
74+
if key:sub(1, #key_prefix) == key_prefix then
75+
table.insert(result, install_path .. "/" .. value)
76+
end
77+
end
78+
end)
79+
end
80+
end
81+
return result
82+
end
83+
4684
local initialized = false
85+
86+
---@param opts bootls.Config
4787
M.setup = function(opts)
4888
if initialized then
4989
return
5090
end
51-
M._config = opts
52-
require("spring_boot.launch").setup()
5391
initialized = true
92+
opts = vim.tbl_deep_extend("keep", opts or {}, require("spring_boot.config"))
93+
if not opts.ls_path then
94+
opts.ls_path = M.get_ls_from_mason() -- get ls from mason-registry
95+
if opts.ls_path then
96+
spring_boot.jdt_expanded_extensions_jars =
97+
M.get_from_mason_registry("vscode-spring-boot-tools", "vscode-spring-boot-tools/jdtls/")
98+
end
99+
end
100+
if not opts.ls_path then
101+
-- try to find ls on standard installation path of vscode
102+
opts.ls_path = require("spring_boot.vscode").find_one("/vmware.vscode-spring-boot-*/language-server")
103+
end
104+
if opts.ls_path then
105+
if vim.fn.isdirectory(opts.ls_path .. "/BOOT-INF") ~= 0 then
106+
-- it's an exploded jar
107+
opts.exploded_ls_jar_data = true
108+
else
109+
-- it's a single jar
110+
local server_jar = vim.split(vim.fn.glob(opts.ls_path .. "/spring-boot-language-server*.jar"), "\n")
111+
if #server_jar > 0 then
112+
opts.ls_path = server_jar[1]
113+
end
114+
end
115+
else
116+
-- all possibilities finding the language server failed
117+
vim.notify("Spring Boot LS is not installed", vim.log.levels.WARN)
118+
return
119+
end
120+
if vim.fn.isdirectory(opts.ls_path .. "/BOOT-INF") ~= 0 then
121+
-- a path was given in opts
122+
opts.exploded_ls_jar_data = true
123+
else
124+
opts.exploded_ls_jar_data = false
125+
end
126+
M.init_lsp_commands()
127+
128+
if opts.autocmd then
129+
require("spring_boot.launch").ls_autocmd(opts)
130+
end
131+
return opts
54132
end
55133

56134
M.java_extensions = function()
57-
local bundles = {}
58-
local function bundle_jar(path)
59-
for _, jar in ipairs(vim.g.spring_boot.jdt_extensions_jars or jdt_ext_jars) do
60-
if vim.endswith(path, jar) then
61-
return true
62-
end
135+
if spring_boot.jdt_expanded_extensions_jars and #spring_boot.jdt_expanded_extensions_jars > 0 then
136+
return spring_boot.jdt_expanded_extensions_jars
137+
end
138+
local bundles = M.get_from_mason_registry("vscode-spring-boot-tools", "vscode-spring-boot-tools/jdtls/")
139+
if #bundles > 0 then
140+
for _, v in pairs(bundles) do
141+
table.insert(spring_boot.jdt_expanded_extensions_jars, v)
63142
end
143+
return bundles
64144
end
65-
local spring_boot_path = vim.g.spring_boot.jdt_extensions_path
145+
local spring_boot_path = spring_boot.jdt_extensions_path
66146
or require("spring_boot.vscode").find_one("/vmware.vscode-spring-boot-*/jars")
67147
if spring_boot_path then
68148
for _, bundle in ipairs(vim.split(vim.fn.glob(spring_boot_path .. "/*.jar"), "\n")) do
69-
if bundle_jar(bundle) then
149+
if spring_boot.is_bundle_jar(bundle) then
70150
table.insert(bundles, bundle)
71151
end
72152
end

lua/spring_boot/config.lua

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
---@field log_file? string|function The path to the spring boot ls log file.
66
---@field server vim.lsp.ClientConfig The language server configuration.
77
---@field exploded_ls_jar_data boolean The exploded language server jar data.
8+
---@field autocmd boolean autimatically setup autocmd in neovim
89

910
---@type bootls.Config
1011
local M = {
@@ -16,13 +17,7 @@ local M = {
1617
server = {
1718
cmd = {},
1819
},
20+
autocmd = true,
1921
}
2022

21-
local function init()
22-
local spring_boot = require("spring_boot")
23-
M = vim.tbl_deep_extend("keep", spring_boot._config or {}, M)
24-
spring_boot._config = nil
25-
end
26-
init()
27-
2823
return M

0 commit comments

Comments
 (0)