| 938 | } |
| 939 | |
| 940 | #createClientState(params: { |
| 941 | key: string; |
| 942 | server: LspServerDefinition; |
| 943 | client: LSPClient; |
| 944 | transportHandle: TransportHandle; |
| 945 | normalizedRootUri: string | null; |
| 946 | originalRootUri: string | null; |
| 947 | }): ClientState { |
| 948 | const { |
| 949 | key, |
| 950 | server, |
| 951 | client, |
| 952 | transportHandle, |
| 953 | normalizedRootUri, |
| 954 | originalRootUri, |
| 955 | } = params; |
| 956 | const fileRefs = new Map<string, Set<EditorView>>(); |
| 957 | const uriAliases = new Map<string, string>(); |
| 958 | const effectiveRoot = normalizedRootUri ?? originalRootUri ?? null; |
| 959 | |
| 960 | const attach = ( |
| 961 | uri: string, |
| 962 | view: EditorView, |
| 963 | aliases: string[] = [], |
| 964 | ): void => { |
| 965 | const existing = fileRefs.get(uri) ?? new Set(); |
| 966 | existing.add(view); |
| 967 | fileRefs.set(uri, existing); |
| 968 | uriAliases.set(uri, uri); |
| 969 | for (const alias of aliases) { |
| 970 | if (!alias || alias === uri) continue; |
| 971 | uriAliases.set(alias, uri); |
| 972 | } |
| 973 | const suffix = effectiveRoot ? ` (root ${effectiveRoot})` : ""; |
| 974 | logLspInfo(`[LSP:${server.id}] attached to ${uri}${suffix}`); |
| 975 | }; |
| 976 | |
| 977 | const detach = (uri: string, view?: EditorView): void => { |
| 978 | const actualUri = uriAliases.get(uri) ?? uri; |
| 979 | const existing = fileRefs.get(actualUri); |
| 980 | if (!existing) return; |
| 981 | if (view) existing.delete(view); |
| 982 | if (!view || !existing.size) { |
| 983 | fileRefs.delete(actualUri); |
| 984 | for (const [alias, target] of uriAliases.entries()) { |
| 985 | if (target === actualUri) { |
| 986 | uriAliases.delete(alias); |
| 987 | } |
| 988 | } |
| 989 | try { |
| 990 | // Only pass uri to closeFile - view is not needed for closing |
| 991 | // and passing it may cause issues if the view is already disposed |
| 992 | (client.workspace as AcodeWorkspace)?.closeFile?.(actualUri); |
| 993 | } catch (error) { |
| 994 | console.warn(`Failed to close LSP file ${actualUri}`, error); |
| 995 | } |
| 996 | } |
| 997 | |