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) {