diff options
author | Mike Krüger <mkrueger@xamarin.com> | 2011-11-11 18:15:56 +0400 |
---|---|---|
committer | Mike Krüger <mkrueger@xamarin.com> | 2011-11-11 18:15:56 +0400 |
commit | 66774014e6a6f5c2b155eae65c07051a3bfd77c5 (patch) | |
tree | 59a7cf2b8606946d4a4f26482d97d7ba9eea841c /main/src/addins/CSharpBinding | |
parent | ccb96e47803a3c1826f7140481a566bd3b519b24 (diff) |
Added redundant internal/private inspectors.
Diffstat (limited to 'main/src/addins/CSharpBinding')
8 files changed, 202 insertions, 61 deletions
diff --git a/main/src/addins/CSharpBinding/CSharpBinding.addin.xml b/main/src/addins/CSharpBinding/CSharpBinding.addin.xml index b32bf3caa1..cbf189af7b 100644 --- a/main/src/addins/CSharpBinding/CSharpBinding.addin.xml +++ b/main/src/addins/CSharpBinding/CSharpBinding.addin.xml @@ -260,6 +260,18 @@ _description = "Use implicitly typed local variable decaration" severity = "Suggestion" class = "MonoDevelop.CSharp.Inspection.UseVarKeywordInspector" /> + + <Inspector mimeType="text/x-csharp" + _title = "Remove redundint '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." + _description = "Removes 'private' modifiers that are not required." + severity = "Warning" + class = "MonoDevelop.CSharp.Inspection.RedundantPrivateInspector" /> </Extension> <Extension path = "/MonoDevelop/Ide/TextEditorResolver"> diff --git a/main/src/addins/CSharpBinding/CSharpBinding.csproj b/main/src/addins/CSharpBinding/CSharpBinding.csproj index b38433f4cc..9bc3405b7e 100644 --- a/main/src/addins/CSharpBinding/CSharpBinding.csproj +++ b/main/src/addins/CSharpBinding/CSharpBinding.csproj @@ -248,6 +248,8 @@ <Compile Include="MonoDevelop.CSharp.Refactoring\RefactorImports\SortImportsRefactoring.cs" /> <Compile Include="MonoDevelop.CSharp.Refactoring\HelperMethods.cs" /> <Compile Include="MonoDevelop.CSharp.Inspection\Inspectors\RedundantThisInspector.cs" /> + <Compile Include="MonoDevelop.CSharp.Inspection\Inspectors\RedundantInternalInspector.cs" /> + <Compile Include="MonoDevelop.CSharp.Inspection\Inspectors\RedundantPrivateInspector.cs" /> </ItemGroup> <ItemGroup> <None Include="ChangeLog" /> diff --git a/main/src/addins/CSharpBinding/Makefile.am b/main/src/addins/CSharpBinding/Makefile.am index 780f4534e8..cf6cd61d40 100644 --- a/main/src/addins/CSharpBinding/Makefile.am +++ b/main/src/addins/CSharpBinding/Makefile.am @@ -76,6 +76,8 @@ FILES = \ MonoDevelop.CSharp.Inspection/CSharpInspector.cs \ MonoDevelop.CSharp.Inspection/Inspectors/ConditionalToNullCoalescingInspector.cs \ MonoDevelop.CSharp.Inspection/Inspectors/NotImplementedExceptionInspector.cs \ + MonoDevelop.CSharp.Inspection/Inspectors/RedundantInternalInspector.cs \ + MonoDevelop.CSharp.Inspection/Inspectors/RedundantPrivateInspector.cs \ MonoDevelop.CSharp.Inspection/Inspectors/RedundantThisInspector.cs \ MonoDevelop.CSharp.Inspection/Inspectors/StringIsNullOrEmptyInspector.cs \ MonoDevelop.CSharp.Inspection/Inspectors/UnusedUsingInspector.cs \ diff --git a/main/src/addins/CSharpBinding/MonoDevelop.CSharp.Inspection/Inspectors/RedundantInternalInspector.cs b/main/src/addins/CSharpBinding/MonoDevelop.CSharp.Inspection/Inspectors/RedundantInternalInspector.cs new file mode 100644 index 0000000000..740ed73709 --- /dev/null +++ b/main/src/addins/CSharpBinding/MonoDevelop.CSharp.Inspection/Inspectors/RedundantInternalInspector.cs @@ -0,0 +1,59 @@ +// +// RedundantInternalInspector.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; + +namespace MonoDevelop.CSharp.Inspection +{ + public class RedundantInternalInspector : CSharpInspector + { + protected override void Attach (ObservableAstVisitor<InspectionData, object> visitor) + { + visitor.TypeDeclarationVisited += HandleTypeDeclarationVisited; + } + + void HandleTypeDeclarationVisited (TypeDeclaration type, InspectionData data) + { + if (type.Parent is TypeDeclaration) + return; + foreach (var token in type.ModifierTokens) { + if (token.Modifier == Modifiers.Internal) { + AddResult (data, + new DomRegion (token.StartLocation, token.EndLocation), + GettextCatalog.GetString ("Remove redundant 'internal' modifier"), + delegate { + int offset = data.Document.Editor.LocationToOffset (token.StartLocation); + int end = data.Document.Editor.LocationToOffset (token.GetNextNode ().StartLocation); + data.Document.Editor.Remove (offset, end - offset); + } + ); + } + } + } + } +} + diff --git a/main/src/addins/CSharpBinding/MonoDevelop.CSharp.Inspection/Inspectors/RedundantPrivateInspector.cs b/main/src/addins/CSharpBinding/MonoDevelop.CSharp.Inspection/Inspectors/RedundantPrivateInspector.cs new file mode 100644 index 0000000000..2c07670eac --- /dev/null +++ b/main/src/addins/CSharpBinding/MonoDevelop.CSharp.Inspection/Inspectors/RedundantPrivateInspector.cs @@ -0,0 +1,70 @@ +// +// RedundantPrivateInspector.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; + +namespace MonoDevelop.CSharp.Inspection +{ + public class RedundantPrivateInspector : CSharpInspector + + { + protected override void Attach (ObservableAstVisitor<InspectionData, object> visitor) + { + visitor.MethodDeclarationVisited += (node, data) => CheckNode (node, data); + visitor.FieldDeclarationVisited += (node, data) => CheckNode (node, data); + visitor.PropertyDeclarationVisited += (node, data) => CheckNode (node, data); + visitor.IndexerDeclarationVisited += (node, data) => CheckNode (node, data); + visitor.EventDeclarationVisited += (node, data) => CheckNode (node, data); + visitor.CustomEventDeclarationVisited += (node, data) => CheckNode (node, data); + visitor.ConstructorDeclarationVisited += (node, data) => CheckNode (node, data); + visitor.ConstructorDeclarationVisited += (node, data) => CheckNode (node, data); + visitor.OperatorDeclarationVisited += (node, data) => CheckNode (node, data); + visitor.FixedFieldDeclarationVisited += (node, data) => CheckNode (node, data); + visitor.TypeDeclarationVisited += delegate(TypeDeclaration node, InspectionData data) { + if (node.Parent is TypeParameterDeclaration) + CheckNode (node, data); + }; + } + + void CheckNode (AttributedNode node, InspectionData data) + { + foreach (var token in node.ModifierTokens) { + if (token.Modifier == Modifiers.Private) { + AddResult (data, + new DomRegion (token.StartLocation, token.EndLocation), + GettextCatalog.GetString ("Remove redundant 'private' modifier"), + delegate { + int offset = data.Document.Editor.LocationToOffset (token.StartLocation); + int end = data.Document.Editor.LocationToOffset (token.GetNextNode ().StartLocation); + data.Document.Editor.Remove (offset, end - offset); + } + ); + } + } + } + } +} diff --git a/main/src/addins/CSharpBinding/MonoDevelop.CSharp.Inspection/Inspectors/UnusedUsingInspector.cs b/main/src/addins/CSharpBinding/MonoDevelop.CSharp.Inspection/Inspectors/UnusedUsingInspector.cs index 3d5572b4b3..74eac4e480 100644 --- a/main/src/addins/CSharpBinding/MonoDevelop.CSharp.Inspection/Inspectors/UnusedUsingInspector.cs +++ b/main/src/addins/CSharpBinding/MonoDevelop.CSharp.Inspection/Inspectors/UnusedUsingInspector.cs @@ -23,21 +23,11 @@ // 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 ICSharpCode.NRefactory.CSharp; -using ICSharpCode.NRefactory.PatternMatching; using MonoDevelop.Core; -using MonoDevelop.AnalysisCore; -using MonoDevelop.CSharp.ContextAction; -using MonoDevelop.Ide.Gui; -using Mono.CSharp; using MonoDevelop.Refactoring; -using MonoDevelop.AnalysisCore.Fixes; -using System.Collections.Generic; using ICSharpCode.NRefactory.TypeSystem; using MonoDevelop.CSharp.Refactoring.RefactorImports; -using System.Linq; - namespace MonoDevelop.CSharp.Inspection { diff --git a/main/src/addins/CSharpBinding/MonoDevelop.CSharp.Refactoring/HelperMethods.cs b/main/src/addins/CSharpBinding/MonoDevelop.CSharp.Refactoring/HelperMethods.cs index aaa6d02913..6b4dee2b49 100644 --- a/main/src/addins/CSharpBinding/MonoDevelop.CSharp.Refactoring/HelperMethods.cs +++ b/main/src/addins/CSharpBinding/MonoDevelop.CSharp.Refactoring/HelperMethods.cs @@ -1,50 +1,56 @@ -//
-// HelperMethods.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 System;
-using Mono.TextEditor;
-using ICSharpCode.NRefactory.CSharp;
-using MonoDevelop.Refactoring;
-
-namespace MonoDevelop.CSharp.Refactoring
-{
- public static class HelperMethods
- {
- public static TextReplaceChange RemoveNode (this TextEditorData editor, AstNode n)
- {
- var change = new TextReplaceChange ();
- change.FileName = editor.FileName;
- change.Offset = editor.LocationToOffset (n.StartLocation);
- change.RemovedChars = editor.LocationToOffset (n.EndLocation) - change.Offset;
-
- // remove EOL, when line is empty
- var line = editor.GetLineByOffset (change.Offset);
- if (line != null && line.EditableLength == change.RemovedChars)
- change.RemovedChars += line.DelimiterLength;
- return change;
- }
- }
-}
-
+// +// HelperMethods.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 System; +using Mono.TextEditor; +using ICSharpCode.NRefactory.CSharp; +using MonoDevelop.Refactoring; + +namespace MonoDevelop.CSharp.Refactoring +{ + public static class HelperMethods + { + public static TextReplaceChange GetRemoveNodeChange (this TextEditorData editor, AstNode n) + { + var change = new TextReplaceChange (); + change.FileName = editor.FileName; + change.Offset = editor.LocationToOffset (n.StartLocation); + change.RemovedChars = editor.LocationToOffset (n.EndLocation) - change.Offset; + + // remove EOL, when line is empty + var line = editor.GetLineByOffset (change.Offset); + if (line != null && line.EditableLength == change.RemovedChars) + change.RemovedChars += line.DelimiterLength; + return change; + } + + public static void RemoveNode (this TextEditorData editor, AstNode n) + { + var change = editor.GetRemoveNodeChange (n); + editor.Remove (change.Offset, change.RemovedChars); + } + } +} + diff --git a/main/src/addins/CSharpBinding/MonoDevelop.CSharp.Refactoring/RefactorImports/RemoveUnusedImportsRefactoring.cs b/main/src/addins/CSharpBinding/MonoDevelop.CSharp.Refactoring/RefactorImports/RemoveUnusedImportsRefactoring.cs index f0bfeb2269..db7bb3f5b2 100644 --- a/main/src/addins/CSharpBinding/MonoDevelop.CSharp.Refactoring/RefactorImports/RemoveUnusedImportsRefactoring.cs +++ b/main/src/addins/CSharpBinding/MonoDevelop.CSharp.Refactoring/RefactorImports/RemoveUnusedImportsRefactoring.cs @@ -54,7 +54,7 @@ namespace MonoDevelop.CSharp.Refactoring.RefactorImports var visitor = new ObservableAstVisitor<object, object> (); visitor.UsingDeclarationVisited += delegate(UsingDeclaration u, object arg) { if (!nav.GetsUsed (options.Dom, resolver, u.StartLocation, u.Namespace)) - result.Add (options.Document.Editor.RemoveNode (u)); + result.Add (options.Document.Editor.GetRemoveNodeChange (u)); }; unit.AcceptVisitor (visitor); return result; |