Browse Source

* ICSharpCode.NRefactory.CSharp.csproj:

* SpecializedCodeAction.cs:
* ConvertDecToHexAction.cs:
* GeneratePropertyAction.cs:
* CheckIfParameterIsNullAction.cs: 

* NRefactory.sln: Added specialized code action class.
newNRvisualizers
Mike Krüger 14 years ago
parent
commit
53acb15563
  1. 1
      ICSharpCode.NRefactory.CSharp/ICSharpCode.NRefactory.CSharp.csproj
  2. 29
      ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/CheckIfParameterIsNullAction.cs
  3. 13
      ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/ConvertDecToHexAction.cs
  4. 7
      ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/GeneratePropertyAction.cs
  5. 38
      ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/SpecializedCodeAction.cs
  6. 8
      NRefactory.sln

1
ICSharpCode.NRefactory.CSharp/ICSharpCode.NRefactory.CSharp.csproj

@ -363,6 +363,7 @@ @@ -363,6 +363,7 @@
<Compile Include="Refactoring\CodeActions\ExtractMethod\ExtractMethodAction.cs" />
<Compile Include="Refactoring\CodeActions\ExtractMethod\StaticVisitor.cs" />
<Compile Include="Refactoring\CodeActions\ExtractMethod\VariableLookupVisitor.cs" />
<Compile Include="Refactoring\CodeActions\SpecializedCodeAction.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ICSharpCode.NRefactory\ICSharpCode.NRefactory.csproj">

29
ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/CheckIfParameterIsNullAction.cs

@ -33,40 +33,25 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -33,40 +33,25 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
/// Creates a 'if (param == null) throw new System.ArgumentNullException ();' contruct for a parameter.
/// </summary>
[ContextAction("Check if parameter is null", Description = "Checks function parameter is not null.")]
public class CheckIfParameterIsNullAction : ICodeActionProvider
public class CheckIfParameterIsNullAction : SpecializedCodeAction<ParameterDeclaration>
{
public IEnumerable<CodeAction> 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<ICSharpCode.NRefactory.CSharp.ParameterDeclaration> ();
}
static bool HasNullCheck (ParameterDeclaration parameter)
{

13
ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/ConvertDecToHexAction.cs

@ -37,16 +37,15 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -37,16 +37,15 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
public IEnumerable<CodeAction> GetActions(RefactoringContext context)
{
var pexpr = context.GetNode<PrimitiveExpression>();
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)));
});
}
}

7
ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/GeneratePropertyAction.cs

@ -36,7 +36,7 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -36,7 +36,7 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
{
public IEnumerable<CodeAction> GetActions(RefactoringContext context)
{
var initializer = GetVariableInitializer(context);
var initializer = context.GetNode<VariableInitializer>();
if (initializer == null || !initializer.NameToken.Contains(context.Location.Line, context.Location.Column)) {
yield break;
}
@ -92,10 +92,7 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -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<VariableInitializer> ();
}
}
}

38
ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/SpecializedCodeAction.cs

@ -0,0 +1,38 @@ @@ -0,0 +1,38 @@
using System;
namespace ICSharpCode.NRefactory.CSharp.Refactoring
{
/// <summary>
/// A specialized code action creates a code action assoziated with one special type of ast nodes.
/// </summary>
public abstract class SpecializedCodeAction<T> : ICodeActionProvider where T : AstNode
{
/// <summary>
/// Gets the action for the specified ast node.
/// </summary>
/// <returns>
/// The code action. May return <c>null</c>, if no action can be provided.
/// </returns>
/// <param name='context'>
/// The refactoring conext.
/// </param>
/// <param name='node'>
/// The AstNode it's ensured that the node is always != null, if called.
/// </param>
protected abstract CodeAction GetAction(RefactoringContext context, T node);
#region ICodeActionProvider implementation
public System.Collections.Generic.IEnumerable<CodeAction> GetActions(RefactoringContext context)
{
var node = context.GetNode<T>();
if (node == null)
yield break;
var action = GetAction(context, node);
if (action == null)
yield break;
yield return action;
}
#endregion
}
}

8
NRefactory.sln

@ -122,6 +122,14 @@ Global @@ -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

Loading…
Cancel
Save