|
1 | 1 | import { CompletionItem, CompletionItemKind, CompletionParams } from 'vscode-languageserver'; |
2 | 2 | import { |
3 | 3 | supportsCreationPolicy, |
4 | | - supportsAutoScalingCreationPolicy, |
5 | | - supportsStartFleet, |
| 4 | + CREATION_POLICY_SCHEMA, |
| 5 | + CreationPolicyPropertySchema, |
6 | 6 | } from '../artifacts/resourceAttributes/CreationPolicyPropertyDocs'; |
7 | 7 | import { Context } from '../context/Context'; |
8 | 8 | import { ResourceAttribute, TopLevelSection, ResourceAttributesSet } from '../context/ContextType'; |
@@ -366,76 +366,88 @@ export class ResourcePropertyCompletionProvider implements CompletionProvider { |
366 | 366 | return []; |
367 | 367 | } |
368 | 368 |
|
369 | | - const completions: CompletionItem[] = []; |
| 369 | + return this.getSchemaBasedCompletions( |
| 370 | + CREATION_POLICY_SCHEMA, |
| 371 | + propertyPath, |
| 372 | + resourceType, |
| 373 | + context, |
| 374 | + existingProperties, |
| 375 | + ); |
| 376 | + } |
370 | 377 |
|
| 378 | + private getSchemaBasedCompletions( |
| 379 | + schema: Record<string, CreationPolicyPropertySchema>, |
| 380 | + propertyPath: ReadonlyArray<string>, |
| 381 | + resourceType: string, |
| 382 | + context: Context, |
| 383 | + existingProperties: Set<string>, |
| 384 | + ): CompletionItem[] { |
| 385 | + const completions: CompletionItem[] = []; |
371 | 386 | const filteredPath = propertyPath.filter((segment) => segment !== ''); |
372 | 387 | const depth = filteredPath.length; |
373 | 388 |
|
374 | | - // Root CreationPolicy level |
375 | | - if (depth === 1 && context.isKey()) { |
376 | | - if (!existingProperties.has('ResourceSignal')) { |
377 | | - completions.push( |
378 | | - createCompletionItem('ResourceSignal', CompletionItemKind.Property, { |
379 | | - data: { type: 'object' }, |
380 | | - context: context, |
381 | | - }), |
382 | | - ); |
383 | | - } |
| 389 | + if (!context.isKey()) { |
| 390 | + return completions; |
| 391 | + } |
384 | 392 |
|
385 | | - if ( |
386 | | - supportsAutoScalingCreationPolicy(resourceType) && |
387 | | - !existingProperties.has('AutoScalingCreationPolicy') |
388 | | - ) { |
389 | | - completions.push( |
390 | | - createCompletionItem('AutoScalingCreationPolicy', CompletionItemKind.Property, { |
391 | | - data: { type: 'object' }, |
392 | | - context: context, |
393 | | - }), |
394 | | - ); |
395 | | - } |
| 393 | + // Root level |
| 394 | + if (depth === 1) { |
| 395 | + for (const [propertyName, propertySchema] of Object.entries(schema)) { |
| 396 | + if (existingProperties.has(propertyName)) { |
| 397 | + continue; |
| 398 | + } |
| 399 | + |
| 400 | + if ( |
| 401 | + propertySchema.supportedResourceTypes && |
| 402 | + !propertySchema.supportedResourceTypes.includes(resourceType) |
| 403 | + ) { |
| 404 | + continue; |
| 405 | + } |
396 | 406 |
|
397 | | - if (supportsStartFleet(resourceType) && !existingProperties.has('StartFleet')) { |
398 | 407 | completions.push( |
399 | | - createCompletionItem('StartFleet', CompletionItemKind.Property, { |
400 | | - data: { type: 'simple' }, |
| 408 | + createCompletionItem(propertyName, CompletionItemKind.Property, { |
| 409 | + data: { type: propertySchema.type }, |
401 | 410 | context: context, |
402 | 411 | }), |
403 | 412 | ); |
404 | 413 | } |
405 | 414 | } |
406 | | - // Nested properties |
407 | | - else if (depth >= 2 && context.isKey()) { |
408 | | - const parentProperty = filteredPath[1]; |
| 415 | + // Nested levels |
| 416 | + else if (depth >= 2) { |
| 417 | + const parentPropertyName = filteredPath[1]; |
| 418 | + const parentSchema = schema[parentPropertyName]; |
| 419 | + |
| 420 | + if (parentSchema?.properties) { |
| 421 | + if ( |
| 422 | + parentSchema.supportedResourceTypes && |
| 423 | + !parentSchema.supportedResourceTypes.includes(resourceType) |
| 424 | + ) { |
| 425 | + return completions; |
| 426 | + } |
409 | 427 |
|
410 | | - if (parentProperty === 'ResourceSignal') { |
411 | | - if (!existingProperties.has('Count')) { |
412 | | - completions.push( |
413 | | - createCompletionItem('Count', CompletionItemKind.Property, { |
414 | | - data: { type: 'simple' }, |
415 | | - context: context, |
416 | | - }), |
417 | | - ); |
| 428 | + let currentSchema = parentSchema.properties; |
| 429 | + for (let i = 2; i < depth - 1; i++) { |
| 430 | + const segmentName = filteredPath[i]; |
| 431 | + const segmentSchema = currentSchema[segmentName]; |
| 432 | + if (segmentSchema?.properties) { |
| 433 | + currentSchema = segmentSchema.properties; |
| 434 | + } else { |
| 435 | + return completions; |
| 436 | + } |
418 | 437 | } |
419 | 438 |
|
420 | | - if (!existingProperties.has('Timeout')) { |
| 439 | + for (const [propertyName, propertySchema] of Object.entries(currentSchema)) { |
| 440 | + if (existingProperties.has(propertyName)) { |
| 441 | + continue; |
| 442 | + } |
| 443 | + |
421 | 444 | completions.push( |
422 | | - createCompletionItem('Timeout', CompletionItemKind.Property, { |
423 | | - data: { type: 'simple' }, |
| 445 | + createCompletionItem(propertyName, CompletionItemKind.Property, { |
| 446 | + data: { type: propertySchema.type }, |
424 | 447 | context: context, |
425 | 448 | }), |
426 | 449 | ); |
427 | 450 | } |
428 | | - } else if ( |
429 | | - parentProperty === 'AutoScalingCreationPolicy' && |
430 | | - supportsAutoScalingCreationPolicy(resourceType) && |
431 | | - !existingProperties.has('MinSuccessfulInstancesPercent') |
432 | | - ) { |
433 | | - completions.push( |
434 | | - createCompletionItem('MinSuccessfulInstancesPercent', CompletionItemKind.Property, { |
435 | | - data: { type: 'simple' }, |
436 | | - context: context, |
437 | | - }), |
438 | | - ); |
439 | 451 | } |
440 | 452 | } |
441 | 453 |
|
|
0 commit comments