Skip to content

Commit 77c8e7b

Browse files
evidolobJPinkney
andauthored
Add custom text document content provider (#418)
* add custom text document content provider Signed-off-by: Yevhen Vydolob <yvydolob@redhat.com> * add cache for json schemas Signed-off-by: Yevhen Vydolob <yvydolob@redhat.com> * Use 'etag' header to refresh cached schema Signed-off-by: Yevhen Vydolob <yvydolob@redhat.com> * Update src/json-schema-content-provider.ts Co-authored-by: Josh Pinkney <Joshpinkney@gmail.com> Co-authored-by: Josh Pinkney <Joshpinkney@gmail.com>
1 parent 680dddc commit 77c8e7b

File tree

6 files changed

+251
-57
lines changed

6 files changed

+251
-57
lines changed

package-lock.json

Lines changed: 104 additions & 35 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@
169169
"vscode:prepublish": "tsc -p ./"
170170
},
171171
"devDependencies": {
172+
"@types/fs-extra": "^9.0.6",
172173
"@types/mocha": "^2.2.48",
173174
"@types/node": "^6.0.52",
174175
"@types/vscode": "^1.31.0",
@@ -182,12 +183,13 @@
182183
"prettier": "^2.0.5",
183184
"rimraf": "^3.0.2",
184185
"ts-node": "^3.3.0",
185-
"typescript": "3.5.1",
186+
"typescript": "4.1.2",
186187
"vscode-test": "^1.4.0"
187188
},
188189
"dependencies": {
190+
"fs-extra": "^9.1.0",
189191
"request-light": "^0.4.0",
190-
"vscode-languageclient": "5.2.1",
192+
"vscode-languageclient": "7.0.0",
191193
"vscode-nls": "^3.2.1",
192194
"vscode-uri": "^2.0.3",
193195
"yaml-language-server": "next"

src/extension.ts

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@ import {
1717
NotificationType,
1818
RequestType,
1919
RevealOutputChannelOn,
20-
} from 'vscode-languageclient';
20+
} from 'vscode-languageclient/node';
2121
import { CUSTOM_SCHEMA_REQUEST, CUSTOM_CONTENT_REQUEST, SchemaExtensionAPI } from './schema-extension-api';
2222
import { joinPath } from './paths';
23-
import { xhr, configure as configureHttpRequests, getErrorStatusDescription, XHRResponse } from 'request-light';
23+
import { getJsonSchemaContent, JSONSchemaDocumentContentProvider } from './json-schema-content-provider';
24+
import { JSONSchemaCache } from './json-schema-cache';
2425

2526
export interface ISchemaAssociations {
2627
[pattern: string]: string[];
@@ -34,27 +35,27 @@ export interface ISchemaAssociation {
3435
// eslint-disable-next-line @typescript-eslint/no-namespace
3536
namespace SchemaAssociationNotification {
3637
// eslint-disable-next-line @typescript-eslint/no-explicit-any
37-
export const type: NotificationType<ISchemaAssociations | ISchemaAssociation[], any> = new NotificationType(
38+
export const type: NotificationType<ISchemaAssociations | ISchemaAssociation[]> = new NotificationType(
3839
'json/schemaAssociations'
3940
);
4041
}
4142

4243
// eslint-disable-next-line @typescript-eslint/no-namespace
4344
namespace VSCodeContentRequestRegistration {
4445
// eslint-disable-next-line @typescript-eslint/ban-types
45-
export const type: NotificationType<{}, {}> = new NotificationType('yaml/registerVSCodeContentRequest');
46+
export const type: NotificationType<{}> = new NotificationType('yaml/registerContentRequest');
4647
}
4748

4849
// eslint-disable-next-line @typescript-eslint/no-namespace
4950
namespace VSCodeContentRequest {
5051
// eslint-disable-next-line @typescript-eslint/no-explicit-any
51-
export const type: RequestType<string, string, any, any> = new RequestType('vscode/content');
52+
export const type: RequestType<string, string, any> = new RequestType('vscode/content');
5253
}
5354

5455
// eslint-disable-next-line @typescript-eslint/no-namespace
5556
namespace DynamicCustomSchemaRequestRegistration {
5657
// eslint-disable-next-line @typescript-eslint/ban-types
57-
export const type: NotificationType<{}, {}> = new NotificationType('yaml/registerCustomSchemaRequest');
58+
export const type: NotificationType<{}> = new NotificationType('yaml/registerCustomSchemaRequest');
5859
}
5960

6061
let client: LanguageClient;
@@ -88,6 +89,8 @@ export function activate(context: ExtensionContext): SchemaExtensionAPI {
8889
revealOutputChannelOn: RevealOutputChannelOn.Never,
8990
};
9091

92+
const schemaCache = new JSONSchemaCache(context.globalStoragePath, context.globalState);
93+
9194
// Create the language client and start it
9295
client = new LanguageClient('yaml', 'YAML Support', serverOptions, clientOptions);
9396
const disposable = client.start();
@@ -97,6 +100,9 @@ export function activate(context: ExtensionContext): SchemaExtensionAPI {
97100
// Push the disposable to the context's subscriptions so that the
98101
// client can be deactivated on extension deactivation
99102
context.subscriptions.push(disposable);
103+
context.subscriptions.push(
104+
workspace.registerTextDocumentContentProvider('json-schema', new JSONSchemaDocumentContentProvider(schemaCache))
105+
);
100106

101107
client.onReady().then(() => {
102108
// Send a notification to the server with any YAML schema associations in all extensions
@@ -118,18 +124,7 @@ export function activate(context: ExtensionContext): SchemaExtensionAPI {
118124
return schemaExtensionAPI.requestCustomSchemaContent(uri);
119125
});
120126
client.onRequest(VSCodeContentRequest.type, (uri: string) => {
121-
const httpSettings = workspace.getConfiguration('http');
122-
configureHttpRequests(httpSettings.http && httpSettings.http.proxy, httpSettings.http && httpSettings.http.proxyStrictSSL);
123-
124-
const headers = { 'Accept-Encoding': 'gzip, deflate' };
125-
return xhr({ url: uri, followRedirects: 5, headers }).then(
126-
(response) => {
127-
return response.responseText;
128-
},
129-
(error: XHRResponse) => {
130-
return Promise.reject(error.responseText || getErrorStatusDescription(error.status) || error.toString());
131-
}
132-
);
127+
return getJsonSchemaContent(uri, schemaCache);
133128
});
134129
});
135130

0 commit comments

Comments
 (0)