@@ -12,7 +12,7 @@ import { setKubernetesParserOption } from '../parser/isKubernetes';
12
12
import { TextDocument } from 'vscode-languageserver-textdocument' ;
13
13
import { yamlDocumentsCache } from '../parser/yaml-documents' ;
14
14
import { SingleYAMLDocument } from '../parser/yamlParser07' ;
15
- import { getNodeValue , IApplicableSchema } from '../parser/jsonParser07' ;
15
+ import { IApplicableSchema } from '../parser/jsonParser07' ;
16
16
import { JSONSchema } from '../jsonSchema' ;
17
17
import { URI } from 'vscode-uri' ;
18
18
import * as path from 'path' ;
@@ -113,27 +113,31 @@ export class YAMLHover {
113
113
114
114
let title : string | undefined = undefined ;
115
115
let markdownDescription : string | undefined = undefined ;
116
- let markdownEnumValueDescription : string | undefined = undefined ;
117
- let enumValue : string | undefined = undefined ;
116
+ let markdownEnumDescriptions : string [ ] = [ ] ;
118
117
const markdownExamples : string [ ] = [ ] ;
118
+ const markdownEnums : markdownEnum [ ] = [ ] ;
119
119
120
120
matchingSchemas . every ( ( s ) => {
121
121
if ( ( s . node === node || ( node . type === 'property' && node . valueNode === s . node ) ) && ! s . inverted && s . schema ) {
122
122
title = title || s . schema . title || s . schema . closestTitle ;
123
123
markdownDescription = markdownDescription || s . schema . markdownDescription || toMarkdown ( s . schema . description ) ;
124
124
if ( s . schema . enum ) {
125
- const idx = s . schema . enum . indexOf ( getNodeValue ( node ) ) ;
126
125
if ( s . schema . markdownEnumDescriptions ) {
127
- markdownEnumValueDescription = s . schema . markdownEnumDescriptions [ idx ] ;
126
+ markdownEnumDescriptions = s . schema . markdownEnumDescriptions ;
128
127
} else if ( s . schema . enumDescriptions ) {
129
- markdownEnumValueDescription = toMarkdown ( s . schema . enumDescriptions [ idx ] ) ;
128
+ markdownEnumDescriptions = s . schema . enumDescriptions . map ( toMarkdown ) ;
129
+ } else {
130
+ markdownEnumDescriptions = [ ] ;
130
131
}
131
- if ( markdownEnumValueDescription ) {
132
- enumValue = s . schema . enum [ idx ] ;
132
+ s . schema . enum . forEach ( ( enumValue , idx ) => {
133
133
if ( typeof enumValue !== 'string' ) {
134
134
enumValue = JSON . stringify ( enumValue ) ;
135
135
}
136
- }
136
+ markdownEnums . push ( {
137
+ value : enumValue ,
138
+ description : markdownEnumDescriptions [ idx ] ,
139
+ } ) ;
140
+ } ) ;
137
141
}
138
142
if ( s . schema . anyOf && isAllSchemasMatched ( node , matchingSchemas , s . schema ) ) {
139
143
//if append title and description of all matched schemas on hover
@@ -163,28 +167,30 @@ export class YAMLHover {
163
167
result = '#### ' + toMarkdown ( title ) ;
164
168
}
165
169
if ( markdownDescription ) {
166
- if ( result . length > 0 ) {
167
- result += '\n\n' ;
168
- }
170
+ result = ensureLineBreak ( result ) ;
169
171
result += markdownDescription ;
170
172
}
171
- if ( markdownEnumValueDescription ) {
172
- if ( result . length > 0 ) {
173
- result += '\n\n' ;
174
- }
175
- result += `\`${ toMarkdownCodeBlock ( enumValue ) } \`: ${ markdownEnumValueDescription } ` ;
173
+ if ( markdownEnums . length !== 0 ) {
174
+ result = ensureLineBreak ( result ) ;
175
+ result += 'Allowed Values:\n\n' ;
176
+ markdownEnums . forEach ( ( me ) => {
177
+ if ( me . description ) {
178
+ result += `* \`${ toMarkdownCodeBlock ( me . value ) } \`: ${ me . description } \n` ;
179
+ } else {
180
+ result += `* \`${ toMarkdownCodeBlock ( me . value ) } \`\n` ;
181
+ }
182
+ } ) ;
176
183
}
177
184
if ( markdownExamples . length !== 0 ) {
178
- if ( result . length > 0 ) {
179
- result += '\n\n' ;
180
- }
181
- result += 'Examples:' ;
185
+ result = ensureLineBreak ( result ) ;
186
+ result += 'Examples:\n\n' ;
182
187
markdownExamples . forEach ( ( example ) => {
183
- result += `\n\n\ `\`\`${ example } \`\`\`` ;
188
+ result += `* \ `\`\`${ example } \`\`\`\n ` ;
184
189
} ) ;
185
190
}
186
191
if ( result . length > 0 && schema . schema . url ) {
187
- result += `\n\nSource: [${ getSchemaName ( schema . schema ) } ](${ schema . schema . url } )` ;
192
+ result = ensureLineBreak ( result ) ;
193
+ result += `Source: [${ getSchemaName ( schema . schema ) } ](${ schema . schema . url } )` ;
188
194
}
189
195
return createHover ( result ) ;
190
196
}
@@ -193,6 +199,21 @@ export class YAMLHover {
193
199
}
194
200
}
195
201
202
+ interface markdownEnum {
203
+ value : string ;
204
+ description : string ;
205
+ }
206
+
207
+ function ensureLineBreak ( content : string ) : string {
208
+ if ( content . length === 0 ) {
209
+ return content ;
210
+ }
211
+ if ( ! content . endsWith ( '\n' ) ) {
212
+ content += '\n' ;
213
+ }
214
+ return content + '\n' ;
215
+ }
216
+
196
217
function getSchemaName ( schema : JSONSchema ) : string {
197
218
let result = 'JSON Schema' ;
198
219
const urlString = schema . url ;
0 commit comments