public override void Refactor(AnnotateDeclarationModel model, IRewriteSession rewriteSession)
        {
            if (model.AdjustAttribute &&
                rewriteSession.TargetCodeKind != CodeKind.AttributesCode &&
                model.Annotation is IAttributeAnnotation)
            {
                throw new AttributeRewriteSessionRequiredException();
            }

            var targetDeclaration = model.Target;

            if (rewriteSession.TargetCodeKind == CodeKind.AttributesCode &&
                targetDeclaration.AttributesPassContext == null &&
                !targetDeclaration.DeclarationType.HasFlag(DeclarationType.Module))
            {
                throw new AttributeRewriteSessionNotSupportedException();
            }

            var arguments = model.Arguments.Select(ToCode).ToList();

            if (model.AdjustAttribute &&
                model.Annotation is IAttributeAnnotation attributeAnnotation)
            {
                var baseAttribute = attributeAnnotation.Attribute(arguments);
                var attribute     = targetDeclaration.DeclarationType.HasFlag(DeclarationType.Module)
                    ? baseAttribute
                    : Attributes.MemberAttributeName(baseAttribute, targetDeclaration.IdentifierName);
                var attributeValues = attributeAnnotation.AnnotationToAttributeValues(arguments);
                _attributesUpdater.AddOrUpdateAttribute(rewriteSession, targetDeclaration, attribute, attributeValues);
            }

            _annotationUpdater.AddAnnotation(rewriteSession, targetDeclaration, model.Annotation, arguments);
        }
Example #2
0
        public override void Fix(IInspectionResult result, IRewriteSession rewriteSession)
        {
            var    declaration   = result.Target;
            string attributeName = result.Properties.AttributeName;
            IReadOnlyList <string> attributeValues = result.Properties.AttributeValues;

            var(annotationType, annotationValues) = declaration.DeclarationType.HasFlag(DeclarationType.Module)
                ? _attributeAnnotationProvider.ModuleAttributeAnnotation(attributeName, attributeValues)
                : _attributeAnnotationProvider.MemberAttributeAnnotation(AttributeBaseName(attributeName, declaration), attributeValues);
            _annotationUpdater.AddAnnotation(rewriteSession, declaration, annotationType, annotationValues);
        }
Example #3
0
        public override void Fix(IInspectionResult result, IRewriteSession rewriteSession)
        {
            var module         = result.QualifiedSelection.QualifiedName;
            var lineToAnnotate = result.QualifiedSelection.Selection.StartLine;
            var existingEntryPointAnnotation = _state.DeclarationFinder
                                               .FindAnnotations(module, lineToAnnotate)
                                               .FirstOrDefault(pta => pta.Annotation is EntryPointAnnotation);

            if (existingEntryPointAnnotation == null)
            {
                _annotationUpdater.AddAnnotation(rewriteSession, new QualifiedContext(module, result.Context), new EntryPointAnnotation());
            }
        }
        protected override void OnExecute(object parameter)
        {
            var target = FindTarget(parameter);

            if (target == null)
            {
                return;
            }

            var rewriteSession = _rewritingManager.CheckOutCodePaneSession();

            _annotationUpdater.AddAnnotation(rewriteSession, target, new NoIndentAnnotation());
            rewriteSession.TryRewrite();
        }
Example #5
0
        private void FixNonModule(IInspectionResult result, IRewriteSession rewriteSession)
        {
            var module                   = result.QualifiedSelection.QualifiedName;
            var lineToAnnotate           = result.QualifiedSelection.Selection.StartLine;
            var existingIgnoreAnnotation = _state.DeclarationFinder.FindAnnotations(module, lineToAnnotate)
                                           .OfType <IgnoreAnnotation>()
                                           .FirstOrDefault();

            var annotationType = AnnotationType.Ignore;

            if (existingIgnoreAnnotation != null)
            {
                var annotationValues = existingIgnoreAnnotation.InspectionNames.ToList();
                annotationValues.Insert(0, result.Inspection.AnnotationName);
                _annotationUpdater.UpdateAnnotation(rewriteSession, existingIgnoreAnnotation, annotationType, annotationValues);
            }
            else
            {
                var annotationValues = new List <string> {
                    result.Inspection.AnnotationName
                };
                _annotationUpdater.AddAnnotation(rewriteSession, new QualifiedContext(module, result.Context), annotationType, annotationValues);
            }
        }
        public override void Fix(IInspectionResult result, IRewriteSession rewriteSession)
        {
            var module            = result.QualifiedSelection.QualifiedName;
            var moduleDeclaration = _state.DeclarationFinder.Members(module, DeclarationType.Module)
                                    .FirstOrDefault();

            if (moduleDeclaration == null)
            {
                return;
            }

            var existingIgnoreModuleAnnotation = moduleDeclaration.Annotations
                                                 .FirstOrDefault(pta => pta.Annotation is IgnoreModuleAnnotation);

            var annotationType = new IgnoreModuleAnnotation();

            if (existingIgnoreModuleAnnotation != null)
            {
                var annotationValues = existingIgnoreModuleAnnotation.AnnotationArguments.ToList();

                if (annotationValues.Contains(result.Inspection.AnnotationName))
                {
                    return;
                }

                annotationValues.Insert(0, result.Inspection.AnnotationName);
                _annotationUpdater.UpdateAnnotation(rewriteSession, existingIgnoreModuleAnnotation, annotationType, annotationValues);
            }
            else
            {
                var newModuleText    = rewriteSession.CheckOutModuleRewriter(module).GetText();
                var ignoreModuleText = $"'{ParseTreeAnnotation.ANNOTATION_MARKER}{annotationType.Name}";
                if (newModuleText.Contains(ignoreModuleText))
                {
                    //Most probably, we have added this already in another invocation on the same rewrite session.
                    return;
                }

                var annotationValues = new List <string> {
                    result.Inspection.AnnotationName
                };
                _annotationUpdater.AddAnnotation(rewriteSession, moduleDeclaration, annotationType, annotationValues);
            }
        }
Example #7
0
        private void AddFolderAnnotation(MoveToFolderModel model, IRewriteSession rewriteSession)
        {
            var(annotation, annotationValues) = NewAnnotation(model.TargetFolder);

            _annotationUpdater.AddAnnotation(rewriteSession, model.Target, annotation, annotationValues);
        }