Beispiel #1
0
 public BicepCompletionContext(
     BicepCompletionContextKind kind,
     Range replacementRange,
     SyntaxBase?enclosingDeclaration,
     ObjectSyntax? @object,
     ObjectPropertySyntax?property,
     ArraySyntax?array,
     PropertyAccessSyntax?propertyAccess,
     ResourceAccessSyntax?resourceAccess,
     ArrayAccessSyntax?arrayAccess,
     TargetScopeSyntax?targetScope,
     ImmutableArray <ILanguageScope> activeScopes)
 {
     this.Kind                 = kind;
     this.ReplacementRange     = replacementRange;
     this.EnclosingDeclaration = enclosingDeclaration;
     this.Object               = @object;
     this.Property             = property;
     this.Array                = array;
     this.PropertyAccess       = propertyAccess;
     this.ResourceAccess       = resourceAccess;
     this.ArrayAccess          = arrayAccess;
     this.TargetScope          = targetScope;
     this.ActiveScopes         = activeScopes;
 }
Beispiel #2
0
        public static ObjectSyntax DeepMerge(this ObjectSyntax?sourceObject, ObjectSyntax targetObject)
        {
            if (sourceObject == null)
            {
                return(targetObject);
            }

            return(targetObject.Properties.Aggregate(sourceObject, (mergedObject, property) =>
                                                     property.TryGetKeyText() is string propertyName
                    ? mergedObject.MergeProperty(propertyName, property.Value)
                    : mergedObject));
        }
Beispiel #3
0
        private DeclaredTypeAssignment?GetObjectPropertyType(TypeSymbol?type, ObjectSyntax?objectSyntax, string propertyName, bool useSyntax)
        {
            // local function
            DeclaredTypeFlags ConvertFlags(TypePropertyFlags flags) => flags.HasFlag(TypePropertyFlags.Constant) ? DeclaredTypeFlags.Constant : DeclaredTypeFlags.None;

            // the declared types on the declaration side of things will take advantage of properties
            // set on objects to resolve discriminators at all levels
            // to take advantage of this, we should first try looking up the property's declared type
            var declaringProperty = objectSyntax?.SafeGetPropertyByName(propertyName);

            if (useSyntax && declaringProperty != null)
            {
                // it is important to get the property value's decl type instead of the property's decl type
                // (the property has the unresolved discriminated object type and the value will have it resolved)
                var declaredPropertyAssignment = this.GetDeclaredTypeAssignment(declaringProperty.Value);
                if (declaredPropertyAssignment != null)
                {
                    return(declaredPropertyAssignment);
                }
            }

            // could not get the declared type via syntax
            // let's use the type info instead
            switch (type)
            {
            case ObjectType objectType:
                // lookup declared property
                if (objectType.Properties.TryGetValue(propertyName, out var property))
                {
                    return(new DeclaredTypeAssignment(property.TypeReference.Type, declaringProperty, ConvertFlags(property.Flags)));
                }

                // if there are additional properties, try those
                if (objectType.AdditionalPropertiesType != null)
                {
                    return(new DeclaredTypeAssignment(objectType.AdditionalPropertiesType.Type, declaringProperty, ConvertFlags(objectType.AdditionalPropertiesFlags)));
                }

                break;

            case DiscriminatedObjectType discriminated:
                if (string.Equals(propertyName, discriminated.DiscriminatorProperty.Name, LanguageConstants.IdentifierComparison))
                {
                    // the property is the discriminator property - use its type
                    return(new DeclaredTypeAssignment(discriminated.DiscriminatorProperty.TypeReference.Type, declaringProperty));
                }

                break;
            }

            return(null);
        }
 public BicepCompletionContext(
     BicepCompletionContextKind kind,
     Range replacementRange,
     SyntaxBase?enclosingDeclaration,
     ObjectSyntax? @object,
     ObjectPropertySyntax?property,
     ArraySyntax?array,
     PropertyAccessSyntax?propertyAccess,
     TargetScopeSyntax?targetScope)
 {
     this.Kind                 = kind;
     this.ReplacementRange     = replacementRange;
     this.EnclosingDeclaration = enclosingDeclaration;
     this.Object               = @object;
     this.Property             = property;
     this.Array                = array;
     this.PropertyAccess       = propertyAccess;
     this.TargetScope          = targetScope;
 }
Beispiel #5
0
        public static ObjectSyntax MergeProperty(this ObjectSyntax?syntax, string propertyName, SyntaxBase propertyValue)
        {
            if (syntax == null)
            {
                return(SyntaxFactory.CreateObject(SyntaxFactory.CreateObjectProperty(propertyName, propertyValue).AsEnumerable()));
            }

            var properties    = syntax.Properties.ToList();
            int matchingIndex = 0;

            while (matchingIndex < properties.Count)
            {
                if (string.Equals(properties[matchingIndex].TryGetKeyText(), propertyName, LanguageConstants.IdentifierComparison))
                {
                    break;
                }

                matchingIndex++;
            }

            if (matchingIndex < properties.Count)
            {
                // If both property values are objects, merge them. Otherwise, replace the matching property value.
                SyntaxBase mergedValue = properties[matchingIndex].Value is ObjectSyntax sourceObject && propertyValue is ObjectSyntax targetObject
                    ? sourceObject.DeepMerge(targetObject)
                    : propertyValue;

                properties[matchingIndex] = SyntaxFactory.CreateObjectProperty(propertyName, mergedValue);
            }
            else
            {
                properties.Add(SyntaxFactory.CreateObjectProperty(propertyName, propertyValue));
            }

            return(SyntaxFactory.CreateObject(properties));
        }
Beispiel #6
0
 public static ObjectSyntax MergeProperty(this ObjectSyntax?syntax, string propertyName, string propertyValue) =>
 syntax.MergeProperty(propertyName, SyntaxFactory.CreateStringLiteral(propertyValue));
Beispiel #7
0
        public ObjectSyntax?Evaluate(DecoratorSyntax decoratorSyntax, TypeSymbol targetType, ObjectSyntax?targetObject)
        {
            if (this.evaluator is null)
            {
                return(targetObject);
            }

            return(this.evaluator(decoratorSyntax, targetType, targetObject));
        }
 public BicepCompletionContext(BicepCompletionContextKind kind, SyntaxBase?enclosingDeclaration = null, ObjectSyntax? @object = null, ObjectPropertySyntax?property = null, ArraySyntax?array = null)
 {
     this.Kind = kind;
     this.EnclosingDeclaration = enclosingDeclaration;
     this.Object   = @object;
     this.Property = property;
     this.Array    = array;
 }
 public BicepCompletionContext(BicepCompletionContextKind kind, ObjectSyntax? @object = null)
 {
     this.Kind   = kind;
     this.Object = @object;
 }