diff --git a/ICSharpCode.NRefactory.CSharp/Refactoring/BaseRefactoringContext.cs b/ICSharpCode.NRefactory.CSharp/Refactoring/BaseRefactoringContext.cs index 2e199715cf..242c9c3b9d 100644 --- a/ICSharpCode.NRefactory.CSharp/Refactoring/BaseRefactoringContext.cs +++ b/ICSharpCode.NRefactory.CSharp/Refactoring/BaseRefactoringContext.cs @@ -46,6 +46,14 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring { return true; } + + /// + /// Gets a value indicating if 'var' keyword should be used or explicit types. + /// + public virtual bool UseExplicitTypes { + get; + set; + } public CancellationToken CancellationToken { get { return cancellationToken; } diff --git a/ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/CreateLocalVariableAction.cs b/ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/CreateLocalVariableAction.cs index e53d68418a..5911c115e8 100644 --- a/ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/CreateLocalVariableAction.cs +++ b/ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/CreateLocalVariableAction.cs @@ -59,7 +59,9 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring Variables = { initializer } }; 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); } else { script.InsertBefore(statement, decl); diff --git a/ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/DeclareLocalVariableAction.cs b/ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/DeclareLocalVariableAction.cs index b224cf7a85..d64fedfe8d 100644 --- a/ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/DeclareLocalVariableAction.cs +++ b/ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/DeclareLocalVariableAction.cs @@ -61,7 +61,8 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring guessedType = GetDelegateType(context, ((MethodGroupResolveResult)resolveResult).Methods.First(), expr); } 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) { script.Replace(expr.Parent, varDecl); script.Select(varDecl.Variables.First().NameToken); @@ -87,7 +88,8 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring } var linkedNodes = new List(); 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); var first = visitor.Matches [0]; if (first.Parent is ExpressionStatement) { diff --git a/ICSharpCode.NRefactory.CSharp/Refactoring/RefactoringContext.cs b/ICSharpCode.NRefactory.CSharp/Refactoring/RefactoringContext.cs index fb1a0262e5..d149983b6b 100644 --- a/ICSharpCode.NRefactory.CSharp/Refactoring/RefactoringContext.cs +++ b/ICSharpCode.NRefactory.CSharp/Refactoring/RefactoringContext.cs @@ -39,8 +39,6 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring { public abstract class RefactoringContext : BaseRefactoringContext { - - public RefactoringContext(CSharpAstResolver resolver, CancellationToken cancellationToken) : base (resolver, cancellationToken) { } diff --git a/ICSharpCode.NRefactory.Tests/CSharp/CodeActions/CreateLocalVariableTests.cs b/ICSharpCode.NRefactory.Tests/CSharp/CodeActions/CreateLocalVariableTests.cs index ec63425c8e..a2684c764b 100644 --- a/ICSharpCode.NRefactory.Tests/CSharp/CodeActions/CreateLocalVariableTests.cs +++ b/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 ( "using System;" + Environment.NewLine + "class TestClass" + Environment.NewLine + diff --git a/ICSharpCode.NRefactory.Tests/CSharp/CodeActions/DeclareLocalVariableTests.cs b/ICSharpCode.NRefactory.Tests/CSharp/CodeActions/DeclareLocalVariableTests.cs index b7446c2b3f..e93b5f2395 100644 --- a/ICSharpCode.NRefactory.Tests/CSharp/CodeActions/DeclareLocalVariableTests.cs +++ b/ICSharpCode.NRefactory.Tests/CSharp/CodeActions/DeclareLocalVariableTests.cs @@ -36,6 +36,7 @@ namespace ICSharpCode.NRefactory.CSharp.CodeActions [Test()] public void TestSimpleInline () { + TestRefactoringContext.UseExplict = true; Test (@"class TestClass { int Foo() {} @@ -52,10 +53,30 @@ namespace ICSharpCode.NRefactory.CSharp.CodeActions } }"); } + [Test()] + public void TestSimpleInlineImplicit () + { + Test (@"class TestClass +{ + int Foo() {} + void Test () + { + <-Foo()->; + } +}", @"class TestClass +{ + int Foo() {} + void Test () + { + var i = Foo (); + } +}"); + } [Test()] public void TestReplaceAll () { + TestRefactoringContext.UseExplict = true; Test (@"class TestClass { void Test () @@ -76,9 +97,33 @@ namespace ICSharpCode.NRefactory.CSharp.CodeActions }", 1); } + [Test()] + public void TestReplaceAllImplicit () + { + Test (@"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()] public void DeclareLocalExpressionTest () { + TestRefactoringContext.UseExplict = true; Test (@"class TestClass { void Test () @@ -103,6 +148,7 @@ namespace ICSharpCode.NRefactory.CSharp.CodeActions [Test()] public void TestBug693855 () { + TestRefactoringContext.UseExplict = true; Test (@"class TestClass { void Test () @@ -141,6 +187,7 @@ namespace ICSharpCode.NRefactory.CSharp.CodeActions [Test()] public void TestBug693875 () { + TestRefactoringContext.UseExplict = true; Test (@"class TestClass { void DoStuff() diff --git a/ICSharpCode.NRefactory.Tests/CSharp/CodeActions/TestRefactoringContext.cs b/ICSharpCode.NRefactory.Tests/CSharp/CodeActions/TestRefactoringContext.cs index 676f7608bf..0809d9770e 100644 --- a/ICSharpCode.NRefactory.Tests/CSharp/CodeActions/TestRefactoringContext.cs +++ b/ICSharpCode.NRefactory.Tests/CSharp/CodeActions/TestRefactoringContext.cs @@ -41,6 +41,11 @@ namespace ICSharpCode.NRefactory.CSharp.CodeActions { public class TestRefactoringContext : RefactoringContext { + public static bool UseExplict { + get; + set; + } + internal readonly IDocument doc; readonly TextLocation location; @@ -48,6 +53,8 @@ namespace ICSharpCode.NRefactory.CSharp.CodeActions { this.doc = document; this.location = location; + this.UseExplicitTypes = UseExplict; + UseExplict = false; Services.AddService (typeof(NamingConventionService), new TestNameService ()); }