diff --git a/src/AddIns/Misc/SharpRefactoring/Project/Src/CSharpMethodExtractor.cs b/src/AddIns/Misc/SharpRefactoring/Project/Src/CSharpMethodExtractor.cs index 471898ebc0..5fc77a3379 100644 --- a/src/AddIns/Misc/SharpRefactoring/Project/Src/CSharpMethodExtractor.cs +++ b/src/AddIns/Misc/SharpRefactoring/Project/Src/CSharpMethodExtractor.cs @@ -69,7 +69,7 @@ namespace SharpRefactoring // Initialise new method newMethod.Body = GetBlock(this.textEditor.SelectedText); newMethod.Body.StartLocation = new Location(0,0); - + this.parentNode = GetParentMember(start, end); if (parentNode == null) { @@ -77,8 +77,21 @@ namespace SharpRefactoring return false; } - if (!CheckForJumpInstructions(newMethod)) + ErrorKind kind = CheckForJumpInstructions(newMethod); + if (kind != ErrorKind.None) { + switch (kind) { + case ErrorKind.ContainsBreak: + MessageService.ShowError("${res:AddIns.SharpRefactoring.ExtractMethod.ContainsBreakError}"); + break; + case ErrorKind.ContainsContinue: + MessageService.ShowError("${res:AddIns.SharpRefactoring.ExtractMethod.ContainsContinueError}"); + break; + case ErrorKind.ContainsGoto: + MessageService.ShowError("${res:AddIns.SharpRefactoring.ExtractMethod.ContainsGotoError}"); + break; + } return false; + } newMethod.Modifier = parentNode.Modifier; diff --git a/src/AddIns/Misc/SharpRefactoring/Project/Src/MethodExtractorBase.cs b/src/AddIns/Misc/SharpRefactoring/Project/Src/MethodExtractorBase.cs index 673c387e3c..61e99c5d53 100644 --- a/src/AddIns/Misc/SharpRefactoring/Project/Src/MethodExtractorBase.cs +++ b/src/AddIns/Misc/SharpRefactoring/Project/Src/MethodExtractorBase.cs @@ -134,13 +134,12 @@ namespace SharpRefactoring } } - protected static bool CheckForJumpInstructions(MethodDeclaration method) + protected ErrorKind CheckForJumpInstructions(MethodDeclaration method) { FindJumpInstructionsVisitor fjiv = new FindJumpInstructionsVisitor(method); method.AcceptVisitor(fjiv, null); - - return fjiv.IsOk; + return fjiv.DoCheck(); } protected bool IsInCurrentSelection(Location location) diff --git a/src/AddIns/Misc/SharpRefactoring/Project/Src/Visitors/FindJumpInstructionsVisitor.cs b/src/AddIns/Misc/SharpRefactoring/Project/Src/Visitors/FindJumpInstructionsVisitor.cs index 9f84e3bf80..ee98c0ae1c 100644 --- a/src/AddIns/Misc/SharpRefactoring/Project/Src/Visitors/FindJumpInstructionsVisitor.cs +++ b/src/AddIns/Misc/SharpRefactoring/Project/Src/Visitors/FindJumpInstructionsVisitor.cs @@ -1,39 +1,53 @@ -/* - * Created by SharpDevelop. - * User: HP - * Date: 01.05.2008 - * Time: 20:37 - */ +// +// +// +// +// $Revision$ +// using System; using System.Collections.Generic; +using System.Linq; + using ICSharpCode.Core; using ICSharpCode.NRefactory; using ICSharpCode.NRefactory.Ast; +using ICSharpCode.NRefactory.PrettyPrinter; using ICSharpCode.NRefactory.Visitors; using Dom = ICSharpCode.SharpDevelop.Dom; namespace SharpRefactoring.Visitors { - /// - /// Description of FindJumpInstructionVisitor. - /// + public enum ErrorKind { + ContainsBreak, + ContainsContinue, + ContainsGoto, + None + } + public class FindJumpInstructionsVisitor : AbstractAstVisitor { MethodDeclaration method; + List labels; List cases; - bool isOk = true; - public bool IsOk { - get { return isOk; } - } + List gotos; + List gotoCases; + + List breaks; + List continues; public FindJumpInstructionsVisitor(MethodDeclaration method) { this.method = method; + this.labels = new List(); this.cases = new List(); + this.gotoCases = new List(); + this.gotos = new List(); + this.breaks = new List(); + this.continues = new List(); } public override object VisitDoLoopStatement(DoLoopStatement doLoopStatement, object data) @@ -63,19 +77,15 @@ namespace SharpRefactoring.Visitors public override object VisitBreakStatement(BreakStatement breakStatement, object data) { - if (!(data is bool)) { - this.isOk = false; - MessageService.ShowError("Selection contains a 'break' statement without the enclosing loop."); - } + if (!(data is bool)) + this.breaks.Add(breakStatement); return base.VisitBreakStatement(breakStatement, data); } public override object VisitContinueStatement(ContinueStatement continueStatement, object data) { - if (!(data is bool)) { - this.isOk = false; - MessageService.ShowError("Selection contains a 'continue' statement without the enclosing loop."); - } + if (!(data is bool)) + this.continues.Add(continueStatement); return base.VisitContinueStatement(continueStatement, data); } @@ -93,30 +103,54 @@ namespace SharpRefactoring.Visitors public override object VisitGotoCaseStatement(GotoCaseStatement gotoCaseStatement, object data) { - this.isOk = false; - foreach (CaseLabel label in this.cases) { - if (label.Label.ToString() == gotoCaseStatement.Expression.ToString()) - this.isOk = true; - } - if (!this.isOk) { - MessageService.ShowError("Case section '" + ((PrimitiveExpression)gotoCaseStatement.Expression).StringValue + "' not found inside the selected range!"); - } - + gotoCases.Add(gotoCaseStatement); return base.VisitGotoCaseStatement(gotoCaseStatement, data); } public override object VisitGotoStatement(GotoStatement gotoStatement, object data) { - this.isOk = false; - foreach (LabelStatement label in this.labels) { - if (label.Label == gotoStatement.Label) - this.isOk = true; + gotos.Add(gotoStatement); + return base.VisitGotoStatement(gotoStatement, data); + } + + public ErrorKind DoCheck() + { + if (this.breaks.Any()) + return ErrorKind.ContainsBreak; + + if (this.continues.Any()) + return ErrorKind.ContainsContinue; + + if (this.gotos.Any()) { + foreach (GotoStatement stmt in this.gotos) { + if (!this.labels.Any(label => label.Label == stmt.Label)) + return ErrorKind.ContainsGoto; + } } - if (!this.isOk) { - MessageService.ShowError("Label '" + gotoStatement.Label + "' not found inside the selected range!"); + + if (this.gotoCases.Any()) { + foreach (GotoCaseStatement stmt in this.gotoCases) { + if (!this.cases.Any(@case => CompareCase(@case, stmt))) + return ErrorKind.ContainsGoto; + } } - return base.VisitGotoStatement(gotoStatement, data); + return ErrorKind.None; + } + + bool CompareCase(CaseLabel label, GotoCaseStatement stmt) + { + if (label.IsDefault && stmt.IsDefaultCase) + return true; + + if (stmt.Expression is PrimitiveExpression && label.Label is PrimitiveExpression) { + PrimitiveExpression e1 = stmt.Expression as PrimitiveExpression; + PrimitiveExpression e2 = label.Label as PrimitiveExpression; + + return object.Equals(e1.Value, e2.Value); + } + + return false; } } } diff --git a/src/AddIns/Misc/SharpRefactoring/Project/Src/Visitors/FindMemberVisitor.cs b/src/AddIns/Misc/SharpRefactoring/Project/Src/Visitors/FindMemberVisitor.cs index 976c934cff..9f35b5f2ef 100644 --- a/src/AddIns/Misc/SharpRefactoring/Project/Src/Visitors/FindMemberVisitor.cs +++ b/src/AddIns/Misc/SharpRefactoring/Project/Src/Visitors/FindMemberVisitor.cs @@ -1,9 +1,9 @@ -/* - * Created by SharpDevelop. - * User: HP - * Date: 28.04.2008 - * Time: 22:18 - */ +// +// +// +// +// $Revision$ +// using System; using ICSharpCode.NRefactory; diff --git a/src/AddIns/Misc/SharpRefactoring/Project/Src/Visitors/FindReferenceVisitor.cs b/src/AddIns/Misc/SharpRefactoring/Project/Src/Visitors/FindReferenceVisitor.cs index 9e52955aca..37f2c01305 100644 --- a/src/AddIns/Misc/SharpRefactoring/Project/Src/Visitors/FindReferenceVisitor.cs +++ b/src/AddIns/Misc/SharpRefactoring/Project/Src/Visitors/FindReferenceVisitor.cs @@ -1,9 +1,9 @@ -/* - * Created by SharpDevelop. - * User: HP - * Date: 28.04.2008 - * Time: 22:18 - */ +// +// +// +// +// $Revision$ +// using System; using System.Collections.Generic; diff --git a/src/AddIns/Misc/SharpRefactoring/Project/Src/Visitors/HasAssignmentsVisitor.cs b/src/AddIns/Misc/SharpRefactoring/Project/Src/Visitors/HasAssignmentsVisitor.cs index 5b14159805..6bb7de2154 100644 --- a/src/AddIns/Misc/SharpRefactoring/Project/Src/Visitors/HasAssignmentsVisitor.cs +++ b/src/AddIns/Misc/SharpRefactoring/Project/Src/Visitors/HasAssignmentsVisitor.cs @@ -1,9 +1,9 @@ -/* - * Created by SharpDevelop. - * User: HP - * Date: 28.04.2008 - * Time: 22:18 - */ +// +// +// +// +// $Revision$ +// using ICSharpCode.NRefactory; using System; diff --git a/src/AddIns/Misc/SharpRefactoring/Project/Src/Visitors/HasReturnStatementVisitor.cs b/src/AddIns/Misc/SharpRefactoring/Project/Src/Visitors/HasReturnStatementVisitor.cs index a7408b2782..288369c837 100644 --- a/src/AddIns/Misc/SharpRefactoring/Project/Src/Visitors/HasReturnStatementVisitor.cs +++ b/src/AddIns/Misc/SharpRefactoring/Project/Src/Visitors/HasReturnStatementVisitor.cs @@ -1,9 +1,9 @@ -/* - * Created by SharpDevelop. - * User: HP - * Date: 28.04.2008 - * Time: 22:18 - */ +// +// +// +// +// $Revision$ +// using System; using ICSharpCode.NRefactory.Ast; diff --git a/src/AddIns/Misc/SharpRefactoring/Test/SharpRefactoring.Tests.csproj b/src/AddIns/Misc/SharpRefactoring/Test/SharpRefactoring.Tests.csproj index 039539aaba..ca375c0694 100644 --- a/src/AddIns/Misc/SharpRefactoring/Test/SharpRefactoring.Tests.csproj +++ b/src/AddIns/Misc/SharpRefactoring/Test/SharpRefactoring.Tests.csproj @@ -78,6 +78,10 @@ NRefactory False + + {870115DD-960A-4406-A6B9-600BCDC36A03} + NRefactoryTests + {2748AD25-9C63-4E12-877B-4DCE96FBED54} ICSharpCode.SharpDevelop