Skip to content

Commit 4a7cbc3

Browse files
author
Loïc Mangeonjean
committed
fix: filter file initialization with document selector
1 parent 8d4dae0 commit 4a7cbc3

File tree

2 files changed

+12
-10
lines changed

2 files changed

+12
-10
lines changed

src/createLanguageClient.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ async function createLanguageClient (
129129
middleware?: Middleware
130130
): Promise<MonacoLanguageClient> {
131131
const allInitializationOptions = () => {
132-
const infrastructureInitOptions = infrastructure.getInitializationOptions?.()
132+
const infrastructureInitOptions = infrastructure.getInitializationOptions?.(documentSelector)
133133
const languageInitOptions = typeof initializationOptions === 'function' ? initializationOptions() : initializationOptions
134134
if (infrastructureInitOptions != null || languageInitOptions != null) {
135135
return {

src/infrastructure.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { IWebSocket, WebSocketMessageReader, WebSocketMessageWriter, toSocket } from 'vscode-ws-jsonrpc'
22
import { MessageTransports } from 'vscode-languageclient'
33
import * as monaco from 'monaco-editor'
4-
import type * as vscode from 'vscode'
4+
import * as vscode from 'vscode'
55
import { LSPAny } from 'vscode-languageserver-protocol'
66
import { getFile, updateFile } from './customRequests'
77
import { LanguageClientManager } from './languageClient'
@@ -48,7 +48,7 @@ export interface Infrastructure {
4848
*/
4949
openConnection (id: LanguageClientId): Promise<MessageTransports>
5050

51-
getInitializationOptions? (): LSPAny
51+
getInitializationOptions? (documentSelector?: vscode.DocumentSelector): LSPAny
5252
}
5353

5454
class CloseOnDisposeWebSocketMessageReader extends WebSocketMessageReader {
@@ -141,18 +141,20 @@ export abstract class CodinGameInfrastructure implements Infrastructure {
141141
}
142142
}
143143

144-
public getInitializationOptions (): LSPAny {
144+
public getInitializationOptions (documentSelector?: vscode.DocumentSelector): LSPAny {
145145
// Provide all open model content to the backend so it's able to write them on the disk
146146
// BEFORE starting the server or registering the workspace folders
147147
// The didOpen notification already contain the file content but some LSP (like gopls)
148148
// don't use it and needs the file to be up-to-date on the disk before the workspace folder is added
149-
const files = monaco.editor
150-
.getModels()
151-
.filter((model) => model.uri.scheme === 'file')
152-
.reduce((map, model) => {
153-
map[model.uri.toString(true)] = model.getValue()
149+
let documents = vscode.workspace.textDocuments.filter(doc => doc.uri.scheme === 'file')
150+
if (documentSelector != null) {
151+
documents = documents.filter(doc => vscode.languages.match(documentSelector, doc))
152+
}
153+
const files = documents
154+
.reduce((map, doc) => {
155+
map[doc.uri.toString(true)] = doc.getText()
154156
return map
155-
}, {} as Record<string, string>)
157+
}, <Record<string, string>>{})
156158
return {
157159
files
158160
}

0 commit comments

Comments
 (0)