Skip to content

Commit

Permalink
feat: support json schema request service
Browse files Browse the repository at this point in the history
close #243
  • Loading branch information
johnsoncodehk committed Jun 6, 2021
1 parent 7df5705 commit f76a672
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 2 deletions.
1 change: 1 addition & 0 deletions packages/vscode-vue-languageservice/package.json
Expand Up @@ -30,6 +30,7 @@
"@vue/shared": "^3.0.11",
"jsonc-parser": "^3.0.0",
"prettier": "^1.16.4",
"request-light": "^0.4.0",
"upath": "^2.0.1",
"vscode-css-languageservice": "^5.1.3",
"vscode-emmet-helper": "^2.6.4",
Expand Down
7 changes: 6 additions & 1 deletion packages/vscode-vue-languageservice/src/sourceFile.ts
Expand Up @@ -728,7 +728,12 @@ export function createSourceFile(
comments: 'ignore',
trailingCommas: 'warning',
});
if (errs) result.set(textDocument.uri, errs);
if (errs) {
for (const err of errs) {
err.source = err.source ?? 'json';
}
result.set(textDocument.uri, errs);
}
}
return result;
});
Expand Down
@@ -0,0 +1,59 @@
import { xhr, XHRResponse, getErrorStatusDescription } from 'request-light';
import { URI as Uri } from 'vscode-uri';
import * as fs from 'fs';
import type * as json from 'vscode-json-languageservice';

function getHTTPRequestService(): json.SchemaRequestService {
return (uri: string, _encoding?: string) => {
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());
});
};
}

function getFileRequestService(): json.SchemaRequestService {
return (location: string, encoding?: string) => {
return new Promise((c, e) => {
const uri = Uri.parse(location);
fs.readFile(uri.fsPath, encoding, (err, buf) => {
if (err) {
return e(err);
}
c(buf.toString());
});
});
};
}

const http = getHTTPRequestService();
const file = getFileRequestService();

export function getSchemaRequestService(handledSchemas: string[] = ['https', 'http', 'file']) {
const builtInHandlers: { [protocol: string]: json.SchemaRequestService | undefined } = {};
for (let protocol of handledSchemas) {
if (protocol === 'file') {
builtInHandlers[protocol] = file;
} else if (protocol === 'http' || protocol === 'https') {
builtInHandlers[protocol] = http;
}
}
return (uri: string): Thenable<string> => {
const protocol = uri.substr(0, uri.indexOf(':'));

const builtInHandler = builtInHandlers[protocol];
if (builtInHandler) {
return builtInHandler(uri);
}

// TODO
// return connection.sendRequest(VSCodeContentRequest.type, uri).then(responseText => {
// return responseText;
// }, error => {
// return Promise.reject(error.message);
// });
return new Promise(resolve => resolve(''));
};
}
3 changes: 2 additions & 1 deletion packages/vscode-vue-languageservice/src/utils/sharedLs.ts
Expand Up @@ -7,6 +7,7 @@ import * as html from 'vscode-html-languageservice';
import * as json from 'vscode-json-languageservice';
import * as pug from 'vscode-pug-languageservice';
import * as ts2 from 'vscode-typescript-languageservice';
import { getSchemaRequestService } from './schemaRequestService';

const fileSystemProvider: html.FileSystemProvider = {
stat: (uri) => {
Expand Down Expand Up @@ -47,7 +48,7 @@ export const cssLs = css.getCSSLanguageService({ fileSystemProvider });
export const scssLs = css.getSCSSLanguageService({ fileSystemProvider });
export const lessLs = css.getLESSLanguageService({ fileSystemProvider });
export const pugLs = pug.getLanguageService(htmlLs);
export const jsonLs = json.getLanguageService({ /* TODO */ });
export const jsonLs = json.getLanguageService({ schemaRequestService: getSchemaRequestService() });
export const postcssLs: css.LanguageService = {
...scssLs,
doValidation: (document, stylesheet, documentSettings) => {
Expand Down
56 changes: 56 additions & 0 deletions yarn.lock
Expand Up @@ -1789,6 +1789,13 @@ add-stream@^1.0.0:
resolved "https://registry.yarnpkg.com/add-stream/-/add-stream-1.0.0.tgz#6a7990437ca736d5e1288db92bd3266d5f5cb2aa"
integrity sha1-anmQQ3ynNtXhKI25K9MmbV9csqo=

agent-base@4, agent-base@^4.3.0:
version "4.3.0"
resolved "https://registry.yarnpkg.com/agent-base/-/agent-base-4.3.0.tgz#8165f01c436009bccad0b1d122f05ed770efc6ee"
integrity sha512-salcGninV0nPrwpGNn4VTXBb1SOuXQBiqbrNXoeizJsHrsL6ERFM2Ne3JUSBWRE6aeNJI2ROP/WEEIDUiDe3cg==
dependencies:
es6-promisify "^5.0.0"

agent-base@6:
version "6.0.2"
resolved "https://registry.yarnpkg.com/agent-base/-/agent-base-6.0.2.tgz#49fff58577cfee3f37176feab4c22e00f86d7f77"
Expand Down Expand Up @@ -2920,6 +2927,13 @@ dateformat@^3.0.0:
resolved "https://registry.yarnpkg.com/dateformat/-/dateformat-3.0.3.tgz#a6e37499a4d9a9cf85ef5872044d62901c9889ae"
integrity sha512-jyCETtSl3VMZMWeRo7iY1FL19ges1t55hMo5yaam4Jrsm5EPL89UQkoQRyiI+Yf4k8r2ZpdngkV8hr1lIdjb3Q==

debug@3.1.0:
version "3.1.0"
resolved "https://registry.yarnpkg.com/debug/-/debug-3.1.0.tgz#5bb5a0672628b64149566ba16819e61518c67261"
integrity sha512-OX8XqP7/1a9cqkxYw2yXss15f26NKWBpDXQd0/uK/KPqdQhxbPa994hnzjcE2VqQpDslf55723cKPUOGSmMY3g==
dependencies:
ms "2.0.0"

debug@4, debug@^4.1.0, debug@^4.1.1:
version "4.3.1"
resolved "https://registry.yarnpkg.com/debug/-/debug-4.3.1.tgz#f0d229c505e0c6d8c49ac553d1b13dc183f6b2ee"
Expand Down Expand Up @@ -3288,6 +3302,18 @@ es-to-primitive@^1.2.1:
is-date-object "^1.0.1"
is-symbol "^1.0.2"

es6-promise@^4.0.3:
version "4.2.8"
resolved "https://registry.yarnpkg.com/es6-promise/-/es6-promise-4.2.8.tgz#4eb21594c972bc40553d276e510539143db53e0a"
integrity sha512-HJDGx5daxeIvxdBxvG2cb9g4tEvwIk3i8+nhX0yGrYmZUzbkdg8QbDevheDB8gd0//uPj4c1EQua8Q+MViT0/w==

es6-promisify@^5.0.0:
version "5.0.0"
resolved "https://registry.yarnpkg.com/es6-promisify/-/es6-promisify-5.0.0.tgz#5109d62f3e56ea967c4b63505aef08291c8a5203"
integrity sha1-UQnWLz5W6pZ8S2NQWu8IKRyKUgM=
dependencies:
es6-promise "^4.0.3"

esbuild@^0.12.5:
version "0.12.6"
resolved "https://registry.yarnpkg.com/esbuild/-/esbuild-0.12.6.tgz#85bc755c7cf3005d4f34b4f10f98049ce0ee67ce"
Expand Down Expand Up @@ -4093,6 +4119,14 @@ http-cache-semantics@^4.0.0, http-cache-semantics@^4.1.0:
resolved "https://registry.yarnpkg.com/http-cache-semantics/-/http-cache-semantics-4.1.0.tgz#49e91c5cbf36c9b94bcfcd71c23d5249ec74e390"
integrity sha512-carPklcUh7ROWRK7Cv27RPtdhYhUsela/ue5/jKzjegVvXDqM2ILE9Q2BGn9JZJh1g87cp56su/FgQSzcWS8cQ==

http-proxy-agent@^2.1.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/http-proxy-agent/-/http-proxy-agent-2.1.0.tgz#e4821beef5b2142a2026bd73926fe537631c5405"
integrity sha512-qwHbBLV7WviBl0rQsOzH6o5lwyOIvwp/BdFnvVxXORldu5TmjFfjzBcWUWS5kWAZhmv+JtiDhSuQCp4sBfbIgg==
dependencies:
agent-base "4"
debug "3.1.0"

http-proxy-agent@^4.0.1:
version "4.0.1"
resolved "https://registry.yarnpkg.com/http-proxy-agent/-/http-proxy-agent-4.0.1.tgz#8a8c8ef7f5932ccf953c296ca8291b95aa74aa3a"
Expand All @@ -4111,6 +4145,14 @@ http-signature@~1.2.0:
jsprim "^1.2.2"
sshpk "^1.7.0"

https-proxy-agent@^2.2.4:
version "2.2.4"
resolved "https://registry.yarnpkg.com/https-proxy-agent/-/https-proxy-agent-2.2.4.tgz#4ee7a737abd92678a293d9b34a1af4d0d08c787b"
integrity sha512-OmvfoQ53WLjtA9HeYP9RNrWMJzzAz1JGaSFr1nijg0PVR1JaD/xbJq1mdEIIlxGpXp9eSe/O2LgU9DJmTPd0Eg==
dependencies:
agent-base "^4.3.0"
debug "^3.1.0"

https-proxy-agent@^5.0.0:
version "5.0.0"
resolved "https://registry.yarnpkg.com/https-proxy-agent/-/https-proxy-agent-5.0.0.tgz#e2a90542abb68a762e0a0850f6c9edadfd8506b2"
Expand Down Expand Up @@ -7205,6 +7247,15 @@ replace-ext@1.0.0:
resolved "https://registry.yarnpkg.com/replace-ext/-/replace-ext-1.0.0.tgz#de63128373fcbf7c3ccfa4de5a480c45a67958eb"
integrity sha1-3mMSg3P8v3w8z6TeWkgMRaZ5WOs=

request-light@^0.4.0:
version "0.4.0"
resolved "https://registry.yarnpkg.com/request-light/-/request-light-0.4.0.tgz#c6b91ef00b18cb0de75d2127e55b3a2c9f7f90f9"
integrity sha512-fimzjIVw506FBZLspTAXHdpvgvQebyjpNyLRd0e6drPPRq7gcrROeGWRyF81wLqFg5ijPgnOQbmfck5wdTqpSA==
dependencies:
http-proxy-agent "^2.1.0"
https-proxy-agent "^2.2.4"
vscode-nls "^4.1.2"

request-promise-core@1.1.4:
version "1.1.4"
resolved "https://registry.yarnpkg.com/request-promise-core/-/request-promise-core-1.1.4.tgz#3eedd4223208d419867b78ce815167d10593a22f"
Expand Down Expand Up @@ -8806,6 +8857,11 @@ vscode-languageserver@^7.1.0-next.4:
dependencies:
vscode-languageserver-protocol "3.17.0-next.5"

vscode-nls@^4.1.2:
version "4.1.2"
resolved "https://registry.yarnpkg.com/vscode-nls/-/vscode-nls-4.1.2.tgz#ca8bf8bb82a0987b32801f9fddfdd2fb9fd3c167"
integrity sha512-7bOHxPsfyuCqmP+hZXscLhiHwe7CSuFE4hyhbs22xPIhQ4jv99FcR4eBzfYYVLP356HNFpdvz63FFb/xw6T4Iw==

vscode-nls@^5.0.0:
version "5.0.0"
resolved "https://registry.yarnpkg.com/vscode-nls/-/vscode-nls-5.0.0.tgz#99f0da0bd9ea7cda44e565a74c54b1f2bc257840"
Expand Down

0 comments on commit f76a672

Please sign in to comment.