static Ast.AbstractNode CreateSuppressAttribute(ICompilationUnit cu, FxCopTaskTag tag) { //System.Diagnostics.CodeAnalysis.SuppressMessageAttribute bool importedCodeAnalysis = CheckImports(cu); // [SuppressMessage("Microsoft.Performance", "CA1801:ReviewUnusedParameters", MessageId:="fileIdentifier"] Ast.Attribute a = new Ast.Attribute { Name = importedCodeAnalysis ? AttributeName : NamespaceName + "." + AttributeName, PositionalArguments = { new Ast.PrimitiveExpression(tag.Category, tag.Category), new Ast.PrimitiveExpression(tag.CheckID, tag.CheckID) } }; if (tag.MessageID != null) { a.NamedArguments.Add(new Ast.NamedArgumentExpression("MessageId", new Ast.PrimitiveExpression(tag.MessageID, tag.MessageID))); } return(new Ast.AttributeSection { AttributeTarget = tag.MemberName == null ? "assembly" : null, Attributes = { a } }); }
public override void Run() { var view = (System.Windows.Controls.ListView)Owner; foreach (var t in view.SelectedItems.OfType <SDTask>().ToArray()) { FxCopTaskTag tag = t.Tag as FxCopTaskTag; if (tag == null) { continue; } CodeGenerator gen = tag.Project.LanguageBinding.CodeGenerator; ICompilation compilation; if (t.FileName != null) { compilation = SD.ParserService.GetCompilationForFile(t.FileName); } else { compilation = SD.ParserService.GetCompilation(tag.Project); } IAttribute attribute = CreateSuppressAttribute(compilation, tag); if (tag.MemberName == null) { gen.AddAssemblyAttribute(tag.Project, attribute); } else { gen.AddAttribute(GetEntity(compilation, tag.TypeName, tag.MemberName), attribute); } } }
static IAttribute CreateSuppressAttribute(ICompilation compilation, FxCopTaskTag tag) { // [SuppressMessage("Microsoft.Performance", "CA1801:ReviewUnusedParameters", MessageId:="fileIdentifier"] IType attributeType = compilation.FindType(new FullTypeName(FullAttributeName)); IType stringType = compilation.FindType(KnownTypeCode.String); KeyValuePair<IMember, ResolveResult>[] namedArgs = null; if (tag.MessageID != null) { IMember messageId = attributeType.GetProperties(p => p.Name == "MessageId").FirstOrDefault(); namedArgs = new[] { new KeyValuePair<IMember, ResolveResult>(messageId, new ConstantResolveResult(stringType, tag.MessageID)) }; } return new DefaultAttribute( attributeType, new[] { new ConstantResolveResult(stringType, tag.Category), new ConstantResolveResult(stringType, tag.CheckID) }, namedArgs); }
static IAttribute CreateSuppressAttribute(ICompilation compilation, FxCopTaskTag tag) { // [SuppressMessage("Microsoft.Performance", "CA1801:ReviewUnusedParameters", MessageId:="fileIdentifier"] IType attributeType = compilation.FindType(new FullTypeName(FullAttributeName)); IType stringType = compilation.FindType(KnownTypeCode.String); KeyValuePair <IMember, ResolveResult>[] namedArgs = null; if (tag.MessageID != null) { IMember messageId = attributeType.GetProperties(p => p.Name == "MessageId").FirstOrDefault(); namedArgs = new[] { new KeyValuePair <IMember, ResolveResult>(messageId, new ConstantResolveResult(stringType, tag.MessageID)) }; } return(new DefaultAttribute( attributeType, new[] { new ConstantResolveResult(stringType, tag.Category), new ConstantResolveResult(stringType, tag.CheckID) }, namedArgs)); }
public override void Run() { TaskView view = (TaskView)Owner; foreach (Task t in new List <Task>(view.SelectedTasks)) { FxCopTaskTag tag = t.Tag as FxCopTaskTag; if (tag == null) { continue; } CodeGenerator codegen = tag.ProjectContent.Language.CodeGenerator; if (codegen == null) { continue; } FilePosition p; if (tag.MemberName == null) { p = GetAssemblyAttributeInsertionPosition(tag.ProjectContent); } else { p = GetPosition(tag.ProjectContent, tag.TypeName, tag.MemberName); } if (p.CompilationUnit == null || p.FileName == null || p.Line <= 0) { continue; } IViewContent viewContent = FileService.OpenFile(p.FileName); ITextEditorControlProvider provider = viewContent as ITextEditorControlProvider; if (provider == null) { continue; } IDocument document = new TextEditorDocument(provider.TextEditorControl.Document); if (p.Line >= document.TotalNumberOfLines) { continue; } IDocumentLine line = document.GetLine(p.Line); StringBuilder indentation = new StringBuilder(); for (int i = line.Offset; i < document.TextLength; i++) { char c = document.GetCharAt(i); if (c == ' ' || c == '\t') { indentation.Append(c); } else { break; } } string code = codegen.GenerateCode(CreateSuppressAttribute(p.CompilationUnit, tag), indentation.ToString()); if (!code.EndsWith("\n")) { code += Environment.NewLine; } document.Insert(line.Offset, code); provider.TextEditorControl.ActiveTextAreaControl.Caret.Line = p.Line - 1; provider.TextEditorControl.ActiveTextAreaControl.ScrollToCaret(); document.UpdateView(); TaskService.Remove(t); ParserService.ParseViewContent(viewContent); } }
static Ast.AbstractNode CreateSuppressAttribute(ICompilationUnit cu, FxCopTaskTag tag) { //System.Diagnostics.CodeAnalysis.SuppressMessageAttribute bool importedCodeAnalysis = CheckImports(cu); // [SuppressMessage("Microsoft.Performance", "CA1801:ReviewUnusedParameters", MessageId:="fileIdentifier"] Ast.Attribute a = new Ast.Attribute { Name = importedCodeAnalysis ? AttributeName : NamespaceName + "." + AttributeName, PositionalArguments = { new Ast.PrimitiveExpression(tag.Category, tag.Category), new Ast.PrimitiveExpression(tag.CheckID, tag.CheckID) } }; if (tag.MessageID != null) { a.NamedArguments.Add(new Ast.NamedArgumentExpression("MessageId", new Ast.PrimitiveExpression(tag.MessageID, tag.MessageID))); } return new Ast.AttributeSection { AttributeTarget = tag.MemberName == null ? "assembly" : null, Attributes = { a } }; }