Browse Source

Improved type guessing.

newNRvisualizers
Mike Krüger 14 years ago
parent
commit
b4b3e6454f
  1. 16
      ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/CreateFieldAction.cs
  2. 39
      ICSharpCode.NRefactory.Tests/CSharp/CodeActions/CreateLocalVariableTests.cs

16
ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/CreateFieldAction.cs

@ -195,11 +195,25 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -195,11 +195,25 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
if (state != null && (state.CurrentMember.ReturnType is ParameterizedType)) {
var pt = (ParameterizedType)state.CurrentMember.ReturnType;
if (pt.FullName == "System.Collections.Generic.IEnumerable") {
return new [] { pt.TypeArguments.First () };
return new [] { pt.TypeArguments.First() };
}
}
}
if (expr.Parent is UnaryOperatorExpression) {
var uop = (UnaryOperatorExpression)expr.Parent;
switch (uop.Operator) {
case UnaryOperatorType.Not:
return new [] { context.Compilation.FindType(KnownTypeCode.Boolean) };
case UnaryOperatorType.Minus:
case UnaryOperatorType.Plus:
case UnaryOperatorType.Increment:
case UnaryOperatorType.Decrement:
case UnaryOperatorType.PostIncrement:
case UnaryOperatorType.PostDecrement:
return new [] { context.Compilation.FindType(KnownTypeCode.Int32) };
}
}
return Enumerable.Empty<IType>();
}
static readonly IType[] emptyTypes = new IType[0];

39
ICSharpCode.NRefactory.Tests/CSharp/CodeActions/CreateLocalVariableTests.cs

@ -200,20 +200,41 @@ namespace ICSharpCode.NRefactory.CSharp.CodeActions @@ -200,20 +200,41 @@ namespace ICSharpCode.NRefactory.CSharp.CodeActions
[Test()]
public void TestWrongContext1 ()
public void TestWrongContext1()
{
// May be syntactically possible, but very unlikely.
TestWrongContext<CreateLocalVariableAction> (
TestWrongContext<CreateLocalVariableAction>(
"using System;" + Environment.NewLine +
"class TestClass" + Environment.NewLine +
"{" + Environment.NewLine +
" void Test ()" + Environment.NewLine +
" {" + Environment.NewLine +
" $foo();" + Environment.NewLine +
" }" + Environment.NewLine +
"}"
"class TestClass" + Environment.NewLine +
"{" + Environment.NewLine +
" void Test ()" + Environment.NewLine +
" {" + Environment.NewLine +
" $foo();" + Environment.NewLine +
" }" + Environment.NewLine +
"}"
);
}
[Test()]
public void TestBool()
{
Test<CreateLocalVariableAction>(
@"class TestClass
{
void Test ()
{
object o = !$foo;
}
}",
@"class TestClass
{
void Test ()
{
bool foo;
object o = !foo;
}
}");
}
}
}
Loading…
Cancel
Save