2023-09-05 16:02:27 -07:00
|
|
|
import type * as LSP from 'vscode-languageserver-protocol'
|
2024-06-30 14:30:44 -07:00
|
|
|
|
|
|
|
import { FromServer, IntoServer } from './codec'
|
|
|
|
import Client from './jsonrpc'
|
|
|
|
import { LanguageServerPlugin } from '../plugin/lsp'
|
2024-02-15 13:56:31 -08:00
|
|
|
|
2023-09-05 16:02:27 -07:00
|
|
|
// https://microsoft.github.io/language-server-protocol/specifications/specification-current/
|
|
|
|
|
|
|
|
// Client to server then server to client
|
|
|
|
interface LSPRequestMap {
|
|
|
|
initialize: [LSP.InitializeParams, LSP.InitializeResult]
|
|
|
|
'textDocument/hover': [LSP.HoverParams, LSP.Hover]
|
|
|
|
'textDocument/completion': [
|
|
|
|
LSP.CompletionParams,
|
|
|
|
LSP.CompletionItem[] | LSP.CompletionList | null
|
|
|
|
]
|
|
|
|
'textDocument/semanticTokens/full': [
|
|
|
|
LSP.SemanticTokensParams,
|
|
|
|
LSP.SemanticTokens
|
|
|
|
]
|
2024-04-15 17:18:32 -07:00
|
|
|
'textDocument/formatting': [
|
|
|
|
LSP.DocumentFormattingParams,
|
|
|
|
LSP.TextEdit[] | null
|
|
|
|
]
|
|
|
|
'textDocument/foldingRange': [LSP.FoldingRangeParams, LSP.FoldingRange[]]
|
2023-09-05 16:02:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Client to server
|
|
|
|
interface LSPNotifyMap {
|
|
|
|
initialized: LSP.InitializedParams
|
|
|
|
'textDocument/didChange': LSP.DidChangeTextDocumentParams
|
|
|
|
'textDocument/didOpen': LSP.DidOpenTextDocumentParams
|
2024-03-11 17:50:31 -07:00
|
|
|
'textDocument/didClose': LSP.DidCloseTextDocumentParams
|
|
|
|
'workspace/didChangeWorkspaceFolders': LSP.DidChangeWorkspaceFoldersParams
|
2024-03-12 13:37:47 -07:00
|
|
|
'workspace/didCreateFiles': LSP.CreateFilesParams
|
|
|
|
'workspace/didRenameFiles': LSP.RenameFilesParams
|
|
|
|
'workspace/didDeleteFiles': LSP.DeleteFilesParams
|
2023-09-05 16:02:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
export interface LanguageServerClientOptions {
|
2024-06-30 14:30:44 -07:00
|
|
|
name: string
|
|
|
|
fromServer: FromServer
|
|
|
|
intoServer: IntoServer
|
|
|
|
initializedCallback: () => void
|
2023-09-05 16:02:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
export class LanguageServerClient {
|
|
|
|
private client: Client
|
2024-06-30 14:30:44 -07:00
|
|
|
readonly name: string
|
2023-09-05 16:02:27 -07:00
|
|
|
|
|
|
|
public ready: boolean
|
|
|
|
|
2024-04-15 17:18:32 -07:00
|
|
|
readonly plugins: LanguageServerPlugin[]
|
2023-09-05 16:02:27 -07:00
|
|
|
|
|
|
|
public initializePromise: Promise<void>
|
|
|
|
|
|
|
|
constructor(options: LanguageServerClientOptions) {
|
2024-02-19 12:33:16 -08:00
|
|
|
this.name = options.name
|
2024-06-30 14:30:44 -07:00
|
|
|
this.plugins = []
|
|
|
|
|
|
|
|
this.client = new Client(
|
|
|
|
options.fromServer,
|
|
|
|
options.intoServer,
|
|
|
|
options.initializedCallback
|
|
|
|
)
|
2023-09-05 16:02:27 -07:00
|
|
|
|
|
|
|
this.ready = false
|
|
|
|
|
|
|
|
this.initializePromise = this.initialize()
|
|
|
|
}
|
|
|
|
|
|
|
|
async initialize() {
|
|
|
|
// Start the client in the background.
|
2024-02-19 12:33:16 -08:00
|
|
|
this.client.setNotifyFn(this.processNotifications.bind(this))
|
2024-09-09 18:17:45 -04:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-floating-promises
|
2024-02-11 12:59:00 +11:00
|
|
|
this.client.start()
|
2023-09-05 16:02:27 -07:00
|
|
|
|
|
|
|
this.ready = true
|
|
|
|
}
|
|
|
|
|
2024-02-19 12:33:16 -08:00
|
|
|
getName(): string {
|
|
|
|
return this.name
|
|
|
|
}
|
|
|
|
|
2023-09-05 16:02:27 -07:00
|
|
|
getServerCapabilities(): LSP.ServerCapabilities<any> {
|
|
|
|
return this.client.getServerCapabilities()
|
|
|
|
}
|
|
|
|
|
|
|
|
close() {}
|
|
|
|
|
|
|
|
textDocumentDidOpen(params: LSP.DidOpenTextDocumentParams) {
|
|
|
|
this.notify('textDocument/didOpen', params)
|
|
|
|
}
|
|
|
|
|
|
|
|
textDocumentDidChange(params: LSP.DidChangeTextDocumentParams) {
|
|
|
|
this.notify('textDocument/didChange', params)
|
|
|
|
}
|
|
|
|
|
2024-03-11 17:50:31 -07:00
|
|
|
textDocumentDidClose(params: LSP.DidCloseTextDocumentParams) {
|
|
|
|
this.notify('textDocument/didClose', params)
|
|
|
|
}
|
|
|
|
|
|
|
|
workspaceDidChangeWorkspaceFolders(
|
|
|
|
added: LSP.WorkspaceFolder[],
|
|
|
|
removed: LSP.WorkspaceFolder[]
|
|
|
|
) {
|
|
|
|
this.notify('workspace/didChangeWorkspaceFolders', {
|
|
|
|
event: { added, removed },
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-03-12 13:37:47 -07:00
|
|
|
workspaceDidCreateFiles(params: LSP.CreateFilesParams) {
|
|
|
|
this.notify('workspace/didCreateFiles', params)
|
|
|
|
}
|
|
|
|
|
|
|
|
workspaceDidRenameFiles(params: LSP.RenameFilesParams) {
|
|
|
|
this.notify('workspace/didRenameFiles', params)
|
|
|
|
}
|
|
|
|
|
|
|
|
workspaceDidDeleteFiles(params: LSP.DeleteFilesParams) {
|
|
|
|
this.notify('workspace/didDeleteFiles', params)
|
|
|
|
}
|
|
|
|
|
2024-06-29 18:10:07 -07:00
|
|
|
async textDocumentSemanticTokensFull(params: LSP.SemanticTokensParams) {
|
2024-02-19 12:33:16 -08:00
|
|
|
const serverCapabilities = this.getServerCapabilities()
|
|
|
|
if (!serverCapabilities.semanticTokensProvider) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-06-29 18:10:07 -07:00
|
|
|
return this.request('textDocument/semanticTokens/full', params)
|
2023-09-05 16:02:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
async textDocumentHover(params: LSP.HoverParams) {
|
2024-02-19 12:33:16 -08:00
|
|
|
const serverCapabilities = this.getServerCapabilities()
|
|
|
|
if (!serverCapabilities.hoverProvider) {
|
|
|
|
return
|
|
|
|
}
|
2023-09-05 16:02:27 -07:00
|
|
|
return await this.request('textDocument/hover', params)
|
|
|
|
}
|
|
|
|
|
2024-04-15 17:18:32 -07:00
|
|
|
async textDocumentFormatting(params: LSP.DocumentFormattingParams) {
|
|
|
|
const serverCapabilities = this.getServerCapabilities()
|
|
|
|
if (!serverCapabilities.documentFormattingProvider) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return await this.request('textDocument/formatting', params)
|
|
|
|
}
|
|
|
|
|
|
|
|
async textDocumentFoldingRange(params: LSP.FoldingRangeParams) {
|
|
|
|
const serverCapabilities = this.getServerCapabilities()
|
|
|
|
if (!serverCapabilities.foldingRangeProvider) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return await this.request('textDocument/foldingRange', params)
|
|
|
|
}
|
|
|
|
|
2023-09-05 16:02:27 -07:00
|
|
|
async textDocumentCompletion(params: LSP.CompletionParams) {
|
2024-02-19 12:33:16 -08:00
|
|
|
const serverCapabilities = this.getServerCapabilities()
|
|
|
|
if (!serverCapabilities.completionProvider) {
|
|
|
|
return
|
|
|
|
}
|
2024-04-16 21:36:19 -07:00
|
|
|
const response = await this.request('textDocument/completion', params)
|
|
|
|
return response
|
2023-09-05 16:02:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
attachPlugin(plugin: LanguageServerPlugin) {
|
|
|
|
this.plugins.push(plugin)
|
|
|
|
}
|
|
|
|
|
|
|
|
detachPlugin(plugin: LanguageServerPlugin) {
|
|
|
|
const i = this.plugins.indexOf(plugin)
|
|
|
|
if (i === -1) return
|
|
|
|
this.plugins.splice(i, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
private request<K extends keyof LSPRequestMap>(
|
|
|
|
method: K,
|
|
|
|
params: LSPRequestMap[K][0]
|
|
|
|
): Promise<LSPRequestMap[K][1]> {
|
|
|
|
return this.client.request(method, params) as Promise<LSPRequestMap[K][1]>
|
|
|
|
}
|
|
|
|
|
2024-06-30 14:30:44 -07:00
|
|
|
requestCustom<P, R>(method: string, params: P): Promise<R> {
|
|
|
|
return this.client.request(method, params) as Promise<R>
|
|
|
|
}
|
|
|
|
|
2023-09-05 16:02:27 -07:00
|
|
|
private notify<K extends keyof LSPNotifyMap>(
|
|
|
|
method: K,
|
|
|
|
params: LSPNotifyMap[K]
|
|
|
|
): void {
|
|
|
|
return this.client.notify(method, params)
|
|
|
|
}
|
|
|
|
|
2024-06-30 14:30:44 -07:00
|
|
|
notifyCustom<P>(method: string, params: P): void {
|
|
|
|
return this.client.notify(method, params)
|
2024-02-15 13:56:31 -08:00
|
|
|
}
|
|
|
|
|
2024-02-19 12:33:16 -08:00
|
|
|
private processNotifications(notification: LSP.NotificationMessage) {
|
2024-09-09 18:17:45 -04:00
|
|
|
for (const plugin of this.plugins) {
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-floating-promises
|
|
|
|
plugin.processNotification(notification)
|
|
|
|
}
|
2023-09-05 16:02:27 -07:00
|
|
|
}
|
|
|
|
}
|