11import 'monaco-editor/esm/vs/language/json/monaco.contribution'
22import * as monaco from 'monaco-editor'
33import { getJsonSchemas , onDidChangeJsonSchema } from 'vscode/monaco'
4+ import { Disposable } from 'vscode'
45import { 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 [ ] = [ ]
611function 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
2033const workerLoader = async ( ) => ( await import ( /* webpackChunkName: "MonacoJsonWorker" */ 'monaco-editor/esm/vs/language/json/json.worker?worker' ) ) . default
2134registerWorkerLoader ( '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