Browse Source

Added flag to specify if explict types should be used or not.

newNRvisualizers
Mike Krüger 14 years ago
parent
commit
93fb7777b5
  1. 8
      ICSharpCode.NRefactory.CSharp/Refactoring/BaseRefactoringContext.cs
  2. 4
      ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/CreateLocalVariableAction.cs
  3. 6
      ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/DeclareLocalVariableAction.cs
  4. 2
      ICSharpCode.NRefactory.CSharp/Refactoring/RefactoringContext.cs
  5. 27
      ICSharpCode.NRefactory.Tests/CSharp/CodeActions/CreateLocalVariableTests.cs
  6. 47
      ICSharpCode.NRefactory.Tests/CSharp/CodeActions/DeclareLocalVariableTests.cs
  7. 7
      ICSharpCode.NRefactory.Tests/CSharp/CodeActions/TestRefactoringContext.cs

8
ICSharpCode.NRefactory.CSharp/Refactoring/BaseRefactoringContext.cs

@ -46,6 +46,14 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
{ {
return true; return true;
} }
/// <summary>
/// Gets a value indicating if 'var' keyword should be used or explicit types.
/// </summary>
public virtual bool UseExplicitTypes {
get;
set;
}
public CancellationToken CancellationToken { public CancellationToken CancellationToken {
get { return cancellationToken; } get { return cancellationToken; }

4
ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/CreateLocalVariableAction.cs

@ -59,7 +59,9 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
Variables = { initializer } Variables = { initializer }
}; };
if (identifier.Parent is AssignmentExpression && ((AssignmentExpression)identifier.Parent).Left == identifier) { if (identifier.Parent is AssignmentExpression && ((AssignmentExpression)identifier.Parent).Left == identifier) {
initializer.Initializer = ((AssignmentExpression)identifier.Parent).Right.Clone (); initializer.Initializer = ((AssignmentExpression)identifier.Parent).Right.Clone();
if (!context.UseExplicitTypes)
decl.Type = new SimpleType("var");
script.Replace(statement, decl); script.Replace(statement, decl);
} else { } else {
script.InsertBefore(statement, decl); script.InsertBefore(statement, decl);

6
ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/DeclareLocalVariableAction.cs

@ -61,7 +61,8 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
guessedType = GetDelegateType(context, ((MethodGroupResolveResult)resolveResult).Methods.First(), expr); guessedType = GetDelegateType(context, ((MethodGroupResolveResult)resolveResult).Methods.First(), expr);
} }
var name = CreateMethodDeclarationAction.CreateBaseName(expr, guessedType); var name = CreateMethodDeclarationAction.CreateBaseName(expr, guessedType);
var varDecl = new VariableDeclarationStatement(context.CreateShortType(guessedType), name, expr.Clone()); var type = context.UseExplicitTypes ? context.CreateShortType(guessedType) : new SimpleType("var");
var varDecl = new VariableDeclarationStatement(type, name, expr.Clone());
if (expr.Parent is ExpressionStatement) { if (expr.Parent is ExpressionStatement) {
script.Replace(expr.Parent, varDecl); script.Replace(expr.Parent, varDecl);
script.Select(varDecl.Variables.First().NameToken); script.Select(varDecl.Variables.First().NameToken);
@ -87,7 +88,8 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
} }
var linkedNodes = new List<AstNode>(); var linkedNodes = new List<AstNode>();
var name = CreateMethodDeclarationAction.CreateBaseName(expr, guessedType); var name = CreateMethodDeclarationAction.CreateBaseName(expr, guessedType);
var varDecl = new VariableDeclarationStatement(context.CreateShortType(guessedType), name, expr.Clone()); var type = context.UseExplicitTypes ? context.CreateShortType(guessedType) : new SimpleType("var");
var varDecl = new VariableDeclarationStatement(type, name, expr.Clone());
linkedNodes.Add(varDecl.Variables.First().NameToken); linkedNodes.Add(varDecl.Variables.First().NameToken);
var first = visitor.Matches [0]; var first = visitor.Matches [0];
if (first.Parent is ExpressionStatement) { if (first.Parent is ExpressionStatement) {

2
ICSharpCode.NRefactory.CSharp/Refactoring/RefactoringContext.cs

@ -39,8 +39,6 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
{ {
public abstract class RefactoringContext : BaseRefactoringContext public abstract class RefactoringContext : BaseRefactoringContext
{ {
public RefactoringContext(CSharpAstResolver resolver, CancellationToken cancellationToken) : base (resolver, cancellationToken) public RefactoringContext(CSharpAstResolver resolver, CancellationToken cancellationToken) : base (resolver, cancellationToken)
{ {
} }

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

@ -74,6 +74,33 @@ namespace ICSharpCode.NRefactory.CSharp.CodeActions
"}" "}"
); );
Assert.AreEqual (
"using System;" + Environment.NewLine +
"class TestClass" + Environment.NewLine +
"{" + Environment.NewLine +
" void Test ()" + Environment.NewLine +
" {" + Environment.NewLine +
" var foo = 0x10;" + Environment.NewLine +
" }" + Environment.NewLine +
"}", result);
}
[Test()]
public void ExplicitTestAssignment ()
{
TestRefactoringContext.UseExplict = true;
string result = RunContextAction (
new CreateLocalVariableAction (),
"using System;" + Environment.NewLine +
"class TestClass" + Environment.NewLine +
"{" + Environment.NewLine +
" void Test ()" + Environment.NewLine +
" {" + Environment.NewLine +
" $foo = 0x10;" + Environment.NewLine +
" }" + Environment.NewLine +
"}"
);
Assert.AreEqual ( Assert.AreEqual (
"using System;" + Environment.NewLine + "using System;" + Environment.NewLine +
"class TestClass" + Environment.NewLine + "class TestClass" + Environment.NewLine +

47
ICSharpCode.NRefactory.Tests/CSharp/CodeActions/DeclareLocalVariableTests.cs

@ -36,6 +36,7 @@ namespace ICSharpCode.NRefactory.CSharp.CodeActions
[Test()] [Test()]
public void TestSimpleInline () public void TestSimpleInline ()
{ {
TestRefactoringContext.UseExplict = true;
Test<DeclareLocalVariableAction> (@"class TestClass Test<DeclareLocalVariableAction> (@"class TestClass
{ {
int Foo() {} int Foo() {}
@ -52,10 +53,30 @@ namespace ICSharpCode.NRefactory.CSharp.CodeActions
} }
}"); }");
} }
[Test()]
public void TestSimpleInlineImplicit ()
{
Test<DeclareLocalVariableAction> (@"class TestClass
{
int Foo() {}
void Test ()
{
<-Foo()->;
}
}", @"class TestClass
{
int Foo() {}
void Test ()
{
var i = Foo ();
}
}");
}
[Test()] [Test()]
public void TestReplaceAll () public void TestReplaceAll ()
{ {
TestRefactoringContext.UseExplict = true;
Test<DeclareLocalVariableAction> (@"class TestClass Test<DeclareLocalVariableAction> (@"class TestClass
{ {
void Test () void Test ()
@ -76,9 +97,33 @@ namespace ICSharpCode.NRefactory.CSharp.CodeActions
}", 1); }", 1);
} }
[Test()]
public void TestReplaceAllImplicit ()
{
Test<DeclareLocalVariableAction> (@"class TestClass
{
void Test ()
{
Console.WriteLine (<-5 + 3->);
Console.WriteLine (5 + 3);
Console.WriteLine (5 + 3);
}
}", @"class TestClass
{
void Test ()
{
var i = 5 + 3;
Console.WriteLine (i);
Console.WriteLine (i);
Console.WriteLine (i);
}
}", 1);
}
[Test()] [Test()]
public void DeclareLocalExpressionTest () public void DeclareLocalExpressionTest ()
{ {
TestRefactoringContext.UseExplict = true;
Test<DeclareLocalVariableAction> (@"class TestClass Test<DeclareLocalVariableAction> (@"class TestClass
{ {
void Test () void Test ()
@ -103,6 +148,7 @@ namespace ICSharpCode.NRefactory.CSharp.CodeActions
[Test()] [Test()]
public void TestBug693855 () public void TestBug693855 ()
{ {
TestRefactoringContext.UseExplict = true;
Test<DeclareLocalVariableAction> (@"class TestClass Test<DeclareLocalVariableAction> (@"class TestClass
{ {
void Test () void Test ()
@ -141,6 +187,7 @@ namespace ICSharpCode.NRefactory.CSharp.CodeActions
[Test()] [Test()]
public void TestBug693875 () public void TestBug693875 ()
{ {
TestRefactoringContext.UseExplict = true;
Test<DeclareLocalVariableAction> (@"class TestClass Test<DeclareLocalVariableAction> (@"class TestClass
{ {
void DoStuff() void DoStuff()

7
ICSharpCode.NRefactory.Tests/CSharp/CodeActions/TestRefactoringContext.cs

@ -41,6 +41,11 @@ namespace ICSharpCode.NRefactory.CSharp.CodeActions
{ {
public class TestRefactoringContext : RefactoringContext public class TestRefactoringContext : RefactoringContext
{ {
public static bool UseExplict {
get;
set;
}
internal readonly IDocument doc; internal readonly IDocument doc;
readonly TextLocation location; readonly TextLocation location;
@ -48,6 +53,8 @@ namespace ICSharpCode.NRefactory.CSharp.CodeActions
{ {
this.doc = document; this.doc = document;
this.location = location; this.location = location;
this.UseExplicitTypes = UseExplict;
UseExplict = false;
Services.AddService (typeof(NamingConventionService), new TestNameService ()); Services.AddService (typeof(NamingConventionService), new TestNameService ());
} }

Loading…
Cancel
Save