Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add custom text document content provider #418

Merged
merged 5 commits into from
Feb 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
139 changes: 104 additions & 35 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@
"vscode:prepublish": "tsc -p ./"
},
"devDependencies": {
"@types/fs-extra": "^9.0.6",
"@types/mocha": "^2.2.48",
"@types/node": "^6.0.52",
"@types/vscode": "^1.31.0",
Expand All @@ -182,12 +183,13 @@
"prettier": "^2.0.5",
"rimraf": "^3.0.2",
"ts-node": "^3.3.0",
"typescript": "3.5.1",
"typescript": "4.1.2",
"vscode-test": "^1.4.0"
},
"dependencies": {
"fs-extra": "^9.1.0",
"request-light": "^0.4.0",
"vscode-languageclient": "5.2.1",
"vscode-languageclient": "7.0.0",
"vscode-nls": "^3.2.1",
"vscode-uri": "^2.0.3",
"yaml-language-server": "next"
Expand Down
31 changes: 13 additions & 18 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ import {
NotificationType,
RequestType,
RevealOutputChannelOn,
} from 'vscode-languageclient';
} from 'vscode-languageclient/node';
import { CUSTOM_SCHEMA_REQUEST, CUSTOM_CONTENT_REQUEST, SchemaExtensionAPI } from './schema-extension-api';
import { joinPath } from './paths';
import { xhr, configure as configureHttpRequests, getErrorStatusDescription, XHRResponse } from 'request-light';
import { getJsonSchemaContent, JSONSchemaDocumentContentProvider } from './json-schema-content-provider';
import { JSONSchemaCache } from './json-schema-cache';

export interface ISchemaAssociations {
[pattern: string]: string[];
Expand All @@ -34,27 +35,27 @@ export interface ISchemaAssociation {
// eslint-disable-next-line @typescript-eslint/no-namespace
namespace SchemaAssociationNotification {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const type: NotificationType<ISchemaAssociations | ISchemaAssociation[], any> = new NotificationType(
export const type: NotificationType<ISchemaAssociations | ISchemaAssociation[]> = new NotificationType(
'json/schemaAssociations'
);
}

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

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

// eslint-disable-next-line @typescript-eslint/no-namespace
namespace DynamicCustomSchemaRequestRegistration {
// eslint-disable-next-line @typescript-eslint/ban-types
export const type: NotificationType<{}, {}> = new NotificationType('yaml/registerCustomSchemaRequest');
export const type: NotificationType<{}> = new NotificationType('yaml/registerCustomSchemaRequest');
}

let client: LanguageClient;
Expand Down Expand Up @@ -88,6 +89,8 @@ export function activate(context: ExtensionContext): SchemaExtensionAPI {
revealOutputChannelOn: RevealOutputChannelOn.Never,
};

const schemaCache = new JSONSchemaCache(context.globalStoragePath, context.globalState);

// Create the language client and start it
client = new LanguageClient('yaml', 'YAML Support', serverOptions, clientOptions);
const disposable = client.start();
Expand All @@ -97,6 +100,9 @@ export function activate(context: ExtensionContext): SchemaExtensionAPI {
// Push the disposable to the context's subscriptions so that the
// client can be deactivated on extension deactivation
context.subscriptions.push(disposable);
context.subscriptions.push(
workspace.registerTextDocumentContentProvider('json-schema', new JSONSchemaDocumentContentProvider(schemaCache))
);

client.onReady().then(() => {
// Send a notification to the server with any YAML schema associations in all extensions
Expand All @@ -118,18 +124,7 @@ export function activate(context: ExtensionContext): SchemaExtensionAPI {
return schemaExtensionAPI.requestCustomSchemaContent(uri);
});
client.onRequest(VSCodeContentRequest.type, (uri: string) => {
const httpSettings = workspace.getConfiguration('http');
configureHttpRequests(httpSettings.http && httpSettings.http.proxy, httpSettings.http && httpSettings.http.proxyStrictSSL);

const headers = { 'Accept-Encoding': 'gzip, deflate' };
return xhr({ url: uri, followRedirects: 5, headers }).then(
(response) => {
return response.responseText;
},
(error: XHRResponse) => {
return Promise.reject(error.responseText || getErrorStatusDescription(error.status) || error.toString());
}
);
return getJsonSchemaContent(uri, schemaCache);
});
});

Expand Down
Loading