diff options
author | Mike Krüger <mkrueger@xamarin.com> | 2011-11-14 21:03:45 +0400 |
---|---|---|
committer | Mike Krüger <mkrueger@xamarin.com> | 2011-11-14 21:03:45 +0400 |
commit | de6aae871e6eea487374c259b4480b8e78f72db1 (patch) | |
tree | 85f7f13d863cbdcf789e3f66f975d2212d0c5b17 /main/src/addins/CSharpBinding | |
parent | 66774014e6a6f5c2b155eae65c07051a3bfd77c5 (diff) |
Added redundant namespace usage inspector.
Diffstat (limited to 'main/src/addins/CSharpBinding')
5 files changed, 84 insertions, 2 deletions
diff --git a/main/src/addins/CSharpBinding/CSharpBinding.addin.xml b/main/src/addins/CSharpBinding/CSharpBinding.addin.xml index cbf189af7b..bcfdd3c2f9 100644 --- a/main/src/addins/CSharpBinding/CSharpBinding.addin.xml +++ b/main/src/addins/CSharpBinding/CSharpBinding.addin.xml @@ -262,16 +262,22 @@ class = "MonoDevelop.CSharp.Inspection.UseVarKeywordInspector" /> <Inspector mimeType="text/x-csharp" - _title = "Remove redundint 'internal' modifier." + _title = "Remove redunaint 'internal' modifier." _description = "Removes 'internal' modifiers that are not required." severity = "Warning" class = "MonoDevelop.CSharp.Inspection.RedundantInternalInspector" /> <Inspector mimeType="text/x-csharp" - _title = "Remove redundint 'private' modifier." + _title = "Remove redundant 'private' modifier." _description = "Removes 'private' modifiers that are not required." severity = "Warning" class = "MonoDevelop.CSharp.Inspection.RedundantPrivateInspector" /> + + <Inspector mimeType="text/x-csharp" + _title = "Remove redundant 'namespace' usages." + _description = "Removes namespace usages." + severity = "Warning" + class = "MonoDevelop.CSharp.Inspection.RedundantNamespaceUsageInspector" /> </Extension> <Extension path = "/MonoDevelop/Ide/TextEditorResolver"> diff --git a/main/src/addins/CSharpBinding/CSharpBinding.csproj b/main/src/addins/CSharpBinding/CSharpBinding.csproj index 9bc3405b7e..4a51d9772d 100644 --- a/main/src/addins/CSharpBinding/CSharpBinding.csproj +++ b/main/src/addins/CSharpBinding/CSharpBinding.csproj @@ -250,6 +250,7 @@ <Compile Include="MonoDevelop.CSharp.Inspection\Inspectors\RedundantThisInspector.cs" /> <Compile Include="MonoDevelop.CSharp.Inspection\Inspectors\RedundantInternalInspector.cs" /> <Compile Include="MonoDevelop.CSharp.Inspection\Inspectors\RedundantPrivateInspector.cs" /> + <Compile Include="MonoDevelop.CSharp.Inspection\Inspectors\RedundantNamespaceUsageInspector.cs" /> </ItemGroup> <ItemGroup> <None Include="ChangeLog" /> diff --git a/main/src/addins/CSharpBinding/Makefile.am b/main/src/addins/CSharpBinding/Makefile.am index cf6cd61d40..50d436f79d 100644 --- a/main/src/addins/CSharpBinding/Makefile.am +++ b/main/src/addins/CSharpBinding/Makefile.am @@ -77,6 +77,7 @@ FILES = \ MonoDevelop.CSharp.Inspection/Inspectors/ConditionalToNullCoalescingInspector.cs \ MonoDevelop.CSharp.Inspection/Inspectors/NotImplementedExceptionInspector.cs \ MonoDevelop.CSharp.Inspection/Inspectors/RedundantInternalInspector.cs \ + MonoDevelop.CSharp.Inspection/Inspectors/RedundantNamespaceUsageInspector.cs \ MonoDevelop.CSharp.Inspection/Inspectors/RedundantPrivateInspector.cs \ MonoDevelop.CSharp.Inspection/Inspectors/RedundantThisInspector.cs \ MonoDevelop.CSharp.Inspection/Inspectors/StringIsNullOrEmptyInspector.cs \ diff --git a/main/src/addins/CSharpBinding/MonoDevelop.CSharp.Inspection/CodeAnalysis.cs b/main/src/addins/CSharpBinding/MonoDevelop.CSharp.Inspection/CodeAnalysis.cs index 8e63ff70ca..e315e7a585 100644 --- a/main/src/addins/CSharpBinding/MonoDevelop.CSharp.Inspection/CodeAnalysis.cs +++ b/main/src/addins/CSharpBinding/MonoDevelop.CSharp.Inspection/CodeAnalysis.cs @@ -39,6 +39,8 @@ using MonoDevelop.CSharp.ContextAction; using MonoDevelop.Refactoring; using MonoDevelop.Inspection; using Mono.Addins; +using ICSharpCode.NRefactory.CSharp.Resolver; +using ICSharpCode.NRefactory.Semantics; namespace MonoDevelop.CSharp.Inspection { @@ -57,6 +59,16 @@ namespace MonoDevelop.CSharp.Inspection { results.Add (result); } + + public CSharpResolver GetResolverStateBefore (AstNode node) + { + return Graph.Visitor.GetResolverStateBefore (node); + } + + public ResolveResult GetResolveResult (AstNode node) + { + return Graph.Visitor.GetResolveResult (node); + } } public static class CodeAnalysis diff --git a/main/src/addins/CSharpBinding/MonoDevelop.CSharp.Inspection/Inspectors/RedundantNamespaceUsageInspector.cs b/main/src/addins/CSharpBinding/MonoDevelop.CSharp.Inspection/Inspectors/RedundantNamespaceUsageInspector.cs new file mode 100644 index 0000000000..06229d798b --- /dev/null +++ b/main/src/addins/CSharpBinding/MonoDevelop.CSharp.Inspection/Inspectors/RedundantNamespaceUsageInspector.cs @@ -0,0 +1,62 @@ +// +// RedundantNamespaceUsageInspector.cs +// +// Author: +// Mike Krüger <mkrueger@xamarin.com> +// +// Copyright (c) 2011 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 ICSharpCode.NRefactory.CSharp; +using MonoDevelop.Core; +using ICSharpCode.NRefactory.TypeSystem; +using ICSharpCode.NRefactory.CSharp.Resolver; +using System.Collections.Generic; +using ICSharpCode.NRefactory.Semantics; + +namespace MonoDevelop.CSharp.Inspection +{ + public class RedundantNamespaceUsageInspector : CSharpInspector + { + protected override void Attach (ObservableAstVisitor<InspectionData, object> visitor) + { + visitor.MemberReferenceExpressionVisited += delegate (MemberReferenceExpression mr, InspectionData data) { + var state = data.GetResolverStateBefore (mr); + var wholeResult = data.GetResolveResult (mr); + var result = data.GetResolveResult (mr.Target); + if (!(result is NamespaceResolveResult) || wholeResult == null) + return; + + var lookupName = state.LookupSimpleNameOrTypeName (mr.MemberName, new List<IType> (), SimpleNameLookupMode.Expression); + + if (lookupName != null && wholeResult.Type.Equals (lookupName.Type)) { + AddResult (data, + new DomRegion (mr.StartLocation, mr.MemberNameToken.StartLocation), + GettextCatalog.GetString ("Remove redundant namespace usage."), + delegate { + int offset = data.Document.Editor.LocationToOffset (mr.StartLocation); + int end = data.Document.Editor.LocationToOffset (mr.MemberNameToken.StartLocation); + data.Document.Editor.Remove (offset, end - offset); + } + ); + } + }; + } + } +} |