2023-09-05 16:02:27 -07:00
|
|
|
import type * as LSP from 'vscode-languageserver-protocol'
|
|
|
|
import Client from './client'
|
2024-02-19 12:33:16 -08:00
|
|
|
import { SemanticToken, deserializeTokens } from './kcl/semantic_tokens'
|
|
|
|
import { LanguageServerPlugin } from 'editor/plugins/lsp/plugin'
|
2023-09-05 16:02:27 -07:00
|
|
|
|
2024-02-15 13:56:31 -08:00
|
|
|
export interface CopilotGetCompletionsParams {
|
|
|
|
doc: {
|
|
|
|
source: string
|
|
|
|
tabSize: number
|
|
|
|
indentSize: number
|
|
|
|
insertSpaces: boolean
|
|
|
|
path: string
|
|
|
|
uri: string
|
|
|
|
relativePath: string
|
|
|
|
languageId: string
|
|
|
|
position: {
|
|
|
|
line: number
|
|
|
|
character: number
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
interface CopilotGetCompletionsResult {
|
|
|
|
completions: {
|
|
|
|
text: string
|
|
|
|
position: {
|
|
|
|
line: number
|
|
|
|
character: number
|
|
|
|
}
|
|
|
|
uuid: string
|
|
|
|
range: {
|
|
|
|
start: {
|
|
|
|
line: number
|
|
|
|
character: number
|
|
|
|
}
|
|
|
|
end: {
|
|
|
|
line: number
|
|
|
|
character: number
|
|
|
|
}
|
|
|
|
}
|
|
|
|
displayText: string
|
|
|
|
point: {
|
|
|
|
line: number
|
|
|
|
character: number
|
|
|
|
}
|
|
|
|
region: {
|
|
|
|
start: {
|
|
|
|
line: number
|
|
|
|
character: number
|
|
|
|
}
|
|
|
|
end: {
|
|
|
|
line: number
|
|
|
|
character: number
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}[]
|
|
|
|
}
|
|
|
|
|
|
|
|
interface CopilotAcceptCompletionParams {
|
|
|
|
uuid: string
|
|
|
|
}
|
|
|
|
|
|
|
|
interface CopilotRejectCompletionParams {
|
|
|
|
uuids: string[]
|
|
|
|
}
|
|
|
|
|
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-02-15 13:56:31 -08:00
|
|
|
getCompletions: [CopilotGetCompletionsParams, CopilotGetCompletionsResult]
|
|
|
|
notifyAccepted: [CopilotAcceptCompletionParams, any]
|
|
|
|
notifyRejected: [CopilotRejectCompletionParams, any]
|
2023-09-05 16:02:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Client to server
|
|
|
|
interface LSPNotifyMap {
|
|
|
|
initialized: LSP.InitializedParams
|
|
|
|
'textDocument/didChange': LSP.DidChangeTextDocumentParams
|
|
|
|
'textDocument/didOpen': LSP.DidOpenTextDocumentParams
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface LanguageServerClientOptions {
|
|
|
|
client: Client
|
2024-02-19 12:33:16 -08:00
|
|
|
name: string
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface LanguageServerOptions {
|
|
|
|
// We assume this is the main project directory, we are currently working in.
|
|
|
|
workspaceFolders: LSP.WorkspaceFolder[]
|
|
|
|
documentUri: string
|
|
|
|
allowHTMLContent: boolean
|
|
|
|
client: LanguageServerClient
|
2023-09-05 16:02:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
export class LanguageServerClient {
|
|
|
|
private client: Client
|
2024-02-19 12:33:16 -08:00
|
|
|
private name: string
|
2023-09-05 16:02:27 -07:00
|
|
|
|
|
|
|
public ready: boolean
|
|
|
|
|
|
|
|
private plugins: LanguageServerPlugin[]
|
|
|
|
|
|
|
|
public initializePromise: Promise<void>
|
|
|
|
|
|
|
|
private isUpdatingSemanticTokens: boolean = false
|
|
|
|
private semanticTokens: SemanticToken[] = []
|
2024-02-15 13:56:31 -08:00
|
|
|
private queuedUids: string[] = []
|
2023-09-05 16:02:27 -07:00
|
|
|
|
|
|
|
constructor(options: LanguageServerClientOptions) {
|
|
|
|
this.plugins = []
|
|
|
|
this.client = options.client
|
2024-02-19 12:33:16 -08:00
|
|
|
this.name = options.name
|
2023-09-05 16:02:27 -07:00
|
|
|
|
|
|
|
this.ready = false
|
|
|
|
|
2024-02-15 13:56:31 -08:00
|
|
|
this.queuedUids = []
|
2023-09-05 16:02:27 -07:00
|
|
|
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-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)
|
|
|
|
|
2024-02-11 12:59:00 +11:00
|
|
|
this.updateSemanticTokens(params.textDocument.uri)
|
2023-09-05 16:02:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
textDocumentDidChange(params: LSP.DidChangeTextDocumentParams) {
|
|
|
|
this.notify('textDocument/didChange', params)
|
2024-02-11 12:59:00 +11:00
|
|
|
this.updateSemanticTokens(params.textDocument.uri)
|
2023-09-05 16:02:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
async updateSemanticTokens(uri: string) {
|
2024-02-19 12:33:16 -08:00
|
|
|
const serverCapabilities = this.getServerCapabilities()
|
|
|
|
if (!serverCapabilities.semanticTokensProvider) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-09-05 16:02:27 -07:00
|
|
|
// Make sure we can only run, if we aren't already running.
|
|
|
|
if (!this.isUpdatingSemanticTokens) {
|
|
|
|
this.isUpdatingSemanticTokens = true
|
|
|
|
|
|
|
|
const result = await this.request('textDocument/semanticTokens/full', {
|
|
|
|
textDocument: {
|
|
|
|
uri,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
this.semanticTokens = deserializeTokens(
|
|
|
|
result.data,
|
|
|
|
this.getServerCapabilities().semanticTokensProvider
|
|
|
|
)
|
|
|
|
|
|
|
|
this.isUpdatingSemanticTokens = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
getSemanticTokens(): SemanticToken[] {
|
|
|
|
return this.semanticTokens
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
async textDocumentCompletion(params: LSP.CompletionParams) {
|
2024-02-19 12:33:16 -08:00
|
|
|
const serverCapabilities = this.getServerCapabilities()
|
|
|
|
if (!serverCapabilities.completionProvider) {
|
|
|
|
return
|
|
|
|
}
|
2023-09-05 16:02:27 -07:00
|
|
|
return await this.request('textDocument/completion', params)
|
|
|
|
}
|
|
|
|
|
|
|
|
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]>
|
|
|
|
}
|
|
|
|
|
|
|
|
private notify<K extends keyof LSPNotifyMap>(
|
|
|
|
method: K,
|
|
|
|
params: LSPNotifyMap[K]
|
|
|
|
): void {
|
|
|
|
return this.client.notify(method, params)
|
|
|
|
}
|
|
|
|
|
2024-02-15 13:56:31 -08:00
|
|
|
async getCompletion(params: CopilotGetCompletionsParams) {
|
|
|
|
const response = await this.request('getCompletions', params)
|
|
|
|
//
|
|
|
|
this.queuedUids = [...response.completions.map((c) => c.uuid)]
|
|
|
|
return response
|
|
|
|
}
|
|
|
|
|
|
|
|
async accept(uuid: string) {
|
|
|
|
const badUids = this.queuedUids.filter((u) => u !== uuid)
|
|
|
|
this.queuedUids = []
|
|
|
|
await this.acceptCompletion({ uuid })
|
|
|
|
await this.rejectCompletions({ uuids: badUids })
|
|
|
|
}
|
|
|
|
|
|
|
|
async reject() {
|
|
|
|
const badUids = this.queuedUids
|
|
|
|
this.queuedUids = []
|
|
|
|
return await this.rejectCompletions({ uuids: badUids })
|
|
|
|
}
|
|
|
|
|
|
|
|
async acceptCompletion(params: CopilotAcceptCompletionParams) {
|
|
|
|
return await this.request('notifyAccepted', params)
|
|
|
|
}
|
2024-02-19 12:33:16 -08:00
|
|
|
|
2024-02-15 13:56:31 -08:00
|
|
|
async rejectCompletions(params: CopilotRejectCompletionParams) {
|
|
|
|
return await this.request('notifyRejected', params)
|
|
|
|
}
|
|
|
|
|
2024-02-19 12:33:16 -08:00
|
|
|
private processNotifications(notification: LSP.NotificationMessage) {
|
2023-09-05 16:02:27 -07:00
|
|
|
for (const plugin of this.plugins) plugin.processNotification(notification)
|
|
|
|
}
|
|
|
|
}
|