|
1 | | -local jdt_ext_jars = { |
| 1 | +local jdt_extensions_jars = { |
2 | 2 | "io.projectreactor.reactor-core.jar", |
3 | 3 | "org.reactivestreams.reactive-streams.jar", |
4 | 4 | "jdt-ls-commons.jar", |
5 | 5 | "jdt-ls-extension.jar", |
6 | 6 | "sts-gradle-tooling.jar", |
7 | 7 | } |
8 | | -vim.g.spring_boot = { |
9 | | - autocmd = true, |
| 8 | + |
| 9 | +local spring_boot = { |
10 | 10 | jdt_extensions_path = nil, |
11 | 11 | -- 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, |
13 | 20 | } |
14 | 21 |
|
15 | 22 | local M = {} |
@@ -43,30 +50,103 @@ M.init_lsp_commands = function() |
43 | 50 | end |
44 | 51 | end |
45 | 52 |
|
| 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 | + |
46 | 84 | local initialized = false |
| 85 | + |
| 86 | +---@param opts bootls.Config |
47 | 87 | M.setup = function(opts) |
48 | 88 | if initialized then |
49 | 89 | return |
50 | 90 | end |
51 | | - M._config = opts |
52 | | - require("spring_boot.launch").setup() |
53 | 91 | 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 |
54 | 132 | end |
55 | 133 |
|
56 | 134 | 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) |
63 | 142 | end |
| 143 | + return bundles |
64 | 144 | end |
65 | | - local spring_boot_path = vim.g.spring_boot.jdt_extensions_path |
| 145 | + local spring_boot_path = spring_boot.jdt_extensions_path |
66 | 146 | or require("spring_boot.vscode").find_one("/vmware.vscode-spring-boot-*/jars") |
67 | 147 | if spring_boot_path then |
68 | 148 | 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 |
70 | 150 | table.insert(bundles, bundle) |
71 | 151 | end |
72 | 152 | end |
|
0 commit comments