diff --git a/ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/CreateFieldAction.cs b/ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/CreateFieldAction.cs index 8fd138316c..158bf4a780 100644 --- a/ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/CreateFieldAction.cs +++ b/ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/CreateFieldAction.cs @@ -176,6 +176,36 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring internal static IEnumerable GetValidTypes(CSharpAstResolver resolver, AstNode expr) { + if (expr.Parent is IfElseStatement) { + var parent = ((IfElseStatement)expr.Parent); + if (parent.Condition == expr) + return new [] { resolver.Compilation.FindType (KnownTypeCode.Boolean) }; + } + + if (expr.Parent is WhileStatement) { + var parent = ((WhileStatement)expr.Parent); + if (parent.Condition == expr) + return new [] { resolver.Compilation.FindType (KnownTypeCode.Boolean) }; + } + + if (expr.Parent is ConditionalExpression) { + var parent = ((ConditionalExpression)expr.Parent); + if (parent.Condition == expr) + return new [] { resolver.Compilation.FindType (KnownTypeCode.Boolean) }; + } + + if (expr.Parent is DoWhileStatement) { + var parent = ((DoWhileStatement)expr.Parent); + if (parent.Condition == expr) + return new [] { resolver.Compilation.FindType (KnownTypeCode.Boolean) }; + } + + if (expr.Parent is ForStatement) { + var parent = ((ForStatement)expr.Parent); + if (parent.Condition == expr) + return new [] { resolver.Compilation.FindType (KnownTypeCode.Boolean) }; + } + if (expr.Parent is DirectionExpression) { var parent = expr.Parent.Parent; if (parent is InvocationExpression) { @@ -186,14 +216,14 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring if (expr.Parent is ArrayInitializerExpression) { if (expr is NamedExpression) - return new [] { resolver.Resolve(((NamedExpression)expr).Expression).Type }; + return new [] { resolver.Resolve(((NamedExpression)expr).Expression).Type }; var aex = expr.Parent as ArrayInitializerExpression; if (aex.IsSingleElement) aex = aex.Parent as ArrayInitializerExpression; var type = GetElementType(resolver, resolver.Resolve(aex.Parent).Type); if (type.Kind != TypeKind.Unknown) - return new [] { type }; + return new [] { type }; } if (expr.Parent is ObjectCreateExpression) { diff --git a/ICSharpCode.NRefactory.Tests/CSharp/CodeActions/CreateFieldTests.cs b/ICSharpCode.NRefactory.Tests/CSharp/CodeActions/CreateFieldTests.cs index 33a6bfabb7..ca1639bdcb 100644 --- a/ICSharpCode.NRefactory.Tests/CSharp/CodeActions/CreateFieldTests.cs +++ b/ICSharpCode.NRefactory.Tests/CSharp/CodeActions/CreateFieldTests.cs @@ -257,5 +257,106 @@ class Foo }"); } + [Test] + public void TestIf () + { + Test (@"class TestClass +{ + void TestMethod () + { + if ($NonExistantProperty) + ; + } +}", @"class TestClass +{ + bool NonExistantProperty; + void TestMethod () + { + if (NonExistantProperty) + ; + } +}"); + } + + [Test] + public void TestWhile () + { + Test (@"class TestClass +{ + void TestMethod () + { + while ($NonExistantProperty) + ; + } +}", @"class TestClass +{ + bool NonExistantProperty; + void TestMethod () + { + while (NonExistantProperty) + ; + } +}"); + } + + [Test] + public void TestDoWhile () + { + Test (@"class TestClass +{ + void TestMethod () + { + do {} + while ($NonExistantProperty); + } +}", @"class TestClass +{ + bool NonExistantProperty; + void TestMethod () + { + do {} + while (NonExistantProperty); + } +}"); + } + + [Test] + public void TestForCondition () + { + Test (@"class TestClass +{ + void TestMethod () + { + for (;$NonExistantProperty;){} + } +}", @"class TestClass +{ + bool NonExistantProperty; + void TestMethod () + { + for (;NonExistantProperty;){} + } +}"); + } + + [Test] + public void TestConditionalOperator () + { + Test (@"class TestClass +{ + void TestMethod () + { + var b = $NonExistantProperty ? 1 : 0; + } +}", @"class TestClass +{ + bool NonExistantProperty; + void TestMethod () + { + var b = NonExistantProperty ? 1 : 0; + } +}"); + } + } } \ No newline at end of file