Browse Source

Fixed create field, local and property actions.

newNRvisualizers
Mike Krüger 14 years ago
parent
commit
0c6ea8083c
  1. 119
      ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/CreateFieldAction.cs
  2. 102
      ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/CreateLocalVariableAction.cs
  3. 39
      ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/CreatePropertyAction.cs
  4. 121
      ICSharpCode.NRefactory.Tests/CSharp/CodeActions/CreateFieldTests.cs
  5. 117
      ICSharpCode.NRefactory.Tests/CSharp/CodeActions/CreateLocalVariableTests.cs
  6. 126
      ICSharpCode.NRefactory.Tests/CSharp/CodeActions/CreatePropertyTests.cs
  7. 25
      ICSharpCode.NRefactory.Tests/CSharp/CodeActions/TestRefactoringContext.cs
  8. 3
      ICSharpCode.NRefactory.Tests/ICSharpCode.NRefactory.Tests.csproj

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

@ -30,6 +30,7 @@ using System.Linq; @@ -30,6 +30,7 @@ using System.Linq;
using ICSharpCode.NRefactory.TypeSystem;
using System.Threading;
using System.Collections.Generic;
using ICSharpCode.NRefactory.CSharp.Resolver;
namespace ICSharpCode.NRefactory.CSharp.Refactoring
{
@ -38,43 +39,117 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -38,43 +39,117 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
{
public IEnumerable<CodeAction> GetActions(RefactoringContext context)
{
var identifier = GetIdentifier(context);
var identifier = context.GetNode<IdentifierExpression>();
if (identifier == null) {
yield break;
}
if (!(context.Resolve(identifier).IsError && GuessType(context, identifier) != null)) {
var statement = context.GetNode<Statement>();
if (statement == null) {
yield break;
}
yield return new CodeAction (context.TranslateString("Create field"), script => {
script.InsertWithCursor(context.TranslateString("Create field"), GenerateFieldDeclaration(context, identifier), Script.InsertPosition.Before);
if (!(context.Resolve(identifier).IsError)) {
yield break;
}
var guessedType = CreateFieldAction.GuessType(context, identifier);
if (guessedType == null) {
yield break;
}
yield return new CodeAction(context.TranslateString("Create field"), script => {
var decl = new FieldDeclaration() {
ReturnType = guessedType,
Variables = { new VariableInitializer(identifier.Identifier) }
};
script.InsertWithCursor(context.TranslateString("Create field"), decl, Script.InsertPosition.Before);
});
}
static AstNode GenerateFieldDeclaration (RefactoringContext context, IdentifierExpression identifier)
#region Type guessing
static int GetArgumentIndex(InvocationExpression invoke, AstNode parameter)
{
return new FieldDeclaration () {
ReturnType = GuessType (context, identifier),
Variables = { new VariableInitializer (identifier.Identifier) }
};
int argumentNumber = 0;
foreach (var arg in invoke.Arguments) {
if (arg == parameter) {
return argumentNumber;
}
argumentNumber++;
}
return -1;
}
internal static AstType GuessType (RefactoringContext context, IdentifierExpression identifier)
static IEnumerable<IType> GetAllValidTypesFromInvokation(RefactoringContext context, InvocationExpression invoke, AstNode parameter)
{
if (identifier.Parent is AssignmentExpression) {
var assign = (AssignmentExpression)identifier.Parent;
var other = assign.Left == identifier ? assign.Right : assign.Left;
return context.CreateShortType (context.Resolve (other).Type);
int index = GetArgumentIndex(invoke, parameter);
if (index < 0) {
yield break;
}
var targetResult = context.Resolve(invoke.Target);
if (targetResult is MethodGroupResolveResult) {
foreach (var method in ((MethodGroupResolveResult)targetResult).Methods) {
if (index < method.Parameters.Count) {
yield return method.Parameters [index].Type;
}
}
}
return null;
}
public static IdentifierExpression GetIdentifier (RefactoringContext context)
internal static IEnumerable<IType> GetValidTypes(RefactoringContext context, Expression expr)
{
return context.GetNode<IdentifierExpression> ();
if (expr.Parent is DirectionExpression) {
var parent = expr.Parent.Parent;
if (parent is InvocationExpression) {
var invoke = (InvocationExpression)parent;
return GetAllValidTypesFromInvokation(context, invoke, expr.Parent);
}
}
if (expr.Parent is InvocationExpression) {
var parent = expr.Parent;
if (parent is InvocationExpression) {
var invoke = (InvocationExpression)parent;
return GetAllValidTypesFromInvokation(context, invoke, expr);
}
}
if (expr.Parent is AssignmentExpression) {
var assign = (AssignmentExpression)expr.Parent;
var other = assign.Left == expr ? assign.Right : assign.Left;
return new [] { context.Resolve(other).Type };
}
if (expr.Parent is BinaryOperatorExpression) {
var assign = (BinaryOperatorExpression)expr.Parent;
var other = assign.Left == expr ? assign.Right : assign.Left;
return new [] { context.Resolve(other).Type };
}
return Enumerable.Empty<IType>();
}
internal static AstType GuessType(RefactoringContext context, IdentifierExpression identifier)
{
IType type = null;
foreach (var t in GetValidTypes(context, identifier)) {
if (type == null || type.GetAllBaseTypes().Contains(t)) {
type = t;
continue;
}
if (!t.GetAllBaseTypes().Contains(type)) {
type = null;
break;
}
}
if (type == null) {
return new PrimitiveType("object");
}
return context.CreateShortType (type);
}
#endregion
}
}

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

@ -23,107 +23,45 @@ @@ -23,107 +23,45 @@
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using System;
using ICSharpCode.NRefactory.PatternMatching;
using System.Linq;
using System.Collections.Generic;
using ICSharpCode.NRefactory.TypeSystem;
using System.Threading;
namespace ICSharpCode.NRefactory.CSharp.Refactoring
{
[ContextAction("Create local variable", Description = "Creates a local variable for a undefined variable.")]
public class CreateLocalVariableAction : ICodeActionProvider
{
public List<IdentifierExpression> GetUnresolvedArguments (RefactoringContext context)
{
var expressions = new List<IdentifierExpression> ();
var invocation = GetInvocation (context);
if (invocation != null) {
foreach (var arg in invocation.Arguments) {
IdentifierExpression identifier;
if (arg is DirectionExpression) {
identifier = ((DirectionExpression)arg).Expression as IdentifierExpression;
} else if (arg is NamedArgumentExpression) {
identifier = ((NamedArgumentExpression)arg).Expression as IdentifierExpression;
} else {
identifier = arg as IdentifierExpression;
}
if (identifier == null)
continue;
if (context.Resolve (identifier) == null && GuessType (context, identifier) != null)
expressions.Insert (0, identifier);
}
}
return expressions;
}
public IEnumerable<CodeAction> GetActions(RefactoringContext context)
{
if (GetUnresolvedArguments(context).Count <= 0) {
var identifier = context.GetNode<IdentifierExpression>();
if (identifier == null) {
yield break;
}
var identifier = CreateFieldAction.GetIdentifier(context);
if (identifier == null) {
var statement = context.GetNode<Statement>();
if (statement == null) {
yield break;
}
if (context.GetNode<Statement>() == null) {
if (!(context.Resolve(identifier).IsError)) {
yield break;
}
if (!(context.Resolve(identifier).IsError && GuessType(context, identifier) != null)) {
var guessedType = CreateFieldAction.GuessType(context, identifier);
if (guessedType == null) {
yield break;
}
yield return new CodeAction (context.TranslateString("Create local variable"), script => {
var stmt = context.GetNode<Statement> ();
var unresolvedArguments = GetUnresolvedArguments (context);
if (unresolvedArguments.Count > 0) {
foreach (var id in unresolvedArguments) {
script.InsertBefore (stmt, GenerateLocalVariableDeclaration (context, id));
}
return;
}
script.InsertBefore (stmt, GenerateLocalVariableDeclaration (context, CreateFieldAction.GetIdentifier (context)));
});
}
AstNode GenerateLocalVariableDeclaration (RefactoringContext context, IdentifierExpression identifier)
{
return new VariableDeclarationStatement () {
Type = GuessType (context, identifier),
Variables = { new VariableInitializer (identifier.Identifier) }
};
}
InvocationExpression GetInvocation (RefactoringContext context)
{
return context.GetNode<InvocationExpression> ();
}
AstType GuessType (RefactoringContext context, IdentifierExpression identifier)
{
var type = CreateFieldAction.GuessType (context, identifier);
if (type != null)
return type;
if (identifier != null && (identifier.Parent is InvocationExpression || identifier.Parent.Parent is InvocationExpression)) {
var invocation = (identifier.Parent as InvocationExpression) ?? (identifier.Parent.Parent as InvocationExpression);
var result = context.Resolve (invocation).Type.GetDelegateInvokeMethod ();
if (result == null)
return null;
int i = 0;
foreach (var arg in invocation.Arguments) {
if (arg.Contains (identifier.StartLocation))
break;
i++;
yield return new CodeAction(context.TranslateString("Create local variable"), script => {
var initializer = new VariableInitializer(identifier.Identifier);
var decl = new VariableDeclarationStatement() {
Type = guessedType,
Variables = { initializer }
};
if (identifier.Parent is AssignmentExpression && ((AssignmentExpression)identifier.Parent).Left == identifier) {
initializer.Initializer = ((AssignmentExpression)identifier.Parent).Right.Clone ();
script.Replace(statement, decl);
} else {
script.InsertBefore(statement, decl);
}
if (result.Parameters.Count < i)
return null;
return context.CreateShortType (result.Parameters[i].Type);
}
return null;
});
}
}
}

39
ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/CreatePropertyAction.cs

@ -36,32 +36,33 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -36,32 +36,33 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
{
public IEnumerable<CodeAction> GetActions(RefactoringContext context)
{
var identifier = CreateFieldAction.GetIdentifier(context);
var identifier = context.GetNode<IdentifierExpression>();
if (identifier == null) {
yield break;
}
if (!(context.Resolve(identifier).IsError && CreateFieldAction.GuessType(context, identifier) != null)) {
var statement = context.GetNode<Statement>();
if (statement == null) {
yield break;
}
yield return new CodeAction (context.TranslateString("Create property"), script => {
script.InsertWithCursor(context.TranslateString("Create property"), GeneratePropertyDeclaration(context, identifier), Script.InsertPosition.Before);
if (!(context.Resolve(identifier).IsError)) {
yield break;
}
var guessedType = CreateFieldAction.GuessType(context, identifier);
if (guessedType == null) {
yield break;
}
yield return new CodeAction(context.TranslateString("Create property"), script => {
var decl = new PropertyDeclaration() {
ReturnType = guessedType,
Name = identifier.Identifier,
Getter = new Accessor(),
Setter = new Accessor()
};
script.InsertWithCursor(context.TranslateString("Create property"), decl, Script.InsertPosition.Before);
});
}
AstNode GeneratePropertyDeclaration (RefactoringContext context, IdentifierExpression identifier)
{
return new PropertyDeclaration () {
ReturnType = CreateFieldAction.GuessType (context, identifier),
Name = identifier.Identifier,
Getter = new Accessor (),
Setter = new Accessor ()
};
}
IdentifierExpression GetIdentifier (RefactoringContext context)
{
return context.GetNode<IdentifierExpression> ();
}
}
}

121
ICSharpCode.NRefactory.Tests/CSharp/CodeActions/CreateFieldTests.cs

@ -0,0 +1,121 @@ @@ -0,0 +1,121 @@
//
// CreateFieldTests.cs
//
// Author:
// Mike Krüger <mkrueger@xamarin.com>
//
// Copyright (c) 2012 Xamarin <http://xamarin.com>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using System;
using NUnit.Framework;
using ICSharpCode.NRefactory.CSharp.Refactoring;
namespace ICSharpCode.NRefactory.CSharp.CodeActions
{
[TestFixture]
public class CreateFieldTests : ContextActionTestBase
{
[Test()]
public void TestSimpleMethodCall ()
{
string result = RunContextAction (
new CreateFieldAction (),
"using System;" + Environment.NewLine +
"class TestClass" + Environment.NewLine +
"{" + Environment.NewLine +
" void Test ()" + Environment.NewLine +
" {" + Environment.NewLine +
" Console.WriteLine ($foo);" + Environment.NewLine +
" }" + Environment.NewLine +
"}"
);
Console.WriteLine (result);
Assert.AreEqual (
"using System;" + Environment.NewLine +
"class TestClass" + Environment.NewLine +
"{" + Environment.NewLine +
" object foo;" + Environment.NewLine +
"" + Environment.NewLine +
" void Test ()" + Environment.NewLine +
" {" + Environment.NewLine +
" Console.WriteLine (foo);" + Environment.NewLine +
" }" + Environment.NewLine +
"}", result);
}
[Test()]
public void TestAssignment ()
{
string result = RunContextAction (
new CreateFieldAction (),
"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 +
"{" + Environment.NewLine +
" int foo;" + Environment.NewLine +
"" + Environment.NewLine +
" void Test ()" + Environment.NewLine +
" {" + Environment.NewLine +
" foo = 0x10;" + Environment.NewLine +
" }" + Environment.NewLine +
"}", result);
}
[Test()]
public void TestOutParamCall ()
{
string result = RunContextAction (
new CreateFieldAction (),
"using System;" + Environment.NewLine +
"class TestClass" + Environment.NewLine +
"{" + Environment.NewLine +
" void FooBar(out string par) {}" + Environment.NewLine +
" void Test ()" + Environment.NewLine +
" {" + Environment.NewLine +
" FooBar(out $foo);" + Environment.NewLine +
" }" + Environment.NewLine +
"}"
);
Assert.AreEqual (
"using System;" + Environment.NewLine +
"class TestClass" + Environment.NewLine +
"{" + Environment.NewLine +
" void FooBar(out string par) {}" + Environment.NewLine +
" string foo;" + Environment.NewLine +
"" + Environment.NewLine +
" void Test ()" + Environment.NewLine +
" {" + Environment.NewLine +
" FooBar(out foo);" + Environment.NewLine +
" }" + Environment.NewLine +
"}", result);
}
}
}

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

@ -0,0 +1,117 @@ @@ -0,0 +1,117 @@
//
// CreateLocalVariableTests.cs
//
// Author:
// Mike Krüger <mkrueger@xamarin.com>
//
// Copyright (c) 2012 Xamarin <http://xamarin.com>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using System;
using NUnit.Framework;
using ICSharpCode.NRefactory.CSharp.Refactoring;
namespace ICSharpCode.NRefactory.CSharp.CodeActions
{
[TestFixture]
public class CreateLocalVariableTests : ContextActionTestBase
{
[Test()]
public void TestSimpleMethodCall ()
{
string result = RunContextAction (
new CreateLocalVariableAction (),
"using System;" + Environment.NewLine +
"class TestClass" + Environment.NewLine +
"{" + Environment.NewLine +
" void Test ()" + Environment.NewLine +
" {" + Environment.NewLine +
" Console.WriteLine ($foo);" + Environment.NewLine +
" }" + Environment.NewLine +
"}"
);
Assert.AreEqual (
"using System;" + Environment.NewLine +
"class TestClass" + Environment.NewLine +
"{" + Environment.NewLine +
" void Test ()" + Environment.NewLine +
" {" + Environment.NewLine +
" object foo;" + Environment.NewLine +
" Console.WriteLine (foo);" + Environment.NewLine +
" }" + Environment.NewLine +
"}", result);
}
[Test()]
public void TestAssignment ()
{
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 +
"{" + Environment.NewLine +
" void Test ()" + Environment.NewLine +
" {" + Environment.NewLine +
" int foo = 0x10;" + Environment.NewLine +
" }" + Environment.NewLine +
"}", result);
}
[Test()]
public void TestOutParamCall ()
{
string result = RunContextAction (
new CreateLocalVariableAction (),
"using System;" + Environment.NewLine +
"class TestClass" + Environment.NewLine +
"{" + Environment.NewLine +
" void FooBar(out string par) {}" + Environment.NewLine +
" void Test ()" + Environment.NewLine +
" {" + Environment.NewLine +
" FooBar(out $foo);" + Environment.NewLine +
" }" + Environment.NewLine +
"}"
);
Assert.AreEqual (
"using System;" + Environment.NewLine +
"class TestClass" + Environment.NewLine +
"{" + Environment.NewLine +
" void FooBar(out string par) {}" + Environment.NewLine +
" void Test ()" + Environment.NewLine +
" {" + Environment.NewLine +
" string foo;" + Environment.NewLine +
" FooBar(out foo);" + Environment.NewLine +
" }" + Environment.NewLine +
"}", result);
}
}
}

126
ICSharpCode.NRefactory.Tests/CSharp/CodeActions/CreatePropertyTests.cs

@ -0,0 +1,126 @@ @@ -0,0 +1,126 @@
//
// CreatePropertyTests.cs
//
// Author:
// Mike Krüger <mkrueger@xamarin.com>
//
// Copyright (c) 2012 Xamarin <http://xamarin.com>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using System;
using NUnit.Framework;
using ICSharpCode.NRefactory.CSharp.Refactoring;
namespace ICSharpCode.NRefactory.CSharp.CodeActions
{
[TestFixture]
public class CreatePropertyTests : ContextActionTestBase
{
[Test()]
public void TestSimpleMethodCall ()
{
string result = RunContextAction (
new CreatePropertyAction (),
"using System;" + Environment.NewLine +
"class TestClass" + Environment.NewLine +
"{" + Environment.NewLine +
" void Test ()" + Environment.NewLine +
" {" + Environment.NewLine +
" Console.WriteLine ($foo);" + Environment.NewLine +
" }" + Environment.NewLine +
"}"
);
Assert.AreEqual (
"using System;" + Environment.NewLine +
"class TestClass" + Environment.NewLine +
"{" + Environment.NewLine +
" object foo {" + Environment.NewLine +
" get;" + Environment.NewLine +
" set;" + Environment.NewLine +
" }" + Environment.NewLine +
" void Test ()" + Environment.NewLine +
" {" + Environment.NewLine +
" Console.WriteLine (foo);" + Environment.NewLine +
" }" + Environment.NewLine +
"}", result);
}
[Test()]
public void TestAssignment ()
{
string result = RunContextAction (
new CreatePropertyAction (),
"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 +
"{" + Environment.NewLine +
" int foo {" + Environment.NewLine +
" get;" + Environment.NewLine +
" set;" + Environment.NewLine +
" }" + Environment.NewLine +
" void Test ()" + Environment.NewLine +
" {" + Environment.NewLine +
" foo = 0x10;" + Environment.NewLine +
" }" + Environment.NewLine +
"}", result);
}
[Test()]
public void TestOutParamCall ()
{
string result = RunContextAction (
new CreatePropertyAction (),
"using System;" + Environment.NewLine +
"class TestClass" + Environment.NewLine +
"{" + Environment.NewLine +
" void FooBar(out string par) {}" + Environment.NewLine +
" void Test ()" + Environment.NewLine +
" {" + Environment.NewLine +
" FooBar(out $foo);" + Environment.NewLine +
" }" + Environment.NewLine +
"}"
);
Assert.AreEqual (
"using System;" + Environment.NewLine +
"class TestClass" + Environment.NewLine +
"{" + Environment.NewLine +
" void FooBar(out string par) {}" + Environment.NewLine +
" string foo {" + Environment.NewLine +
" get;" + Environment.NewLine +
" set;" + Environment.NewLine +
" }" + Environment.NewLine +
" void Test ()" + Environment.NewLine +
" {" + Environment.NewLine +
" FooBar(out foo);" + Environment.NewLine +
" }" + Environment.NewLine +
"}", result);
}
}
}

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

@ -66,8 +66,10 @@ namespace ICSharpCode.NRefactory.CSharp.CodeActions @@ -66,8 +66,10 @@ namespace ICSharpCode.NRefactory.CSharp.CodeActions
sealed class TestScript : DocumentScript
{
readonly TestRefactoringContext context;
public TestScript(TestRefactoringContext context) : base(context.doc, new CSharpFormattingOptions())
{
this.context = context;
this.eolMarker = context.EolMarker;
}
@ -78,6 +80,12 @@ namespace ICSharpCode.NRefactory.CSharp.CodeActions @@ -78,6 +80,12 @@ namespace ICSharpCode.NRefactory.CSharp.CodeActions
Assert.IsNotNull (GetSegment (node));
}
}
public override void InsertWithCursor (string operation, AstNode node, InsertPosition defaultPosition)
{
var entity = context.GetNode<EntityDeclaration> ();
InsertBefore (entity, node);
}
}
public override T RequestData<T> ()
@ -132,7 +140,7 @@ namespace ICSharpCode.NRefactory.CSharp.CodeActions @@ -132,7 +140,7 @@ namespace ICSharpCode.NRefactory.CSharp.CodeActions
return doc.Text;
}
}
public static TestRefactoringContext Create(string content)
public static TestRefactoringContext Create (string content)
{
int idx = content.IndexOf ("$");
if (idx >= 0)
@ -156,18 +164,19 @@ namespace ICSharpCode.NRefactory.CSharp.CodeActions @@ -156,18 +164,19 @@ namespace ICSharpCode.NRefactory.CSharp.CodeActions
if (parser.HasErrors)
parser.ErrorPrinter.Errors.ForEach (e => Console.WriteLine (e.Message));
Assert.IsFalse (parser.HasErrors, "File contains parsing errors.");
unit.Freeze();
var parsedFile = unit.ToTypeSystem();
unit.Freeze ();
var parsedFile = unit.ToTypeSystem ();
IProjectContent pc = new CSharpProjectContent();
pc = pc.UpdateProjectContent(null, parsedFile);
pc = pc.AddAssemblyReferences(new[] { CecilLoaderTests.Mscorlib, CecilLoaderTests.SystemCore });
IProjectContent pc = new CSharpProjectContent ();
pc = pc.UpdateProjectContent (null, parsedFile);
pc = pc.AddAssemblyReferences (new[] { CecilLoaderTests.Mscorlib, CecilLoaderTests.SystemCore });
var compilation = pc.CreateCompilation();
var resolver = new CSharpAstResolver(compilation, unit, parsedFile);
var compilation = pc.CreateCompilation ();
var resolver = new CSharpAstResolver (compilation, unit, parsedFile);
TextLocation location = TextLocation.Empty;
if (idx >= 0)
location = doc.GetLocation (idx);
Console.WriteLine ("idx:" + location);
return new TestRefactoringContext(doc, location, resolver) {
selectionStart = selectionStart,
selectionEnd = selectionEnd

3
ICSharpCode.NRefactory.Tests/ICSharpCode.NRefactory.Tests.csproj

@ -249,6 +249,9 @@ @@ -249,6 +249,9 @@
<Compile Include="CSharp\CodeActions\GeneratePropertyTests.cs" />
<Compile Include="CSharp\Inspector\InconsistentNamingIssueTests.cs" />
<Compile Include="CSharp\CodeIssues\InconsistentNamingTests.cs" />
<Compile Include="CSharp\CodeActions\CreateLocalVariableTests.cs" />
<Compile Include="CSharp\CodeActions\CreateFieldTests.cs" />
<Compile Include="CSharp\CodeActions\CreatePropertyTests.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\Mono.Cecil\Mono.Cecil.csproj">

Loading…
Cancel
Save