Skip to content

Commit 5c55513

Browse files
authored
Merge pull request #82 from CodinGame/initialize-files-codingame-infrastructure
Initialize files codingame infrastructure
2 parents 709c689 + e7c21f2 commit 5c55513

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

src/createLanguageClient.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,18 @@ async function createLanguageClient (
113113
errorHandler: ErrorHandler,
114114
middleware?: Middleware
115115
): Promise<MonacoLanguageClient> {
116+
const allInitializationOptions = () => {
117+
const infrastructureInitOptions = infrastructure.getInitializationOptions?.()
118+
const languageInitOptions = typeof initializationOptions === 'function' ? initializationOptions() : initializationOptions
119+
if (infrastructureInitOptions != null || languageInitOptions != null) {
120+
return {
121+
...(infrastructureInitOptions ?? {}),
122+
...(languageInitOptions ?? {})
123+
}
124+
}
125+
return undefined
126+
}
127+
116128
const client = new MonacoLanguageClient({
117129
id: `${id}-languageclient`,
118130
name: `CodinGame ${id} Language Client`,
@@ -123,7 +135,7 @@ async function createLanguageClient (
123135
errorHandler,
124136
middleware,
125137
synchronize,
126-
initializationOptions
138+
initializationOptions: allInitializationOptions
127139
},
128140
connectionProvider: new CGLSPConnectionProvider(id, infrastructure)
129141
})

src/infrastructure.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { IWebSocket, WebSocketMessageReader, WebSocketMessageWriter, toSocket }
22
import { MessageTransports } from 'monaco-languageclient'
33
import * as monaco from 'monaco-editor'
44
import type * as vscode from 'vscode'
5-
import { TextDocumentSaveReason } from 'vscode-languageserver-protocol'
5+
import { TextDocumentSaveReason, LSPAny } from 'vscode-languageserver-protocol'
66
import { getFile, updateFile } from './customRequests'
77
import { LanguageClientManager } from './languageClient'
88
import { LanguageClientId, LanguageClientOptions } from './languageClientOptions'
@@ -47,6 +47,8 @@ export interface Infrastructure {
4747
* @param id The language server id
4848
*/
4949
openConnection (id: LanguageClientId): Promise<MessageTransports>
50+
51+
getInitializationOptions? (): LSPAny
5052
}
5153

5254
async function openWebsocketConnection (url: URL | string): Promise<MessageTransports> {
@@ -133,4 +135,21 @@ export abstract class CodinGameInfrastructure implements Infrastructure {
133135
throw new Error('Unable to connect to server')
134136
}
135137
}
138+
139+
public getInitializationOptions (): LSPAny {
140+
// Provide all open model content to the backend so it's able to write them on the disk
141+
// BEFORE starting the server or registering the workspace folders
142+
// The didOpen notification already contain the file content but some LSP (like gopls)
143+
// don't use it and needs the file to be up-to-date on the disk before the workspace folder is added
144+
const files = monaco.editor
145+
.getModels()
146+
.filter((model) => model.uri.scheme === 'file')
147+
.reduce((map, model) => {
148+
map[model.uri.toString(true)] = model.getValue()
149+
return map
150+
}, {} as Record<string, string>)
151+
return {
152+
files
153+
}
154+
}
136155
}

0 commit comments

Comments
 (0)