Skip to content

Commit 8c241a5

Browse files
committed
handle l10n setup on web using webworker messaging
Signed-off-by: David Thompson <davthomp@redhat.com>
1 parent 7b38a36 commit 8c241a5

File tree

4 files changed

+86
-81
lines changed

4 files changed

+86
-81
lines changed

src/server.ts

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,16 @@
55
* Licensed under the MIT License. See License.txt in the project root for license information.
66
*--------------------------------------------------------------------------------------------*/
77

8-
import { createConnection, Connection, ProposedFeatures } from 'vscode-languageserver/node';
8+
import { promises as fs, existsSync } from 'fs';
9+
import { Connection, createConnection, InitializeParams, ProposedFeatures } from 'vscode-languageserver/node';
10+
import { TelemetryImpl } from './languageserver/telemetry';
911
import { schemaRequestHandler, workspaceContext } from './languageservice/services/schemaRequestHandler';
12+
import { convertErrorToTelemetryMsg } from './languageservice/utils/objects';
1013
import { YAMLServerInit } from './yamlServerInit';
1114
import { SettingsState } from './yamlSettings';
12-
import { promises as fs } from 'fs';
13-
import { convertErrorToTelemetryMsg } from './languageservice/utils/objects';
14-
import { TelemetryImpl } from './languageserver/telemetry';
15+
import * as path from 'path';
16+
import * as l10n from '@vscode/l10n';
17+
import { URI } from 'vscode-uri';
1518

1619
// Create a connection for the server.
1720
let connection: Connection = null;
@@ -66,4 +69,23 @@ const schemaRequestHandlerWrapper = (connection: Connection, uri: string): Promi
6669
const schemaRequestService = schemaRequestHandlerWrapper.bind(this, connection);
6770
const telemetry = new TelemetryImpl(connection);
6871

69-
new YAMLServerInit(connection, yamlSettings, workspaceContext, schemaRequestService, telemetry).start();
72+
async function setupl10nBundle(params: InitializeParams): Promise<void> {
73+
const __dirname = path.dirname(__filename);
74+
const l10nPath: string = params.initializationOptions?.l10nPath || path.join(__dirname, '../../../l10n');
75+
const locale: string = params.locale || 'en';
76+
if (l10nPath) {
77+
const bundleFile = !existsSync(path.join(l10nPath, `bundle.l10n.${locale}.json`))
78+
? `bundle.l10n.json`
79+
: `bundle.l10n.${locale}.json`;
80+
const baseBundleFile = path.join(l10nPath, bundleFile);
81+
process.env.VSCODE_NLS_CONFIG = JSON.stringify({
82+
locale,
83+
_languagePackSupport: true,
84+
});
85+
await l10n.config({
86+
uri: URI.file(baseBundleFile).toString(),
87+
});
88+
}
89+
}
90+
91+
new YAMLServerInit(connection, yamlSettings, workspaceContext, schemaRequestService, telemetry, setupl10nBundle).start();

src/webworker/yamlServerMain.ts

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
* Copyright (c) Microsoft Corporation. All rights reserved.
44
* Licensed under the MIT License. See License.txt in the project root for license information.
55
*--------------------------------------------------------------------------------------------*/
6+
import * as l10n from '@vscode/l10n';
67
import { Connection, RequestType } from 'vscode-languageserver';
7-
import { createConnection, BrowserMessageReader, BrowserMessageWriter } from 'vscode-languageserver/browser';
8+
import { BrowserMessageReader, BrowserMessageWriter, createConnection } from 'vscode-languageserver/browser';
89
import { TelemetryImpl } from '../languageserver/telemetry';
910
import { schemaRequestHandler, workspaceContext } from '../languageservice/services/schemaRequestHandler';
1011
import { YAMLServerInit } from '../yamlServerInit';
@@ -15,35 +16,38 @@ namespace FSReadFile {
1516
export const type: RequestType<string, string, unknown> = new RequestType('fs/readFile');
1617
}
1718

18-
const messageReader = new BrowserMessageReader(globalThis);
19-
const messageWriter = new BrowserMessageWriter(globalThis);
20-
21-
const connection = createConnection(messageReader, messageWriter);
22-
23-
const yamlSettings = new SettingsState();
24-
25-
const fileSystem = {
26-
readFile: (fsPath: string) => {
27-
return connection.sendRequest(FSReadFile.type, fsPath);
28-
},
19+
self.onmessage = (e) => {
20+
const messageReader = new BrowserMessageReader(globalThis);
21+
const messageWriter = new BrowserMessageWriter(globalThis);
22+
23+
const connection = createConnection(messageReader, messageWriter);
24+
25+
const yamlSettings = new SettingsState();
26+
27+
const fileSystem = {
28+
readFile: (fsPath: string) => {
29+
return connection.sendRequest(FSReadFile.type, fsPath);
30+
},
31+
};
32+
33+
/**
34+
* Handles schema content requests given the schema URI
35+
* @param uri can be a local file, vscode request, http(s) request or a custom request
36+
*/
37+
const schemaRequestHandlerWrapper = (connection: Connection, uri: string): Promise<string> => {
38+
return schemaRequestHandler(
39+
connection,
40+
uri,
41+
yamlSettings.workspaceFolders,
42+
yamlSettings.workspaceRoot,
43+
yamlSettings.useVSCodeContentRequest,
44+
fileSystem
45+
);
46+
};
47+
48+
const schemaRequestService = schemaRequestHandlerWrapper.bind(this, connection);
49+
const telemetry = new TelemetryImpl(connection);
50+
51+
l10n.config({ contents: e.data.l10nBundle });
52+
new YAMLServerInit(connection, yamlSettings, workspaceContext, schemaRequestService, telemetry).start();
2953
};
30-
31-
/**
32-
* Handles schema content requests given the schema URI
33-
* @param uri can be a local file, vscode request, http(s) request or a custom request
34-
*/
35-
const schemaRequestHandlerWrapper = (connection: Connection, uri: string): Promise<string> => {
36-
return schemaRequestHandler(
37-
connection,
38-
uri,
39-
yamlSettings.workspaceFolders,
40-
yamlSettings.workspaceRoot,
41-
yamlSettings.useVSCodeContentRequest,
42-
fileSystem
43-
);
44-
};
45-
46-
const schemaRequestService = schemaRequestHandlerWrapper.bind(this, connection);
47-
const telemetry = new TelemetryImpl(connection);
48-
49-
new YAMLServerInit(connection, yamlSettings, workspaceContext, schemaRequestService, telemetry).start();

src/yamlServerInit.ts

Lines changed: 14 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,23 @@
11
import { Connection, InitializeParams, InitializeResult, TextDocumentSyncKind } from 'vscode-languageserver';
2-
import {
3-
getLanguageService as getCustomLanguageService,
4-
LanguageService,
5-
SchemaRequestService,
6-
WorkspaceContextService,
7-
} from './languageservice/yamlLanguageService';
8-
import { workspaceFoldersChanged } from './languageservice/utils/paths';
92
import { URI } from 'vscode-uri';
10-
import { SettingsState } from './yamlSettings';
3+
import { YamlCommands } from './commands';
4+
import { commandExecutor } from './languageserver/commandExecutor';
115
import { LanguageHandlers } from './languageserver/handlers/languageHandlers';
126
import { NotificationHandlers } from './languageserver/handlers/notificationHandlers';
137
import { RequestHandlers } from './languageserver/handlers/requestHandlers';
14-
import { ValidationHandler } from './languageserver/handlers/validationHandlers';
158
import { SettingsHandler } from './languageserver/handlers/settingsHandlers';
16-
import { YamlCommands } from './commands';
9+
import { ValidationHandler } from './languageserver/handlers/validationHandlers';
1710
import { WorkspaceHandlers } from './languageserver/handlers/workspaceHandlers';
18-
import { commandExecutor } from './languageserver/commandExecutor';
19-
import { Telemetry } from './languageservice/telemetry';
2011
import { registerCommands } from './languageservice/services/yamlCommands';
21-
import * as l10n from '@vscode/l10n';
22-
import * as path from 'path';
23-
import * as fs from 'fs';
12+
import { Telemetry } from './languageservice/telemetry';
13+
import { workspaceFoldersChanged } from './languageservice/utils/paths';
14+
import {
15+
getLanguageService as getCustomLanguageService,
16+
LanguageService,
17+
SchemaRequestService,
18+
WorkspaceContextService,
19+
} from './languageservice/yamlLanguageService';
20+
import { SettingsState } from './yamlSettings';
2421

2522
export class YAMLServerInit {
2623
languageService: LanguageService;
@@ -33,7 +30,8 @@ export class YAMLServerInit {
3330
private yamlSettings: SettingsState,
3431
private workspaceContext: WorkspaceContextService,
3532
private schemaRequestService: SchemaRequestService,
36-
private telemetry: Telemetry
33+
private telemetry: Telemetry,
34+
public setupl10nBundle: (params: InitializeParams) => Promise<void> = () => Promise.resolve()
3735
) {
3836
this.yamlSettings.documents.listen(this.connection);
3937

@@ -57,25 +55,6 @@ export class YAMLServerInit {
5755
});
5856
}
5957

60-
public async setupl10nBundle(params: InitializeParams): Promise<void> {
61-
const __dirname = path.dirname(__filename);
62-
const l10nPath: string = params.initializationOptions?.l10nPath || path.join(__dirname, '../../../l10n');
63-
const locale: string = params.locale || 'en';
64-
if (l10nPath) {
65-
const bundleFile = !fs.existsSync(path.join(l10nPath, `bundle.l10n.${locale}.json`))
66-
? `bundle.l10n.json`
67-
: `bundle.l10n.${locale}.json`;
68-
const baseBundleFile = path.join(l10nPath, bundleFile);
69-
process.env.VSCODE_NLS_CONFIG = JSON.stringify({
70-
locale,
71-
_languagePackSupport: true,
72-
});
73-
await l10n.config({
74-
uri: URI.file(baseBundleFile).toString(),
75-
});
76-
}
77-
}
78-
7958
// public for test setup
8059
async connectionInitialized(params: InitializeParams): Promise<InitializeResult> {
8160
this.yamlSettings.capabilities = params.capabilities;

test/utils/testHelper.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,20 @@
22
* Copyright (c) Red Hat. All rights reserved.
33
* Licensed under the MIT License. See License.txt in the project root for license information.
44
*--------------------------------------------------------------------------------------------*/
5-
import { createConnection, Connection, ClientCapabilities as LSPClientCapabilities } from 'vscode-languageserver/node';
6-
import path = require('path');
75
import { promises as fs } from 'fs';
8-
import { SettingsState } from '../../src/yamlSettings';
9-
import { FileSystem, schemaRequestHandler, workspaceContext } from '../../src/languageservice/services/schemaRequestHandler';
10-
import { YAMLServerInit } from '../../src/yamlServerInit';
6+
import { ClientCapabilities } from 'vscode-json-languageservice';
7+
import { TextDocument } from 'vscode-languageserver-textdocument';
8+
import { Connection, createConnection, ClientCapabilities as LSPClientCapabilities } from 'vscode-languageserver/node';
119
import { LanguageService, LanguageSettings } from '../../src';
12-
import { ValidationHandler } from '../../src/languageserver/handlers/validationHandlers';
1310
import { LanguageHandlers } from '../../src/languageserver/handlers/languageHandlers';
14-
import { TextDocument } from 'vscode-languageserver-textdocument';
15-
import { ClientCapabilities } from 'vscode-json-languageservice';
11+
import { ValidationHandler } from '../../src/languageserver/handlers/validationHandlers';
12+
import { JSONSchema } from '../../src/languageservice/jsonSchema';
1613
import { yamlDocumentsCache } from '../../src/languageservice/parser/yaml-documents';
14+
import { FileSystem, schemaRequestHandler, workspaceContext } from '../../src/languageservice/services/schemaRequestHandler';
15+
import { YAMLServerInit } from '../../src/yamlServerInit';
16+
import { SettingsState } from '../../src/yamlSettings';
1717
import { TestTelemetry } from './testsTypes';
18-
import { JSONSchema } from '../../src/languageservice/jsonSchema';
18+
import * as path from 'path';
1919

2020
export function toFsPath(str: unknown): string {
2121
if (typeof str !== 'string') {

0 commit comments

Comments
 (0)