11import { URI } from 'vscode-uri'
22import { LanguageClient , RequestType } from 'vscode-languageclient' ;
3+ import { workspace } from 'vscode' ;
34
45interface SchemaContributorProvider {
5- readonly requestSchema : ( resource : string ) => string ;
6- readonly requestSchemaContent : ( uri : string ) => string ;
6+ readonly requestSchema : ( resource : string ) => string ;
7+ readonly requestSchemaContent : ( uri : string ) => string ;
8+ readonly label ?: string ;
79}
810
911export enum MODIFICATION_ACTIONS {
10- 'delete' ,
11- 'add'
12+ 'delete' ,
13+ 'add'
1214}
1315
1416export interface SchemaAdditions {
15- schema : string ,
16- action : MODIFICATION_ACTIONS . add ,
17- path : string ,
18- key : string ,
19- content : any
17+ schema : string ,
18+ action : MODIFICATION_ACTIONS . add ,
19+ path : string ,
20+ key : string ,
21+ content : any
2022}
2123
2224export interface SchemaDeletions {
23- schema : string ,
24- action : MODIFICATION_ACTIONS . delete ,
25- path : string ,
26- key : string
25+ schema : string ,
26+ action : MODIFICATION_ACTIONS . delete ,
27+ path : string ,
28+ key : string
2729}
2830
2931namespace SchemaModificationNotification {
30- export const type : RequestType < SchemaAdditions | SchemaDeletions , void , { } , { } > = new RequestType ( 'json/schema/modify' ) ;
32+ export const type : RequestType < SchemaAdditions | SchemaDeletions , void , { } , { } > = new RequestType ( 'json/schema/modify' ) ;
3133}
3234
3335export interface ExtensionAPI {
34- registerContributor ( schema : string , requestSchema : ( resource : string ) => string , requestSchemaContent : ( uri : string ) => string ) : boolean ;
35- modifySchemaContent ( schemaModifications : SchemaAdditions | SchemaDeletions ) : Promise < void > ;
36+ registerContributor ( schema : string , requestSchema : ( resource : string ) => string , requestSchemaContent : ( uri : string ) => string , label ?: string ) : boolean ;
37+ modifySchemaContent ( schemaModifications : SchemaAdditions | SchemaDeletions ) : Promise < void > ;
3638}
3739
3840class SchemaExtensionAPI implements ExtensionAPI {
39- private _customSchemaContributors : { [ index : string ] : SchemaContributorProvider } = { } ;
40- private _yamlClient : LanguageClient ;
41+ private _customSchemaContributors : { [ index : string ] : SchemaContributorProvider } = { } ;
42+ private _yamlClient : LanguageClient ;
4143
42- constructor ( client : LanguageClient ) {
43- this . _yamlClient = client ;
44- }
44+ constructor ( client : LanguageClient ) {
45+ this . _yamlClient = client ;
46+ }
4547
4648 /**
4749 * Register a custom schema provider
4850 *
4951 * @param {string } the provider's name
5052 * @param requestSchema the requestSchema function
5153 * @param requestSchemaContent the requestSchemaContent function
54+ * @param label the content label, yaml key value pair, like 'apiVersion:some.api/v1'
5255 * @returns {boolean }
5356 */
54- public registerContributor ( schema : string ,
55- requestSchema : ( resource : string ) => string ,
56- requestSchemaContent : ( uri : string ) => string ) : boolean {
57- if ( this . _customSchemaContributors [ schema ] ) {
58- return false ;
59- }
60-
61- if ( ! requestSchema ) {
62- throw new Error ( "Illegal parameter for requestSchema." ) ;
63- }
64-
65- this . _customSchemaContributors [ schema ] = < SchemaContributorProvider > {
66- requestSchema,
67- requestSchemaContent
68- } ;
69-
70- return true ;
71- }
57+ public registerContributor ( schema : string ,
58+ requestSchema : ( resource : string ) => string ,
59+ requestSchemaContent : ( uri : string ) => string ,
60+ label ?: string ) : boolean {
61+ if ( this . _customSchemaContributors [ schema ] ) {
62+ return false ;
63+ }
64+
65+ if ( ! requestSchema ) {
66+ throw new Error ( "Illegal parameter for requestSchema." ) ;
67+ }
68+
69+ if ( label ) {
70+ let [ first , second ] = label . split ( ':' ) ;
71+ if ( first && second ) {
72+ second = second . trim ( ) ;
73+ second = second . replace ( '.' , '\\.' ) ;
74+ label = `${ first } :[\t ]+${ second } ` ;
75+ }
76+ }
77+ this . _customSchemaContributors [ schema ] = < SchemaContributorProvider > {
78+ requestSchema,
79+ requestSchemaContent,
80+ label
81+ } ;
82+
83+ return true ;
84+ }
7285
7386 /**
7487 * Call requestSchema for each provider and finds all matches.
@@ -77,42 +90,55 @@ class SchemaExtensionAPI implements ExtensionAPI {
7790 * @returns {string } the schema uri
7891 */
7992 public requestCustomSchema ( resource : string ) : string [ ] {
80- const matches = [ ] ;
81- for ( let customKey of Object . keys ( this . _customSchemaContributors ) ) {
82- const contributor = this . _customSchemaContributors [ customKey ] ;
83- const uri = contributor . requestSchema ( resource ) ;
84-
85- if ( uri ) {
86- matches . push ( uri ) ;
87- }
88- }
89- return matches ;
90- }
93+ const matches = [ ] ;
94+ for ( let customKey of Object . keys ( this . _customSchemaContributors ) ) {
95+ const contributor = this . _customSchemaContributors [ customKey ] ;
96+ let uri : string ;
97+ if ( contributor . label && workspace . textDocuments ) {
98+ const labelRegexp = new RegExp ( contributor . label , 'g' ) ;
99+ for ( const doc of workspace . textDocuments ) {
100+ if ( doc . uri . toString ( ) === resource ) {
101+ if ( labelRegexp . test ( doc . getText ( ) ) ) {
102+ uri = contributor . requestSchema ( resource ) ;
103+ return [ uri ] ;
104+ }
105+ }
106+ }
107+ }
108+
109+ uri = contributor . requestSchema ( resource ) ;
110+
111+ if ( uri ) {
112+ matches . push ( uri ) ;
113+ }
114+ }
115+ return matches ;
116+ }
91117
92118 /**
93119 * Call requestCustomSchemaContent for named provider and get the schema content.
94120 *
95121 * @param {string } uri the schema uri returned from requestSchema.
96122 * @returns {string } the schema content
97123 */
98- public requestCustomSchemaContent ( uri : string ) : string {
99- if ( uri ) {
100- let _uri = URI . parse ( uri ) ;
101-
102- if ( _uri . scheme && this . _customSchemaContributors [ _uri . scheme ] &&
103- this . _customSchemaContributors [ _uri . scheme ] . requestSchemaContent ) {
104- return this . _customSchemaContributors [ _uri . scheme ] . requestSchemaContent ( uri ) ;
105- }
106- }
107- }
108-
109- public async modifySchemaContent ( schemaModifications : SchemaAdditions | SchemaDeletions ) {
110- return this . _yamlClient . sendRequest ( SchemaModificationNotification . type , schemaModifications ) ;
111- }
124+ public requestCustomSchemaContent ( uri : string ) : string {
125+ if ( uri ) {
126+ let _uri = URI . parse ( uri ) ;
127+
128+ if ( _uri . scheme && this . _customSchemaContributors [ _uri . scheme ] &&
129+ this . _customSchemaContributors [ _uri . scheme ] . requestSchemaContent ) {
130+ return this . _customSchemaContributors [ _uri . scheme ] . requestSchemaContent ( uri ) ;
131+ }
132+ }
133+ }
134+
135+ public async modifySchemaContent ( schemaModifications : SchemaAdditions | SchemaDeletions ) {
136+ return this . _yamlClient . sendRequest ( SchemaModificationNotification . type , schemaModifications ) ;
137+ }
112138}
113139
114140// constants
115141export const CUSTOM_SCHEMA_REQUEST = 'custom/schema/request' ;
116142export const CUSTOM_CONTENT_REQUEST = 'custom/schema/content' ;
117143
118- export { SchemaExtensionAPI } ;
144+ export { SchemaExtensionAPI } ;
0 commit comments