public bool HasAttributeFor(AnnotationType annotationType, string memberName = null)
        {
            if (!annotationType.HasFlag(AnnotationType.Attribute))
            {
                return(false);
            }

            var name = "VB_" + annotationType;

            if (annotationType.HasFlag(AnnotationType.MemberAnnotation))
            {
                //Debug.Assert(memberName != null);
                return(this.Any(a => a.Name.Equals($"{memberName}{name}", StringComparison.OrdinalIgnoreCase)));
            }

            if (annotationType.HasFlag(AnnotationType.ModuleAnnotation))
            {
                //Debug.Assert(memberName == null);
                return(this
                       .Any(a => a.Name.Equals(name, StringComparison.OrdinalIgnoreCase) &&
                            a.Values.Any(v => v.Equals("True", StringComparison.OrdinalIgnoreCase))));
            }

            return(false);
        }
        private static IReadOnlyList <AnnotationType> ModuleAnnotations()
        {
            var type = typeof(AnnotationType);

            return(Enum.GetValues(type)
                   .Cast <AnnotationType>()
                   .Where(annotationType => annotationType.HasFlag(AnnotationType.ModuleAnnotation))
                   .ToList());
        }
Exemple #3
0
        public void AddAnnotation(IRewriteSession rewriteSession, IdentifierReference reference, AnnotationType annotationType,
                                  IReadOnlyList <string> values = null)
        {
            var annotationValues = values ?? new List <string>();

            if (reference == null)
            {
                _logger.Warn("Tried to add an annotation to an identifier reference that is null.");
                _logger.Trace($"Tried to add annotation {annotationType} with values {AnnotationValuesText(annotationValues)} to an identifier reference that is null.");
                return;
            }

            if (!annotationType.HasFlag(AnnotationType.IdentifierAnnotation))
            {
                _logger.Warn("Tried to add an annotation without the identifier reference annotation flag to an identifier reference.");
                _logger.Trace($"Tried to add annotation {annotationType} with values {AnnotationValuesText(annotationValues)} to the identifier reference to {reference.Declaration.QualifiedName} at {reference.Selection} in module {reference.QualifiedModuleName}.");
                return;
            }

            if (rewriteSession.TargetCodeKind != CodeKind.CodePaneCode)
            {
                _logger.Warn($"Tried to add an annotation to an identifier reference with a rewriter not suitable for annotationss. (target code kind = {rewriteSession.TargetCodeKind})");
                _logger.Trace($"Tried to add annotation {annotationType} with values {AnnotationValuesText(annotationValues)} to the the identifier reference {reference.IdentifierName} at {reference.Selection} in module {reference.QualifiedModuleName} using a rewriter not suitable for annotations.");
                return;
            }

            AddAnnotation(rewriteSession, new QualifiedContext(reference.QualifiedModuleName, reference.Context), annotationType, annotationValues);
        }
Exemple #4
0
        private string GetAttributeInstruction(VBAParser.AnnotationContext context, string attributeName, AnnotationType annotationType)
        {
            string attributeInstruction = string.Empty;

            if (annotationType.HasFlag(AnnotationType.ModuleAnnotation))
            {
                switch (annotationType)
                {
                case AnnotationType.Exposed:
                    attributeInstruction = $"Attribute {attributeName} = True\n";
                    break;

                case AnnotationType.PredeclaredId:
                    attributeInstruction = $"Attribute {attributeName} = True\n";
                    break;
                }
            }
            else if (annotationType.HasFlag(AnnotationType.MemberAnnotation))
            {
                switch (annotationType)
                {
                case AnnotationType.Description:
                    var description = context.annotationArgList().annotationArg().FirstOrDefault()?.GetText() ?? string.Empty;
                    description = description.StartsWith("\"") && description.EndsWith("\"")
                            ? description
                            : $"\"{description}\"";

                    attributeInstruction = $"Attribute {attributeName} = \"{description}\"\n";
                    break;

                case AnnotationType.DefaultMember:
                    attributeInstruction = $"Attribute {attributeName} = 0";
                    break;

                case AnnotationType.Enumerator:
                    attributeInstruction = $"Attribute {attributeName} = -4";
                    break;
                }
            }

            return(attributeInstruction);
        }
Exemple #5
0
        private void AddMemberAnnotation(IRewriteSession rewriteSession, Declaration declaration, AnnotationType annotationType, IReadOnlyList <string> annotationValues)
        {
            if (!annotationType.HasFlag(AnnotationType.MemberAnnotation))
            {
                _logger.Warn("Tried to add an annotation without the member annotation flag to a member declaration.");
                _logger.Trace($"Tried to add the annotation {annotationType} with values {AnnotationValuesText(annotationValues)} to the member declaration for {declaration.QualifiedName}.");
                return;
            }

            if (rewriteSession.TargetCodeKind != CodeKind.CodePaneCode)
            {
                _logger.Warn($"Tried to add an annotation to a member with a rewriter not suitable for annotationss. (target code kind = {rewriteSession.TargetCodeKind})");
                _logger.Trace($"Tried to add annotation {annotationType} with values {AnnotationValuesText(annotationValues)} to the the member {declaration.IdentifierName} at {declaration.Selection} in module {declaration.QualifiedModuleName} using a rewriter not suitable for annotations.");
                return;
            }

            AddAnnotation(rewriteSession, new QualifiedContext(declaration.QualifiedName, declaration.Context), annotationType, annotationValues);
        }
Exemple #6
0
        private void AddModuleAnnotation(IRewriteSession rewriteSession, Declaration declaration, AnnotationType annotationType, IReadOnlyList <string> annotationValues)
        {
            if (!annotationType.HasFlag(AnnotationType.ModuleAnnotation))
            {
                _logger.Warn("Tried to add an annotation without the module annotation flag to a module.");
                _logger.Trace($"Tried to add the annotation {annotationType} with values {AnnotationValuesText(annotationValues)} to the module {declaration.QualifiedModuleName}.");
                return;
            }

            if (rewriteSession.TargetCodeKind != CodeKind.CodePaneCode)
            {
                _logger.Warn($"Tried to add an annotation to a module with a rewriter not suitable for annotationss. (target code kind = {rewriteSession.TargetCodeKind})");
                _logger.Trace($"Tried to add annotation {annotationType} with values {AnnotationValuesText(annotationValues)} to the module {declaration.QualifiedModuleName} using a rewriter not suitable for annotations.");
                return;
            }

            var codeToAdd = $"{AnnotationText(annotationType, annotationValues)}{Environment.NewLine}";

            var rewriter = rewriteSession.CheckOutModuleRewriter(declaration.QualifiedModuleName);

            rewriter.InsertBefore(0, codeToAdd);
        }