Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 16 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,36 +12,30 @@

## 安装

- `lazy.nvim`
- `lazy.nvim` 使用 ``
```lua
-- 使用 `autocmd` 方式启动(默认)
-- 默认使用 mason 或 ~/.vscode/extensions/vmware.vscode-spring-boot-x.xx.x 中的 jar
{
"JavaHello/spring-boot.nvim",
ft = "java",
ft = {"java", "yaml", "jproperties"},
dependencies = {
"mfussenegger/nvim-jdtls", -- or nvim-java, nvim-lspconfig
"ibhagwan/fzf-lua", -- 可选
},
init = function()
vim.g.spring_boot = {
jdt_extensions_path = nil, -- 默认使用 ~/.vscode/extensions/vmware.vscode-spring-boot-x.xx.x
jdt_extensions_jars = {
"io.projectreactor.reactor-core.jar",
"org.reactivestreams.reactive-streams.jar",
"jdt-ls-commons.jar",
"jdt-ls-extension.jar",
"sts-gradle-tooling.jar",
},
}
end,
config = function()
require("spring_boot").setup {
ls_path = nil, -- 默认使用 ~/.vscode/extensions/vmware.vscode-spring-boot-x.xx.x
jdtls_name = "jdtls",
log_file = nil,
java_cmd = nil,
}
end,
---@type bootls.Config
opts = {}
},

-- 使用 `ftplugin` 或自定义 方式启动
{
"JavaHello/spring-boot.nvim",
lazy = true,
dependencies = {
"mfussenegger/nvim-jdtls", -- or nvim-java, nvim-lspconfig
},
config = false
}
```
- [Visual Studio Code](https://code.visualstudio.com/) 中安装[VScode Spring Boot](https://marketplace.visualstudio.com/items?itemName=vmware.vscode-spring-boot)(可选的)

Expand Down
108 changes: 94 additions & 14 deletions lua/spring_boot.lua
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
local jdt_ext_jars = {
local jdt_extensions_jars = {
"io.projectreactor.reactor-core.jar",
"org.reactivestreams.reactive-streams.jar",
"jdt-ls-commons.jar",
"jdt-ls-extension.jar",
"sts-gradle-tooling.jar",
}
vim.g.spring_boot = {
autocmd = true,

local spring_boot = {
jdt_extensions_path = nil,
-- https://github.com/spring-projects/sts4/blob/7d3d91ecfa6087ae2d0e0f595da61ce8f52fed96/vscode-extensions/vscode-spring-boot/package.json#L33
jdt_extensions_jars = jdt_ext_jars,
jdt_expanded_extensions_jars = {},
is_bundle_jar = function(path)
for _, jar in ipairs(jdt_extensions_jars) do
if vim.endswith(path, jar) then
return true
end
end
end,
}

local M = {}
Expand Down Expand Up @@ -43,30 +50,103 @@ M.init_lsp_commands = function()
end
end

M.get_ls_from_mason = function()
local result = M.get_from_mason_registry("vscode-spring-boot-tools", "vscode-spring-boot-tools/language-server.jar")
if #result > 0 then
return result[1]
end
return nil
end

M.get_from_mason_registry = function(package_name, key_prefix)
local success, mason_registry = pcall(require, "mason-registry")
local result = {}
if success then
mason_registry.refresh()
local mason_package = mason_registry.get_package(package_name)
if mason_package == nil then
return result
end
if mason_package:is_installed() then
local install_path = mason_package:get_install_path()
mason_package:get_receipt():if_present(function(recipe)
for key, value in pairs(recipe.links.share) do
if key:sub(1, #key_prefix) == key_prefix then
table.insert(result, install_path .. "/" .. value)
end
end
end)
end
end
return result
end

local initialized = false

---@param opts bootls.Config
M.setup = function(opts)
if initialized then
return
end
M._config = opts
require("spring_boot.launch").setup()
initialized = true
opts = vim.tbl_deep_extend("keep", opts or {}, require("spring_boot.config"))
if not opts.ls_path then
opts.ls_path = M.get_ls_from_mason() -- get ls from mason-registry
if opts.ls_path then
spring_boot.jdt_expanded_extensions_jars =
M.get_from_mason_registry("vscode-spring-boot-tools", "vscode-spring-boot-tools/jdtls/")
end
end
if not opts.ls_path then
-- try to find ls on standard installation path of vscode
opts.ls_path = require("spring_boot.vscode").find_one("/vmware.vscode-spring-boot-*/language-server")
end
if opts.ls_path then
if vim.fn.isdirectory(opts.ls_path .. "/BOOT-INF") ~= 0 then
-- it's an exploded jar
opts.exploded_ls_jar_data = true
else
-- it's a single jar
local server_jar = vim.split(vim.fn.glob(opts.ls_path .. "/spring-boot-language-server*.jar"), "\n")
if #server_jar > 0 then
opts.ls_path = server_jar[1]
end
end
else
-- all possibilities finding the language server failed
vim.notify("Spring Boot LS is not installed", vim.log.levels.WARN)
return
end
if vim.fn.isdirectory(opts.ls_path .. "/BOOT-INF") ~= 0 then
-- a path was given in opts
opts.exploded_ls_jar_data = true
else
opts.exploded_ls_jar_data = false
end
M.init_lsp_commands()

if opts.autocmd then
require("spring_boot.launch").ls_autocmd(opts)
end
return opts
end

M.java_extensions = function()
local bundles = {}
local function bundle_jar(path)
for _, jar in ipairs(vim.g.spring_boot.jdt_extensions_jars or jdt_ext_jars) do
if vim.endswith(path, jar) then
return true
end
if spring_boot.jdt_expanded_extensions_jars and #spring_boot.jdt_expanded_extensions_jars > 0 then
return spring_boot.jdt_expanded_extensions_jars
end
local bundles = M.get_from_mason_registry("vscode-spring-boot-tools", "vscode-spring-boot-tools/jdtls/")
if #bundles > 0 then
for _, v in pairs(bundles) do
table.insert(spring_boot.jdt_expanded_extensions_jars, v)
end
return bundles
end
local spring_boot_path = vim.g.spring_boot.jdt_extensions_path
local spring_boot_path = spring_boot.jdt_extensions_path
or require("spring_boot.vscode").find_one("/vmware.vscode-spring-boot-*/jars")
if spring_boot_path then
for _, bundle in ipairs(vim.split(vim.fn.glob(spring_boot_path .. "/*.jar"), "\n")) do
if bundle_jar(bundle) then
if spring_boot.is_bundle_jar(bundle) then
table.insert(bundles, bundle)
end
end
Expand Down
9 changes: 2 additions & 7 deletions lua/spring_boot/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
---@field log_file? string|function The path to the spring boot ls log file.
---@field server vim.lsp.ClientConfig The language server configuration.
---@field exploded_ls_jar_data boolean The exploded language server jar data.
---@field autocmd boolean autimatically setup autocmd in neovim

---@type bootls.Config
local M = {
Expand All @@ -16,13 +17,7 @@ local M = {
server = {
cmd = {},
},
autocmd = true,
}

local function init()
local spring_boot = require("spring_boot")
M = vim.tbl_deep_extend("keep", spring_boot._config or {}, M)
spring_boot._config = nil
end
init()

return M
Loading