diff options
author | Mike Krüger <mkrueger@xamarin.com> | 2011-11-10 19:59:55 +0400 |
---|---|---|
committer | Mike Krüger <mkrueger@xamarin.com> | 2011-11-10 19:59:55 +0400 |
commit | b27964ac2857b7ed12140eba0765fb7b52231acf (patch) | |
tree | 0945e60bd5e5db8aaaffa1be857014406aa30003 /main/src/addins/CSharpBinding | |
parent | fd337c6c0311da5923030ed736a565ad785a3794 (diff) |
Added redundant "this." inspector.
Diffstat (limited to 'main/src/addins/CSharpBinding')
5 files changed, 81 insertions, 6 deletions
diff --git a/main/src/addins/CSharpBinding/CSharpBinding.addin.xml b/main/src/addins/CSharpBinding/CSharpBinding.addin.xml index db2951fd3e..b32bf3caa1 100644 --- a/main/src/addins/CSharpBinding/CSharpBinding.addin.xml +++ b/main/src/addins/CSharpBinding/CSharpBinding.addin.xml @@ -240,6 +240,11 @@ severity = "Warning" class = "MonoDevelop.CSharp.Inspection.UnusedUsingInspector" /> <Inspector mimeType="text/x-csharp" + _title = "Remove redundant 'this.'" + _description = "Removes 'this.' references that are not required." + severity = "Warning" + class = "MonoDevelop.CSharp.Inspection.RedundantThisInspector" /> + <Inspector mimeType="text/x-csharp" _title = "Show NotImplementedException" _description = "Shows NotImplementedException throws in the quick task bar." severity = "Suggestion" diff --git a/main/src/addins/CSharpBinding/CSharpBinding.csproj b/main/src/addins/CSharpBinding/CSharpBinding.csproj index d8df3c0fd9..b38433f4cc 100644 --- a/main/src/addins/CSharpBinding/CSharpBinding.csproj +++ b/main/src/addins/CSharpBinding/CSharpBinding.csproj @@ -247,6 +247,7 @@ <Compile Include="MonoDevelop.CSharp.Refactoring\RefactorImports\RefactorImportsHandler.cs" /> <Compile Include="MonoDevelop.CSharp.Refactoring\RefactorImports\SortImportsRefactoring.cs" /> <Compile Include="MonoDevelop.CSharp.Refactoring\HelperMethods.cs" /> + <Compile Include="MonoDevelop.CSharp.Inspection\Inspectors\RedundantThisInspector.cs" /> </ItemGroup> <ItemGroup> <None Include="ChangeLog" /> diff --git a/main/src/addins/CSharpBinding/Makefile.am b/main/src/addins/CSharpBinding/Makefile.am index 1439d139bc..674914d395 100644 --- a/main/src/addins/CSharpBinding/Makefile.am +++ b/main/src/addins/CSharpBinding/Makefile.am @@ -164,7 +164,8 @@ TESTFILES = \ Parser/SharpDevelopTree/Method.cs \ Parser/SharpDevelopTree/ReturnType.cs \ Parser/TypeVisitor.cs \ - Project/CSharpCompilerParameters.cs + Project/CSharpCompilerParameters.cs \ + MonoDevelop.CSharp.Inspection/Inspectors/RedundantThisInspector.cs TESTDLLS = \ $(NUNIT_LIBS) diff --git a/main/src/addins/CSharpBinding/MonoDevelop.CSharp.Inspection/CallGraph.cs b/main/src/addins/CSharpBinding/MonoDevelop.CSharp.Inspection/CallGraph.cs index 7c2869b88a..740915fd57 100644 --- a/main/src/addins/CSharpBinding/MonoDevelop.CSharp.Inspection/CallGraph.cs +++ b/main/src/addins/CSharpBinding/MonoDevelop.CSharp.Inspection/CallGraph.cs @@ -46,8 +46,7 @@ namespace MonoDevelop.CSharp.Inspection public CSharpResolver CSharpResolver; public VisitNamespaceNodesNavigator Navigator; - - ResolveVisitor visitor; + public ResolveVisitor Visitor; public CallGraph () { @@ -55,7 +54,7 @@ namespace MonoDevelop.CSharp.Inspection public ResolveResult Resolve (AstNode node) { - return visitor.GetResolveResult (node); + return Visitor.GetResolveResult (node); } public void Inspect (MonoDevelop.Ide.Gui.Document doc, ParsedDocument parsedDocument) @@ -65,8 +64,8 @@ namespace MonoDevelop.CSharp.Inspection var ctx = doc.TypeResolveContext; CSharpResolver = new CSharpResolver (ctx, System.Threading.CancellationToken.None); Navigator = new VisitNamespaceNodesNavigator (); - visitor = new ResolveVisitor (CSharpResolver, pf, Navigator); - visitor.Scan (unit); + Visitor = new ResolveVisitor (CSharpResolver, pf, Navigator); + Visitor.Scan (unit); /*unit.AcceptVisitor (visitor, null); diff --git a/main/src/addins/CSharpBinding/MonoDevelop.CSharp.Inspection/Inspectors/RedundantThisInspector.cs b/main/src/addins/CSharpBinding/MonoDevelop.CSharp.Inspection/Inspectors/RedundantThisInspector.cs new file mode 100644 index 0000000000..60f7987a23 --- /dev/null +++ b/main/src/addins/CSharpBinding/MonoDevelop.CSharp.Inspection/Inspectors/RedundantThisInspector.cs @@ -0,0 +1,69 @@ +//
+// RedundantThisInspector.cs
+//
+// Author:
+// Mike Krüger <mkrueger@xamarin.com>
+//
+// Copyright (c) 2011 Xamarin Inc.
+//
+// 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 MonoDevelop.Refactoring;
+using ICSharpCode.NRefactory.TypeSystem;
+using MonoDevelop.CSharp.Refactoring.RefactorImports;
+using ICSharpCode.NRefactory.CSharp.Resolver;
+using MonoDevelop.CSharp.Refactoring;
+using System.Collections.Generic;
+using ICSharpCode.NRefactory.Semantics;
+
+namespace MonoDevelop.CSharp.Inspection
+{
+ public class RedundantThisInspector: CSharpInspector
+ {
+ protected override void Attach (ObservableAstVisitor<InspectionData, object> visitor)
+ {
+ visitor.ThisReferenceExpressionVisited += HandleThisReferenceExpressionVisited;
+ }
+
+ void HandleThisReferenceExpressionVisited (ThisReferenceExpression expr, InspectionData data)
+ {
+ var memberReference = expr.Parent as MemberReferenceExpression;
+ if (memberReference == null)
+ return;
+ var state = data.Graph.Visitor.GetResolverStateBefore (expr);
+ var wholeResult = data.Graph.Visitor.GetResolveResult (memberReference) as MemberResolveResult;
+ var result = state.LookupSimpleNameOrTypeName (memberReference.MemberName, new List<IType> (), SimpleNameLookupMode.Expression) as MemberResolveResult;
+ if (result == null || wholeResult == null)
+ return;
+ if (result.Member.Region.Equals (wholeResult.Member.Region)) {
+ AddResult (data,
+ new DomRegion (expr.StartLocation, memberReference.MemberNameToken.StartLocation),
+ GettextCatalog.GetString ("Remove redundant 'this.'"),
+ delegate {
+ int offset = data.Document.Editor.LocationToOffset (expr.StartLocation);
+ int end = data.Document.Editor.LocationToOffset (memberReference.MemberNameToken.StartLocation);
+ data.Document.Editor.Remove (offset, end - offset);
+ }
+ );
+ }
+ }
+ }
+}
+
|