Skip to content

Commit 9461e8d

Browse files
authored
Merge pull request #64 from CodinGame/additional-json-schema
Additional json schemas
2 parents 1480e14 + b732a11 commit 9461e8d

File tree

1 file changed

+29
-4
lines changed

1 file changed

+29
-4
lines changed

src/features/jsonContribution.ts

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,29 @@
11
import 'monaco-editor/esm/vs/language/json/monaco.contribution'
22
import * as monaco from 'monaco-editor'
33
import { getJsonSchemas, onDidChangeJsonSchema } from 'vscode/monaco'
4+
import { Disposable } from 'vscode'
45
import { registerWorkerLoader } from '../worker'
56

7+
type Unpacked<T> = T extends (infer U)[] ? U : T
8+
type Schema = Unpacked<NonNullable<monaco.languages.json.DiagnosticsOptions['schemas']>>
9+
10+
const customSchemas: Schema[] = []
611
function updateDiagnosticsOptions () {
712
monaco.languages.json.jsonDefaults.setDiagnosticsOptions({
813
comments: 'ignore',
914
validate: true,
10-
schemas: getJsonSchemas({
11-
keybindings: ['file:///keybindings.json'],
12-
'settings/user': ['file:///settings.json']
13-
})
15+
enableSchemaRequest: true,
16+
schemas: [
17+
...getJsonSchemas({
18+
keybindings: ['file:///keybindings.json'],
19+
'settings/user': ['file:///settings.json']
20+
})!,
21+
{
22+
uri: 'https://json-schema.org/draft/2019-09/schema',
23+
fileMatch: ['*.schema.json']
24+
},
25+
...customSchemas
26+
]
1427
})
1528
}
1629

@@ -19,3 +32,15 @@ onDidChangeJsonSchema(updateDiagnosticsOptions)
1932

2033
const workerLoader = async () => (await import(/* webpackChunkName: "MonacoJsonWorker" */'monaco-editor/esm/vs/language/json/json.worker?worker')).default
2134
registerWorkerLoader('json', workerLoader)
35+
36+
export function addJsonSchema (schema: Unpacked<NonNullable<monaco.languages.json.DiagnosticsOptions['schemas']>>): Disposable {
37+
customSchemas.push(schema)
38+
updateDiagnosticsOptions()
39+
return new Disposable(() => {
40+
const index = customSchemas.indexOf(schema)
41+
if (index >= 0) {
42+
customSchemas.splice(index, 1)
43+
updateDiagnosticsOptions()
44+
}
45+
})
46+
}

0 commit comments

Comments
 (0)