Browse Source

Analysis/CodeAnalysis/Src/AnalysisProjectOptionsPanelXaml.xaml - Read FxCop-Rules List

pull/30/head
PeterForstmeier 14 years ago
parent
commit
d87460b415
  1. 4
      src/AddIns/Analysis/CodeAnalysis/CodeAnalysis.csproj
  2. 2
      src/AddIns/Analysis/CodeAnalysis/Src/AnalysisProjectOptions.cs
  3. 63
      src/AddIns/Analysis/CodeAnalysis/Src/AnalysisProjectOptionsPanelXaml.xaml
  4. 299
      src/AddIns/Analysis/CodeAnalysis/Src/AnalysisProjectOptionsPanelXaml.xaml.cs

4
src/AddIns/Analysis/CodeAnalysis/CodeAnalysis.csproj

@ -81,6 +81,10 @@
<EmbeddedResource Include="Resources\AnalysisIdeOptionPanel.xfrm" /> <EmbeddedResource Include="Resources\AnalysisIdeOptionPanel.xfrm" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\..\..\Libraries\SharpTreeView\ICSharpCode.TreeView\ICSharpCode.TreeView.csproj">
<Project>{DDE2A481-8271-4EAC-A330-8FA6A38D13D1}</Project>
<Name>ICSharpCode.TreeView</Name>
</ProjectReference>
<ProjectReference Include="..\..\..\Main\Base\Project\ICSharpCode.SharpDevelop.csproj"> <ProjectReference Include="..\..\..\Main\Base\Project\ICSharpCode.SharpDevelop.csproj">
<Project>{2748AD25-9C63-4E12-877B-4DCE96FBED54}</Project> <Project>{2748AD25-9C63-4E12-877B-4DCE96FBED54}</Project>
<Name>ICSharpCode.SharpDevelop</Name> <Name>ICSharpCode.SharpDevelop</Name>

2
src/AddIns/Analysis/CodeAnalysis/Src/AnalysisProjectOptions.cs

@ -306,7 +306,7 @@ namespace ICSharpCode.CodeAnalysis
public override bool Save() public override bool Save()
{ {
Set(po.RuleString); Set(po.RuleString);
Helper.SetProperty("CodeAnalysisRuleAssemblies", Helper.SetProperty("CodeAnalysisRuleAssemblies",
(po.RuleAssemblies == DefaultRuleAssemblies) ? "" : po.RuleAssemblies, (po.RuleAssemblies == DefaultRuleAssemblies) ? "" : po.RuleAssemblies,
false, false,
Location); Location);

63
src/AddIns/Analysis/CodeAnalysis/Src/AnalysisProjectOptionsPanelXaml.xaml

@ -0,0 +1,63 @@
<optionpanels:ProjectOptionPanel x:Class="ICSharpCode.CodeAnalysis.AnalysisProjectOptionsPanelXaml"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:gui="clr-namespace:ICSharpCode.SharpDevelop.Gui;assembly=ICSharpCode.SharpDevelop"
xmlns:optionpanels="clr-namespace:ICSharpCode.SharpDevelop.Gui.OptionPanels;assembly=ICSharpCode.SharpDevelop"
xmlns:project="clr-namespace:ICSharpCode.SharpDevelop.Project;assembly=ICSharpCode.SharpDevelop"
xmlns:widgets="http://icsharpcode.net/sharpdevelop/widgets"
xmlns:tv="http://icsharpcode.net/sharpdevelop/treeview"
xmlns:core="http://icsharpcode.net/sharpdevelop/core">
<ScrollViewer VerticalScrollBarVisibility="Auto">
<Grid >
<Grid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"></ColumnDefinition>
<ColumnDefinition Width="30"></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<optionpanels:StorageLocationPicker Location="{Binding RunCodeAnalysis.Location}"></optionpanels:StorageLocationPicker>
<CheckBox Grid.Column="1" VerticalAlignment="Center" Grid.ColumnSpan="2"
IsChecked="{Binding RunCodeAnalysis.Value}"
Content="{core:Localize ICSharpCode.CodeAnalysis.ProjectOptions.RunOnBuild}"></CheckBox>
<Button Grid.Row="1" VerticalAlignment="Center" Grid.ColumnSpan="2" Margin="0,3,0,3"
Style="{x:Static core:GlobalStyles.ButtonStyle}"
Content="{core:Localize ICSharpCode.CodeAnalysis.ProjectOptions.AddRemoveRuleAssembly}"></Button>
<tv:SharpTreeView x:Name="ruleTreeView" ShowRoot="False" ShowLines="False"
Grid.Row="2" Grid.ColumnSpan="3" >
<ListView.View>
<tv:SharpGridView>
<tv:SharpGridView.Columns>
<GridViewColumn Width="500" Header="ID">
<GridViewColumn.CellTemplate>
<DataTemplate>
<tv:SharpTreeNodeView />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Action" Width="100">
<GridViewColumn.CellTemplate>
<DataTemplate>
<ComboBox Width="70" Background="Transparent"
ItemsSource="{Binding Path=BLA}"
DisplayMemberPath="DisplayValue"
></ComboBox>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</tv:SharpGridView.Columns>
</tv:SharpGridView>
</ListView.View>
</tv:SharpTreeView>
</Grid>
</ScrollViewer>
</optionpanels:ProjectOptionPanel>

299
src/AddIns/Analysis/CodeAnalysis/Src/AnalysisProjectOptionsPanelXaml.xaml.cs

@ -7,19 +7,24 @@
*/ */
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text; using System.Text;
using System.Text.RegularExpressions;
using System.Windows; using System.Windows;
using System.Windows.Controls; using System.Windows.Controls;
using System.Windows.Data; using System.Windows.Data;
using System.Windows.Documents; using System.Windows.Documents;
using System.Windows.Input; using System.Windows.Input;
using System.Windows.Media; using System.Windows.Media;
using Gui.Dialogs.OptionPanels.ProjectOptions;
using ICSharpCode.Core; using ICSharpCode.Core;
using ICSharpCode.SharpDevelop; using ICSharpCode.SharpDevelop;
using ICSharpCode.SharpDevelop.Dom; using ICSharpCode.SharpDevelop.Dom;
using ICSharpCode.SharpDevelop.Editor; using ICSharpCode.SharpDevelop.Editor;
using ICSharpCode.SharpDevelop.Gui;
using ICSharpCode.SharpDevelop.Gui.OptionPanels; using ICSharpCode.SharpDevelop.Gui.OptionPanels;
using ICSharpCode.SharpDevelop.Project; using ICSharpCode.SharpDevelop.Project;
using ICSharpCode.TreeView;
namespace ICSharpCode.CodeAnalysis namespace ICSharpCode.CodeAnalysis
{ {
@ -28,11 +33,21 @@ namespace ICSharpCode.CodeAnalysis
/// </summary> /// </summary>
public partial class AnalysisProjectOptionsPanelXaml : ProjectOptionPanel public partial class AnalysisProjectOptionsPanelXaml : ProjectOptionPanel
{ {
private bool initSuccess;
private bool userCheck;
private Dictionary<string, RuleTreeNode> rules = new Dictionary<string, RuleTreeNode>();
private List<KeyItemPair> bla = new List<KeyItemPair>();
public AnalysisProjectOptionsPanelXaml() public AnalysisProjectOptionsPanelXaml()
{ {
InitializeComponent(); InitializeComponent();
DataContext = this;
bla.Add(new KeyItemPair("Warning","Warning"));
bla.Add(new KeyItemPair("Error","Error"));
bla.Add(new KeyItemPair("None","None"));
} }
public ProjectProperty<bool> RunCodeAnalysis { public ProjectProperty<bool> RunCodeAnalysis {
get { return GetProperty("RunCodeAnalysis", false); } get { return GetProperty("RunCodeAnalysis", false); }
} }
@ -41,5 +56,289 @@ namespace ICSharpCode.CodeAnalysis
get { return GetProperty("CodeAnalysisRuleAssemblies","",TextBoxEditMode.EditEvaluatedProperty); } get { return GetProperty("CodeAnalysisRuleAssemblies","",TextBoxEditMode.EditEvaluatedProperty); }
} }
public ProjectProperty<string> CodeAnalysisRules {
get { return GetProperty("CodeAnalysisRules","",TextBoxEditMode.EditEvaluatedProperty); }
}
#region Rule Assemblies Property
string ruleAssemblies;
const string DefaultRuleAssemblies = @"$(FxCopDir)\rules";
public string RuleAssemblies {
get {
return ruleAssemblies;
}
set {
if (string.IsNullOrEmpty(value)) {
value = DefaultRuleAssemblies;
}
if (ruleAssemblies != value) {
ruleAssemblies = value;
if (initSuccess) {
//// OnOptionChanged(EventArgs.Empty);
ReloadRuleList();
}
}
}
}
#endregion
#region Rule string Property
string CreateRuleString()
{
StringBuilder b = new StringBuilder();
foreach (SharpTreeNode category in ruleTreeView.Items) {
foreach (RuleTreeNode rule in category.Children) {
if (!(bool)rule.IsChecked || rule.isError) {
if (b.Length > 0)
b.Append(';');
if ((bool)rule.IsChecked)
b.Append('+');
else
b.Append('-');
if (rule.isError)
b.Append('!');
b.Append(rule.Identifier);
}
}
}
return b.ToString();
}
void ReadRuleString()
{
userCheck = false;
foreach (SharpTreeNode cat in ruleTreeView.Root.Children) {
foreach (RuleTreeNode rtn in cat.Children) {
rtn.IsChecked = true;
rtn.isError = false;
}
}
foreach (string rule2 in ruleString.Split(';')) {
string rule = rule2;
if (rule.Length == 0) continue;
bool active = true;
bool error = false;
if (rule.StartsWith("-")) {
active = false;
rule = rule.Substring(1);
} else if (rule.StartsWith("+")) {
rule = rule.Substring(1);
}
if (rule.StartsWith("!")) {
error = true;
rule = rule.Substring(1);
}
RuleTreeNode ruleNode;
if (rules.TryGetValue(rule, out ruleNode)) {
ruleNode.IsChecked = active;
ruleNode.isError = error;
}
}
foreach (SharpTreeNode cat in ruleTreeView.Root.Children) {
bool noneChecked = true;
foreach (RuleTreeNode rtn in cat.Children) {
if ((bool)rtn.IsChecked) {
noneChecked = false;
break;
}
}
cat.IsChecked = !noneChecked;
}
userCheck = true;
}
string ruleString = "";
public string RuleString {
get {
if (initSuccess)
return CreateRuleString();
else
return ruleString;
}
set {
ruleString = value;
if (initSuccess) {
ReadRuleString();
}
}
}
#endregion
#region overrides
protected override void Load(MSBuildBasedProject project, string configuration, string platform)
{
base.Load(project, configuration, platform);
RuleString = this.CodeAnalysisRules.Value;
RuleAssemblies = CodeAnalysisRuleAssemblies.Value;
ReloadRuleList();
}
#endregion
#region RuleList
void ReloadRuleList()
{
ruleTreeView.Root = new SharpTreeNode();
FxCopWrapper.GetRuleList(GetRuleAssemblyList(true), Callback);
if (ruleTreeView.Root.Children.Count == 0) {
ruleTreeView.Root.Children.Add(new MessageNode(StringParser.Parse("${res:ICSharpCode.CodeAnalysis.ProjectOptions.LoadingRules}")));
}
}
void Callback(List<FxCopCategory> ruleList)
{
if (WorkbenchSingleton.InvokeRequired) {
WorkbenchSingleton.SafeThreadAsyncCall((Action<List<FxCopCategory>>)Callback, ruleList);
} else {
ruleTreeView.Root = new SharpTreeNode();
rules.Clear();
if (ruleList == null || ruleList.Count == 0) {
ruleTreeView.Root.Children.Add(new MessageNode(StringParser.Parse("${res:ICSharpCode.CodeAnalysis.ProjectOptions.CannotFindFxCop}")));
ruleTreeView.Root.Children.Add(new MessageNode(StringParser.Parse("${res:ICSharpCode.CodeAnalysis.ProjectOptions.SpecifyFxCopPath}")));
} else {
foreach (FxCopCategory cat in ruleList) {
CategoryTreeNode catNode = new CategoryTreeNode(cat,bla);
ruleTreeView.Root.Children.Add(catNode);
foreach (RuleTreeNode ruleNode in catNode.Children) {
rules[ruleNode.Identifier] = ruleNode;
}
}
initSuccess = true;
ReadRuleString();
}
}
}
string[] GetRuleAssemblyList(bool replacePath)
{
List<string> list = new List<string>();
string fxCopPath = FxCopWrapper.FindFxCopPath();
foreach (string dir in ruleAssemblies.Split(';')) {
if (string.Equals(dir, "$(FxCopDir)\\rules", StringComparison.OrdinalIgnoreCase))
continue;
if (string.Equals(dir, "$(FxCopDir)/rules", StringComparison.OrdinalIgnoreCase))
continue;
if (replacePath && !string.IsNullOrEmpty(fxCopPath)) {
list.Add(Regex.Replace(dir, @"\$\(FxCopDir\)", fxCopPath, RegexOptions.CultureInvariant | RegexOptions.IgnoreCase));
} else {
list.Add(dir);
}
}
return list.ToArray();
}
#endregion
#region TreeView
class BaseTree:SharpTreeNode
{
public BaseTree(IEnumerable<KeyItemPair> bla)
{
this.BLA = bla;
}
public IEnumerable<KeyItemPair> BLA {get;set;}
}
class CategoryTreeNode : BaseTree
{
internal FxCopCategory category;
public CategoryTreeNode(FxCopCategory category,IEnumerable<KeyItemPair> bla):base(bla)
{
this.category = category;
foreach (FxCopRule rule in category.Rules) {
this.Children.Add(new RuleTreeNode(rule,bla));
}
}
public override bool IsCheckable {
get { return true; }
}
public override object Text {
get { return category.DisplayName; }
}
internal int ErrorState {
get {
bool allWarn = true;
bool allErr = true;
foreach (RuleTreeNode tn in Children) {
if (tn.isError)
allWarn = false;
else
allErr = false;
}
if (allErr)
return 1;
else if (allWarn)
return 0;
else
return -1;
}
}
}
class RuleTreeNode :BaseTree
{
internal FxCopRule rule;
internal bool isError;
public RuleTreeNode(FxCopRule rule,IEnumerable<KeyItemPair> bla):base(bla)
{
this.rule = rule;
}
public override bool IsCheckable {
get { return true; }
}
public override object Text {
get { return rule.CheckId + " : " + rule.DisplayName; }
}
public string Identifier {
get {
return rule.CategoryName + "#" + rule.CheckId;
}
}
}
class MessageNode : SharpTreeNode
{
private string message;
public MessageNode (string message)
{
this.message = message;
}
public override object Text {
get { return message; }
}
}
#endregion
} }
} }
Loading…
Cancel
Save