Skip to content

Commit d3d3d93

Browse files
committed
Fix resolving schema content on web
Use a new extension point, which requests schema content by URI instead of filepath. This is helpful, since web instances may use URIs with schemas other than `file://` to refer to the files in the workspace. Add a smoke test. Fixes #1194 Signed-off-by: David Thompson <davthomp@redhat.com>
1 parent 2bea998 commit d3d3d93

File tree

5 files changed

+39
-3
lines changed

5 files changed

+39
-3
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@
287287
"smoke-test": "npm run test-compile && vscode-test smoke-test/*",
288288
"compile-smoke-test-web": "webpack --config smoke-test/webpack.config",
289289
"smoke-test-web": "npm run compile-smoke-test-web && vscode-test-web --extensionDevelopmentPath=. --extensionTestsPath=./out/smoke-test/smoke-test-runner.js ./smoke-test",
290-
"run-in-chromium": "npm run compile && vscode-test-web --browserType=chromium --extensionDevelopmentPath=. ."
290+
"run-in-chromium": "npm run compile && vscode-test-web --browserType=chromium --extensionDevelopmentPath=. smoke-test"
291291
},
292292
"devDependencies": {
293293
"@types/chai": "^4.2.12",

smoke-test/.vscode/settings.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"yaml.schemas": {
3+
"dressSize.json": "references-schema-settings.yaml"
4+
}
5+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
dress:
2+
size: -2

smoke-test/smoke.test.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,19 @@ suite('Smoke test suite', function () {
99
const DIAGNOSTICS_DELAY = 4_000;
1010

1111
const SCHEMA_INSTANCE_NAME = 'references-schema.yaml';
12+
const THROUGH_SETTINGS_NAME = 'references-schema-settings.yaml';
1213

1314
let schemaInstanceUri: URI;
15+
let throughSettingsUri: URI;
1416

1517
this.beforeAll(async function () {
1618
const workspaceUri = vscode.workspace.workspaceFolders[0].uri;
17-
schemaInstanceUri = workspaceUri.with({ path: workspaceUri.path + (workspaceUri.path.endsWith('/') ? '' : '/') + SCHEMA_INSTANCE_NAME });
19+
schemaInstanceUri = workspaceUri.with({
20+
path: workspaceUri.path + (workspaceUri.path.endsWith('/') ? '' : '/') + SCHEMA_INSTANCE_NAME,
21+
});
22+
throughSettingsUri = workspaceUri.with({
23+
path: workspaceUri.path + (workspaceUri.path.endsWith('/') ? '' : '/') + THROUGH_SETTINGS_NAME,
24+
});
1825
});
1926

2027
test('instance has right diagnostics', async function () {
@@ -26,4 +33,14 @@ suite('Smoke test suite', function () {
2633
assert.strictEqual(diagnostics.length, 1);
2734
assert.strictEqual(diagnostics[0].message, 'Value is below the minimum of 0.');
2835
});
36+
37+
test('has right diagnostics when schema is referenced through settings', async function () {
38+
const textDocument = await vscode.workspace.openTextDocument(throughSettingsUri);
39+
await vscode.window.showTextDocument(textDocument);
40+
await new Promise((resolve) => setTimeout(resolve, DIAGNOSTICS_DELAY));
41+
const diagnostics = vscode.languages.getDiagnostics(throughSettingsUri);
42+
43+
assert.strictEqual(diagnostics.length, 1);
44+
assert.strictEqual(diagnostics[0].message, 'Value is below the minimum of 0.');
45+
});
2946
});

src/extension.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*--------------------------------------------------------------------------------------------*/
77
'use strict';
88

9-
import { workspace, ExtensionContext, extensions, window, commands, Uri, l10n } from 'vscode';
9+
import { workspace, ExtensionContext, extensions, window, commands, Uri } from 'vscode';
1010
import {
1111
CommonLanguageClient,
1212
LanguageClientOptions,
@@ -67,6 +67,8 @@ namespace FSReadFile {
6767
export const type: RequestType<string, string, {}> = new RequestType('fs/readFile');
6868
}
6969

70+
export const FSReadUriType: RequestType<string, string, unknown> = new RequestType('fs/readUri');
71+
7072
// eslint-disable-next-line @typescript-eslint/no-namespace
7173
namespace DynamicCustomSchemaRequestRegistration {
7274
// eslint-disable-next-line @typescript-eslint/ban-types
@@ -199,6 +201,16 @@ export function startClient(
199201
return new TextDecoder().decode(uint8array);
200202
}
201203
});
204+
client.onRequest(FSReadUriType, async (uri: string) => {
205+
try {
206+
const parsedUri = Uri.parse(uri);
207+
window.showInformationMessage(`uri: ${parsedUri.toString()}`);
208+
const uint8array = await workspace.fs.readFile(parsedUri);
209+
return new TextDecoder().decode(uint8array);
210+
} catch (e) {
211+
window.showErrorMessage(`Error while retrieving content of '${uri}': ${e}`);
212+
}
213+
});
202214

203215
sendStartupTelemetryEvent(runtime.telemetry, true);
204216
// Adapted from:

0 commit comments

Comments
 (0)