@@ -24,6 +24,8 @@ import { SingleYAMLDocument } from '../parser/yamlParser07';
2424import { JSONDocument } from '../parser/jsonParser07' ;
2525import { parse } from 'yaml' ;
2626import * as path from 'path' ;
27+ import { getSchemaFromModeline } from './modelineUtil' ;
28+ import { JSONSchemaDescriptionExt } from '../../requestTypes' ;
2729
2830const localize = nls . loadMessageBundle ( ) ;
2931
@@ -94,6 +96,8 @@ export class YAMLSchemaService extends JSONSchemaService {
9496 private requestService : SchemaRequestService ;
9597 public schemaPriorityMapping : Map < string , Set < SchemaPriority > > ;
9698
99+ private schemaUriToNameAndDescription = new Map < string , [ string , string ] > ( ) ;
100+
97101 constructor (
98102 requestService : SchemaRequestService ,
99103 contextService ?: WorkspaceContextService ,
@@ -109,6 +113,33 @@ export class YAMLSchemaService extends JSONSchemaService {
109113 this . customSchemaProvider = customSchemaProvider ;
110114 }
111115
116+ getAllSchemas ( ) : JSONSchemaDescriptionExt [ ] {
117+ const result : JSONSchemaDescriptionExt [ ] = [ ] ;
118+ const schemaUris = new Set < string > ( ) ;
119+ for ( const filePattern of this . filePatternAssociations ) {
120+ const schemaUri = filePattern . uris [ 0 ] ;
121+ if ( schemaUris . has ( schemaUri ) ) {
122+ continue ;
123+ }
124+ schemaUris . add ( schemaUri ) ;
125+ const schemaHandle : JSONSchemaDescriptionExt = {
126+ uri : schemaUri ,
127+ fromStore : false ,
128+ usedForCurrentFile : false ,
129+ } ;
130+
131+ if ( this . schemaUriToNameAndDescription . has ( schemaUri ) ) {
132+ const [ name , description ] = this . schemaUriToNameAndDescription . get ( schemaUri ) ;
133+ schemaHandle . name = name ;
134+ schemaHandle . description = description ;
135+ schemaHandle . fromStore = true ;
136+ }
137+ result . push ( schemaHandle ) ;
138+ }
139+
140+ return result ;
141+ }
142+
112143 async resolveSchemaContent (
113144 schemaToResolve : UnresolvedSchema ,
114145 schemaURL : string ,
@@ -294,7 +325,7 @@ export class YAMLSchemaService extends JSONSchemaService {
294325 const seen : { [ schemaId : string ] : boolean } = Object . create ( null ) ;
295326 const schemas : string [ ] = [ ] ;
296327
297- let schemaFromModeline = this . getSchemaFromModeline ( doc ) ;
328+ let schemaFromModeline = getSchemaFromModeline ( doc ) ;
298329 if ( schemaFromModeline !== undefined ) {
299330 if ( ! schemaFromModeline . startsWith ( 'file:' ) && ! schemaFromModeline . startsWith ( 'http' ) ) {
300331 if ( ! path . isAbsolute ( schemaFromModeline ) ) {
@@ -438,32 +469,6 @@ export class YAMLSchemaService extends JSONSchemaService {
438469 return priorityMapping . get ( highestPrio ) || [ ] ;
439470 }
440471
441- /**
442- * Retrieve schema if declared as modeline.
443- * Public for testing purpose, not part of the API.
444- * @param doc
445- */
446- public getSchemaFromModeline ( doc : SingleYAMLDocument | JSONDocument ) : string {
447- if ( doc instanceof SingleYAMLDocument ) {
448- const yamlLanguageServerModeline = doc . lineComments . find ( ( lineComment ) => {
449- const matchModeline = lineComment . match ( / ^ # \s + y a m l - l a n g u a g e - s e r v e r \s * : / g) ;
450- return matchModeline !== null && matchModeline . length === 1 ;
451- } ) ;
452- if ( yamlLanguageServerModeline != undefined ) {
453- const schemaMatchs = yamlLanguageServerModeline . match ( / \$ s c h e m a = \S + / g) ;
454- if ( schemaMatchs !== null && schemaMatchs . length >= 1 ) {
455- if ( schemaMatchs . length >= 2 ) {
456- console . log (
457- 'Several $schema attributes have been found on the yaml-language-server modeline. The first one will be picked.'
458- ) ;
459- }
460- return schemaMatchs [ 0 ] . substring ( '$schema=' . length ) ;
461- }
462- }
463- }
464- return undefined ;
465- }
466-
467472 private async resolveCustomSchema ( schemaUri , doc ) : ResolvedSchema {
468473 const unresolvedSchema = await this . loadSchema ( schemaUri ) ;
469474 const schema = await this . resolveSchemaContent ( unresolvedSchema , schemaUri , [ ] ) ;
@@ -635,11 +640,25 @@ export class YAMLSchemaService extends JSONSchemaService {
635640 ) ;
636641 }
637642 unresolvedJsonSchema . uri = schemaUri ;
643+ if ( this . schemaUriToNameAndDescription . has ( schemaUri ) ) {
644+ const [ name , description ] = this . schemaUriToNameAndDescription . get ( schemaUri ) ;
645+ unresolvedJsonSchema . schema . title = name ?? unresolvedJsonSchema . schema . title ;
646+ unresolvedJsonSchema . schema . description = description ?? unresolvedJsonSchema . schema . description ;
647+ }
638648 return unresolvedJsonSchema ;
639649 } ) ;
640650 }
641651
642- registerExternalSchema ( uri : string , filePatterns ?: string [ ] , unresolvedSchema ?: JSONSchema ) : SchemaHandle {
652+ registerExternalSchema (
653+ uri : string ,
654+ filePatterns ?: string [ ] ,
655+ unresolvedSchema ?: JSONSchema ,
656+ name ?: string ,
657+ description ?: string
658+ ) : SchemaHandle {
659+ if ( name || description ) {
660+ this . schemaUriToNameAndDescription . set ( uri , [ name , description ] ) ;
661+ }
643662 return super . registerExternalSchema ( uri , filePatterns , unresolvedSchema ) ;
644663 }
645664
0 commit comments