Browse Source
Conflicts: ICSharpCode.NRefactory.CSharp/ICSharpCode.NRefactory.CSharp.csproj ICSharpCode.NRefactory.Tests/ICSharpCode.NRefactory.Tests.csprojnewNRvisualizers
50 changed files with 1482 additions and 613 deletions
@ -0,0 +1,198 @@
@@ -0,0 +1,198 @@
|
||||
//
|
||||
// IMemberProvider.cs
|
||||
//
|
||||
// Author:
|
||||
// Mike Krüger <mkrueger@xamarin.com>
|
||||
//
|
||||
// Copyright (c) 2012 Xamarin Inc.
|
||||
//
|
||||
// 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 System.Collections.Generic; |
||||
using ICSharpCode.NRefactory.TypeSystem; |
||||
using ICSharpCode.NRefactory.Editor; |
||||
using ICSharpCode.NRefactory.CSharp.TypeSystem; |
||||
using System.Linq; |
||||
using ICSharpCode.NRefactory.CSharp.Resolver; |
||||
|
||||
namespace ICSharpCode.NRefactory.CSharp.Completion |
||||
{ |
||||
public interface ICompletionContextProvider |
||||
{ |
||||
void GetCurrentMembers (int offset, out IUnresolvedTypeDefinition currentType, out IUnresolvedMember currentMember); |
||||
|
||||
Tuple<string, TextLocation> GetMemberTextToCaret(int caretOffset, IUnresolvedTypeDefinition currentType, IUnresolvedMember currentMember); |
||||
|
||||
CSharpAstResolver GetResolver (CSharpResolver resolver, AstNode rootNode); |
||||
} |
||||
|
||||
public class DefaultCompletionContextProvider : ICompletionContextProvider |
||||
{ |
||||
readonly IDocument document; |
||||
readonly CSharpParsedFile parsedFile; |
||||
|
||||
public DefaultCompletionContextProvider (IDocument document, CSharpParsedFile parsedFile) |
||||
{ |
||||
if (document == null) |
||||
throw new ArgumentNullException("document"); |
||||
if (parsedFile == null) |
||||
throw new ArgumentNullException("parsedFile"); |
||||
this.document = document; |
||||
this.parsedFile = parsedFile; |
||||
} |
||||
|
||||
public void GetCurrentMembers(int offset, out IUnresolvedTypeDefinition currentType, out IUnresolvedMember currentMember) |
||||
{ |
||||
//var document = engine.document;
|
||||
var location = document.GetLocation(offset); |
||||
|
||||
currentType = null; |
||||
|
||||
foreach (var type in parsedFile.TopLevelTypeDefinitions) { |
||||
if (type.Region.Begin < location) |
||||
currentType = type; |
||||
} |
||||
currentType = FindInnerType (currentType, location); |
||||
|
||||
// location is beyond last reported end region, now we need to check, if the end region changed
|
||||
if (currentType != null && currentType.Region.End < location) { |
||||
if (!IsInsideType (currentType, location)) |
||||
currentType = null; |
||||
} |
||||
currentMember = null; |
||||
if (currentType != null) { |
||||
foreach (var member in currentType.Members) { |
||||
if (member.Region.Begin < location && (currentMember == null || currentMember.Region.Begin < member.Region.Begin)) |
||||
currentMember = member; |
||||
} |
||||
} |
||||
|
||||
// location is beyond last reported end region, now we need to check, if the end region changed
|
||||
// NOTE: Enums are a special case, there the "last" field needs to be treated as current member
|
||||
if (currentMember != null && currentMember.Region.End < location && currentType.Kind != TypeKind.Enum) { |
||||
if (!IsInsideType (currentMember, location)) |
||||
currentMember = null; |
||||
}/* |
||||
var stack = GetBracketStack (engine.GetMemberTextToCaret ().Item1); |
||||
if (stack.Count == 0) |
||||
currentMember = null;*/ |
||||
} |
||||
|
||||
IUnresolvedTypeDefinition FindInnerType (IUnresolvedTypeDefinition parent, TextLocation location) |
||||
{ |
||||
if (parent == null) |
||||
return null; |
||||
var currentType = parent; |
||||
foreach (var type in parent.NestedTypes) { |
||||
if (type.Region.Begin < location && location < type.Region.End) |
||||
currentType = FindInnerType (type, location); |
||||
} |
||||
|
||||
return currentType; |
||||
} |
||||
|
||||
bool IsInsideType (IUnresolvedEntity currentType, TextLocation location) |
||||
{ |
||||
int startOffset = document.GetOffset (currentType.Region.Begin); |
||||
int endOffset = document.GetOffset (location); |
||||
//bool foundEndBracket = false;
|
||||
|
||||
var bracketStack = new Stack<char> (); |
||||
|
||||
bool isInString = false, isInChar = false; |
||||
bool isInLineComment = false, isInBlockComment = false; |
||||
|
||||
for (int i = startOffset; i < endOffset; i++) { |
||||
char ch = document.GetCharAt (i); |
||||
switch (ch) { |
||||
case '(': |
||||
case '[': |
||||
case '{': |
||||
if (!isInString && !isInChar && !isInLineComment && !isInBlockComment) |
||||
bracketStack.Push (ch); |
||||
break; |
||||
case ')': |
||||
case ']': |
||||
case '}': |
||||
if (!isInString && !isInChar && !isInLineComment && !isInBlockComment) |
||||
if (bracketStack.Count > 0) |
||||
bracketStack.Pop (); |
||||
break; |
||||
case '\r': |
||||
case '\n': |
||||
isInLineComment = false; |
||||
break; |
||||
case '/': |
||||
if (isInBlockComment) { |
||||
if (i > 0 && document.GetCharAt (i - 1) == '*') |
||||
isInBlockComment = false; |
||||
} else if (!isInString && !isInChar && i + 1 < document.TextLength) { |
||||
char nextChar = document.GetCharAt (i + 1); |
||||
if (nextChar == '/') |
||||
isInLineComment = true; |
||||
if (!isInLineComment && nextChar == '*') |
||||
isInBlockComment = true; |
||||
} |
||||
break; |
||||
case '"': |
||||
if (!(isInChar || isInLineComment || isInBlockComment)) |
||||
isInString = !isInString; |
||||
break; |
||||
case '\'': |
||||
if (!(isInString || isInLineComment || isInBlockComment)) |
||||
isInChar = !isInChar; |
||||
break; |
||||
default : |
||||
break; |
||||
} |
||||
} |
||||
return bracketStack.Any (t => t == '{'); |
||||
} |
||||
|
||||
public Tuple<string, TextLocation> GetMemberTextToCaret(int caretOffset, IUnresolvedTypeDefinition currentType, IUnresolvedMember currentMember) |
||||
{ |
||||
int startOffset; |
||||
if (currentMember != null && currentType != null && currentType.Kind != TypeKind.Enum) { |
||||
startOffset = document.GetOffset(currentMember.Region.Begin); |
||||
} else if (currentType != null) { |
||||
startOffset = document.GetOffset(currentType.Region.Begin); |
||||
} else { |
||||
startOffset = 0; |
||||
} |
||||
while (startOffset > 0) { |
||||
char ch = document.GetCharAt(startOffset - 1); |
||||
if (ch != ' ' && ch != '\t') { |
||||
break; |
||||
} |
||||
--startOffset; |
||||
} |
||||
|
||||
return Tuple.Create (document.GetText (startOffset, caretOffset - startOffset), document.GetLocation (startOffset)); |
||||
} |
||||
|
||||
|
||||
public CSharpAstResolver GetResolver (CSharpResolver resolver, AstNode rootNode) |
||||
{ |
||||
return new CSharpAstResolver (resolver, rootNode, parsedFile); |
||||
} |
||||
|
||||
|
||||
} |
||||
} |
||||
|
||||
@ -1,40 +0,0 @@
@@ -1,40 +0,0 @@
|
||||
//
|
||||
// IMemberProvider.cs
|
||||
//
|
||||
// Author:
|
||||
// Mike Krüger <mkrueger@xamarin.com>
|
||||
//
|
||||
// Copyright (c) 2012 Xamarin Inc.
|
||||
//
|
||||
// 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 System.Collections.Generic; |
||||
using ICSharpCode.NRefactory.TypeSystem; |
||||
using ICSharpCode.NRefactory.Editor; |
||||
using ICSharpCode.NRefactory.CSharp.TypeSystem; |
||||
using System.Linq; |
||||
|
||||
namespace ICSharpCode.NRefactory.CSharp.Completion |
||||
{ |
||||
public interface IMemberProvider |
||||
{ |
||||
void GetCurrentMembers (int offset, out IUnresolvedTypeDefinition currentType, out IUnresolvedMember currentMember); |
||||
} |
||||
} |
||||
|
||||
@ -0,0 +1,93 @@
@@ -0,0 +1,93 @@
|
||||
//
|
||||
// ExtractFieldAction.cs
|
||||
//
|
||||
// Author:
|
||||
// Nieve <>
|
||||
//
|
||||
// Copyright (c) 2012 Nieve
|
||||
//
|
||||
// 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 System.Collections.Generic; |
||||
using System.Linq; |
||||
using ICSharpCode.NRefactory.PatternMatching; |
||||
using Mono.CSharp; |
||||
|
||||
namespace ICSharpCode.NRefactory.CSharp.Refactoring |
||||
{ |
||||
[ContextAction("Extract field", Description = "Extracts a field from a local variable declaration.")] |
||||
public class ExtractFieldAction : ICodeActionProvider |
||||
{ |
||||
public IEnumerable<CodeAction> GetActions(RefactoringContext context) |
||||
{ |
||||
//TODO: implement variable assignment & ctor param
|
||||
var varInit = context.GetNode<VariableInitializer>(); |
||||
if (varInit != null) { |
||||
AstType type = varInit.GetPrevNode() as AstType; |
||||
if (type == null) yield break; |
||||
if (varInit.Parent is FieldDeclaration) yield break; |
||||
if (CannotExtractField(varInit)) yield break; |
||||
|
||||
yield return new CodeAction("Extract field", s=>{ |
||||
var name = varInit.Name; |
||||
FieldDeclaration field = new FieldDeclaration(){ |
||||
ReturnType = type.Clone(), |
||||
Variables = { new VariableInitializer(name) } |
||||
}; |
||||
AstNode nodeToRemove = RemoveDeclaration(varInit) ? varInit.Parent : type; |
||||
s.Remove(nodeToRemove, true); |
||||
s.InsertWithCursor(context.TranslateString("Extract field"),Script.InsertPosition.Before,field); |
||||
s.FormatText(varInit.Parent); |
||||
}); |
||||
} |
||||
|
||||
var idntf = context.GetNode<Identifier>(); |
||||
if (idntf == null) yield break; |
||||
var paramDec = idntf.Parent as ParameterDeclaration; |
||||
if (paramDec != null) { |
||||
var ctor = paramDec.Parent as ConstructorDeclaration; |
||||
if (ctor == null) yield break; |
||||
MemberReferenceExpression thisField = new MemberReferenceExpression(new ThisReferenceExpression(), idntf.Name, new AstType[]{}); |
||||
var assign = new AssignmentExpression(thisField, AssignmentOperatorType.Assign, new IdentifierExpression(idntf.Name)); |
||||
var statement = new ExpressionStatement(assign); |
||||
var type = (idntf.GetPrevNode() as AstType).Clone(); |
||||
FieldDeclaration field = new FieldDeclaration(){ |
||||
ReturnType = type.Clone(), |
||||
Variables = { new VariableInitializer(idntf.Name) } |
||||
}; |
||||
yield return new CodeAction("Extract field", s=>{ |
||||
s.InsertWithCursor(context.TranslateString("Extract field"),Script.InsertPosition.Before,field); |
||||
s.AddTo(ctor.Body, statement); |
||||
}); |
||||
} |
||||
} |
||||
|
||||
static bool RemoveDeclaration (VariableInitializer varInit){ |
||||
var result = varInit.Parent as VariableDeclarationStatement; |
||||
return result.Variables.First ().Initializer.IsNull; |
||||
} |
||||
|
||||
static bool CannotExtractField (VariableInitializer varInit) |
||||
{ |
||||
var result = varInit.Parent as VariableDeclarationStatement; |
||||
return result == null || result.Variables.Count != 1; |
||||
} |
||||
} |
||||
} |
||||
|
||||
@ -0,0 +1,149 @@
@@ -0,0 +1,149 @@
|
||||
//
|
||||
// CreateFieldTests.cs
|
||||
//
|
||||
// Author:
|
||||
// Nieve Goor
|
||||
//
|
||||
// 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 ExtractFieldTests : ContextActionTestBase |
||||
{ |
||||
[Test] |
||||
public void TestWrongContext1 () |
||||
{ |
||||
TestWrongContext<ExtractFieldAction> ( |
||||
"using System;" + Environment.NewLine + |
||||
"class TestClass" + Environment.NewLine + |
||||
"{" + Environment.NewLine + |
||||
" void Test ()" + Environment.NewLine + |
||||
" {" + Environment.NewLine + |
||||
" $foo = 2;" + Environment.NewLine + |
||||
" }" + Environment.NewLine + |
||||
"}" |
||||
); |
||||
} |
||||
|
||||
[Test] |
||||
public void TestWrongContext2 () |
||||
{ |
||||
TestWrongContext<ExtractFieldAction> ( |
||||
"using System;" + Environment.NewLine + |
||||
"class TestClass" + Environment.NewLine + |
||||
"{" + Environment.NewLine + |
||||
" int foo;" + Environment.NewLine + |
||||
" void Test ()" + Environment.NewLine + |
||||
" {" + Environment.NewLine + |
||||
" $foo = 2;" + Environment.NewLine + |
||||
" }" + Environment.NewLine + |
||||
"}" |
||||
); |
||||
} |
||||
|
||||
[Test] |
||||
public void TestLocalInitializer() |
||||
{ |
||||
string result = RunContextAction ( |
||||
new ExtractFieldAction (), |
||||
"using System;" + Environment.NewLine + |
||||
"class TestClass" + Environment.NewLine + |
||||
"{" + Environment.NewLine + |
||||
" void Test ()" + Environment.NewLine + |
||||
" {" + Environment.NewLine + |
||||
" int $foo = 5;" + Environment.NewLine + |
||||
" }" + Environment.NewLine + |
||||
"}" |
||||
); |
||||
Console.WriteLine (result); |
||||
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 = 5;" + Environment.NewLine + |
||||
" }" + Environment.NewLine + |
||||
"}", result); |
||||
} |
||||
|
||||
[Test] |
||||
public void TestLocalDeclaration() |
||||
{ |
||||
string result = RunContextAction ( |
||||
new ExtractFieldAction (), |
||||
"using System;" + Environment.NewLine + |
||||
"class TestClass" + Environment.NewLine + |
||||
"{" + Environment.NewLine + |
||||
" void Test ()" + Environment.NewLine + |
||||
" {" + Environment.NewLine + |
||||
" int $foo;" + Environment.NewLine + |
||||
" }" + Environment.NewLine + |
||||
"}" |
||||
); |
||||
Console.WriteLine (result); |
||||
Assert.AreEqual ( |
||||
"using System;" + Environment.NewLine + |
||||
"class TestClass" + Environment.NewLine + |
||||
"{" + Environment.NewLine + |
||||
" int foo;" + Environment.NewLine + |
||||
"" + Environment.NewLine + |
||||
" void Test ()" + Environment.NewLine + |
||||
" {" + Environment.NewLine + |
||||
" }" + Environment.NewLine + |
||||
"}", result); |
||||
} |
||||
|
||||
[Test] |
||||
public void TestCtorParam () |
||||
{ |
||||
string result = RunContextAction ( |
||||
new ExtractFieldAction (), |
||||
"using System;" + Environment.NewLine + |
||||
"class TestClass" + Environment.NewLine + |
||||
"{" + Environment.NewLine + |
||||
" TestClass (int $foo)" + Environment.NewLine + |
||||
" {" + Environment.NewLine + |
||||
" " + Environment.NewLine + |
||||
" }" + Environment.NewLine + |
||||
"}" |
||||
); |
||||
|
||||
Assert.AreEqual ( |
||||
"using System;" + Environment.NewLine + |
||||
"class TestClass" + Environment.NewLine + |
||||
"{" + Environment.NewLine + |
||||
" int foo;" + Environment.NewLine + |
||||
"" + Environment.NewLine + |
||||
" TestClass (int foo)" + Environment.NewLine + |
||||
" {" + Environment.NewLine + |
||||
" this.foo = foo;" + Environment.NewLine + |
||||
" " + Environment.NewLine + |
||||
" }" + Environment.NewLine + |
||||
"}", result); |
||||
} |
||||
} |
||||
} |
||||
|
||||
Loading…
Reference in new issue