/// <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 int FitsOnOneLine(IPropertyAssignment expression, int remainingSpace) { var space = remainingSpace; space -= expression.Name.Text.Length; space -= 2; return(FitsOnOneLine(expression.Initializer, space)); }
public bool IsAvailable(IUserDataHolder cache) { _propertyAssignment = null; _dllImportAttribute = null; Func<IAttribute, bool> isDllImportAttribute = a => { var typeReference = a.TypeReference; if (typeReference != null) { var typeElement = typeReference.Resolve().DeclaredElement as ITypeElement; if (typeElement != null && Equals(typeElement.GetClrName(), DllImportMissingAnalyzer.DllImportAttribute)) { return true; } } return false; }; var method = _provider.GetSelectedElement<IMethodDeclaration>(true, true); if (method == null) { return false; } var attribute = method.Attributes.FirstOrDefault(isDllImportAttribute); if (attribute == null || !attribute.IsValid()) { return false; } _dllImportAttribute = attribute; var entryPoint = attribute.PropertyAssignments.FirstOrDefault(pa => pa.PropertyNameIdentifier.Name == "EntryPoint"); if (entryPoint == null) { return true; } _propertyAssignment = entryPoint; var literal = entryPoint.Source as ICSharpLiteralExpression; if (literal != null) { if (!literal.IsConstantValue()) { return true; } var value = literal.ConstantValue.Value as string; return value != method.NameIdentifier.Name; } return false; }
public static IPropertyAssignment AddPropertyAssignmentAfter(this IAttribute attr, IPropertyAssignment param, IPropertyAssignment anchor) { using (WriteLockCookie.Create(attr.IsPhysical())) { if (attr.LPar == null) { ModificationUtil.AddChild<LeafElementBase>((ITreeNode)attr, TreeElementFactory.CreateLeafElement(CSharpTokenType.LPARENTH)); ModificationUtil.AddChild<LeafElementBase>((ITreeNode)attr, TreeElementFactory.CreateLeafElement(CSharpTokenType.RPARENTH)); } if (anchor == null) { if (attr.Arguments.Any()) return ModificationUtil.AddChildAfter<IPropertyAssignment>((ITreeNode)ModificationUtil.AddChildAfter<LeafElementBase>((ITreeNode)attr.Arguments.Last(), TreeElementFactory.CreateLeafElement(CSharpTokenType.COMMA)), param); if (attr.PropertyAssignments.Any()) return ModificationUtil.AddChildBefore<IPropertyAssignment>((ITreeNode)ModificationUtil.AddChildBefore<LeafElementBase>((ITreeNode)attr.PropertyAssignments.First(), TreeElementFactory.CreateLeafElement(CSharpTokenType.COMMA)), param); return ModificationUtil.AddChildAfter<IPropertyAssignment>((ITreeNode)attr.LPar, param); } return ModificationUtil.AddChildAfter<IPropertyAssignment>((ITreeNode)ModificationUtil.AddChildAfter<LeafElementBase>((ITreeNode)anchor, TreeElementFactory.CreateLeafElement(CSharpTokenType.COMMA)), param); } }
public static IPropertyAssignment AddPropertyAssignmentBefore(this IAttribute attr, IPropertyAssignment param, IPropertyAssignment anchor) { using (WriteLockCookie.Create(attr.IsPhysical())) { if (attr.LPar == null) { ModificationUtil.AddChild<LeafElementBase>((ITreeNode)attr, TreeElementFactory.CreateLeafElement(CSharpTokenType.LPARENTH)); ModificationUtil.AddChild<LeafElementBase>((ITreeNode)attr, TreeElementFactory.CreateLeafElement(CSharpTokenType.RPARENTH)); } else if (attr.RPar == null) ModificationUtil.AddChild<LeafElementBase>((ITreeNode)attr, TreeElementFactory.CreateLeafElement(CSharpTokenType.RPARENTH)); Logger.Assert(attr.RPar != null, "The condition (RPar != null) is false."); if (anchor == null) { if (attr.Arguments.Any() || attr.PropertyAssignments.Any()) return ModificationUtil.AddChildAfter<IPropertyAssignment>((ITreeNode)ModificationUtil.AddChildBefore<LeafElementBase>((ITreeNode)attr.RPar, TreeElementFactory.CreateLeafElement(CSharpTokenType.COMMA)), param); return ModificationUtil.AddChildBefore<IPropertyAssignment>((ITreeNode)attr.RPar, param); } Logger.Assert(anchor.Parent == attr, "anchor.Parent == this"); Logger.Assert((int)GetChildRole((TreeElement)anchor) == 106, "GetChildRole((TreeElement)anchor) == SPECIAL_ARGUMENT"); return ModificationUtil.AddChildBefore<IPropertyAssignment>((ITreeNode)ModificationUtil.AddChildBefore<LeafElementBase>((ITreeNode)anchor, TreeElementFactory.CreateLeafElement(CSharpTokenType.COMMA)), param); } }
/// <inheritdoc /> public override void VisitPropertyAssignment(IPropertyAssignment node) { CreateAndRegisterReferenceSpan(node.Name, node.Name, m_codex.Property, m_codex.Write); base.VisitPropertyAssignment(node); }
public static void RemovePropertyAssignment(this IAttribute attr, IPropertyAssignment param) { using (WriteLockCookie.Create(attr.IsPhysical())) { ITreeNode prevSibling = param.PrevSibling; while (prevSibling != null && (int)GetChildRole((TreeElement)prevSibling) != 12) prevSibling = prevSibling.PrevSibling; if (prevSibling != null) { ModificationUtil.DeleteChildRange(prevSibling, (ITreeNode)param); } else { ITreeNode nextSibling = param.NextSibling; while (nextSibling != null && (int)GetChildRole((TreeElement)nextSibling) != 12) nextSibling = nextSibling.NextSibling; if (nextSibling != null) ModificationUtil.DeleteChildRange((ITreeNode)param, nextSibling); else ModificationUtil.DeleteChild((ITreeNode)param); } } }