/// <summary> /// Adds a list of violations to the task provider. /// </summary> /// <param name="violations">The list of violations to add.</param> public void AddResults(List <ViolationInfo> violations) { Param.AssertNotNull(violations, "violations"); StyleCopTrace.In(violations); this.SuspendRefresh(); var hierarchyItems = new Dictionary <string, IVsHierarchy>(); for (int index = 0; index < violations.Count; index++) { ViolationInfo violation = violations[index]; IVsHierarchy hierarchyItem = hierarchyItems.ContainsKey(violation.File) ? hierarchyItems[violation.File] : null; var task = new ViolationTask(this.serviceProvider, violation, hierarchyItem); hierarchyItem = task.HierarchyItem; if (!hierarchyItems.ContainsKey(violation.File)) { hierarchyItems.Add(violation.File, hierarchyItem); } this.Tasks.Add(task); } this.ResumeRefresh(); StyleCopTrace.Out(); }
/// <summary> /// Adds a single violation to the task provider. /// </summary> /// <param name="violation">The violation to add.</param> private void AddResult(ViolationInfo violation) { Param.AssertNotNull(violation, "violation"); Task task = new ViolationTask(this.serviceProvider, violation); this.Tasks.Add(task); }
/// <summary> /// Called when a violation is found. /// </summary> /// <param name="sender">The event sender.</param> /// <param name="e">The event arguments.</param> private void CoreViolationEncountered(object sender, ViolationEventArgs e) { Param.AssertNotNull(e, "e"); Param.Ignore(sender); // Make sure this is running on the main thread. if (InvisibleForm.Instance.InvokeRequired) { EventHandler <ViolationEventArgs> violationDelegate = this.CoreViolationEncountered; InvisibleForm.Instance.Invoke(violationDelegate, sender, e); } else { if (!e.Warning) { ++this.violationCount; } // Check the count. At some point we don't allow any more violations // so we cancel the analyze run. if (this.maxViolationCount > 0 && this.violationCount == this.maxViolationCount) { this.Cancel(); } ICodeElement element = e.Element; ViolationInfo violation = new ViolationInfo(); violation.Description = string.Concat(e.Violation.Rule.CheckId, ": ", e.Message); violation.LineNumber = e.LineNumber; violation.Rule = e.Violation.Rule; if (element != null) { violation.File = element.Document.SourceCode.Path; } else { string file = string.Empty; if (e.SourceCode != null) { file = e.SourceCode.Path; } violation.File = file; } this.violations.Add(violation); } }
/// <summary> /// Initializes a new instance of the ViolationTask class. /// </summary> /// <param name="serviceProvider">System service provider.</param> /// <param name="violation">The StyleCop violation that this task represents.</param> /// <param name="hierarchyItem">The HierarchyItem for the violations File or null if we don't know it yet.</param> internal ViolationTask(IServiceProvider serviceProvider, ViolationInfo violation, IVsHierarchy hierarchyItem) { Param.AssertNotNull(serviceProvider, "serviceProvider"); Param.Ignore(violation); Param.Ignore(hierarchyItem); this.violation = violation; this.Column = violation.ColumnNumber - 1; this.Document = violation.File; this.Line = violation.LineNumber - 1; this.Text = violation.Description; this.ErrorCategory = violation.Severity; this.serviceProvider = serviceProvider; this.HierarchyItem = hierarchyItem ?? this.GetHierarchyItem(); }
/// <summary> /// Initializes a new instance of the ViolationTask class. /// </summary> /// <param name="serviceProvider">System service provider.</param> /// <param name="violation">The StyleCop violation that this task represents.</param> internal ViolationTask(IServiceProvider serviceProvider, ViolationInfo violation) { Param.AssertNotNull(serviceProvider, "serviceProvider"); Param.Ignore(violation); this.violation = violation; this.Column = 0; this.Document = violation.File; this.Line = violation.LineNumber - 1; this.Text = violation.Description; this.ErrorCategory = TaskErrorCategory.Warning; StyleCopVSPackage package = serviceProvider.GetService(typeof(StyleCopVSPackage)) as StyleCopVSPackage; Debug.Assert(package != null, "Unable to locate the package"); this.core = package.Core; }
/// <summary> /// Called when a violation is found. /// </summary> /// <param name="sender">The event sender.</param> /// <param name="e">The event arguments.</param> private void CoreViolationEncountered(object sender, ViolationEventArgs e) { Param.AssertNotNull(e, "e"); Param.Ignore(sender); // Make sure this is running on the main thread. if (InvisibleForm.Instance.InvokeRequired) { EventHandler<ViolationEventArgs> violationDelegate = this.CoreViolationEncountered; InvisibleForm.Instance.Invoke(violationDelegate, sender, e); } else { // Check the violation only occured in the file we were analysing (or we were analysing a solution/project/folder) var sourceCodePath = e.SourceCode.Path; var documentFullName = this.analysisFilePath; if ((this.analysisType == AnalysisType.File && sourceCodePath.Equals(documentFullName, StringComparison.OrdinalIgnoreCase)) || this.analysisType == AnalysisType.Folder || this.analysisType == AnalysisType.Project || this.analysisType == AnalysisType.Solution || this.analysisType == AnalysisType.Item) { if (!e.Warning) { ++this.violationCount; } // Check the count. At some point we don't allow any more violations so we cancel the analyze run. if (e.SourceCode.Project.MaxViolationCount > 0 && this.violationCount == e.SourceCode.Project.MaxViolationCount) { this.Cancel(); } var element = e.Element; var violationInfo = new ViolationInfo(); violationInfo.Severity = e.SourceCode.Project.ViolationsAsErrors ? TaskErrorCategory.Error : TaskErrorCategory.Warning; var trimmedNamespace = e.Violation.Rule.Namespace.SubstringAfter(StyleCop.Constants.ProductName + ".", StringComparison.Ordinal); trimmedNamespace = trimmedNamespace.SubstringBeforeLast("Rules", StringComparison.Ordinal); violationInfo.Description = string.Concat(e.Violation.Rule.CheckId, " : ", trimmedNamespace, " : ", e.Message); violationInfo.LineNumber = e.LineNumber; violationInfo.ColumnNumber = e.Location != null ? e.Location.Value.StartPoint.IndexOnLine : 1; violationInfo.Rule = e.Violation.Rule; if (element != null) { violationInfo.File = element.Document.SourceCode.Path; } else { string file = string.Empty; if (e.SourceCode != null) { file = e.SourceCode.Path; } violationInfo.File = file; } this.violations.Add(violationInfo); } } }
public void MyTestInitialize() { // Creating a package will set the factory service provider. this.package = new StyleCopVSPackage(); this.mockServiceProvider = new Mock<IServiceProvider>(); this.violation = CreateDummyViolationInfo(); this.package.Core.DisplayUI = false; this.taskUnderTest = new ViolationTask(this.package, this.violation, null); this.taskUnderTestShell = taskUnderTest; }
private static ViolationInfo CreateDummyViolationInfo() { ViolationInfo violation = new ViolationInfo() { File = @"c:\MyFile.cs", LineNumber = 666, Description = "My Description" }; return violation; }
/// <summary> /// Called when a violation is found. /// </summary> /// <param name="sender">The event sender.</param> /// <param name="e">The event arguments.</param> private void CoreViolationEncountered(object sender, ViolationEventArgs e) { Param.AssertNotNull(e, "e"); Param.Ignore(sender); // Make sure this is running on the main thread. if (InvisibleForm.Instance.InvokeRequired) { EventHandler <ViolationEventArgs> violationDelegate = this.CoreViolationEncountered; InvisibleForm.Instance.Invoke(violationDelegate, sender, e); } else { // Check the violation only occured in the file we were analysing (or we were analysing a solution/project/folder) var sourceCodePath = e.SourceCode.Path; var documentFullName = this.analysisFilePath; if ((this.analysisType == AnalysisType.File && sourceCodePath.Equals(documentFullName, StringComparison.OrdinalIgnoreCase)) || this.analysisType == AnalysisType.Folder || this.analysisType == AnalysisType.Project || this.analysisType == AnalysisType.Solution || this.analysisType == AnalysisType.Item) { if (!e.Warning) { ++this.violationCount; } // Check the count. At some point we don't allow any more violations so we cancel the analyze run. if (e.SourceCode.Project.MaxViolationCount > 0 && this.violationCount == e.SourceCode.Project.MaxViolationCount) { this.Cancel(); } var element = e.Element; var violationInfo = new ViolationInfo(); violationInfo.Severity = e.SourceCode.Project.ViolationsAsErrors ? TaskErrorCategory.Error : TaskErrorCategory.Warning; var trimmedNamespace = e.Violation.Rule.Namespace.SubstringAfter(StyleCop.Constants.ProductName + ".", StringComparison.Ordinal); trimmedNamespace = trimmedNamespace.SubstringBeforeLast("Rules", StringComparison.Ordinal); violationInfo.Description = string.Concat(e.Violation.Rule.CheckId, " : ", trimmedNamespace, " : ", e.Message); violationInfo.LineNumber = e.LineNumber; violationInfo.ColumnNumber = e.Location != null ? e.Location.Value.StartPoint.IndexOnLine : 1; violationInfo.Rule = e.Violation.Rule; if (element != null) { violationInfo.File = element.Document.SourceCode.Path; } else { string file = string.Empty; if (e.SourceCode != null) { file = e.SourceCode.Path; } violationInfo.File = file; } this.violations.Add(violationInfo); } } }
public void MyTestInitialize() { // Creating a package will set the factory service provider. this.package = new StyleCopVSPackage(); this.mockServiceProvider = new Mock<IServiceProvider>(); // Creates a dummy violation (In visual studio the violation is displayed in Error List panel) this.violation = CreateDummyViolationInfo(); Assert.IsNotNull(this.package, "this.package is null"); PrivateObject actual = new PrivateObject(this.package, new PrivateType(typeof(StyleCopVSPackage))); StyleCopCore core = (StyleCopCore)actual.GetFieldOrProperty("Core"); Assert.IsNotNull(core, "core is null"); core.DisplayUI = false; this.taskUnderTest = new ViolationTask(this.package, this.violation, null); this.taskUnderTestShell = this.taskUnderTest; }
/// <summary> /// Called when a violation is found. /// </summary> /// <param name="sender">The event sender.</param> /// <param name="e">The event arguments.</param> private void CoreViolationEncountered(object sender, ViolationEventArgs e) { Param.AssertNotNull(e, "e"); Param.Ignore(sender); // Make sure this is running on the main thread. if (InvisibleForm.Instance.InvokeRequired) { EventHandler<ViolationEventArgs> violationDelegate = this.CoreViolationEncountered; InvisibleForm.Instance.Invoke(violationDelegate, sender, e); } else { if (!e.Warning) { ++this.violationCount; } // Check the count. At some point we don't allow any more violations // so we cancel the analyze run. if (this.maxViolationCount > 0 && this.violationCount == this.maxViolationCount) { this.Cancel(); } ICodeElement element = e.Element; ViolationInfo violation = new ViolationInfo(); violation.Description = string.Concat(e.Violation.Rule.CheckId, ": ", e.Message); violation.LineNumber = e.LineNumber; violation.Rule = e.Violation.Rule; if (element != null) { violation.File = element.Document.SourceCode.Path; } else { string file = string.Empty; if (e.SourceCode != null) { file = e.SourceCode.Path; } violation.File = file; } this.violations.Add(violation); } }