From ad7853244394342a26771963c3bf9c1d5b8e66d8 Mon Sep 17 00:00:00 2001 From: Siegfried Pammer Date: Fri, 8 May 2009 16:03:57 +0000 Subject: [PATCH] fixed bug in CSharpMethodExtractor git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/branches/3.0@4056 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61 --- .../SharpRefactoring/Src/CSharpMethodExtractor.cs | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/AddIns/Misc/SharpRefactoring/Src/CSharpMethodExtractor.cs b/src/AddIns/Misc/SharpRefactoring/Src/CSharpMethodExtractor.cs index a29cd08acd..e1f6c68bb5 100644 --- a/src/AddIns/Misc/SharpRefactoring/Src/CSharpMethodExtractor.cs +++ b/src/AddIns/Misc/SharpRefactoring/Src/CSharpMethodExtractor.cs @@ -90,7 +90,7 @@ namespace SharpRefactoring parentNode.AcceptVisitor(ltv, null); var variablesList = (from list in ltv.Variables.Values from item in list select new Variable(item)) - .Where(v => !(v.StartPos > end || v.EndPos < start)) + .Where(v => !(v.StartPos > end || v.EndPos < start) && HasReferencesInSelection(newMethod, v)) .Union(FromParameters(newMethod)) .Select(va => ResolveVariable(va)); @@ -98,7 +98,7 @@ namespace SharpRefactoring LoggingService.Debug(variable); bool hasOccurrencesAfter = HasOccurrencesAfter(CSharpNameComparer, this.parentNode, end, variable.Name, variable.StartPos, variable.EndPos); - bool isInitialized = !variable.Initializer.IsNull; + bool isInitialized = (variable.Initializer != null) ? !variable.Initializer.IsNull : false; bool hasAssignment = HasAssignment(newMethod, variable); if (IsInSel(variable.StartPos, this.currentSelection) && hasOccurrencesAfter) { @@ -106,7 +106,7 @@ namespace SharpRefactoring otherReturnValues.Add(new VariableDeclaration(variable.Name, variable.Initializer, variable.Type)); } - if (!(IsInSel(variable.StartPos, this.currentSelection) || IsInSel(variable.EndPos, this.currentSelection))) { + if (!(IsInSel(variable.StartPos, this.currentSelection) || IsInSel(variable.EndPos, this.currentSelection))) { ParameterDeclarationExpression newParam = null; if ((hasOccurrencesAfter && isInitialized) || variable.WasRefParam) @@ -153,6 +153,15 @@ namespace SharpRefactoring return true; } + + bool HasReferencesInSelection(MethodDeclaration newMethod, Variable variable) + { + FindReferenceVisitor frv = new FindReferenceVisitor(CSharpNameComparer, variable.Name, + newMethod.Body.StartLocation, newMethod.Body.EndLocation); + + newMethod.AcceptVisitor(frv, null); + return frv.Identifiers.Count > 0; + } Variable ResolveVariable(Variable variable) {