(
bundle: LspServerBundle,
options: { replace?: boolean } = {},
)
| 39 | } |
| 40 | |
| 41 | export function registerServerBundle( |
| 42 | bundle: LspServerBundle, |
| 43 | options: { replace?: boolean } = {}, |
| 44 | ): LspServerBundle { |
| 45 | const { replace = false } = options; |
| 46 | const key = toKey(bundle.id); |
| 47 | if (!key) { |
| 48 | throw new Error("LSP server bundle requires a non-empty id"); |
| 49 | } |
| 50 | |
| 51 | if (bundles.has(key) && !replace) { |
| 52 | const existing = bundles.get(key); |
| 53 | if (existing) return existing; |
| 54 | } |
| 55 | |
| 56 | const registry = requireRegistry(); |
| 57 | const definitions = resolveBundleServers(bundle); |
| 58 | const previousIds = bundleServers.get(key) || new Set<string>(); |
| 59 | const nextIds = new Set<string>(); |
| 60 | |
| 61 | for (const definition of definitions) { |
| 62 | const serverId = toKey(definition.id); |
| 63 | if (!serverId) { |
| 64 | throw new Error(`LSP server bundle ${key} returned a server without id`); |
| 65 | } |
| 66 | |
| 67 | const owner = serverOwners.get(serverId); |
| 68 | if (owner && owner !== key && !replace) { |
| 69 | throw new Error( |
| 70 | `LSP server ${serverId} is already provided by ${owner}; ${key} must replace explicitly`, |
| 71 | ); |
| 72 | } |
| 73 | |
| 74 | registry.registerServer(definition, { replace: true }); |
| 75 | serverOwners.set(serverId, key); |
| 76 | nextIds.add(serverId); |
| 77 | } |
| 78 | |
| 79 | for (const previousId of previousIds) { |
| 80 | if (!nextIds.has(previousId) && serverOwners.get(previousId) === key) { |
| 81 | registry.unregisterServer(previousId); |
| 82 | serverOwners.delete(previousId); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | const normalizedBundle = { |
| 87 | ...bundle, |
| 88 | id: key, |
| 89 | }; |
| 90 | bundles.set(key, normalizedBundle); |
| 91 | bundleServers.set(key, nextIds); |
| 92 | return normalizedBundle; |
| 93 | } |
| 94 | |
| 95 | export function unregisterServerBundle(id: string): boolean { |
| 96 | const key = toKey(id); |
no test coverage detected