private IAttribute CreateExcludeFromCodeCoverageAttribute() { ITypeElement type = TypeFactory.CreateTypeByCLRName( typeof(ExcludeFromCodeCoverageAttribute).FullName, _provider.PsiModule).GetTypeElement(); return(_factory.CreateAttribute(type)); }
/// <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); } }
private static IAttribute CreateAttribute(IClrTypeName attributeTypeName, IPsiModule module, CSharpElementFactory elementFactory) { var typeElement = TypeFactory.CreateTypeByCLRName(attributeTypeName, module).GetTypeElement(); return(typeElement != null?elementFactory.CreateAttribute(typeElement) : null); }
private static IAttribute CreateAttribute(IClrTypeName attributeTypeName, [NotNull] AttributeValue[] attributeValues, IPsiModule module, CSharpElementFactory elementFactory) { var typeElement = TypeFactory.CreateTypeByCLRName(attributeTypeName, module).GetTypeElement(); return(typeElement != null ? elementFactory.CreateAttribute(typeElement, attributeValues, EmptyArray <Pair <string, AttributeValue> > .Instance) : null); }
/// <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) { IDeclaration declaration = Utils.GetDeclarationClosestToTextControl(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); IAttributesOwnerDeclaration attributesOwnerDeclaration = declaration as IAttributesOwnerDeclaration; if (attributesOwnerDeclaration != null) { 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); attribute.AddArgumentAfter(Utils.CreateArgumentValueExpression(declaration.GetPsiModule(), "Justification = \"" + justificationText + "\""), newArg2); attributesOwnerDeclaration.AddAttributeAfter(attribute, null); IFile file = declaration.GetContainingFile(); if (file != null) { LanguageService languageService = CSharpLanguage.Instance.LanguageService(); if (languageService != null) { ICSharpCodeFormatter codeFormatter = (ICSharpCodeFormatter)languageService.CodeFormatter; if (codeFormatter != null) { codeFormatter.FormatFile(file, CodeFormatProfile.DEFAULT, NullProgressIndicator.Instance); } } } } } }
public static void AddAttribute([CanBeNull] IFieldDeclaration fieldDeclaration, IClrTypeName attributeTypeName, IPsiModule psiModule, CSharpElementFactory elementFactory) { if (fieldDeclaration == null) { return; } var typeElement = TypeFactory.CreateTypeByCLRName(attributeTypeName, psiModule).GetTypeElement(); if (typeElement != null) { var attribute = elementFactory.CreateAttribute(typeElement); fieldDeclaration.AddAttributeAfter(attribute, null); } }
private IAttribute CreateContractClassAttribute(string contractClassName) { ITypeElement type = TypeFactory.CreateTypeByCLRName( typeof(ContractClassAttribute).FullName, _provider.PsiModule, _currentFile.GetResolveContext()).GetTypeElement(); var expression = _factory.CreateExpressionAsIs( string.Format("typeof({0})", contractClassName)); var attribute = _factory.CreateAttribute(type); attribute.AddArgumentAfter( _factory.CreateArgument(ParameterKind.VALUE, expression), null); return(attribute); }
protected override IAttribute CreateAttribute(ITypeElement resolvedAttributeType, CSharpElementFactory factory, IPsiModule psiModule) { if (this.MatchingFlagName == null) { return(base.CreateAttribute(resolvedAttributeType, factory, psiModule)); } var enumType = TypeElementUtil.GetTypeElementByClrName(AutoFixtureConstants.MatchingEnumType, psiModule) as IEnum; var enumValue = enumType?.EnumMembers.FirstOrDefault(f => f.ShortName == this.MatchingFlagName)?.ConstantValue; if (enumValue == null) { return(null); } return(factory.CreateAttribute(resolvedAttributeType, new[] { new AttributeValue(enumValue) }, EmptyArray <Pair <string, AttributeValue> > .Instance)); }
/// <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) { IDeclaration declaration = Utils.GetDeclarationClosestToTextControl(solution, textControl); if (declaration != null) { string rulesNamespace = this.Rule.Namespace; string ruleText = string.Format("{0}:{1}", this.Rule.CheckId, this.Rule.Name); string justificationText = StyleCopOptions.Instance.SuppressStyleCopAttributeJustificationText; IAttributesOwnerDeclaration attributesOwnerDeclaration = declaration as IAttributesOwnerDeclaration; if (attributesOwnerDeclaration != null) { 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); attribute.AddArgumentAfter(Utils.CreateArgumentValueExpression(declaration.GetPsiModule(), "Justification = \"" + justificationText + "\""), newArg2); attributesOwnerDeclaration.AddAttributeAfter(attribute, null); IFile file = declaration.GetContainingFile(); CSharpFormatterHelper.FormatterInstance.FormatFile( file, SolutionCodeStyleSettings.GetInstance(solution).CodeStyleSettings, CodeFormatProfile.DEFAULT, NullProgressIndicator.Instance); } } }
protected virtual IAttribute CreateAttribute([NotNull] ITypeElement resolvedAttributeType, [NotNull] CSharpElementFactory factory, [NotNull] IPsiModule psiModule) { return(factory.CreateAttribute(resolvedAttributeType)); }
/// <summary> /// Adds the attribute. /// </summary> /// <param name="solution"> /// The solution. /// </param> /// <param name="classDeclaration"> /// The class declaration. /// </param> /// <param name="factory"> /// The factory. /// </param> private static void AddAttribute(ISolution solution, IClassDeclaration classDeclaration, CSharpElementFactory factory) { var typeName = new CLRTypeName("System.SerializableAttribute"); var scope = DeclarationsScopeFactory.SolutionScope(solution, true); var cache = PsiManager.GetInstance(solution).GetDeclarationsCache(scope, true); var typeElement = cache.GetTypeElementByCLRName(typeName); if (typeElement == null) { return; } var attribute = factory.CreateAttribute(typeElement); classDeclaration.AddAttributeAfter(attribute, null); }