Skip to content

Commit

Permalink
support workspace.resolveJSONSchema for configuration schema
Browse files Browse the repository at this point in the history
  • Loading branch information
chemzqm committed Oct 26, 2022
1 parent b98380d commit 1a58e4f
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 15 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@
"license": "MIT",
"devDependencies": {
"@chemzqm/tsconfig": "^0.0.3",
"@types/node": "12.12.0",
"coc.nvim": "^0.0.81",
"@types/node": "^14.14",
"coc.nvim": "^0.0.83-next.8",
"esbuild": "^0.8.29",
"strip-bom": "^4.0.0",
"typescript": "^4.1.3",
Expand Down
27 changes: 23 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { commands, CompletionContext, CompletionItem, CompletionItemKind, Comple
import fs from 'fs'
import path from 'path'
import stripBom from 'strip-bom'
import os from 'os'
import { promisify } from 'util'
import { CancellationToken, Diagnostic, DidChangeConfigurationNotification, ResponseError } from 'vscode-languageserver-protocol'
import { URI } from 'vscode-uri'
Expand All @@ -10,6 +11,9 @@ import { joinPath, RequestService } from './requests'
import extensionPkg from './schemas/extension-package.schema.json'
import { hash } from './utils/hash'

const resolveJson = typeof workspace.resolveJSONSchema === 'function'
const networkSchemes = ['http', 'https']

namespace ForceValidateRequest {
export const type: RequestType<string, Diagnostic[], any> = new RequestType('json/validate')
}
Expand Down Expand Up @@ -214,6 +218,14 @@ export async function activate(context: ExtensionContext): Promise<void> {
if (uri.scheme === 'untitled') {
return Promise.reject(new ResponseError(3, `Unable to load ${uri.scheme}`))
}
if (uriPath == 'vscode://schemas/vscode-extensions') {
return JSON.stringify(extensionPkg)
}
if (resolveJson && uri.scheme === 'vscode') {
let schema = workspace.resolveJSONSchema(uriPath)
if (!schema) void window.showErrorMessage(`Failed to resolve schema for ${uriPath}`)
return Promise.resolve(JSON.stringify(schema ?? {}))
}
if (uriPath == 'vscode://settings') {
let schemaContent = await promisify(fs.readFile)(path.join(workspace.pluginRoot, 'data/schema.json'), 'utf8')
let schema: any = JSON.parse(schemaContent)
Expand All @@ -233,10 +245,10 @@ export async function activate(context: ExtensionContext): Promise<void> {
})
return JSON.stringify(schema)
}
if (uriPath == 'vscode://schemas/vscode-extensions') {
return JSON.stringify(extensionPkg)
if (uriPath.startsWith('vscode://')) {
return {}
}
if (uri.scheme !== 'http' && uri.scheme !== 'https') {
if (!networkSchemes.includes(uri.scheme)) {
let doc = await workspace.loadFile(uriPath)
schemaDocuments[uri.toString()] = true
return doc.getDocumentContent()
Expand Down Expand Up @@ -433,7 +445,14 @@ function getHTTPRequestService(): RequestService {

function getSchemaAssociations(): ISchemaAssociation[] {
const associations: ISchemaAssociation[] = []
associations.push({ fileMatch: ['coc-settings.json'], uri: 'vscode://settings' })
if (resolveJson) {
let home = path.normalize(process.env.COC_VIMCONFIG) ?? path.join(os.homedir(), '.vim')
let userConfigFile = URI.file(path.join(home, 'coc-settings.json')).fsPath
associations.push({ fileMatch: [userConfigFile], uri: 'vscode://schemas/settings/user' })
associations.push({ fileMatch: ['coc-settings.json', `!${userConfigFile}`], uri: 'vscode://schemas/settings/folder' })
} else {
associations.push({ fileMatch: ['coc-settings.json'], uri: 'vscode://settings' })
}
associations.push({ fileMatch: ['package.json'], uri: 'vscode://schemas/vscode-extensions' })
extensions.all.forEach(extension => {
const packageJSON = extension.packageJSON
Expand Down
18 changes: 9 additions & 9 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
resolved "https://registry.yarnpkg.com/@chemzqm/tsconfig/-/tsconfig-0.0.3.tgz#ce3480d15d8cec46a315488caa07c9fca819aecc"
integrity sha512-MjF25vbqLYR+S+JJLgBi0vn4gZqv/C87H+yPSlVKEqlIJAJOGJOgFPUFvRS7pdRHqkv2flX/oRxzxhlu2V0X1w==

"@types/node@12.12.0":
version "12.12.0"
resolved "https://registry.yarnpkg.com/@types/node/-/node-12.12.0.tgz#ff3201972d6dc851a9275308a17b9b5094e68057"
integrity sha512-6N8Sa5AaENRtJnpKXZgvc119PKxT1Lk9VPy4kfT8JF23tIe1qDfaGkBR2DRKJFIA7NptMz+fps//C6aLi1Uoug==

coc.nvim@^0.0.81:
version "0.0.81"
resolved "https://registry.yarnpkg.com/coc.nvim/-/coc.nvim-0.0.81.tgz#6c28ed4e773e7f274f1c295dd4fa2d46e351d306"
integrity sha512-e9FSAXPNs3vAgjSrspYRuPcX8L+AQw1T+0SfY0srJ6tsDtWnainnwSvyjInw1tEmGXRMgdBmVowW0JY2qjv9Dw==
"@types/node@^14.14":
version "14.18.32"
resolved "https://registry.yarnpkg.com/@types/node/-/node-14.18.32.tgz#8074f7106731f1a12ba993fe8bad86ee73905014"
integrity sha512-Y6S38pFr04yb13qqHf8uk1nHE3lXgQ30WZbv1mLliV9pt0NjvqdWttLcrOYLnXbOafknVYRHZGoMSpR9UwfYow==

coc.nvim@^0.0.83-next.8:
version "0.0.83-next.8"
resolved "https://registry.yarnpkg.com/coc.nvim/-/coc.nvim-0.0.83-next.8.tgz#98478b703d70de0941731e0b1fdac4c0f9fe5f44"
integrity sha512-ngYyjDnrMfQTffHQeEyvWoLCKEMTx+Z/7GSphyzQ/b3+l0jDHfZJY0lt3e+Vl53og8Ylpa9rF32zjVUmztmxYQ==

esbuild@^0.8.29:
version "0.8.57"
Expand Down

0 comments on commit 1a58e4f

Please sign in to comment.