Browse Source

Merge remote-tracking branch 'upstream/master' into mansheng

newNRvisualizers
Mansheng Yang 14 years ago
parent
commit
aa59c76669
  1. 2
      ICSharpCode.NRefactory.CSharp/Formatter/AstFormattingVisitor.cs
  2. 3
      ICSharpCode.NRefactory.CSharp/ICSharpCode.NRefactory.CSharp.csproj
  3. 77
      ICSharpCode.NRefactory.CSharp/Parser/CSharpParser.cs
  4. 2
      ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/CreatePropertyAction.cs
  5. 93
      ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/ExtractFieldAction.cs
  6. 2
      ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/UseExplicitTypeAction.cs
  7. 43
      ICSharpCode.NRefactory.CSharp/Refactoring/CodeIssues/InconsistentNamingIssue/DefaultRules.cs
  8. 2
      ICSharpCode.NRefactory.ConsistencyCheck/CSharpProject.cs
  9. 2
      ICSharpCode.NRefactory.Demo/CSDemo.cs
  10. 149
      ICSharpCode.NRefactory.Tests/CSharp/CodeActions/ExtractFieldTests.cs
  11. 6
      ICSharpCode.NRefactory.Tests/CSharp/CodeActions/TestRefactoringContext.cs
  12. 106
      ICSharpCode.NRefactory.Tests/CSharp/CodeIssues/InconsistentNamingTests.cs
  13. 20
      ICSharpCode.NRefactory.Tests/CSharp/Parser/ParseUtil.cs
  14. 34
      ICSharpCode.NRefactory.Tests/FormattingTests/TestWrapping.cs
  15. 3
      ICSharpCode.NRefactory.Tests/ICSharpCode.NRefactory.Tests.csproj
  16. 1
      ICSharpCode.NRefactory.Tests/TypeSystem/CecilLoaderTests.cs

2
ICSharpCode.NRefactory.CSharp/Formatter/AstFormattingVisitor.cs

@ -1822,7 +1822,7 @@ namespace ICSharpCode.NRefactory.CSharp @@ -1822,7 +1822,7 @@ namespace ICSharpCode.NRefactory.CSharp
if (methodCallArgumentWrapping == Wrapping.DoNotWrap) {
ForceSpacesBeforeRemoveNewLines(rParToken, spaceWithinMethodCallParentheses);
} else {
bool sameLine = rParToken.GetPrevNode().StartLocation.Line == rParToken.StartLocation.Line;
bool sameLine = rParToken.GetPrevNode().EndLocation.Line == rParToken.StartLocation.Line;
if (sameLine) {
ForceSpacesBeforeRemoveNewLines(rParToken, spaceWithinMethodCallParentheses);
} else {

3
ICSharpCode.NRefactory.CSharp/ICSharpCode.NRefactory.CSharp.csproj

@ -1,4 +1,4 @@ @@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Build">
<PropertyGroup>
<ProjectGuid>{53DCA265-3C3C-42F9-B647-F72BA678122B}</ProjectGuid>
@ -387,6 +387,7 @@ @@ -387,6 +387,7 @@
<Compile Include="Refactoring\CodeActions\ImplementInterfaceAction.cs" />
<Compile Include="Refactoring\CodeActions\ImplementInterfaceExplicitAction.cs" />
<Compile Include="Refactoring\CodeActions\ImplementAbstractMembersAction.cs" />
<Compile Include="Refactoring\CodeActions\ExtractFieldAction.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ICSharpCode.NRefactory\ICSharpCode.NRefactory.csproj">

77
ICSharpCode.NRefactory.CSharp/Parser/CSharpParser.cs

@ -177,7 +177,7 @@ namespace ICSharpCode.NRefactory.CSharp @@ -177,7 +177,7 @@ namespace ICSharpCode.NRefactory.CSharp
}
return result;
}
AstType ConvertToType (Mono.CSharp.Expression typeName)
{
if (typeName == null) // may happen in typeof(Generic<,,,,>)
@ -190,7 +190,7 @@ namespace ICSharpCode.NRefactory.CSharp @@ -190,7 +190,7 @@ namespace ICSharpCode.NRefactory.CSharp
if (typeName is Mono.CSharp.QualifiedAliasMember) {
var qam = (Mono.CSharp.QualifiedAliasMember)typeName;
var memberType = new MemberType ();
var memberType = new MemberType ();
memberType.Target = new SimpleType (qam.alias, Convert (qam.Location));
memberType.IsDoubleColon = true;
memberType.MemberName = qam.Name;
@ -267,7 +267,7 @@ namespace ICSharpCode.NRefactory.CSharp @@ -267,7 +267,7 @@ namespace ICSharpCode.NRefactory.CSharp
int pos = 0;
if (loc != null)
result.AddChild (new CSharpTokenNode (Convert (loc [pos++])), Roles.LPar);
if (attr.PositionalArguments != null) {
foreach (var arg in attr.PositionalArguments) {
var na = arg as NamedArgument;
@ -289,7 +289,7 @@ namespace ICSharpCode.NRefactory.CSharp @@ -289,7 +289,7 @@ namespace ICSharpCode.NRefactory.CSharp
result.AddChild (new CSharpTokenNode (Convert (loc [pos++])), Roles.Comma);
}
}
if (attr.NamedArguments != null) {
if (attr.NamedArguments != null) {
foreach (NamedArgument na in attr.NamedArguments) {
var newArg = new NamedExpression ();
newArg.AddChild (Identifier.Create (na.Name, Convert (na.Location)), Roles.Identifier);
@ -387,13 +387,13 @@ namespace ICSharpCode.NRefactory.CSharp @@ -387,13 +387,13 @@ namespace ICSharpCode.NRefactory.CSharp
}
// public override void Visit (UsingsBag.Namespace nspace)
// {
//
//
//
//
// VisitNamespaceUsings (nspace);
// VisitNamespaceBody (nspace);
//
//
// }
//
//
void ConvertNamespaceName (MemberName memberName, NamespaceDeclaration namespaceDecl)
{
AstNode insertPos = null;
@ -439,7 +439,7 @@ namespace ICSharpCode.NRefactory.CSharp @@ -439,7 +439,7 @@ namespace ICSharpCode.NRefactory.CSharp
ud.AddChild (new CSharpTokenNode (Convert (loc [1])), Roles.Semicolon);
AddToNamespace (ud);
}
public override void Visit (UsingExternAlias uea)
{
var ud = new ExternAliasDeclaration ();
@ -755,7 +755,7 @@ namespace ICSharpCode.NRefactory.CSharp @@ -755,7 +755,7 @@ namespace ICSharpCode.NRefactory.CSharp
var bracketLocations = LocationsBag.GetLocations (f.Initializer);
if (bracketLocations != null && bracketLocations.Count > 1)
variable.AddChild (new CSharpTokenNode (Convert (bracketLocations [0])), Roles.LBracket);
variable.AddChild ((Expression)f.Initializer.Accept (this), Roles.Expression);
if (bracketLocations != null && bracketLocations.Count > 1)
variable.AddChild (new CSharpTokenNode (Convert (bracketLocations [0])), Roles.RBracket);
@ -1062,7 +1062,7 @@ namespace ICSharpCode.NRefactory.CSharp @@ -1062,7 +1062,7 @@ namespace ICSharpCode.NRefactory.CSharp
modifierTable [Mono.CSharp.Modifiers.ASYNC] = ICSharpCode.NRefactory.CSharp.Modifiers.Async;
keywordTable = new string[255];
for (int i = 0; i< keywordTable.Length; i++)
for (int i = 0; i< keywordTable.Length; i++)
keywordTable [i] = "unknown";
keywordTable [(int)BuiltinTypeSpec.Type.Other] = "void";
@ -2118,7 +2118,7 @@ namespace ICSharpCode.NRefactory.CSharp @@ -2118,7 +2118,7 @@ namespace ICSharpCode.NRefactory.CSharp
result.AddChild (new CSharpTokenNode (Convert (memberAccess.DotLocation)), Roles.Dot);
}
}
result.AddChild (Identifier.Create (memberAccess.Name, Convert (memberAccess.Location)), Roles.Identifier);
AddTypeArguments (result, memberAccess);
@ -2137,7 +2137,7 @@ namespace ICSharpCode.NRefactory.CSharp @@ -2137,7 +2137,7 @@ namespace ICSharpCode.NRefactory.CSharp
public override object Visit (Constant constant)
{
if (constant.GetValue () == null)
if (constant.GetValue () == null)
return new NullReferenceExpression (Convert (constant.Location));
string literalValue;
if (constant is ILiteralConstant) {
@ -2699,7 +2699,7 @@ namespace ICSharpCode.NRefactory.CSharp @@ -2699,7 +2699,7 @@ namespace ICSharpCode.NRefactory.CSharp
if (par == null)
continue;
var parLocation = LocationsBag.GetLocations (par);
if (parLocation == null) {
if (par.Expr != null)
result.AddChild ((Expression)par.Expr.Accept (this), Roles.Expression);
@ -2718,16 +2718,16 @@ namespace ICSharpCode.NRefactory.CSharp @@ -2718,16 +2718,16 @@ namespace ICSharpCode.NRefactory.CSharp
return result;
}
ArrayInitializerExpression ConvertCollectionOrObjectInitializers(CollectionOrObjectInitializers minit)
{
if (minit == null)
return null;
var init = new ArrayInitializerExpression();
AddConvertCollectionOrObjectInitializers(init, minit);
return init;
}
ArrayInitializerExpression ConvertCollectionOrObjectInitializers(CollectionOrObjectInitializers minit)
{
if (minit == null)
return null;
var init = new ArrayInitializerExpression();
AddConvertCollectionOrObjectInitializers(init, minit);
return init;
}
void AddConvertCollectionOrObjectInitializers(Expression init, CollectionOrObjectInitializers minit)
void AddConvertCollectionOrObjectInitializers(Expression init, CollectionOrObjectInitializers minit)
{
var initLoc = LocationsBag.GetLocations(minit);
var commaLoc = LocationsBag.GetLocations(minit.Initializers);
@ -2738,12 +2738,12 @@ namespace ICSharpCode.NRefactory.CSharp @@ -2738,12 +2738,12 @@ namespace ICSharpCode.NRefactory.CSharp
var collectionInit = expr as CollectionElementInitializer;
if (collectionInit != null) {
AstNode parent;
// For ease of use purposes in the resolver the ast representation
// For ease of use purposes in the resolver the ast representation
// of { a, b, c } is { {a}, {b}, {c} } - but the generated ArrayInitializerExpression
// can be identified by expr.IsSingleElement.
if (!collectionInit.IsSingle) {
parent = new ArrayInitializerExpression();
parent.AddChild(new CSharpTokenNode(Convert(collectionInit.Location)), Roles.LBrace);
parent.AddChild(new CSharpTokenNode(Convert(collectionInit.Location)), Roles.LBrace);
} else {
parent = ArrayInitializerExpression.CreateSingleElementInitializer ();
}
@ -2832,7 +2832,7 @@ namespace ICSharpCode.NRefactory.CSharp @@ -2832,7 +2832,7 @@ namespace ICSharpCode.NRefactory.CSharp
result.AddChild (new CSharpTokenNode (Convert (arrayCreationExpression.Location)), ArrayCreateExpression.NewKeywordRole);
if (arrayCreationExpression.TypeExpression != null)
result.AddChild (ConvertToType (arrayCreationExpression.TypeExpression), Roles.Type);
var next = arrayCreationExpression.Rank;
if (arrayCreationExpression.Arguments != null) {
// skip first array rank.
@ -3518,8 +3518,8 @@ namespace ICSharpCode.NRefactory.CSharp @@ -3518,8 +3518,8 @@ namespace ICSharpCode.NRefactory.CSharp
var start = new TextLocation (comment.Line, comment.Col);
var end = new TextLocation (comment.EndLine, comment.EndCol);
newLeaf = new Comment (type, start, end) {
StartsLine = comment.StartsLine,
Content = isMultilineDocumentationComment ? comment.Content.Substring(1) : comment.Content
StartsLine = comment.StartsLine,
Content = isMultilineDocumentationComment ? comment.Content.Substring(1) : comment.Content
};
} else {
var directive = special as SpecialsBag.PreProcessorDirective;
@ -3598,6 +3598,7 @@ namespace ICSharpCode.NRefactory.CSharp @@ -3598,6 +3598,7 @@ namespace ICSharpCode.NRefactory.CSharp
}
ErrorReportPrinter errorReportPrinter = new ErrorReportPrinter (null);
[Obsolete("Use the Errors/Warnings/ErrorsAndWarnings properties instead")]
public ErrorReportPrinter ErrorPrinter {
get {
return errorReportPrinter;
@ -3616,6 +3617,22 @@ namespace ICSharpCode.NRefactory.CSharp @@ -3616,6 +3617,22 @@ namespace ICSharpCode.NRefactory.CSharp
}
}
public IEnumerable<Error> Errors {
get {
return errorReportPrinter.Errors.Where(e => e.ErrorType == ErrorType.Error);
}
}
public IEnumerable<Error> Warnings {
get {
return errorReportPrinter.Errors.Where(e => e.ErrorType == ErrorType.Warning);
}
}
public IEnumerable<Error> ErrorsAndWarnings {
get { return errorReportPrinter.Errors; }
}
public CompilationUnit Parse (ITextSource textSource, string fileName, int lineModifier = 0)
{
return Parse (textSource.CreateReader (), fileName, lineModifier);
@ -3689,12 +3706,12 @@ namespace ICSharpCode.NRefactory.CSharp @@ -3689,12 +3706,12 @@ namespace ICSharpCode.NRefactory.CSharp
var module = new ModuleContainer (ctx);
var parser = Driver.Parse (reader, file, module, lineModifier);
var top = new CompilerCompilationUnit () {
var top = new CompilerCompilationUnit () {
ModuleCompiled = module,
LocationsBag = parser.LocationsBag,
SpecialsBag = parser.Lexer.sbag
};
var unit = Parse (top, fileName, lineModifier);
var unit = Parse (top, fileName, lineModifier);
unit.Errors.AddRange (errorReportPrinter.Errors);
CompilerCallableEntryPoint.Reset ();
return unit;

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

@ -63,6 +63,8 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -63,6 +63,8 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
ResolveResult targetResolveResult = null;
if (identifier is MemberReferenceExpression) {
targetResolveResult = context.Resolve(((MemberReferenceExpression)identifier).Target);
if (targetResolveResult.Type.GetDefinition() == null || targetResolveResult.Type.GetDefinition().Region.IsEmpty)
yield break;
createInOtherType = !state.CurrentTypeDefinition.Equals(targetResolveResult.Type.GetDefinition());
}

93
ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/ExtractFieldAction.cs

@ -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;
}
}
}

2
ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/UseExplicitTypeAction.cs

@ -52,7 +52,7 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -52,7 +52,7 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
if (!(!type.Equals(SpecialType.NullType) && !type.Equals(SpecialType.UnknownType))) {
yield break;
}
yield return new CodeAction (context.TranslateString("Use expcicit type"), script => {
yield return new CodeAction (context.TranslateString("Use explicit type"), script => {
if (varDecl != null) {
script.Replace (varDecl.Type, context.CreateShortType (type));
} else {

43
ICSharpCode.NRefactory.CSharp/Refactoring/CodeIssues/InconsistentNamingIssue/DefaultRules.cs

@ -39,24 +39,28 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -39,24 +39,28 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
yield return new NamingRule(AffectedEntity.Class | AffectedEntity.Struct | AffectedEntity.Enum | AffectedEntity.Delegate) {
Name = "Types",
VisibilityMask = Modifiers.Public,
NamingStyle = NamingStyle.PascalCase
};
yield return new NamingRule(AffectedEntity.Interface) {
Name = "Interfaces",
NamingStyle = NamingStyle.PascalCase,
VisibilityMask = Modifiers.Public,
RequiredPrefixes = new [] { "I" }
};
yield return new NamingRule(AffectedEntity.CustomAttributes) {
Name = "Attributes",
NamingStyle = NamingStyle.PascalCase,
VisibilityMask = Modifiers.Public,
RequiredSuffixes = new [] { "Attribute" }
};
yield return new NamingRule(AffectedEntity.CustomEventArgs) {
Name = "Event Arguments",
NamingStyle = NamingStyle.PascalCase,
VisibilityMask = Modifiers.Public,
RequiredSuffixes = new [] { "EventArgs" }
};
@ -68,66 +72,45 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -68,66 +72,45 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
yield return new NamingRule(AffectedEntity.Methods) {
Name = "Methods",
VisibilityMask = Modifiers.Public | Modifiers.Protected,
NamingStyle = NamingStyle.PascalCase
};
yield return new NamingRule(AffectedEntity.ReadonlyField) {
Name = "Static Readonly Fields",
VisibilityMask = Modifiers.Public | Modifiers.Protected | Modifiers.Internal,
VisibilityMask = Modifiers.Public | Modifiers.Protected,
NamingStyle = NamingStyle.PascalCase,
IncludeInstanceMembers = false
};
yield return new NamingRule(AffectedEntity.Field) {
Name = "Fields (Non Private)",
Name = "Fields",
NamingStyle = NamingStyle.PascalCase,
VisibilityMask = Modifiers.Public | Modifiers.Protected | Modifiers.Internal
VisibilityMask = Modifiers.Public | Modifiers.Protected
};
yield return new NamingRule(AffectedEntity.ReadonlyField) {
Name = "ReadOnly Fields (Non Private)",
Name = "ReadOnly Fields",
NamingStyle = NamingStyle.PascalCase,
VisibilityMask = Modifiers.Public | Modifiers.Protected | Modifiers.Internal,
IncludeStaticEntities = false
};
yield return new NamingRule(AffectedEntity.Field | AffectedEntity.ReadonlyField) {
Name = "Fields (Private)",
NamingStyle = NamingStyle.CamelCase,
AllowedPrefixes = new [] { "_", "m_" },
VisibilityMask = Modifiers.Private,
IncludeStaticEntities = false
};
yield return new NamingRule(AffectedEntity.Field) {
Name = "Static Fields (Private)",
NamingStyle = NamingStyle.CamelCase,
VisibilityMask = Modifiers.Private,
IncludeStaticEntities = true,
IncludeInstanceMembers = false
};
yield return new NamingRule(AffectedEntity.ReadonlyField) {
Name = "ReadOnly Fields (Private)",
NamingStyle = NamingStyle.CamelCase,
VisibilityMask = Modifiers.Private,
AllowedPrefixes = new [] { "_", "m_" },
VisibilityMask = Modifiers.Public | Modifiers.Protected,
IncludeStaticEntities = false
};
yield return new NamingRule(AffectedEntity.ConstantField) {
Name = "Constant Fields",
NamingStyle = NamingStyle.PascalCase,
VisibilityMask = Modifiers.Public | Modifiers.Protected | Modifiers.Internal | Modifiers.Private
VisibilityMask = Modifiers.Public | Modifiers.Protected
};
yield return new NamingRule(AffectedEntity.Property) {
Name = "Properties",
VisibilityMask = Modifiers.Public | Modifiers.Protected,
NamingStyle = NamingStyle.PascalCase
};
yield return new NamingRule(AffectedEntity.Event) {
Name = "Events",
VisibilityMask = Modifiers.Public | Modifiers.Protected,
NamingStyle = NamingStyle.PascalCase
};

2
ICSharpCode.NRefactory.ConsistencyCheck/CSharpProject.cs

@ -180,7 +180,7 @@ namespace ICSharpCode.NRefactory.ConsistencyCheck @@ -180,7 +180,7 @@ namespace ICSharpCode.NRefactory.ConsistencyCheck
this.CompilationUnit = p.Parse(Content.CreateReader(), fileName);
if (p.HasErrors) {
Console.WriteLine("Error parsing " + fileName + ":");
foreach (var error in p.ErrorPrinter.Errors) {
foreach (var error in p.ErrorsAndWarnings) {
Console.WriteLine(" " + error.Region + " " + error.Message);
}
}

2
ICSharpCode.NRefactory.Demo/CSDemo.cs

@ -94,7 +94,7 @@ namespace ICSharpCode.NRefactory.Demo @@ -94,7 +94,7 @@ namespace ICSharpCode.NRefactory.Demo
b.Append(node.GetType().Name);
bool hasProperties = false;
foreach (PropertyInfo p in node.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance)) {
if (p.Name == "NodeType" || p.Name == "IsNull")
if (p.Name == "NodeType" || p.Name == "IsNull" || p.Name == "IsFrozen" || p.Name == "HasChildren")
continue;
if (p.PropertyType == typeof(string) || p.PropertyType.IsEnum || p.PropertyType == typeof(bool)) {
if (!hasProperties) {

149
ICSharpCode.NRefactory.Tests/CSharp/CodeActions/ExtractFieldTests.cs

@ -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);
}
}
}

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

@ -1,4 +1,4 @@ @@ -1,4 +1,4 @@
//
//
// TestRefactoringContext.cs
//
// Author:
@ -246,8 +246,8 @@ namespace ICSharpCode.NRefactory.CSharp.CodeActions @@ -246,8 +246,8 @@ namespace ICSharpCode.NRefactory.CSharp.CodeActions
var doc = new StringBuilderDocument (content);
var parser = new CSharpParser ();
var unit = parser.Parse (content, "program.cs");
if (parser.HasErrors)
parser.ErrorPrinter.Errors.ForEach (e => Console.WriteLine (e.Message));
foreach (var error in parser.Errors)
Console.WriteLine (error.Message);
Assert.IsFalse (parser.HasErrors, "File contains parsing errors.");
unit.Freeze ();
var parsedFile = unit.ToTypeSystem ();

106
ICSharpCode.NRefactory.Tests/CSharp/CodeIssues/InconsistentNamingTests.cs

@ -57,24 +57,24 @@ namespace ICSharpCode.NRefactory.CSharp.CodeIssues @@ -57,24 +57,24 @@ namespace ICSharpCode.NRefactory.CSharp.CodeIssues
[Test]
public void TestClassName ()
{
var input = @"class anIssue {}";
var output = @"class AnIssue {}";
var input = @"public class anIssue {}";
var output = @"public class AnIssue {}";
CheckNaming (input, output);
}
[Test]
public void TestAttributeName ()
{
var input = @"class test : System.Attribute {}";
var output = @"class TestAttribute : System.Attribute {}";
var input = @"public class test : System.Attribute {}";
var output = @"public class TestAttribute : System.Attribute {}";
CheckNaming (input, output);
}
[Test]
public void TestEventArgsName ()
{
var input = @"class test : System.EventArgs {}";
var output = @"class TestEventArgs : System.EventArgs {}";
var input = @"public class test : System.EventArgs {}";
var output = @"public class TestEventArgs : System.EventArgs {}";
CheckNaming (input, output);
}
@ -89,50 +89,50 @@ namespace ICSharpCode.NRefactory.CSharp.CodeIssues @@ -89,50 +89,50 @@ namespace ICSharpCode.NRefactory.CSharp.CodeIssues
[Test]
public void TestStructName ()
{
var input = @"struct anIssue {}";
var output = @"struct AnIssue {}";
var input = @"public struct anIssue {}";
var output = @"public struct AnIssue {}";
CheckNaming (input, output);
}
[Test]
public void TestInterfaceName ()
{
var input = @"interface anIssue {}";
var output = @"interface IAnIssue {}";
var input = @"public interface anIssue {}";
var output = @"public interface IAnIssue {}";
CheckNaming (input, output);
}
[Test]
public void TestEnumName ()
{
var input = @"enum anIssue {}";
var output = @"enum AnIssue {}";
var input = @"public enum anIssue {}";
var output = @"public enum AnIssue {}";
CheckNaming (input, output);
}
[Test]
public void TestDelegateName ()
{
var input = @"delegate void anIssue ();";
var output = @"delegate void AnIssue ();";
var input = @"public delegate void anIssue ();";
var output = @"public delegate void AnIssue ();";
CheckNaming (input, output);
}
[Test]
public void TestPrivateFieldName ()
{
var input = @"class AClass { int Field; }";
var output = @"class AClass { int field; }";
CheckNaming (input, output);
}
// [Test]
// public void TestPrivateFieldName ()
// {
// var input = @"class AClass { int Field; }";
// var output = @"class AClass { int field; }";
// CheckNaming (input, output);
// }
[Test]
public void TestUnderscoreFieldName ()
{
var input = @"class AClass { int _Field; }";
var output = @"class AClass { int _field; }";
CheckNaming (input, output);
}
// [Test]
// public void TestUnderscoreFieldName ()
// {
// var input = @"class AClass { int _Field; }";
// var output = @"class AClass { int _field; }";
// CheckNaming (input, output);
// }
[Test]
public void TestPublicFieldName ()
@ -142,13 +142,13 @@ namespace ICSharpCode.NRefactory.CSharp.CodeIssues @@ -142,13 +142,13 @@ namespace ICSharpCode.NRefactory.CSharp.CodeIssues
CheckNaming (input, output);
}
[Test]
public void TestPrivateConstantFieldName ()
{
var input = @"class AClass { const int field = 5; }";
var output = @"class AClass { const int Field = 5; }";
CheckNaming (input, output);
}
// [Test]
// public void TestPrivateConstantFieldName ()
// {
// var input = @"class AClass { const int field = 5; }";
// var output = @"class AClass { const int Field = 5; }";
// CheckNaming (input, output);
// }
[Test]
public void TestPublicReadOnlyFieldName ()
@ -174,13 +174,13 @@ namespace ICSharpCode.NRefactory.CSharp.CodeIssues @@ -174,13 +174,13 @@ namespace ICSharpCode.NRefactory.CSharp.CodeIssues
CheckNaming (input, output, true);
}
[Test]
public void TestPrivateStaticFieldName ()
{
var input = @"class AClass { static int Field; }";
var output = @"class AClass { static int field; }";
CheckNaming (input, output);
}
// [Test]
// public void TestPrivateStaticFieldName ()
// {
// var input = @"class AClass { static int Field; }";
// var output = @"class AClass { static int field; }";
// CheckNaming (input, output);
// }
[Test]
public void TestPublicStaticReadOnlyFieldName ()
@ -190,13 +190,13 @@ namespace ICSharpCode.NRefactory.CSharp.CodeIssues @@ -190,13 +190,13 @@ namespace ICSharpCode.NRefactory.CSharp.CodeIssues
CheckNaming (input, output);
}
[Test]
public void TestPrivateReadOnlyFieldName ()
{
var input = @"class AClass { readonly int Field; }";
var output = @"class AClass { readonly int field; }";
CheckNaming (input, output);
}
// [Test]
// public void TestPrivateReadOnlyFieldName ()
// {
// var input = @"class AClass { readonly int Field; }";
// var output = @"class AClass { readonly int field; }";
// CheckNaming (input, output);
// }
[Test]
public void TestPublicConstantFieldName ()
@ -209,16 +209,16 @@ namespace ICSharpCode.NRefactory.CSharp.CodeIssues @@ -209,16 +209,16 @@ namespace ICSharpCode.NRefactory.CSharp.CodeIssues
[Test]
public void TestMethodName ()
{
var input = @"class AClass { int method () {} }";
var output = @"class AClass { int Method () {} }";
var input = @"class AClass { public int method () {} }";
var output = @"class AClass { public int Method () {} }";
CheckNaming (input, output);
}
[Test]
public void TestPropertyName ()
{
var input = @"class AClass { int property { get; set; } }";
var output = @"class AClass { int Property { get; set; } }";
var input = @"class AClass { public int property { get; set; } }";
var output = @"class AClass { public int Property { get; set; } }";
CheckNaming (input, output);
}

20
ICSharpCode.NRefactory.Tests/CSharp/Parser/ParseUtil.cs

@ -35,8 +35,8 @@ namespace ICSharpCode.NRefactory.CSharp.Parser @@ -35,8 +35,8 @@ namespace ICSharpCode.NRefactory.CSharp.Parser
CSharpParser parser = new CSharpParser();
CompilationUnit cu = parser.Parse(new StringReader(code), "parsed.cs");
if (parser.HasErrors)
parser.ErrorPrinter.Errors.ForEach (err => Console.WriteLine (err.Message));
foreach (var error in parser.Errors)
Console.WriteLine (error.Message);
Assert.AreEqual(expectErrors, parser.HasErrors, "HasErrors");
AstNode node = cu.Children.Single();
@ -58,8 +58,8 @@ namespace ICSharpCode.NRefactory.CSharp.Parser @@ -58,8 +58,8 @@ namespace ICSharpCode.NRefactory.CSharp.Parser
CSharpParser parser = new CSharpParser();
var statements = parser.ParseStatements(new StringReader(stmt));
if (parser.HasErrors)
parser.ErrorPrinter.Errors.ForEach (err => Console.WriteLine (err.Message));
foreach (var error in parser.Errors)
Console.WriteLine (error.Message);
Assert.AreEqual(expectErrors, parser.HasErrors, "HasErrors");
AstNode statement = statements.Single();
@ -81,8 +81,8 @@ namespace ICSharpCode.NRefactory.CSharp.Parser @@ -81,8 +81,8 @@ namespace ICSharpCode.NRefactory.CSharp.Parser
CSharpParser parser = new CSharpParser();
AstNode parsedExpression = parser.ParseExpression(new StringReader(expr));
if (parser.HasErrors)
parser.ErrorPrinter.Errors.ForEach (err => Console.WriteLine (err.Message));
foreach (var error in parser.Errors)
Console.WriteLine (error.Message);
Assert.AreEqual(expectErrors, parser.HasErrors, "HasErrors");
if (expectErrors && parsedExpression == null)
return default (T);
@ -103,8 +103,8 @@ namespace ICSharpCode.NRefactory.CSharp.Parser @@ -103,8 +103,8 @@ namespace ICSharpCode.NRefactory.CSharp.Parser
{
CSharpParser parser = new CSharpParser();
var members = parser.ParseTypeMembers(new StringReader(expr));
if (parser.HasErrors)
parser.ErrorPrinter.Errors.ForEach (err => Console.WriteLine (err.Message));
foreach (var error in parser.Errors)
Console.WriteLine (error.Message);
Assert.AreEqual(expectErrors, parser.HasErrors, "HasErrors");
EntityDeclaration m = members.Single();
Type type = typeof(T);
@ -125,8 +125,8 @@ namespace ICSharpCode.NRefactory.CSharp.Parser @@ -125,8 +125,8 @@ namespace ICSharpCode.NRefactory.CSharp.Parser
CSharpParser parser = new CSharpParser();
var parsedExpression = parser.ParseDocumentationReference(cref);
if (parser.HasErrors)
parser.ErrorPrinter.Errors.ForEach (err => Console.WriteLine (err.Message));
foreach (var error in parser.Errors)
Console.WriteLine (error.Message);
Assert.AreEqual(expectErrors, parser.HasErrors, "HasErrors");
if (expectErrors && parsedExpression == null)
return null;

34
ICSharpCode.NRefactory.Tests/FormattingTests/TestWrapping.cs

@ -579,6 +579,40 @@ namespace ICSharpCode.NRefactory.CSharp.FormattingTests @@ -579,6 +579,40 @@ namespace ICSharpCode.NRefactory.CSharp.FormattingTests
}
}");
}
[Test()]
public void TestNoBlankLinesBetweenEndBraceAndEndParenthesis ()
{
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
policy.BlankLinesBetweenMembers = 1;
var adapter = Test (policy, @"class Test
{
int Foo (int i, double d, Action a)
{
a ();
}
void Bar ()
{
Foo (1, 2, () => {
});
}
}",
@"class Test
{
int Foo (int i, double d, Action a)
{
a ();
}
void Bar ()
{
Foo (1, 2, () => {
});
}
}", FormattingMode.Intrusive);
}
}
}

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

@ -1,4 +1,4 @@ @@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Build">
<PropertyGroup>
<ProjectGuid>{63D3B27A-D966-4902-90B3-30290E1692F1}</ProjectGuid>
@ -283,6 +283,7 @@ @@ -283,6 +283,7 @@
<Compile Include="CSharp\CodeActions\ImplementInterfaceTests.cs" />
<Compile Include="CSharp\CodeActions\ImplementInterfaceExplicitTests.cs" />
<Compile Include="CSharp\CodeActions\ImplementAbstractMembersTest.cs" />
<Compile Include="CSharp\CodeActions\ExtractFieldTests.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\Mono.Cecil\Mono.Cecil.csproj">

1
ICSharpCode.NRefactory.Tests/TypeSystem/CecilLoaderTests.cs

@ -145,7 +145,6 @@ namespace ICSharpCode.NRefactory.TypeSystem @@ -145,7 +145,6 @@ namespace ICSharpCode.NRefactory.TypeSystem
Assert.AreEqual(0, c.GetProperties().Count());
Assert.AreEqual(0, c.GetEvents().Count());
Assert.AreEqual(0, c.GetFields().Count());
Assert.AreEqual(3, c.Attributes.Count);
}
[Test]

Loading…
Cancel
Save