/// <summary> /// The execute inner. /// </summary> /// <param name="solution"> /// The solution. /// </param> /// <param name="textControl"> /// The text control. /// </param> public override void ExecuteTransactionInner(ISolution solution, ITextControl textControl) { IAttributesOwnerDeclaration declaration = Utils.GetTypeClosestToTextControl <IAttributesOwnerDeclaration>(solution, textControl); if (declaration != null) { string rulesNamespace = this.Rule.Namespace; string ruleText = string.Format("{0}:{1}", this.Rule.CheckId, this.Rule.Name); IContextBoundSettingsStore settingsStore = PsiSourceFileExtensions.GetSettingsStore(null, solution); string justificationText = settingsStore.GetValue((StyleCopOptionsSettingsKey key) => key.SuppressStyleCopAttributeJustificationText); CSharpElementFactory factory = CSharpElementFactory.GetInstance(declaration.GetPsiModule()); ITypeElement typeElement = Utils.GetTypeElement(declaration, "System.Diagnostics.CodeAnalysis.SuppressMessageAttribute"); IAttribute attribute = factory.CreateAttribute(typeElement); ICSharpArgument newArg1 = attribute.AddArgumentAfter(Utils.CreateConstructorArgumentValueExpression(declaration.GetPsiModule(), rulesNamespace), null); ICSharpArgument newArg2 = attribute.AddArgumentAfter(Utils.CreateConstructorArgumentValueExpression(declaration.GetPsiModule(), ruleText), newArg1); IPropertyAssignment propertyAssignment = factory.CreatePropertyAssignment( "Justification", factory.CreateExpression("\"$0\"", justificationText)); attribute.AddPropertyAssignmentAfter(propertyAssignment, null); declaration.AddAttributeAfter(attribute, null); } }
/// <summary>Sets the attribute.</summary> /// <param name="owner">The owner.</param> /// <param name="attributeShortName">Short name of the attribute.</param> public static void SetAttribute([NotNull] IAttributesOwnerDeclaration owner, [NotNull] string attributeShortName) { if (owner == null) { throw new ArgumentNullException("owner"); } if (!owner.IsValid()) { return; } if (attributeShortName == null) { throw new ArgumentNullException("attributeShortName"); } var psiServices = owner.GetPsiServices(); if (psiServices == null) { throw new InvalidOperationException("psiServices"); } var cache = psiServices.GetCodeAnnotationsCache(); if (cache == null) { return; } var attributeTypeElement = cache.GetAttributeTypeForElement(owner, attributeShortName); if (attributeTypeElement == null) { return; } var factory = CSharpElementFactory.GetInstance(owner.GetPsiModule()); var attribute = factory.CreateAttribute(attributeTypeElement); owner.AddAttributeAfter(attribute, null); }
/// <summary> /// If the declaration or its parent has the rule provided suppressed it returns true. /// </summary> /// <param name="attributesOwnerDeclaration"> /// The declaration to check. /// </param> /// <param name="ruleId"> /// The ruleId to see if its suppressed. /// </param> /// <returns> /// True if suppressed. /// </returns> private static bool IsRuleSuppressedInternal(IAttributesOwnerDeclaration attributesOwnerDeclaration, string ruleId) { if (attributesOwnerDeclaration != null) { CSharpElementFactory factory = CSharpElementFactory.GetInstance(attributesOwnerDeclaration.GetPsiModule()); ITypeElement typeElement = Utils.GetTypeElement(attributesOwnerDeclaration, "System.Diagnostics.CodeAnalysis.SuppressMessageAttribute"); IAttribute attribute = factory.CreateAttribute(typeElement); foreach (IAttribute s in attributesOwnerDeclaration.Attributes) { if (s.Name.ShortName == attribute.Name.ShortName) { ICSharpLiteralExpression b = s.ConstructorArgumentExpressions[1] as ICSharpLiteralExpression; if (b == null) { return false; } string d = b.GetText(); if (string.IsNullOrEmpty(d)) { return false; } string e = d.Trim('\"').Substring(2); // removes the 'SA' bit string f = ruleId.Substring(2); // removes the 'SA' bit return e == f; } } } return false; }