diff options
Diffstat (limited to 'main/src/addins/CSharpBinding/MonoDevelop.CSharp.Diagnostics')
15 files changed, 1592 insertions, 0 deletions
diff --git a/main/src/addins/CSharpBinding/MonoDevelop.CSharp.Diagnostics/DiagnosticCustomTags.cs b/main/src/addins/CSharpBinding/MonoDevelop.CSharp.Diagnostics/DiagnosticCustomTags.cs new file mode 100644 index 0000000000..d05d91c55f --- /dev/null +++ b/main/src/addins/CSharpBinding/MonoDevelop.CSharp.Diagnostics/DiagnosticCustomTags.cs @@ -0,0 +1,35 @@ +// +// DiagnosticCustomTags.cs +// +// Author: +// Mike Krüger <mkrueger@xamarin.com> +// +// Copyright (c) 2015 Xamarin Inc. (http://xamarin.com) +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. +using System; +using Microsoft.CodeAnalysis; + +namespace MonoDevelop.CSharp.Diagnostics +{ + static class DiagnosticCustomTags + { + public static readonly string[] Unnecessary = { WellKnownDiagnosticTags.Unnecessary, WellKnownDiagnosticTags.Telemetry }; + } +}
\ No newline at end of file diff --git a/main/src/addins/CSharpBinding/MonoDevelop.CSharp.Diagnostics/IDEDiagnosticIds.cs b/main/src/addins/CSharpBinding/MonoDevelop.CSharp.Diagnostics/IDEDiagnosticIds.cs new file mode 100644 index 0000000000..7d067e38d2 --- /dev/null +++ b/main/src/addins/CSharpBinding/MonoDevelop.CSharp.Diagnostics/IDEDiagnosticIds.cs @@ -0,0 +1,43 @@ +// +// IDEDiagnosticIds.cs +// +// Author: +// Mike Krüger <mkrueger@xamarin.com> +// +// Copyright (c) 2015 Xamarin Inc. (http://xamarin.com) +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. +using System; + +namespace MonoDevelop.CSharp.Diagnostics +{ + static class IDEDiagnosticIds + { + public const string SimplifyNamesDiagnosticId = "IDE0001"; + public const string SimplifyMemberAccessDiagnosticId = "IDE0002"; + public const string SimplifyThisOrMeDiagnosticId = "IDE0003"; + public const string RemoveUnnecessaryCastDiagnosticId = "IDE0004"; + public const string RemoveUnnecessaryImportsDiagnosticId = "IDE0005"; + + public const string MonoTODODiagnosticDiagnosticId = "XS0001"; + + + } +} + diff --git a/main/src/addins/CSharpBinding/MonoDevelop.CSharp.Diagnostics/InconsistentNaming/MonoNameConventionPolicy.xml b/main/src/addins/CSharpBinding/MonoDevelop.CSharp.Diagnostics/InconsistentNaming/MonoNameConventionPolicy.xml new file mode 100644 index 0000000000..4643473284 --- /dev/null +++ b/main/src/addins/CSharpBinding/MonoDevelop.CSharp.Diagnostics/InconsistentNaming/MonoNameConventionPolicy.xml @@ -0,0 +1,179 @@ +<NameConventionPolicy>
+ <Rules>
+ <NamingRule>
+ <Name>Namespaces</Name>
+ <AffectedEntity>Namespace</AffectedEntity>
+ <VisibilityMask>VisibilityMask</VisibilityMask>
+ <NamingStyle>PascalCase</NamingStyle>
+ <IncludeInstanceMembers>True</IncludeInstanceMembers>
+ <IncludeStaticEntities>True</IncludeStaticEntities>
+ </NamingRule>
+ <NamingRule>
+ <Name>Types</Name>
+ <AffectedEntity>Class, Struct, Enum, Delegate</AffectedEntity>
+ <VisibilityMask>VisibilityMask</VisibilityMask>
+ <NamingStyle>PascalCase</NamingStyle>
+ <IncludeInstanceMembers>True</IncludeInstanceMembers>
+ <IncludeStaticEntities>True</IncludeStaticEntities>
+ </NamingRule>
+ <NamingRule>
+ <Name>Interfaces</Name>
+ <RequiredPrefixes>
+ <String>I</String>
+ </RequiredPrefixes>
+ <AffectedEntity>Interface</AffectedEntity>
+ <VisibilityMask>VisibilityMask</VisibilityMask>
+ <NamingStyle>PascalCase</NamingStyle>
+ <IncludeInstanceMembers>True</IncludeInstanceMembers>
+ <IncludeStaticEntities>True</IncludeStaticEntities>
+ </NamingRule>
+ <NamingRule>
+ <Name>Attributes</Name>
+ <RequiredSuffixes>
+ <String>Attribute</String>
+ </RequiredSuffixes>
+ <AffectedEntity>CustomAttributes</AffectedEntity>
+ <VisibilityMask>VisibilityMask</VisibilityMask>
+ <NamingStyle>PascalCase</NamingStyle>
+ <IncludeInstanceMembers>True</IncludeInstanceMembers>
+ <IncludeStaticEntities>True</IncludeStaticEntities>
+ </NamingRule>
+ <NamingRule>
+ <Name>Event Arguments</Name>
+ <RequiredSuffixes>
+ <String>EventArgs</String>
+ </RequiredSuffixes>
+ <AffectedEntity>CustomEventArgs</AffectedEntity>
+ <VisibilityMask>VisibilityMask</VisibilityMask>
+ <NamingStyle>PascalCase</NamingStyle>
+ <IncludeInstanceMembers>True</IncludeInstanceMembers>
+ <IncludeStaticEntities>True</IncludeStaticEntities>
+ </NamingRule>
+ <NamingRule>
+ <Name>Exceptions</Name>
+ <RequiredSuffixes>
+ <String>Exception</String>
+ </RequiredSuffixes>
+ <AffectedEntity>CustomExceptions</AffectedEntity>
+ <VisibilityMask>VisibilityMask</VisibilityMask>
+ <NamingStyle>PascalCase</NamingStyle>
+ <IncludeInstanceMembers>True</IncludeInstanceMembers>
+ <IncludeStaticEntities>True</IncludeStaticEntities>
+ </NamingRule>
+ <NamingRule>
+ <Name>Methods</Name>
+ <AffectedEntity>Methods</AffectedEntity>
+ <VisibilityMask>VisibilityMask</VisibilityMask>
+ <NamingStyle>PascalCase</NamingStyle>
+ <IncludeInstanceMembers>True</IncludeInstanceMembers>
+ <IncludeStaticEntities>True</IncludeStaticEntities>
+ </NamingRule>
+ <NamingRule>
+ <Name>Static Readonly Fields</Name>
+ <AffectedEntity>ReadonlyField</AffectedEntity>
+ <VisibilityMask>Internal, Protected, Public</VisibilityMask>
+ <NamingStyle>PascalCase</NamingStyle>
+ <IncludeInstanceMembers>False</IncludeInstanceMembers>
+ <IncludeStaticEntities>True</IncludeStaticEntities>
+ </NamingRule>
+ <NamingRule>
+ <Name>Fields (Non Private)</Name>
+ <AffectedEntity>Field</AffectedEntity>
+ <VisibilityMask>Internal, Protected, Public</VisibilityMask>
+ <NamingStyle>PascalCase</NamingStyle>
+ <IncludeInstanceMembers>True</IncludeInstanceMembers>
+ <IncludeStaticEntities>True</IncludeStaticEntities>
+ </NamingRule>
+ <NamingRule>
+ <Name>ReadOnly Fields (Non Private)</Name>
+ <AffectedEntity>ReadonlyField</AffectedEntity>
+ <VisibilityMask>Internal, Protected, Public</VisibilityMask>
+ <NamingStyle>PascalCase</NamingStyle>
+ <IncludeInstanceMembers>True</IncludeInstanceMembers>
+ <IncludeStaticEntities>False</IncludeStaticEntities>
+ </NamingRule>
+ <NamingRule>
+ <Name>Fields (Private)</Name>
+ <AllowedPrefixes>
+ <String>_</String>
+ <String>m_</String>
+ </AllowedPrefixes>
+ <AffectedEntity>Field, ReadonlyField</AffectedEntity>
+ <VisibilityMask>Private</VisibilityMask>
+ <NamingStyle>CamelCase</NamingStyle>
+ <IncludeInstanceMembers>True</IncludeInstanceMembers>
+ <IncludeStaticEntities>False</IncludeStaticEntities>
+ </NamingRule>
+ <NamingRule>
+ <Name>Static Fields (Private)</Name>
+ <AffectedEntity>Field</AffectedEntity>
+ <VisibilityMask>Private</VisibilityMask>
+ <NamingStyle>CamelCase</NamingStyle>
+ <IncludeInstanceMembers>False</IncludeInstanceMembers>
+ <IncludeStaticEntities>True</IncludeStaticEntities>
+ </NamingRule>
+ <NamingRule>
+ <Name>ReadOnly Fields (Private)</Name>
+ <AllowedPrefixes>
+ <String>_</String>
+ <String>m_</String>
+ </AllowedPrefixes>
+ <AffectedEntity>ReadonlyField</AffectedEntity>
+ <VisibilityMask>Private</VisibilityMask>
+ <NamingStyle>CamelCase</NamingStyle>
+ <IncludeInstanceMembers>True</IncludeInstanceMembers>
+ <IncludeStaticEntities>False</IncludeStaticEntities>
+ </NamingRule>
+ <NamingRule>
+ <Name>Constant Fields</Name>
+ <AffectedEntity>ConstantField</AffectedEntity>
+ <VisibilityMask>VisibilityMask</VisibilityMask>
+ <NamingStyle>PascalCase</NamingStyle>
+ <IncludeInstanceMembers>True</IncludeInstanceMembers>
+ <IncludeStaticEntities>True</IncludeStaticEntities>
+ </NamingRule>
+ <NamingRule>
+ <Name>Properties</Name>
+ <AffectedEntity>Property</AffectedEntity>
+ <VisibilityMask>VisibilityMask</VisibilityMask>
+ <NamingStyle>PascalCase</NamingStyle>
+ <IncludeInstanceMembers>True</IncludeInstanceMembers>
+ <IncludeStaticEntities>True</IncludeStaticEntities>
+ </NamingRule>
+ <NamingRule>
+ <Name>Events</Name>
+ <AffectedEntity>Event</AffectedEntity>
+ <VisibilityMask>VisibilityMask</VisibilityMask>
+ <NamingStyle>PascalCase</NamingStyle>
+ <IncludeInstanceMembers>True</IncludeInstanceMembers>
+ <IncludeStaticEntities>True</IncludeStaticEntities>
+ </NamingRule>
+ <NamingRule>
+ <Name>Enum Members</Name>
+ <AffectedEntity>EnumMember</AffectedEntity>
+ <VisibilityMask>VisibilityMask</VisibilityMask>
+ <NamingStyle>PascalCase</NamingStyle>
+ <IncludeInstanceMembers>True</IncludeInstanceMembers>
+ <IncludeStaticEntities>True</IncludeStaticEntities>
+ </NamingRule>
+ <NamingRule>
+ <Name>Parameters</Name>
+ <AffectedEntity>Parameter</AffectedEntity>
+ <VisibilityMask>VisibilityMask</VisibilityMask>
+ <NamingStyle>CamelCase</NamingStyle>
+ <IncludeInstanceMembers>True</IncludeInstanceMembers>
+ <IncludeStaticEntities>True</IncludeStaticEntities>
+ </NamingRule>
+ <NamingRule>
+ <Name>Type Parameters</Name>
+ <RequiredPrefixes>
+ <String>T</String>
+ </RequiredPrefixes>
+ <AffectedEntity>TypeParameter</AffectedEntity>
+ <VisibilityMask>VisibilityMask</VisibilityMask>
+ <NamingStyle>PascalCase</NamingStyle>
+ <IncludeInstanceMembers>True</IncludeInstanceMembers>
+ <IncludeStaticEntities>True</IncludeStaticEntities>
+ </NamingRule>
+ </Rules>
+</NameConventionPolicy>
\ No newline at end of file diff --git a/main/src/addins/CSharpBinding/MonoDevelop.CSharp.Diagnostics/InconsistentNaming/NameConventionEditRuleDialog.cs b/main/src/addins/CSharpBinding/MonoDevelop.CSharp.Diagnostics/InconsistentNaming/NameConventionEditRuleDialog.cs new file mode 100644 index 0000000000..3c64d104fa --- /dev/null +++ b/main/src/addins/CSharpBinding/MonoDevelop.CSharp.Diagnostics/InconsistentNaming/NameConventionEditRuleDialog.cs @@ -0,0 +1,211 @@ +// +// NamingConventionEditRuleDialog.cs +// +// Author: +// Mike Krüger <mkrueger@xamarin.com> +// +// Copyright (c) 2012 Xamarin Inc. (http://xamarin.com) +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. +using System; +using System.Linq; +using Gtk; +using System.Collections.Generic; +using MonoDevelop.Core; +using ICSharpCode.NRefactory6.CSharp.Diagnostics; + +namespace MonoDevelop.CSharp.Diagnostics.InconsistentNaming +{ + partial class NameConventionEditRuleDialog : Gtk.Dialog + { + static readonly Dictionary<AffectedEntity, string> EntityName = new Dictionary<AffectedEntity, string> (); + static readonly Dictionary<Modifiers, string> AccessibilityName = new Dictionary<Modifiers, string> (); + + static NameConventionEditRuleDialog () + { + EntityName [AffectedEntity.Namespace] = GettextCatalog.GetString ("Namespaces"); + + EntityName [AffectedEntity.Class] = GettextCatalog.GetString ("Classes"); + EntityName [AffectedEntity.Struct] = GettextCatalog.GetString ("Structs"); + EntityName [AffectedEntity.Enum] = GettextCatalog.GetString ("Enums"); + EntityName [AffectedEntity.Interface] = GettextCatalog.GetString ("Interfaces"); + EntityName [AffectedEntity.Delegate] = GettextCatalog.GetString ("Delegates"); + + EntityName [AffectedEntity.CustomAttributes] = GettextCatalog.GetString ("Attributes"); + EntityName [AffectedEntity.CustomEventArgs] = GettextCatalog.GetString ("Event Arguments"); + EntityName [AffectedEntity.CustomExceptions] = GettextCatalog.GetString ("Exceptions"); + + EntityName [AffectedEntity.Property] = GettextCatalog.GetString ("Properties"); + EntityName [AffectedEntity.AsyncMethod] = GettextCatalog.GetString ("Async methods"); + EntityName [AffectedEntity.Method] = GettextCatalog.GetString ("Methods"); + EntityName [AffectedEntity.Field] = GettextCatalog.GetString ("Fields"); + EntityName [AffectedEntity.ConstantField] = GettextCatalog.GetString ("Constant fields"); + EntityName [AffectedEntity.ReadonlyField] = GettextCatalog.GetString ("Readonly fields"); + EntityName [AffectedEntity.Event] = GettextCatalog.GetString ("Events"); + EntityName [AffectedEntity.EnumMember] = GettextCatalog.GetString ("Enum Members"); + + EntityName [AffectedEntity.Parameter] = GettextCatalog.GetString ("Parameters"); + EntityName [AffectedEntity.TypeParameter] = GettextCatalog.GetString ("Type Parameters"); + + // Unit test special case + EntityName [AffectedEntity.TestType] = GettextCatalog.GetString ("Test Types"); + EntityName [AffectedEntity.TestMethod] = GettextCatalog.GetString ("Test Methods"); + + // private entities + EntityName [AffectedEntity.LambdaParameter] = GettextCatalog.GetString ("Lambda Parameters"); + EntityName [AffectedEntity.LocalVariable] = GettextCatalog.GetString ("Local Variables"); + EntityName [AffectedEntity.LocalConstant] = GettextCatalog.GetString ("Local Constants"); + EntityName [AffectedEntity.Label] = GettextCatalog.GetString ("Labels"); + + AccessibilityName [Modifiers.Public] = GettextCatalog.GetString ("Public"); + AccessibilityName [Modifiers.Private] = GettextCatalog.GetString ("Private"); + AccessibilityName [Modifiers.Internal] = GettextCatalog.GetString ("Internal"); + AccessibilityName [Modifiers.Protected] = GettextCatalog.GetString ("Protected"); + } + + NameConventionRule rule; + + ListStore entityStore = new ListStore (typeof(string), typeof(AffectedEntity), typeof(bool)); + ListStore accessibiltyStore = new ListStore (typeof(string), typeof(Modifiers), typeof(bool)); + + public NameConventionEditRuleDialog (NameConventionRule rule) + { + if (rule == null) + throw new System.ArgumentNullException ("rule"); + this.rule = rule; + this.Build (); + + var ct1 = new CellRendererToggle (); + ct1.Toggled += delegate(object o, Gtk.ToggledArgs args) { + TreeIter iter; + if (!entityStore.GetIterFromString (out iter, args.Path)) + return; + entityStore.SetValue (iter, 2, !(bool)entityStore.GetValue (iter, 2)); + }; + treeviewEntities.AppendColumn ("IsChecked", ct1, "active", 2); + treeviewEntities.AppendColumn ("Entity", new CellRendererText (), "text", 0); + treeviewEntities.Model = entityStore; + + var ct2 = new CellRendererToggle (); + ct2.Toggled += delegate(object o, Gtk.ToggledArgs args) { + TreeIter iter; + if (!accessibiltyStore.GetIterFromString (out iter, args.Path)) + return; + accessibiltyStore.SetValue (iter, 2, !(bool)accessibiltyStore.GetValue (iter, 2)); + }; + treeviewAccessibility.AppendColumn ("IsChecked", ct2, "active", 2); + treeviewAccessibility.AppendColumn ("Entity", new CellRendererText (), "text", 0); + treeviewAccessibility.Model = accessibiltyStore; + buttonOk.Clicked += (sender, e) => Apply (); + + FillDialog (); + } + + public void FillDialog () + { + entryRuleName.Text = rule.Name ?? ""; + if (rule.RequiredPrefixes != null) + entryPrefix.Text = rule.RequiredPrefixes.FirstOrDefault (); + if (rule.AllowedPrefixes != null) + entryPrefixAllowed.Text = string.Join (", ", rule.AllowedPrefixes); + if (rule.RequiredSuffixes != null) + entrySuffix.Text = rule.RequiredSuffixes.FirstOrDefault (); + styleComboBox.AppendText ("PascalCase"); + styleComboBox.AppendText ("CamelCase"); + styleComboBox.AppendText ("ALLUPPER"); + styleComboBox.AppendText ("alllower"); + styleComboBox.AppendText ("Firstupper"); + styleComboBox.AppendText ("PascalCase_underscoreTolerant"); + styleComboBox.AppendText ("PascalCase_UnderscoreTolerant"); + styleComboBox.AppendText ("CamelCase_underscoreTolerant"); + styleComboBox.AppendText ("CamelCase_UnderscoreTolerant"); + + styleComboBox.Active = (int)rule.NamingStyle - 1; + + foreach (AffectedEntity ae in Enum.GetValues (typeof (AffectedEntity))) { + if (!EntityName.ContainsKey (ae)) + continue; + entityStore.AppendValues (EntityName [ae], ae, rule.AffectedEntity.HasFlag (ae)); + } + + foreach (Modifiers mod in Enum.GetValues (typeof (Modifiers))) { + if (!AccessibilityName.ContainsKey (mod)) + continue; + accessibiltyStore.AppendValues (AccessibilityName [mod], mod, rule.VisibilityMask.HasFlag (mod)); + } + + checkbuttonStatic.Active = rule.IncludeStaticEntities; + checkbuttonInstanceMembers.Active = rule.IncludeInstanceMembers; + + } + + public void Apply () + { + rule.Name = entryRuleName.Text; + rule.NamingStyle = (NamingStyle)(1 + styleComboBox.Active); + + var prefix = entryPrefix.Text.Trim (); + if (string.IsNullOrEmpty (prefix)) { + rule.RequiredPrefixes = null; + } else { + rule.RequiredPrefixes = new [] { prefix }; + } + + var allowedPrefix = entryPrefixAllowed.Text.Trim (); + if (string.IsNullOrEmpty (allowedPrefix)) { + rule.AllowedPrefixes = null; + } else { + rule.AllowedPrefixes = allowedPrefix.Split (',', ';').Select (s => s.Trim ()).ToArray (); + } + + var suffix = entrySuffix.Text.Trim (); + if (string.IsNullOrEmpty (suffix)) { + rule.RequiredSuffixes = null; + } else { + rule.RequiredSuffixes = new [] { suffix }; + } + + var ae = AffectedEntity.None; + TreeIter iter; + if (entityStore.GetIterFirst (out iter)) { + do { + var entity = (AffectedEntity)entityStore.GetValue (iter, 1); + var include = (bool)entityStore.GetValue (iter, 2); + if (include) + ae |= entity; + } while (entityStore.IterNext (ref iter)); + } + rule.AffectedEntity = ae; + + var mod = Modifiers.None; + if (accessibiltyStore.GetIterFirst (out iter)) { + do { + var entity = (Modifiers)accessibiltyStore.GetValue (iter, 1); + var include = (bool)accessibiltyStore.GetValue (iter, 2); + if (include) + mod |= entity; + } while (accessibiltyStore.IterNext (ref iter)); + } + rule.VisibilityMask = mod; + rule.IncludeStaticEntities = checkbuttonStatic.Active; + rule.IncludeInstanceMembers = checkbuttonInstanceMembers.Active; + } + } +} + diff --git a/main/src/addins/CSharpBinding/MonoDevelop.CSharp.Diagnostics/InconsistentNaming/NameConventionPanel.cs b/main/src/addins/CSharpBinding/MonoDevelop.CSharp.Diagnostics/InconsistentNaming/NameConventionPanel.cs new file mode 100644 index 0000000000..e91a6242ba --- /dev/null +++ b/main/src/addins/CSharpBinding/MonoDevelop.CSharp.Diagnostics/InconsistentNaming/NameConventionPanel.cs @@ -0,0 +1,73 @@ +// +// NamingConventionPanel.cs +// +// Author: +// Mike Krüger <mkrueger@xamarin.com> +// +// Copyright (c) 2012 Xamarin <http://xamarin.com> +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. +using System; +using MonoDevelop.Ide.Gui.Dialogs; +using Gtk; +using MonoDevelop.Core; +using MonoDevelop.Ide.Editor; + +namespace MonoDevelop.CSharp.Diagnostics.InconsistentNaming +{ + class NameConventionPanel : PolicyOptionsPanel<NameConventionPolicy> + { + NameConventionPanelWidget panel; + + static NameConventionPanel () + { + // ensure that custom text editor shemes are loaded. + TextEditorDisplayBinding.InitSourceEditor (); + } + + protected override string PolicyTitleWithMnemonic { + get { + return GettextCatalog.GetString ("_Naming Style"); + } + } + + public override Widget CreatePanelWidget () + { + panel = new NameConventionPanelWidget (); + panel.PolicyChanged += delegate { + UpdateSelectedNamedPolicy (); + }; + return panel; + } + + protected override void LoadFrom (NameConventionPolicy policy) + { + panel.Policy = policy.Clone (); + } + + protected override NameConventionPolicy GetPolicy () + { + // return cloned policy + panel.ApplyChanges (); + return panel.Policy; + } + } + +} + diff --git a/main/src/addins/CSharpBinding/MonoDevelop.CSharp.Diagnostics/InconsistentNaming/NameConventionPanelWidget.cs b/main/src/addins/CSharpBinding/MonoDevelop.CSharp.Diagnostics/InconsistentNaming/NameConventionPanelWidget.cs new file mode 100644 index 0000000000..f4da2ff9ba --- /dev/null +++ b/main/src/addins/CSharpBinding/MonoDevelop.CSharp.Diagnostics/InconsistentNaming/NameConventionPanelWidget.cs @@ -0,0 +1,154 @@ +// +// NamingConventionPanelWidget.cs +// +// Author: +// Mike Krüger <mkrueger@novell.com> +// +// Copyright (c) 2011 Novell, Inc (http://www.novell.com) +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. +using System; +using Gtk; +using MonoDevelop.Core; +using MonoDevelop.Ide; +using System.Collections.Generic; + +namespace MonoDevelop.CSharp.Diagnostics.InconsistentNaming +{ + [System.ComponentModel.ToolboxItem(true)] + partial class NameConventionPanelWidget : Gtk.Bin + { + TreeStore treeStore = new TreeStore (typeof(NameConventionRule)); + NameConventionPolicy policy; + + internal NameConventionPolicy Policy { + get { + return policy; + } + set { + policy = value; + FillRules (policy.Rules); + } + } + + public NameConventionPanelWidget () + { + Build (); + Show (); + + var ct1 = new CellRendererText (); + var col1 = treeviewConventions.AppendColumn (GettextCatalog.GetString ("Rule"), ct1); + col1.Expand = true; + col1.SetCellDataFunc (ct1, delegate (TreeViewColumn column, CellRenderer cell, TreeModel model, TreeIter iter) { + var rule = (NameConventionRule)model.GetValue (iter, 0); + ct1.Text = rule.Name; + }); + + + var ct2 = new CellRendererText (); + var col2 = treeviewConventions.AppendColumn (GettextCatalog.GetString ("Example"), ct2); + col2.Expand = true; + col2.SetCellDataFunc (ct2, delegate (TreeViewColumn column, CellRenderer cell, TreeModel model, TreeIter iter) { + var rule = (NameConventionRule)model.GetValue (iter, 0); + ct2.Text = rule.GetPreview (); + }); + + treeviewConventions.Model = treeStore; + treeviewConventions.Selection.Changed += HandleSelectionChanged; + treeviewConventions.RowActivated += (o, args) => EditSelectedEntry (); + buttonEdit.Clicked += (o, s) => EditSelectedEntry (); + buttonRemove.Clicked += (o, s) => RemoveSelectedEntry (); + buttonAdd.Clicked += (o, s) => AddEntry (); + + HandleSelectionChanged (null, null); + } + + void HandleSelectionChanged (object sender, EventArgs e) + { + TreeIter iter; + buttonEdit.Sensitive = treeviewConventions.Selection.GetSelected (out iter); + } + + public void ApplyChanges () + { + var rules = new List<NameConventionRule> (); + TreeIter iter; + if (treeStore.GetIterFirst (out iter)) { + do { + var rule = (NameConventionRule)treeStore.GetValue (iter, 0); + rules.Add (rule); + } while (treeStore.IterNext (ref iter)); + } + policy.Rules = rules.ToArray (); + if (IdeApp.Workbench.ActiveDocument != null) + IdeApp.Workbench.ActiveDocument.UpdateParseDocument (); + } + + void AddEntry () + { + var newRule = new NameConventionRule (); + newRule.Name = "New Rule"; + var diag = new NameConventionEditRuleDialog (newRule); + var result = MessageService.ShowCustomDialog (diag); + if (result == (int)ResponseType.Ok) + treeStore.AppendValues (newRule); + OnPolicyChanged (EventArgs.Empty); + } + + void EditSelectedEntry () + { + TreeIter iter; + if (!treeviewConventions.Selection.GetSelected (out iter)) + return; + var rule = treeStore.GetValue (iter, 0) as NameConventionRule; + var diag = new NameConventionEditRuleDialog (rule); + int result = MessageService.ShowCustomDialog (diag); + treeviewConventions.QueueResize (); + if (result == (int)Gtk.ResponseType.Ok) + OnPolicyChanged (EventArgs.Empty); + } + + void RemoveSelectedEntry () + { + TreeIter iter; + if (!treeviewConventions.Selection.GetSelected (out iter)) + return; + treeStore.Remove (ref iter); + OnPolicyChanged (EventArgs.Empty); + } + + void FillRules (IEnumerable<NameConventionRule> rules) + { + treeStore.Clear (); + foreach (var rule in rules) { + treeStore.AppendValues (rule); + } + } + + protected virtual void OnPolicyChanged (EventArgs e) + { + var handler = PolicyChanged; + if (handler != null) + handler (this, e); + } + + public event EventHandler PolicyChanged; + } +} + diff --git a/main/src/addins/CSharpBinding/MonoDevelop.CSharp.Diagnostics/InconsistentNaming/NameConventionPolicy.cs b/main/src/addins/CSharpBinding/MonoDevelop.CSharp.Diagnostics/InconsistentNaming/NameConventionPolicy.cs new file mode 100644 index 0000000000..c2b8500bd3 --- /dev/null +++ b/main/src/addins/CSharpBinding/MonoDevelop.CSharp.Diagnostics/InconsistentNaming/NameConventionPolicy.cs @@ -0,0 +1,96 @@ +// +// NamingConventions.cs +// +// Author: +// Mike Krüger <mkrueger@novell.com> +// +// Copyright (c) 2011 Novell, Inc (http://www.novell.com) +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. +using System; +using System.Linq; +using System.Collections.Generic; +using MonoDevelop.Projects.Policies; +using MonoDevelop.Core.Serialization; +using ICSharpCode.NRefactory6.CSharp.Diagnostics; + +namespace MonoDevelop.CSharp.Diagnostics.InconsistentNaming +{ + [PolicyType ("Naming Conventions Policy")] + class NameConventionPolicy : IEquatable<NameConventionPolicy> + { + NameConventionRule[] rules = new NameConventionRule[0]; + + [ItemProperty] + public NameConventionRule[] Rules { + get { return rules; } + set { rules = value; } + } + + public NameConventionPolicy Clone () + { + var result = new NameConventionPolicy (); + result.rules = new List<NameConventionRule> (rules.Select (r => r.Clone ())).ToArray (); + return result; + } + + public NameConventionPolicy () + { + rules = new List<NameConventionRule> (DefaultRules.GetFdgRules ().Select (r => new NameConventionRule (r))).ToArray (); + } + + class NamingConventionService : ICSharpCode.NRefactory6.CSharp.Diagnostics.NamingConventionService + { + NameConventionPolicy policy; + NamingRule[] rules = null; + public override IEnumerable<ICSharpCode.NRefactory6.CSharp.Diagnostics.NamingRule> Rules { + get { + if (rules == null) { + this.rules = policy.Rules.Select (r => r.GetNRefactoryRule ()).ToArray (); + } + return rules; + } + } + + public NamingConventionService (NameConventionPolicy policy) + { + this.policy = policy; + } + + } + + public ICSharpCode.NRefactory6.CSharp.Diagnostics.NamingConventionService CreateNRefactoryService () + { + return new NamingConventionService (this); + } + + #region IEquatable implementation + public bool Equals (NameConventionPolicy other) + { + if (Rules.Length != other.Rules.Length) + return false; + for (int i = 0; i < rules.Length; i++) { + if (!rules [i].Equals (other.Rules[i])) + return false; + } + return true; + } + #endregion + } +}
\ No newline at end of file diff --git a/main/src/addins/CSharpBinding/MonoDevelop.CSharp.Diagnostics/InconsistentNaming/NameConventionRule.cs b/main/src/addins/CSharpBinding/MonoDevelop.CSharp.Diagnostics/InconsistentNaming/NameConventionRule.cs new file mode 100644 index 0000000000..2453efff88 --- /dev/null +++ b/main/src/addins/CSharpBinding/MonoDevelop.CSharp.Diagnostics/InconsistentNaming/NameConventionRule.cs @@ -0,0 +1,132 @@ +// +// NamingRule.cs +// +// Author: +// Mike Krüger <mkrueger@xamarin.com> +// +// Copyright (c) 2012 Xamarin Inc. (http://xamarin.com) +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. +using System; +using System.Text; +using MonoDevelop.Projects.Policies; +using MonoDevelop.Core.Serialization; +using ICSharpCode.NRefactory6.CSharp.Refactoring; + +namespace MonoDevelop.CSharp.Diagnostics.InconsistentNaming +{ + [DataItem ("NamingRule")] + sealed class NameConventionRule + { + ICSharpCode.NRefactory6.CSharp.Diagnostics.NamingRule wrappedRule = new ICSharpCode.NRefactory6.CSharp.Diagnostics.NamingRule (ICSharpCode.NRefactory6.CSharp.Diagnostics.AffectedEntity.None); + + [ItemProperty] + public string Name { + get { return wrappedRule.Name; } + set { wrappedRule.Name = value;} + } + + [ItemProperty] + public string[] RequiredPrefixes { + get { return wrappedRule.RequiredPrefixes; } + set { wrappedRule.RequiredPrefixes = value;} + } + + [ItemProperty] + public string[] AllowedPrefixes { + get { return wrappedRule.AllowedPrefixes; } + set { wrappedRule.AllowedPrefixes = value;} + } + + [ItemProperty] + public string[] RequiredSuffixes { + get { return wrappedRule.RequiredSuffixes; } + set { wrappedRule.RequiredSuffixes = value;} + } + + [ItemProperty] + public string[] ForbiddenPrefixes { + get { return wrappedRule.ForbiddenPrefixes; } + set { wrappedRule.ForbiddenPrefixes = value;} + } + + [ItemProperty] + public string[] ForbiddenSuffixes { + get { return wrappedRule.ForbiddenSuffixes; } + set { wrappedRule.ForbiddenSuffixes = value;} + } + + [ItemProperty] + public ICSharpCode.NRefactory6.CSharp.Diagnostics.AffectedEntity AffectedEntity { + get { return wrappedRule.AffectedEntity; } + set { wrappedRule.AffectedEntity = value;} + } + + [ItemProperty] + public ICSharpCode.NRefactory6.CSharp.Diagnostics.Modifiers VisibilityMask { + get { return wrappedRule.VisibilityMask; } + set { wrappedRule.VisibilityMask = value;} + } + + [ItemProperty] + public ICSharpCode.NRefactory6.CSharp.Diagnostics.NamingStyle NamingStyle { + get { return wrappedRule.NamingStyle; } + set { wrappedRule.NamingStyle = value;} + } + + [ItemProperty] + public bool IncludeInstanceMembers { + get { return wrappedRule.IncludeInstanceMembers; } + set { wrappedRule.IncludeInstanceMembers = value;} + } + + [ItemProperty] + public bool IncludeStaticEntities { + get { return wrappedRule.IncludeStaticEntities; } + set { wrappedRule.IncludeStaticEntities = value;} + } + + internal NameConventionRule (ICSharpCode.NRefactory6.CSharp.Diagnostics.NamingRule wrappedRule) + { + this.wrappedRule = wrappedRule; + } + + public NameConventionRule () + { + } + + public NameConventionRule Clone () + { + return new NameConventionRule () { + wrappedRule = this.wrappedRule.Clone () + }; + } + + public string GetPreview () + { + return wrappedRule.GetPreview (); + } + + internal ICSharpCode.NRefactory6.CSharp.Diagnostics.NamingRule GetNRefactoryRule () + { + return wrappedRule; + } + } +} + diff --git a/main/src/addins/CSharpBinding/MonoDevelop.CSharp.Diagnostics/MonoTODO/MonoTODODiagnosticAnalyzer.cs b/main/src/addins/CSharpBinding/MonoDevelop.CSharp.Diagnostics/MonoTODO/MonoTODODiagnosticAnalyzer.cs new file mode 100644 index 0000000000..b17d6fd0f5 --- /dev/null +++ b/main/src/addins/CSharpBinding/MonoDevelop.CSharp.Diagnostics/MonoTODO/MonoTODODiagnosticAnalyzer.cs @@ -0,0 +1,103 @@ +// +// MonoTODODiagnosticAnalyzer.cs +// +// Author: +// Mike Krüger <mkrueger@xamarin.com> +// +// Copyright (c) 2013 Xamarin Inc. (http://xamarin.com) +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +using System.Collections.Generic; +using System.Collections.Immutable; +using Microsoft.CodeAnalysis.Diagnostics; +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.CSharp; +using ICSharpCode.NRefactory6.CSharp; +using System.Threading; + +namespace MonoDevelop.CSharp.Diagnostics.MonoTODODiagnostic +{ + [DiagnosticAnalyzer(LanguageNames.CSharp)] + sealed class MonoTODODiagnosticAnalyzer : DiagnosticAnalyzer + { + static readonly ImmutableArray<SyntaxKind> syntaxKindsOfInterest = ImmutableArray.Create( + SyntaxKind.IdentifierName, // foo + SyntaxKind.SimpleMemberAccessExpression, // foo.bar + SyntaxKind.PointerMemberAccessExpression, // foo->bar + SyntaxKind.ConditionalAccessExpression // foo?.bar + ); + + static readonly DiagnosticDescriptor descriptor = new DiagnosticDescriptor( + IDEDiagnosticIds.MonoTODODiagnosticDiagnosticId, + "Find usages of mono todo items", + "{0}", + DiagnosticAnalyzerCategories.Notifications, + DiagnosticSeverity.Warning, + isEnabledByDefault: true); + + public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { + get { + return ImmutableArray.Create(descriptor); + } + } + + public override void Initialize(AnalysisContext context) + { + context.RegisterSyntaxNodeAction( + (nodeContext) => { + Diagnostic diagnostic; + if (TryFindMonoTODO(nodeContext.SemanticModel, nodeContext.Node, out diagnostic, nodeContext.CancellationToken)) + nodeContext.ReportDiagnostic (diagnostic); + }, + syntaxKindsOfInterest); + } + + static readonly Dictionary<string, string> attributes = new Dictionary<string, string> { + { "MonoTODOAttribute", "Mono TODO" }, + { "MonoNotSupportedAttribute", "Mono NOT SUPPORTED" }, + { "MonoLimitationAttribute", "Mono LIMITATION" } + }; + + bool TryFindMonoTODO (SemanticModel semanticModel, SyntaxNode node, out Diagnostic diagnostic, CancellationToken cancellationToken) + { + var info = semanticModel.GetSymbolInfo (node); + diagnostic = default(Diagnostic); + if (info.Symbol == null || semanticModel.IsFromGeneratedCode (cancellationToken)) + return false; + + foreach (var attr in info.Symbol.GetAttributes ()) { + if (attr.AttributeClass.ContainingNamespace.GetFullName () != "System") + continue; + string val; + if (attributes.TryGetValue (attr.AttributeClass.Name, out val)) { + string msg = null; + if (attr.ConstructorArguments.Length > 0) { + var arg = attr.ConstructorArguments [0]; + msg = arg.Value != null ? arg.Value.ToString () : null; + } + var tree = semanticModel.SyntaxTree; + diagnostic = Diagnostic.Create(descriptor, tree.GetLocation(node.Span), string.IsNullOrEmpty (msg) ? val : val + ": " + msg); + return true; + } + } + return false; + } + } +}
\ No newline at end of file diff --git a/main/src/addins/CSharpBinding/MonoDevelop.CSharp.Diagnostics/RemoveUnnecessaryCast/CSharpRemoveUnnecessaryCastDiagnosticAnalyzer.cs b/main/src/addins/CSharpBinding/MonoDevelop.CSharp.Diagnostics/RemoveUnnecessaryCast/CSharpRemoveUnnecessaryCastDiagnosticAnalyzer.cs new file mode 100644 index 0000000000..dcb06caf87 --- /dev/null +++ b/main/src/addins/CSharpBinding/MonoDevelop.CSharp.Diagnostics/RemoveUnnecessaryCast/CSharpRemoveUnnecessaryCastDiagnosticAnalyzer.cs @@ -0,0 +1,42 @@ +// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System; +using System.Collections.Generic; +using System.Collections.Immutable; +using System.Threading; +using Microsoft.CodeAnalysis.CSharp.Extensions; +using Microsoft.CodeAnalysis.CSharp.Syntax; +using Microsoft.CodeAnalysis.Diagnostics; +using Microsoft.CodeAnalysis.Text; +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.CSharp; +using ICSharpCode.NRefactory6.CSharp; + +namespace MonoDevelop.CSharp.Diagnostics.RemoveUnnecessaryCast +{ + [DiagnosticAnalyzer(LanguageNames.CSharp)] + internal sealed class CSharpRemoveUnnecessaryCastDiagnosticAnalyzer : RemoveUnnecessaryCastDiagnosticAnalyzerBase<SyntaxKind> + { + private static readonly ImmutableArray<SyntaxKind> s_kindsOfInterest = ImmutableArray.Create(SyntaxKind.CastExpression); + + public override ImmutableArray<SyntaxKind> SyntaxKindsOfInterest + { + get + { + return s_kindsOfInterest; + } + } + + protected override bool IsUnnecessaryCast(SemanticModel model, SyntaxNode node, CancellationToken cancellationToken) + { + var cast = (CastExpressionSyntax)node; + return cast.IsUnnecessaryCast(model, cancellationToken); + } + + protected override TextSpan GetDiagnosticSpan(SyntaxNode node) + { + var cast = (CastExpressionSyntax)node; + return TextSpan.FromBounds(cast.OpenParenToken.SpanStart, cast.CloseParenToken.Span.End); + } + } +} diff --git a/main/src/addins/CSharpBinding/MonoDevelop.CSharp.Diagnostics/RemoveUnnecessaryCast/RemoveUnnecessaryCastDiagnosticAnalyzerBase.cs b/main/src/addins/CSharpBinding/MonoDevelop.CSharp.Diagnostics/RemoveUnnecessaryCast/RemoveUnnecessaryCastDiagnosticAnalyzerBase.cs new file mode 100644 index 0000000000..615293f380 --- /dev/null +++ b/main/src/addins/CSharpBinding/MonoDevelop.CSharp.Diagnostics/RemoveUnnecessaryCast/RemoveUnnecessaryCastDiagnosticAnalyzerBase.cs @@ -0,0 +1,84 @@ +// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System; +using System.Collections.Generic; +using System.Collections.Immutable; +using System.Linq; +using System.Threading; +using Microsoft.CodeAnalysis.Diagnostics; +using Microsoft.CodeAnalysis.Shared.Extensions; +using Microsoft.CodeAnalysis.Text; +using Roslyn.Utilities; +using MonoDevelop.Core; +using Microsoft.CodeAnalysis; +using ICSharpCode.NRefactory6.CSharp; + +namespace MonoDevelop.CSharp.Diagnostics.RemoveUnnecessaryCast +{ + internal abstract class RemoveUnnecessaryCastDiagnosticAnalyzerBase<TLanguageKindEnum> : DiagnosticAnalyzer where TLanguageKindEnum : struct + { + private static string s_localizableTitle = GettextCatalog.GetString ("Remove Unnecessary Cast"); + private static string s_localizableMessage = GettextCatalog.GetString ("Cast is redundant."); + + private static readonly DiagnosticDescriptor s_descriptor = new DiagnosticDescriptor(IDEDiagnosticIds.RemoveUnnecessaryCastDiagnosticId, + s_localizableTitle, + s_localizableMessage, + DiagnosticAnalyzerCategories.RedundanciesInCode, + DiagnosticSeverity.Warning, + isEnabledByDefault: true, + customTags: DiagnosticCustomTags.Unnecessary); + + #region Interface methods + + public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics + { + get + { + return ImmutableArray.Create(s_descriptor); + } + } + + public override void Initialize(AnalysisContext context) + { + context.RegisterSyntaxNodeAction( + (nodeContext) => + { + Diagnostic diagnostic; + if (TryRemoveCastExpression(nodeContext.SemanticModel, nodeContext.Node, out diagnostic, nodeContext.CancellationToken)) + { + nodeContext.ReportDiagnostic(diagnostic); + } + }, + this.SyntaxKindsOfInterest.ToArray()); + } + + public abstract ImmutableArray<TLanguageKindEnum> SyntaxKindsOfInterest { get; } + + #endregion + + protected abstract bool IsUnnecessaryCast(SemanticModel model, SyntaxNode node, CancellationToken cancellationToken); + protected abstract TextSpan GetDiagnosticSpan(SyntaxNode node); + + private bool TryRemoveCastExpression( + SemanticModel model, SyntaxNode node, out Diagnostic diagnostic, CancellationToken cancellationToken) + { + diagnostic = default(Diagnostic); + if (model.IsFromGeneratedCode (cancellationToken)) + return false; + if (!IsUnnecessaryCast(model, node, cancellationToken)) + { + return false; + } + + var tree = model.SyntaxTree; + var span = GetDiagnosticSpan(node); + if (tree.OverlapsHiddenPosition(span, cancellationToken)) + { + return false; + } + + diagnostic = Diagnostic.Create(s_descriptor, tree.GetLocation(span)); + return true; + } + } +} diff --git a/main/src/addins/CSharpBinding/MonoDevelop.CSharp.Diagnostics/RemoveUnnecessaryImports/CSharpRemoveUnnecessaryImportsDiagnosticAnalyzer.cs b/main/src/addins/CSharpBinding/MonoDevelop.CSharp.Diagnostics/RemoveUnnecessaryImports/CSharpRemoveUnnecessaryImportsDiagnosticAnalyzer.cs new file mode 100644 index 0000000000..4ae9b07748 --- /dev/null +++ b/main/src/addins/CSharpBinding/MonoDevelop.CSharp.Diagnostics/RemoveUnnecessaryImports/CSharpRemoveUnnecessaryImportsDiagnosticAnalyzer.cs @@ -0,0 +1,50 @@ +// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System.Collections.Generic; +using System.Linq; +using System.Threading; +using Microsoft.CodeAnalysis.CSharp.Syntax; +using Microsoft.CodeAnalysis.Diagnostics; +using Microsoft.CodeAnalysis.Shared.Extensions; +using Microsoft.CodeAnalysis.Text; +using Roslyn.Utilities; +using Microsoft.CodeAnalysis; +using ICSharpCode.NRefactory6.CSharp.Features.RemoveUnnecessaryImports; +using ICSharpCode.NRefactory6.CSharp; +using MonoDevelop.Core; + +namespace MonoDevelop.CSharp.Diagnostics.RemoveUnnecessaryImports +{ + [DiagnosticAnalyzer(LanguageNames.CSharp)] + internal sealed class CSharpRemoveUnnecessaryImportsDiagnosticAnalyzer : RemoveUnnecessaryImportsDiagnosticAnalyzerBase + { + private static readonly string s_TitleAndMessageFormat = GettextCatalog.GetString ("Using directive is unnecessary."); + + protected override LocalizableString GetTitleAndMessageFormatForClassificationIdDescriptor() + { + return s_TitleAndMessageFormat; + } + + protected override IEnumerable<SyntaxNode> GetUnnecessaryImports(SemanticModel semanticModel, SyntaxNode root, CancellationToken cancellationToken = default(CancellationToken)) + { + return CSharpRemoveUnnecessaryImportsService.GetUnnecessaryImports(semanticModel, root, cancellationToken); + } + + protected override IEnumerable<TextSpan> GetFixableDiagnosticSpans(IEnumerable<SyntaxNode> nodes, SyntaxTree tree, CancellationToken cancellationToken = default(CancellationToken)) + { + var nodesContainingUnnecessaryUsings = new HashSet<SyntaxNode>(); + foreach (var node in nodes) { + yield return node.Span; +// var nodeContainingUnnecessaryUsings = node.GetAncestors().First(n => n is NamespaceDeclarationSyntax || n is CompilationUnitSyntax); +// if (!nodesContainingUnnecessaryUsings.Add(nodeContainingUnnecessaryUsings)) +// { +// continue; +// } +// +// yield return nodeContainingUnnecessaryUsings is NamespaceDeclarationSyntax ? +// ((NamespaceDeclarationSyntax)nodeContainingUnnecessaryUsings).Usings.GetContainedSpan() : +// ((CompilationUnitSyntax)nodeContainingUnnecessaryUsings).Usings.GetContainedSpan(); + } + } + } +} diff --git a/main/src/addins/CSharpBinding/MonoDevelop.CSharp.Diagnostics/RemoveUnnecessaryImports/RemoveUnnecessaryImportsDiagnosticAnalyzerBase.cs b/main/src/addins/CSharpBinding/MonoDevelop.CSharp.Diagnostics/RemoveUnnecessaryImports/RemoveUnnecessaryImportsDiagnosticAnalyzerBase.cs new file mode 100644 index 0000000000..f04a068105 --- /dev/null +++ b/main/src/addins/CSharpBinding/MonoDevelop.CSharp.Diagnostics/RemoveUnnecessaryImports/RemoveUnnecessaryImportsDiagnosticAnalyzerBase.cs @@ -0,0 +1,126 @@ +// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System; +using System.Collections.Generic; +using System.Collections.Immutable; +using System.Linq; +using System.Threading; +using Microsoft.CodeAnalysis.Shared.Extensions; +using Microsoft.CodeAnalysis.Text; +using Roslyn.Utilities; +using Microsoft.CodeAnalysis.Diagnostics; +using Microsoft.CodeAnalysis; +using ICSharpCode.NRefactory6.CSharp; + +namespace MonoDevelop.CSharp.Diagnostics.RemoveUnnecessaryImports +{ + internal abstract class RemoveUnnecessaryImportsDiagnosticAnalyzerBase : DiagnosticAnalyzer + { + // NOTE: This is a trigger diagnostic, which doesn't show up in the ruleset editor and hence doesn't need a conventional IDE Diagnostic ID string. + internal const string DiagnosticFixableId = "RemoveUnnecessaryImportsFixable"; + + // The NotConfigurable custom tag ensures that user can't turn this diagnostic into a warning / error via + // ruleset editor or solution explorer. Setting messageFormat to empty string ensures that we won't display + // this diagnostic in the preview pane header. + private static readonly DiagnosticDescriptor s_fixableIdDescriptor = + new DiagnosticDescriptor(DiagnosticFixableId, + title: "", messageFormat: "", category: "", + defaultSeverity: DiagnosticSeverity.Hidden, + isEnabledByDefault: true, + customTags: WellKnownDiagnosticTags.NotConfigurable); + + protected abstract LocalizableString GetTitleAndMessageFormatForClassificationIdDescriptor(); + + private DiagnosticDescriptor _classificationIdDescriptor; + private DiagnosticDescriptor GetClassificationIdDescriptor() + { + if (_classificationIdDescriptor == null) + { + var titleAndMessageFormat = GetTitleAndMessageFormatForClassificationIdDescriptor(); + _classificationIdDescriptor = + new DiagnosticDescriptor(IDEDiagnosticIds.RemoveUnnecessaryImportsDiagnosticId, + titleAndMessageFormat, + titleAndMessageFormat, + DiagnosticAnalyzerCategories.RedundanciesInCode, + DiagnosticSeverity.Warning, + isEnabledByDefault: true, + customTags: DiagnosticCustomTags.Unnecessary); + } + + return _classificationIdDescriptor; + } + + private ImmutableArray<DiagnosticDescriptor> _descriptors; + public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics + { + get + { + if (_descriptors == null) + { + _descriptors = ImmutableArray.Create(GetClassificationIdDescriptor()); + } + + return _descriptors; + } + } + + public override void Initialize(AnalysisContext context) + { + context.RegisterSemanticModelAction(this.AnalyzeSemanticModel); + } + + private void AnalyzeSemanticModel(SemanticModelAnalysisContext context) + { + if (context.IsFromGeneratedCode ()) + return; + var tree = context.SemanticModel.SyntaxTree; + var root = tree.GetRoot(); + var unncessaryImports = GetUnnecessaryImports(context.SemanticModel, root); + if (unncessaryImports != null && unncessaryImports.Any()) + { + Func<SyntaxNode, SyntaxToken> getLastTokenFunc = GetLastTokenDelegateForContiguousSpans(); + var contiguousSpans = unncessaryImports.GetContiguousSpans(getLastTokenFunc); + var diagnostics = CreateClassificationDiagnostics(contiguousSpans, tree).Concat( + CreateFixableDiagnostics(unncessaryImports, tree)); + var spans = new List<TextSpan> (); + foreach (var diagnostic in diagnostics) + { + if (spans.Any (s => s.OverlapsWith (diagnostic.Location.SourceSpan))) + continue; + spans.Add (diagnostic.Location.SourceSpan); + context.ReportDiagnostic(diagnostic); + } + } + } + + protected abstract IEnumerable<SyntaxNode> GetUnnecessaryImports(SemanticModel semanticModel, SyntaxNode root, CancellationToken cancellationToken = default(CancellationToken)); + protected virtual Func<SyntaxNode, SyntaxToken> GetLastTokenDelegateForContiguousSpans() + { + return null; + } + + // Create one diagnostic for each unnecessary span that will be classified as Unnecessary + private IEnumerable<Diagnostic> CreateClassificationDiagnostics(IEnumerable<TextSpan> contiguousSpans, SyntaxTree tree, CancellationToken cancellationToken = default(CancellationToken)) + { + foreach (var span in contiguousSpans) + { + if (tree.OverlapsHiddenPosition(span, cancellationToken)) + { + continue; + } + + yield return Diagnostic.Create(GetClassificationIdDescriptor(), tree.GetLocation(span)); + } + } + + protected abstract IEnumerable<TextSpan> GetFixableDiagnosticSpans(IEnumerable<SyntaxNode> nodes, SyntaxTree tree, CancellationToken cancellationToken = default(CancellationToken)); + + private IEnumerable<Diagnostic> CreateFixableDiagnostics(IEnumerable<SyntaxNode> nodes, SyntaxTree tree, CancellationToken cancellationToken = default(CancellationToken)) + { + var spans = GetFixableDiagnosticSpans(nodes, tree, cancellationToken); + foreach (var span in spans) { + yield return Diagnostic.Create(GetClassificationIdDescriptor(), tree.GetLocation(span)); + } + } + } +} diff --git a/main/src/addins/CSharpBinding/MonoDevelop.CSharp.Diagnostics/SimplifyTypeNames/CSharpSimplifyTypeNamesDiagnosticAnalyzer.cs b/main/src/addins/CSharpBinding/MonoDevelop.CSharp.Diagnostics/SimplifyTypeNames/CSharpSimplifyTypeNamesDiagnosticAnalyzer.cs new file mode 100644 index 0000000000..8a8de8f79d --- /dev/null +++ b/main/src/addins/CSharpBinding/MonoDevelop.CSharp.Diagnostics/SimplifyTypeNames/CSharpSimplifyTypeNamesDiagnosticAnalyzer.cs @@ -0,0 +1,152 @@ +// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System; +using System.Collections.Immutable; +using System.Linq; +using System.Threading; +using Microsoft.CodeAnalysis.CSharp.Extensions; +using Microsoft.CodeAnalysis.CSharp.Syntax; +using Microsoft.CodeAnalysis.Diagnostics; +using Microsoft.CodeAnalysis.Options; +using Microsoft.CodeAnalysis.Text; +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.CSharp; +using ICSharpCode.NRefactory6.CSharp; + +namespace MonoDevelop.CSharp.Diagnostics.SimplifyTypeNames +{ + [DiagnosticAnalyzer(LanguageNames.CSharp)] + internal sealed class CSharpSimplifyTypeNamesDiagnosticAnalyzer : SimplifyTypeNamesDiagnosticAnalyzerBase<SyntaxKind> + { + private static readonly ImmutableArray<SyntaxKind> s_kindsOfInterest = ImmutableArray.Create(SyntaxKind.QualifiedName, + SyntaxKind.AliasQualifiedName, + SyntaxKind.GenericName, + SyntaxKind.IdentifierName, + SyntaxKind.SimpleMemberAccessExpression, + SyntaxKind.QualifiedCref); + + public override void Initialize(AnalysisContext analysisContext) + { + analysisContext.RegisterSyntaxNodeAction(AnalyzeNode, s_kindsOfInterest.ToArray()); + } + + protected override void AnalyzeNode(SyntaxNodeAnalysisContext context) + { + if (context.IsFromGeneratedCode ()) + return; + if (context.Node.Ancestors(ascendOutOfTrivia: false).Any(n => s_kindsOfInterest.Contains(n.Kind()))) + { + // Already simplified an ancestor of this node. + return; + } + + Diagnostic diagnostic; + Func<SyntaxNode, bool> descendIntoChildren = n => + { + if (!IsRegularCandidate(n) || + !TrySimplifyTypeNameExpression(context.SemanticModel, n, context.Options, out diagnostic, context.CancellationToken)) + { + return true; + } + context.ReportDiagnostic(diagnostic); + return false; + }; + + // find regular node first - search from top to down. once found one, don't get into its children + foreach (var candidate in context.Node.DescendantNodesAndSelf(descendIntoChildren)) + { + context.CancellationToken.ThrowIfCancellationRequested(); + } + + // now search structure trivia + foreach (var candidate in context.Node.DescendantNodesAndSelf(descendIntoChildren: n => !IsCrefCandidate(n), descendIntoTrivia: true)) + { + context.CancellationToken.ThrowIfCancellationRequested(); + + if (IsCrefCandidate(candidate) && + TrySimplifyTypeNameExpression(context.SemanticModel, candidate, context.Options, out diagnostic, context.CancellationToken)) + { + context.ReportDiagnostic(diagnostic); + } + } + } + + internal static bool IsCandidate(SyntaxNode node) + { + return IsRegularCandidate(node) || IsCrefCandidate(node); + } + + private static bool IsRegularCandidate(SyntaxNode node) + { + return node != null && s_kindsOfInterest.Contains(node.Kind()); + } + + private static bool IsCrefCandidate(SyntaxNode node) + { + return node is QualifiedCrefSyntax; + } + + protected sealed override bool CanSimplifyTypeNameExpressionCore(SemanticModel model, SyntaxNode node, OptionSet optionSet, out TextSpan issueSpan, out string diagnosticId, CancellationToken cancellationToken) + { + return CanSimplifyTypeNameExpression(model, node, optionSet, out issueSpan, out diagnosticId, cancellationToken); + } + + internal static bool CanSimplifyTypeNameExpression(SemanticModel model, SyntaxNode node, OptionSet optionSet, out TextSpan issueSpan, out string diagnosticId, CancellationToken cancellationToken) + { + issueSpan = default(TextSpan); + diagnosticId = IDEDiagnosticIds.SimplifyNamesDiagnosticId; + + // For Crefs, currently only Qualified Crefs needs to be handled separately + if (node.Kind() == SyntaxKind.QualifiedCref) + { + if (node.ContainsDiagnostics) + { + return false; + } + + var crefSyntax = (CrefSyntax)node; + + CrefSyntax replacementNode; + if (!crefSyntax.TryReduceOrSimplifyExplicitName (model, out replacementNode, out issueSpan, optionSet, cancellationToken)) + { + return false; + } + } + else + { + var expression = (ExpressionSyntax)node; + if (expression.ContainsDiagnostics) + { + return false; + } + + // in case of an As or Is expression we need to handle the binary expression, because it might be + // required to add parenthesis around the expression. Adding the parenthesis is done in the CSharpNameSimplifier.Rewriter + var expressionToCheck = expression.Kind() == SyntaxKind.AsExpression || expression.Kind() == SyntaxKind.IsExpression + ? ((BinaryExpressionSyntax)expression).Right + : expression; + + ExpressionSyntax replacementSyntax; + if (!expressionToCheck.TryReduceOrSimplifyExplicitName(model, out replacementSyntax, out issueSpan, optionSet, cancellationToken)) + { + return false; + } + + if (expression.Kind() == SyntaxKind.SimpleMemberAccessExpression) + { + var memberAccess = (MemberAccessExpressionSyntax)expression; + diagnosticId = memberAccess.Expression.Kind() == SyntaxKind.ThisExpression ? + IDEDiagnosticIds.SimplifyThisOrMeDiagnosticId : + IDEDiagnosticIds.SimplifyMemberAccessDiagnosticId; + } + } + + return true; + } + + protected override string GetLanguageName() + { + return LanguageNames.CSharp; + } + } +} diff --git a/main/src/addins/CSharpBinding/MonoDevelop.CSharp.Diagnostics/SimplifyTypeNames/SimplifyTypeNamesDiagnosticAnalyzerBase.cs b/main/src/addins/CSharpBinding/MonoDevelop.CSharp.Diagnostics/SimplifyTypeNames/SimplifyTypeNamesDiagnosticAnalyzerBase.cs new file mode 100644 index 0000000000..f3d456a6eb --- /dev/null +++ b/main/src/addins/CSharpBinding/MonoDevelop.CSharp.Diagnostics/SimplifyTypeNames/SimplifyTypeNamesDiagnosticAnalyzerBase.cs @@ -0,0 +1,112 @@ +// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System.Collections.Immutable; +using System.Threading; +using Microsoft.CodeAnalysis.Options; +using Microsoft.CodeAnalysis.Shared.Extensions; +using Microsoft.CodeAnalysis.Text; +using Roslyn.Utilities; +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.Diagnostics; +using ICSharpCode.NRefactory6.CSharp; +using MonoDevelop.Core; +using MonoDevelop.Ide.TypeSystem; +using System; + +namespace MonoDevelop.CSharp.Diagnostics.SimplifyTypeNames +{ + internal abstract class SimplifyTypeNamesDiagnosticAnalyzerBase<TLanguageKindEnum> : DiagnosticAnalyzer where TLanguageKindEnum : struct + { + private static string s_localizableMessage = GettextCatalog.GetString ("Name can be simplified."); + private static string s_localizableTitleSimplifyNames = GettextCatalog.GetString ("Simplify Names"); + + private static readonly DiagnosticDescriptor s_descriptorSimplifyNames = new DiagnosticDescriptor(IDEDiagnosticIds.SimplifyNamesDiagnosticId, + s_localizableTitleSimplifyNames, + s_localizableMessage, + DiagnosticAnalyzerCategories.RedundanciesInCode, + DiagnosticSeverity.Warning, + isEnabledByDefault: true, + customTags: DiagnosticCustomTags.Unnecessary); + + private static string s_localizableTitleSimplifyMemberAccess = GettextCatalog.GetString ("Simplify member access '{0}'"); + private static readonly DiagnosticDescriptor s_descriptorSimplifyMemberAccess = new DiagnosticDescriptor(IDEDiagnosticIds.SimplifyMemberAccessDiagnosticId, + s_localizableTitleSimplifyMemberAccess, + s_localizableMessage, + DiagnosticAnalyzerCategories.RedundanciesInCode, + DiagnosticSeverity.Warning, + isEnabledByDefault: true, + customTags: DiagnosticCustomTags.Unnecessary); + + private static string s_localizableTitleSimplifyThisOrMe = GettextCatalog.GetString ("Remove 'this'"); + private static readonly DiagnosticDescriptor s_descriptorSimplifyThisOrMe = new DiagnosticDescriptor(IDEDiagnosticIds.SimplifyThisOrMeDiagnosticId, + s_localizableTitleSimplifyThisOrMe, + s_localizableMessage, + DiagnosticAnalyzerCategories.RedundanciesInCode, + DiagnosticSeverity.Warning, + isEnabledByDefault: true, + customTags: DiagnosticCustomTags.Unnecessary); + + private OptionSet _lazyDefaultOptionSet; + + public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics + { + get + { + return ImmutableArray.Create(s_descriptorSimplifyNames, s_descriptorSimplifyMemberAccess, s_descriptorSimplifyThisOrMe); + } + } + + protected abstract void AnalyzeNode(SyntaxNodeAnalysisContext context); + + protected abstract bool CanSimplifyTypeNameExpressionCore(SemanticModel model, SyntaxNode node, OptionSet optionSet, out TextSpan issueSpan, out string diagnosticId, CancellationToken cancellationToken); + + private OptionSet GetOptionSet(AnalyzerOptions analyzerOptions) + { + return TypeSystemService.Workspace.Options; + } + + protected abstract string GetLanguageName(); + + protected bool TrySimplifyTypeNameExpression(SemanticModel model, SyntaxNode node, AnalyzerOptions analyzerOptions, out Diagnostic diagnostic, CancellationToken cancellationToken) + { + diagnostic = default(Diagnostic); + + var optionSet = GetOptionSet(analyzerOptions); + string diagnosticId; + + TextSpan issueSpan; + if (!CanSimplifyTypeNameExpressionCore(model, node, optionSet, out issueSpan, out diagnosticId, cancellationToken)) + { + return false; + } + + if (model.SyntaxTree.OverlapsHiddenPosition(issueSpan, cancellationToken)) + { + return false; + } + + DiagnosticDescriptor descriptor; + switch (diagnosticId) + { + case IDEDiagnosticIds.SimplifyNamesDiagnosticId: + descriptor = s_descriptorSimplifyNames; + break; + + case IDEDiagnosticIds.SimplifyMemberAccessDiagnosticId: + descriptor = s_descriptorSimplifyMemberAccess; + break; + + case IDEDiagnosticIds.SimplifyThisOrMeDiagnosticId: + descriptor = s_descriptorSimplifyThisOrMe; + break; + + default: + throw new InvalidOperationException(); + } + + var tree = model.SyntaxTree; + diagnostic = Diagnostic.Create(descriptor, tree.GetLocation(issueSpan)); + return true; + } + } +} |