From 53acb1556399c7b3ff5b3fe55859340676dab768 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20Kr=C3=BCger?= Date: Thu, 5 Apr 2012 17:46:00 +0200 Subject: [PATCH] * ICSharpCode.NRefactory.CSharp.csproj: * SpecializedCodeAction.cs: * ConvertDecToHexAction.cs: * GeneratePropertyAction.cs: * CheckIfParameterIsNullAction.cs: * NRefactory.sln: Added specialized code action class. --- .../ICSharpCode.NRefactory.CSharp.csproj | 1 + .../CheckIfParameterIsNullAction.cs | 29 ++++---------- .../CodeActions/ConvertDecToHexAction.cs | 13 +++---- .../CodeActions/GeneratePropertyAction.cs | 7 +--- .../CodeActions/SpecializedCodeAction.cs | 38 +++++++++++++++++++ NRefactory.sln | 8 ++++ 6 files changed, 62 insertions(+), 34 deletions(-) create mode 100644 ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/SpecializedCodeAction.cs diff --git a/ICSharpCode.NRefactory.CSharp/ICSharpCode.NRefactory.CSharp.csproj b/ICSharpCode.NRefactory.CSharp/ICSharpCode.NRefactory.CSharp.csproj index 7eab76a93d..b0a5aeccb1 100644 --- a/ICSharpCode.NRefactory.CSharp/ICSharpCode.NRefactory.CSharp.csproj +++ b/ICSharpCode.NRefactory.CSharp/ICSharpCode.NRefactory.CSharp.csproj @@ -363,6 +363,7 @@ + diff --git a/ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/CheckIfParameterIsNullAction.cs b/ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/CheckIfParameterIsNullAction.cs index 2f30111a4b..c5bb6c24c7 100644 --- a/ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/CheckIfParameterIsNullAction.cs +++ b/ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/CheckIfParameterIsNullAction.cs @@ -33,40 +33,25 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring /// Creates a 'if (param == null) throw new System.ArgumentNullException ();' contruct for a parameter. /// [ContextAction("Check if parameter is null", Description = "Checks function parameter is not null.")] - public class CheckIfParameterIsNullAction : ICodeActionProvider + public class CheckIfParameterIsNullAction : SpecializedCodeAction { - public IEnumerable GetActions(RefactoringContext context) + protected override CodeAction GetAction(RefactoringContext context, ParameterDeclaration parameter) { - var parameter = GetParameterDeclaration(context); - if (parameter == null) { - yield break; - } - var bodyStatement = parameter.Parent.GetChildByRole(Roles.Body); - - if (bodyStatement == null) { - yield break; - } - + if (bodyStatement == null) + return null; var type = context.ResolveType(parameter.Type); - if (type.IsReferenceType == false || HasNullCheck(parameter)) { - yield break; - } + if (type.IsReferenceType == false || HasNullCheck(parameter)) + return null; - yield return new CodeAction (context.TranslateString("Add null check for parameter"), script => { + return new CodeAction (context.TranslateString("Add null check for parameter"), script => { var statement = new IfElseStatement () { Condition = new BinaryOperatorExpression (new IdentifierExpression (parameter.Name), BinaryOperatorType.Equality, new NullReferenceExpression ()), TrueStatement = new ThrowStatement (new ObjectCreateExpression (context.CreateShortType("System", "ArgumentNullException"), new PrimitiveExpression (parameter.Name))) }; - script.AddTo(bodyStatement, statement); }); } - - static ParameterDeclaration GetParameterDeclaration (RefactoringContext context) - { - return context.GetNode (); - } static bool HasNullCheck (ParameterDeclaration parameter) { diff --git a/ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/ConvertDecToHexAction.cs b/ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/ConvertDecToHexAction.cs index e377f24182..cd0ebf4723 100644 --- a/ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/ConvertDecToHexAction.cs +++ b/ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/ConvertDecToHexAction.cs @@ -37,16 +37,15 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring public IEnumerable GetActions(RefactoringContext context) { var pexpr = context.GetNode(); - if (pexpr == null || pexpr.LiteralValue.StartsWith("0X", System.StringComparison.OrdinalIgnoreCase)) { + if (pexpr == null || pexpr.LiteralValue.StartsWith("0X", System.StringComparison.OrdinalIgnoreCase)) yield break; - } - if (!((pexpr.Value is int) || (pexpr.Value is long) || (pexpr.Value is short) || (pexpr.Value is sbyte) || - (pexpr.Value is uint) || (pexpr.Value is ulong) || (pexpr.Value is ushort) || (pexpr.Value is byte))) { + + var value = pexpr.Value; + if (value is string) yield break; - } - + yield return new CodeAction(context.TranslateString("Convert dec to hex"), script => { - script.Replace(pexpr, new PrimitiveExpression (pexpr.Value, string.Format("0x{0:x}", pexpr.Value))); + script.Replace(pexpr, new PrimitiveExpression (value, string.Format("0x{0:x}", value))); }); } } diff --git a/ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/GeneratePropertyAction.cs b/ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/GeneratePropertyAction.cs index 079b3df6dc..1f5c6381f1 100644 --- a/ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/GeneratePropertyAction.cs +++ b/ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/GeneratePropertyAction.cs @@ -36,7 +36,7 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring { public IEnumerable GetActions(RefactoringContext context) { - var initializer = GetVariableInitializer(context); + var initializer = context.GetNode(); if (initializer == null || !initializer.NameToken.Contains(context.Location.Line, context.Location.Column)) { yield break; } @@ -92,10 +92,7 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring ret.Expression.IsMatch (new MemberReferenceExpression (new ThisReferenceExpression (), initializer.Name)); } - VariableInitializer GetVariableInitializer (RefactoringContext context) - { - return context.GetNode (); - } + } } diff --git a/ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/SpecializedCodeAction.cs b/ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/SpecializedCodeAction.cs new file mode 100644 index 0000000000..2d204e1b1f --- /dev/null +++ b/ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/SpecializedCodeAction.cs @@ -0,0 +1,38 @@ +using System; + +namespace ICSharpCode.NRefactory.CSharp.Refactoring +{ + /// + /// A specialized code action creates a code action assoziated with one special type of ast nodes. + /// + public abstract class SpecializedCodeAction : ICodeActionProvider where T : AstNode + { + /// + /// Gets the action for the specified ast node. + /// + /// + /// The code action. May return null, if no action can be provided. + /// + /// + /// The refactoring conext. + /// + /// + /// The AstNode it's ensured that the node is always != null, if called. + /// + protected abstract CodeAction GetAction(RefactoringContext context, T node); + + #region ICodeActionProvider implementation + public System.Collections.Generic.IEnumerable GetActions(RefactoringContext context) + { + var node = context.GetNode(); + if (node == null) + yield break; + var action = GetAction(context, node); + if (action == null) + yield break; + yield return action; + } + #endregion + } +} + diff --git a/NRefactory.sln b/NRefactory.sln index 08a5c6549e..bfa04b3f1d 100644 --- a/NRefactory.sln +++ b/NRefactory.sln @@ -122,6 +122,14 @@ Global EndGlobalSection GlobalSection(MonoDevelopProperties) = preSolution StartupItem = ICSharpCode.NRefactory.Demo\ICSharpCode.NRefactory.Demo.csproj + Policies = $0 + $0.TextStylePolicy = $1 + $1.FileWidth = 120 + $1.TabWidth = 4 + $1.EolMarker = Unix + $1.inheritsSet = Mono + $1.inheritsScope = text/plain + $1.scope = text/plain EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE