Browse Source

Extract Method: Add ref keyword to extracted parameters only if the type is a reference type

git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/branches/3.0@4041 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61
shortcuts
Siegfried Pammer 16 years ago
parent
commit
8c06710d74
  1. 27
      src/AddIns/Misc/SharpRefactoring/Src/CSharpMethodExtractor.cs
  2. 4
      src/AddIns/Misc/SharpRefactoring/Src/MethodExtractorBase.cs

27
src/AddIns/Misc/SharpRefactoring/Src/CSharpMethodExtractor.cs

@ -4,10 +4,10 @@ @@ -4,10 +4,10 @@
// <owner name="Siegfried Pammer" email="sie_pam@gmx.at"/>
// <version>$Revision: 3287 $</version>
// </file>
using System;
using System.Collections.Generic;
using System.IO;
using ICSharpCode.Core;
using ICSharpCode.NRefactory;
using ICSharpCode.NRefactory.Ast;
@ -121,17 +121,18 @@ namespace SharpRefactoring @@ -121,17 +121,18 @@ namespace SharpRefactoring
foreach (KeyValuePair<string, List<LocalLookupVariable>> pair in ltv.Variables) {
foreach (LocalLookupVariable v in pair.Value) {
Variable variable = new Variable(v);
if (variable.StartPos > end || variable.EndPos < start)
continue;
variable.IsReferenceType = true; // TODO : implement check for reference type
Dom.ParseInformation info = ParserService.GetParseInformation(this.textEditor.FileName);
Dom.ExpressionResult res = new Dom.ExpressionResult(variable.Name, Dom.DomRegion.FromLocation(variable.StartPos, variable.EndPos), Dom.ExpressionContext.Default, null);
Dom.ResolveResult result = this.GetResolver().Resolve(res, info, this.textEditor.Document.TextContent);
if (variable.Type.Type == "var") {
Dom.ParseInformation info = ParserService.GetParseInformation(this.textEditor.FileName);
Dom.ExpressionResult res = new Dom.ExpressionResult(variable.Name, Dom.DomRegion.FromLocation(variable.StartPos, variable.EndPos), Dom.ExpressionContext.Default, null);
Dom.ResolveResult result = this.GetResolver().Resolve(res, info, this.textEditor.Document.TextContent);
if (variable.Type.Type == "var")
variable.Type = Dom.Refactoring.CodeGenerator.ConvertType(result.ResolvedType, new Dom.ClassFinder(result.CallingMember));
}
variable.IsReferenceType = result.ResolvedType.IsReferenceType == true;
if (IsInSel(variable.StartPos, this.currentSelection) && HasOccurrencesAfter(CSharpNameComparer, this.parentNode, new Location(this.currentSelection.EndPosition.Column + 1, this.currentSelection.EndPosition.Line + 1), variable.Name, variable.StartPos, variable.EndPos)) {
possibleReturnValues.Add(new VariableDeclaration(variable.Name, variable.Initializer, variable.Type));
@ -148,22 +149,26 @@ namespace SharpRefactoring @@ -148,22 +149,26 @@ namespace SharpRefactoring
bool hasAssignment = HasAssignment(newMethod, variable);
bool getsAssigned = pair.Value.Count > 0;
ParameterDeclarationExpression newParam = null;
if (hasOccurrencesAfter && isInitialized)
newMethod.Parameters.Add(new ParameterDeclarationExpression(variable.Type, variable.Name, ParameterModifiers.Ref));
newParam = new ParameterDeclarationExpression(variable.Type, variable.Name, (variable.IsReferenceType) ? ParameterModifiers.None : ParameterModifiers.Ref);
else {
if (hasOccurrencesAfter && hasAssignment)
newMethod.Parameters.Add(new ParameterDeclarationExpression(variable.Type, variable.Name, ParameterModifiers.Out));
newParam = new ParameterDeclarationExpression(variable.Type, variable.Name, ParameterModifiers.Out);
else {
if (!hasOccurrencesAfter && getsAssigned)
newMethod.Parameters.Add(new ParameterDeclarationExpression(variable.Type, variable.Name, ParameterModifiers.None));
newParam = new ParameterDeclarationExpression(variable.Type, variable.Name, ParameterModifiers.None);
else {
if (!hasOccurrencesAfter && !isInitialized)
newMethod.Body.Children.Insert(0, new LocalVariableDeclaration(new VariableDeclaration(variable.Name, variable.Initializer, variable.Type)));
else
newMethod.Parameters.Add(new ParameterDeclarationExpression(variable.Type, variable.Name, ParameterModifiers.In));
newParam = new ParameterDeclarationExpression(variable.Type, variable.Name, ParameterModifiers.In);
}
}
}
if (newParam != null)
newMethod.Parameters.Add(newParam);
}
}
}

4
src/AddIns/Misc/SharpRefactoring/Src/MethodExtractorBase.cs

@ -6,17 +6,15 @@ @@ -6,17 +6,15 @@
// </file>
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text;
using ICSharpCode.Core;
using ICSharpCode.NRefactory;
using ICSharpCode.NRefactory.Ast;
using ICSharpCode.NRefactory.PrettyPrinter;
using ICSharpCode.NRefactory.Visitors;
using ICSharpCode.SharpDevelop;
using ICSharpCode.SharpDevelop.Dom.NRefactoryResolver;
using ICSharpCode.SharpDevelop.Project;
using ICSharpCode.TextEditor;
using ICSharpCode.TextEditor.Document;
using SharpRefactoring.Visitors;

Loading…
Cancel
Save