Skip to content

Commit 6a9e6a1

Browse files
used matchPathWithLogicalId and used schema instead of hardcoding
1 parent d1dab3c commit 6a9e6a1

File tree

3 files changed

+93
-55
lines changed

3 files changed

+93
-55
lines changed

src/artifacts/resourceAttributes/CreationPolicyPropertyDocs.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,3 +140,30 @@ export function supportsAutoScalingCreationPolicy(resourceType: string): boolean
140140
export function supportsStartFleet(resourceType: string): boolean {
141141
return START_FLEET_SUPPORTED_RESOURCE_TYPES.includes(resourceType);
142142
}
143+
144+
export interface CreationPolicyPropertySchema {
145+
type: 'object' | 'simple';
146+
supportedResourceTypes?: ReadonlyArray<string>;
147+
properties?: Record<string, CreationPolicyPropertySchema>;
148+
}
149+
150+
export const CREATION_POLICY_SCHEMA: Record<string, CreationPolicyPropertySchema> = {
151+
[CreationPolicyProperty.ResourceSignal]: {
152+
type: 'object',
153+
properties: {
154+
[ResourceSignalProperty.Count]: { type: 'simple' },
155+
[ResourceSignalProperty.Timeout]: { type: 'simple' },
156+
},
157+
},
158+
[CreationPolicyProperty.AutoScalingCreationPolicy]: {
159+
type: 'object',
160+
supportedResourceTypes: AUTO_SCALING_CREATION_POLICY_SUPPORTED_RESOURCE_TYPES,
161+
properties: {
162+
[AutoScalingCreationPolicyProperty.MinSuccessfulInstancesPercent]: { type: 'simple' },
163+
},
164+
},
165+
[CreationPolicyProperty.StartFleet]: {
166+
type: 'simple',
167+
supportedResourceTypes: START_FLEET_SUPPORTED_RESOURCE_TYPES,
168+
},
169+
};

src/autocomplete/ResourcePropertyCompletionProvider.ts

Lines changed: 64 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { CompletionItem, CompletionItemKind, CompletionParams } from 'vscode-languageserver';
22
import {
33
supportsCreationPolicy,
4-
supportsAutoScalingCreationPolicy,
5-
supportsStartFleet,
4+
CREATION_POLICY_SCHEMA,
5+
CreationPolicyPropertySchema,
66
} from '../artifacts/resourceAttributes/CreationPolicyPropertyDocs';
77
import { Context } from '../context/Context';
88
import { ResourceAttribute, TopLevelSection, ResourceAttributesSet } from '../context/ContextType';
@@ -366,76 +366,88 @@ export class ResourcePropertyCompletionProvider implements CompletionProvider {
366366
return [];
367367
}
368368

369-
const completions: CompletionItem[] = [];
369+
return this.getSchemaBasedCompletions(
370+
CREATION_POLICY_SCHEMA,
371+
propertyPath,
372+
resourceType,
373+
context,
374+
existingProperties,
375+
);
376+
}
370377

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[] = [];
371386
const filteredPath = propertyPath.filter((segment) => segment !== '');
372387
const depth = filteredPath.length;
373388

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+
}
384392

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+
}
396406

397-
if (supportsStartFleet(resourceType) && !existingProperties.has('StartFleet')) {
398407
completions.push(
399-
createCompletionItem('StartFleet', CompletionItemKind.Property, {
400-
data: { type: 'simple' },
408+
createCompletionItem(propertyName, CompletionItemKind.Property, {
409+
data: { type: propertySchema.type },
401410
context: context,
402411
}),
403412
);
404413
}
405414
}
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+
}
409427

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+
}
418437
}
419438

420-
if (!existingProperties.has('Timeout')) {
439+
for (const [propertyName, propertySchema] of Object.entries(currentSchema)) {
440+
if (existingProperties.has(propertyName)) {
441+
continue;
442+
}
443+
421444
completions.push(
422-
createCompletionItem('Timeout', CompletionItemKind.Property, {
423-
data: { type: 'simple' },
445+
createCompletionItem(propertyName, CompletionItemKind.Property, {
446+
data: { type: propertySchema.type },
424447
context: context,
425448
}),
426449
);
427450
}
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-
);
439451
}
440452
}
441453

src/autocomplete/ResourceSectionCompletionProvider.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { CompletionItem, CompletionParams, CompletionTriggerKind } from 'vscode-languageserver';
22
import { Context } from '../context/Context';
3-
import { ResourceAttributesSet } from '../context/ContextType';
3+
import { ResourceAttributesSet, TopLevelSection } from '../context/ContextType';
44
import { Resource } from '../context/semantic/Entity';
55
import { ServerComponents } from '../server/ServerComponents';
66
import { LoggerFactory } from '../telemetry/LoggerFactory';
@@ -59,8 +59,7 @@ export class ResourceSectionCompletionProvider implements CompletionProvider {
5959

6060
if (
6161
params.context?.triggerKind === CompletionTriggerKind.Invoked &&
62-
context.propertyPath.length === 3 &&
63-
context.entitySection === 'Properties'
62+
context.matchPathWithLogicalId(TopLevelSection.Resources, 'Properties')
6463
) {
6564
const resource = context.entity as Resource;
6665
if (resource.Type) {

0 commit comments

Comments
 (0)