Skip to content

Commit db7fe5c

Browse files
committed
feat: 兼容 ftplugin 配置启动
1 parent 21483b5 commit db7fe5c

3 files changed

Lines changed: 83 additions & 61 deletions

File tree

lua/spring_boot.lua

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1+
local jdt_ext_jars = {
2+
"io.projectreactor.reactor-core.jar",
3+
"org.reactivestreams.reactive-streams.jar",
4+
"jdt-ls-commons.jar",
5+
"jdt-ls-extension.jar",
6+
"sts-gradle-tooling.jar",
7+
}
18
vim.g.spring_boot = {
9+
autocmd = true,
210
jdt_extensions_path = nil,
311
-- https://github.com/spring-projects/sts4/blob/7d3d91ecfa6087ae2d0e0f595da61ce8f52fed96/vscode-extensions/vscode-spring-boot/package.json#L33
4-
jdt_extensions_jars = {
5-
"io.projectreactor.reactor-core.jar",
6-
"org.reactivestreams.reactive-streams.jar",
7-
"jdt-ls-commons.jar",
8-
"jdt-ls-extension.jar",
9-
"sts-gradle-tooling.jar",
10-
},
12+
jdt_extensions_jars = jdt_ext_jars,
1113
}
1214

1315
local M = {}
@@ -54,7 +56,7 @@ end
5456
M.java_extensions = function()
5557
local bundles = {}
5658
local function bundle_jar(path)
57-
for _, jar in ipairs(vim.g.spring_boot.jdt_extensions_jars) do
59+
for _, jar in ipairs(vim.g.spring_boot.jdt_extensions_jars or jdt_ext_jars) do
5860
if vim.endswith(path, jar) then
5961
return true
6062
end

lua/spring_boot/config.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ local M = {
2020

2121
local function init()
2222
local spring_boot = require("spring_boot")
23-
M = vim.tbl_deep_extend("keep", spring_boot._config, M)
23+
M = vim.tbl_deep_extend("keep", spring_boot._config or {}, M)
2424
spring_boot._config = nil
2525
end
2626
init()

lua/spring_boot/launch.lua

Lines changed: 72 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@ local root_dir = function()
1313
return vim.loop.cwd()
1414
end
1515

16-
local bootls_path = function()
17-
if config.ls_path then
18-
return config.ls_path
16+
---@param opts bootls.Config
17+
local bootls_path = function(opts)
18+
if opts.ls_path then
19+
return opts.ls_path
1920
end
2021
local bls = vscode.find_one("/vmware.vscode-spring-boot-*/language-server")
2122
if bls then
@@ -27,25 +28,27 @@ M.enable_classpath_listening = function()
2728
util.boot_execute_command("sts.vscode-spring-boot.enableClasspathListening", { true })
2829
end
2930

30-
local logfile = function(rt_dir)
31+
---@param opts bootls.Config
32+
local logfile = function(opts, rt_dir)
3133
local lf
32-
if config.log_file ~= nil then
33-
if type(config.log_file) == "function" then
34-
lf = config.log_file(rt_dir)
35-
elseif type(config.log_file) == "string" then
36-
lf = config.log_file == "" and nil or config.log_file
34+
if opts.log_file ~= nil then
35+
if type(opts.log_file) == "function" then
36+
lf = opts.log_file(rt_dir)
37+
elseif type(opts.log_file) == "string" then
38+
lf = opts.log_file == "" and nil or opts.log_file
3739
end
3840
end
3941
return lf or "/dev/null"
4042
end
4143

42-
local function bootls_cmd(rt_dir, java_cmd)
43-
local boot_path = bootls_path()
44+
---@param opts bootls.Config
45+
local function bootls_cmd(opts, rt_dir, java_cmd)
46+
local boot_path = bootls_path(opts)
4447
if not boot_path then
4548
vim.notify("Spring Boot LS is not installed", vim.log.levels.WARN)
4649
return
4750
end
48-
if config.exploded_ls_jar_data then
51+
if opts.exploded_ls_jar_data then
4952
local boot_classpath = {}
5053
table.insert(boot_classpath, boot_path .. "/BOOT-INF/classes")
5154
table.insert(boot_classpath, boot_path .. "/BOOT-INF/lib/*")
@@ -58,7 +61,7 @@ local function bootls_cmd(rt_dir, java_cmd)
5861
"-cp",
5962
table.concat(boot_classpath, util.is_win and ";" or ":"),
6063
"-Dsts.lsp.client=vscode",
61-
"-Dsts.log.file=" .. logfile(rt_dir),
64+
"-Dsts.log.file=" .. logfile(opts, rt_dir),
6265
"-Dspring.config.location=file:" .. boot_path .. "/BOOT-INF/classes/application.properties",
6366
-- "-Dlogging.level.org.springframework=DEBUG",
6467
"org.springframework.ide.vscode.boot.app.BootLanguageServerBootApp",
@@ -76,15 +79,15 @@ local function bootls_cmd(rt_dir, java_cmd)
7679
"-Xmx1G",
7780
"-XX:+UseZGC",
7881
"-Dsts.lsp.client=vscode",
79-
"-Dsts.log.file=" .. logfile(rt_dir),
82+
"-Dsts.log.file=" .. logfile(opts, rt_dir),
8083
"-jar",
8184
server_jar[1],
8285
}
8386
return cmd
8487
end
8588
end
8689

87-
local ls_config = {
90+
M.ls_config = {
8891
name = "spring-boot",
8992
filetypes = { "java", "yaml", "jproperties" },
9093
-- root_dir = root_dir,
@@ -112,63 +115,80 @@ local ls_config = {
112115
return filetype
113116
end,
114117
}
115-
classpath.register_classpath_service(ls_config)
116-
java_data.register_java_data_service(ls_config)
118+
classpath.register_classpath_service(M.ls_config)
119+
java_data.register_java_data_service(M.ls_config)
117120

118121
vim.lsp.commands["vscode-spring-boot.ls.start"] = function(_, _, _)
119122
M.enable_classpath_listening()
120123
return {}
121124
end
122125

123-
ls_config.handlers["sts/highlight"] = function() end
124-
ls_config.handlers["sts/moveCursor"] = function(err, result, ctx, config)
126+
M.ls_config.handlers["sts/highlight"] = function() end
127+
M.ls_config.handlers["sts/moveCursor"] = function(err, result, ctx, config)
125128
-- TODO: move cursor
126129
return { applied = true }
127130
end
128-
129-
M.setup = function(_)
130-
ls_config = vim.tbl_deep_extend("keep", ls_config, config.server)
131-
local capabilities = ls_config.capabilities or vim.lsp.protocol.make_client_capabilities()
131+
M._update_config = false
132+
M.update_config = function(opts)
133+
if M._update_config then
134+
return
135+
end
136+
M._update_config = true
137+
M.ls_config = vim.tbl_deep_extend("keep", M.ls_config, opts.server)
138+
local capabilities = M.ls_config.capabilities or vim.lsp.protocol.make_client_capabilities()
132139
capabilities.workspace = {
133140
executeCommand = { value = true },
134141
}
135-
ls_config.capabilities = capabilities
136-
if not ls_config.root_dir then
137-
ls_config.root_dir = root_dir()
142+
M.ls_config.capabilities = capabilities
143+
if not M.ls_config.root_dir then
144+
M.ls_config.root_dir = root_dir()
138145
end
139-
ls_config.cmd = (ls_config.cmd and #ls_config.cmd > 0) and ls_config.cmd
140-
or bootls_cmd(ls_config.root_dir, config.java_cmd)
141-
if not ls_config.cmd then
146+
M.ls_config.cmd = (M.ls_config.cmd and #M.ls_config.cmd > 0) and M.ls_config.cmd
147+
or bootls_cmd(opts, M.ls_config.root_dir, opts.java_cmd)
148+
if not M.ls_config.cmd then
142149
return
143150
end
144-
ls_config.init_options.workspaceFolders = ls_config.root_dir
145-
local on_init = ls_config.on_init
146-
ls_config.on_init = function(client, ctx)
151+
M.ls_config.init_options.workspaceFolders = M.ls_config.root_dir
152+
local on_init = M.ls_config.on_init
153+
M.ls_config.on_init = function(client, ctx)
147154
util.boot_ls_init(client, ctx)
148155
if on_init then
149156
on_init(client, ctx)
150157
end
151158
end
152-
local group = vim.api.nvim_create_augroup("spring_boot_ls", { clear = true })
153-
vim.api.nvim_create_autocmd({ "FileType" }, {
154-
group = group,
155-
pattern = { "java", "yaml", "jproperties" },
156-
desc = "Spring Boot Language Server",
157-
callback = function(e)
158-
if e.file == "java" and vim.bo[e.buf].buftype == "nofile" then
159-
return
160-
end
161-
if vim.endswith(e.file, "pom.xml") then
162-
return
163-
end
164-
if vim.endswith(e.file, ".yaml") or vim.endswith(e.file, ".yml") then
165-
if not util.is_application_yml_file(e.file) then
166-
return
167-
end
168-
end
169-
vim.lsp.start(ls_config)
170-
end,
171-
})
159+
end
160+
161+
M.setup = function(_)
162+
M.update_config(config)
163+
if vim.g.spring_boot.autocmd then
164+
local group = vim.api.nvim_create_augroup("spring_boot_ls", { clear = true })
165+
vim.api.nvim_create_autocmd({ "FileType" }, {
166+
group = group,
167+
pattern = { "java", "yaml", "jproperties" },
168+
desc = "Spring Boot Language Server",
169+
callback = function(_)
170+
M.start(M.ls_config)
171+
end,
172+
})
173+
end
174+
end
175+
M.start = function(opts)
176+
local buf = vim.api.nvim_get_current_buf()
177+
local filename = vim.uri_from_bufnr(buf)
178+
if vim.endswith(filename, "pom.xml") then
179+
return
180+
end
181+
if vim.endswith(filename, ".yaml") or vim.endswith(filename, ".yml") then
182+
if not util.is_application_yml_file(filename) then
183+
return
184+
end
185+
end
186+
if "jproperties" == vim.bo[buf].filetype then
187+
if not util.is_application_properties_file(filename) then
188+
return
189+
end
190+
end
191+
vim.lsp.start(opts)
172192
end
173193

174194
-- 参考资料

0 commit comments

Comments
 (0)