Browse Source

added support for implicitly typed local variables in ExtractMethod, now the type is resolved before using it as a parameter in the new method's signature.

git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/branches/3.0@3762 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61
shortcuts
Siegfried Pammer 17 years ago
parent
commit
dcbc8d5fdd
  1. 34
      src/AddIns/Misc/SharpRefactoring/Src/CSharpMethodExtractor.cs
  2. 30
      src/AddIns/Misc/SharpRefactoring/Src/MethodExtractorBase.cs

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

@ -4,7 +4,7 @@
// <owner name="Siegfried Pammer" email="sie_pam@gmx.at"/> // <owner name="Siegfried Pammer" email="sie_pam@gmx.at"/>
// <version>$Revision: 3287 $</version> // <version>$Revision: 3287 $</version>
// </file> // </file>
using ICSharpCode.SharpDevelop; using ICSharpCode.TextEditor;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
@ -15,6 +15,7 @@ using ICSharpCode.NRefactory.Ast;
using ICSharpCode.NRefactory.AstBuilder; using ICSharpCode.NRefactory.AstBuilder;
using ICSharpCode.NRefactory.PrettyPrinter; using ICSharpCode.NRefactory.PrettyPrinter;
using ICSharpCode.NRefactory.Visitors; using ICSharpCode.NRefactory.Visitors;
using ICSharpCode.SharpDevelop;
using ICSharpCode.SharpDevelop.Dom.NRefactoryResolver; using ICSharpCode.SharpDevelop.Dom.NRefactoryResolver;
using ICSharpCode.SharpDevelop.Project; using ICSharpCode.SharpDevelop.Project;
using ICSharpCode.TextEditor.Document; using ICSharpCode.TextEditor.Document;
@ -116,13 +117,23 @@ namespace SharpRefactoring
Location end = new Location(this.currentSelection.EndPosition.Column + 1, this.currentSelection.EndPosition.Line + 1); Location end = new Location(this.currentSelection.EndPosition.Column + 1, this.currentSelection.EndPosition.Line + 1);
foreach (KeyValuePair<string, List<LocalLookupVariable>> pair in ltv.Variables) { foreach (KeyValuePair<string, List<LocalLookupVariable>> pair in ltv.Variables) {
foreach (LocalLookupVariable variable in pair.Value) { foreach (LocalLookupVariable v in pair.Value) {
Variable variable = new Variable(v);
if (variable.StartPos > end || variable.EndPos < start) if (variable.StartPos > end || variable.EndPos < start)
continue; continue;
variable.IsReferenceType = true; // TODO : implement check for reference type
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);
variable.Type = Dom.Refactoring.CodeGenerator.ConvertType(result.ResolvedType, new Dom.ClassFinder(result.CallingMember));
}
if (IsInSel(variable.StartPos, this.currentSelection) && HasOccurrencesAfter(true, this.parentNode, new Location(this.currentSelection.EndPosition.Column + 1, this.currentSelection.EndPosition.Line + 1), variable.Name, variable.StartPos, variable.EndPos)) { if (IsInSel(variable.StartPos, this.currentSelection) && HasOccurrencesAfter(true, 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.TypeRef)); possibleReturnValues.Add(new VariableDeclaration(variable.Name, variable.Initializer, variable.Type));
otherReturnValues.Add(new VariableDeclaration(variable.Name, variable.Initializer, variable.TypeRef)); otherReturnValues.Add(new VariableDeclaration(variable.Name, variable.Initializer, variable.Type));
} }
FindReferenceVisitor frv = new FindReferenceVisitor(true, variable.Name, start, end); FindReferenceVisitor frv = new FindReferenceVisitor(true, variable.Name, start, end);
@ -136,18 +147,18 @@ namespace SharpRefactoring
bool getsAssigned = pair.Value.Count > 0; bool getsAssigned = pair.Value.Count > 0;
if (hasOccurrencesAfter && isInitialized) if (hasOccurrencesAfter && isInitialized)
newMethod.Parameters.Add(new ParameterDeclarationExpression(variable.TypeRef, variable.Name, ParameterModifiers.Ref)); newMethod.Parameters.Add(new ParameterDeclarationExpression(variable.Type, variable.Name, ParameterModifiers.Ref));
else { else {
if (hasOccurrencesAfter && hasAssignment) if (hasOccurrencesAfter && hasAssignment)
newMethod.Parameters.Add(new ParameterDeclarationExpression(variable.TypeRef, variable.Name, ParameterModifiers.Out)); newMethod.Parameters.Add(new ParameterDeclarationExpression(variable.Type, variable.Name, ParameterModifiers.Out));
else { else {
if (!hasOccurrencesAfter && getsAssigned) if (!hasOccurrencesAfter && getsAssigned)
newMethod.Parameters.Add(new ParameterDeclarationExpression(variable.TypeRef, variable.Name, ParameterModifiers.None)); newMethod.Parameters.Add(new ParameterDeclarationExpression(variable.Type, variable.Name, ParameterModifiers.None));
else { else {
if (!hasOccurrencesAfter && !isInitialized) if (!hasOccurrencesAfter && !isInitialized)
newMethod.Body.Children.Insert(0, new LocalVariableDeclaration(new VariableDeclaration(variable.Name, variable.Initializer, variable.TypeRef))); newMethod.Body.Children.Insert(0, new LocalVariableDeclaration(new VariableDeclaration(variable.Name, variable.Initializer, variable.Type)));
else else
newMethod.Parameters.Add(new ParameterDeclarationExpression(variable.TypeRef, variable.Name, ParameterModifiers.In)); newMethod.Parameters.Add(new ParameterDeclarationExpression(variable.Type, variable.Name, ParameterModifiers.In));
} }
} }
} }
@ -187,5 +198,10 @@ namespace SharpRefactoring
{ {
return new CSharpOutputVisitor(); return new CSharpOutputVisitor();
} }
public override Dom.IResolver GetResolver()
{
return new NRefactoryResolver(Dom.LanguageProperties.CSharp);
}
} }
} }

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

@ -9,13 +9,13 @@ using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.IO; using System.IO;
using System.Text; using System.Text;
using ICSharpCode.Core; using ICSharpCode.Core;
using ICSharpCode.NRefactory; using ICSharpCode.NRefactory;
using ICSharpCode.NRefactory.Ast; using ICSharpCode.NRefactory.Ast;
using ICSharpCode.NRefactory.PrettyPrinter; using ICSharpCode.NRefactory.PrettyPrinter;
using ICSharpCode.NRefactory.Visitors; using ICSharpCode.NRefactory.Visitors;
using ICSharpCode.SharpDevelop; using ICSharpCode.SharpDevelop;
using ICSharpCode.SharpDevelop.Dom.NRefactoryResolver;
using ICSharpCode.SharpDevelop.Project; using ICSharpCode.SharpDevelop.Project;
using ICSharpCode.TextEditor; using ICSharpCode.TextEditor;
using ICSharpCode.TextEditor.Document; using ICSharpCode.TextEditor.Document;
@ -260,7 +260,7 @@ namespace SharpRefactoring
return false; return false;
} }
protected bool IsInitializedVariable(bool caseSensitive, ParametrizedNode member, LocalLookupVariable variable) protected bool IsInitializedVariable(bool caseSensitive, ParametrizedNode member, Variable variable)
{ {
if (!(variable.Initializer.IsNull)) { if (!(variable.Initializer.IsNull)) {
return true; return true;
@ -279,9 +279,11 @@ namespace SharpRefactoring
return false; return false;
} }
protected static bool HasAssignment(MethodDeclaration method, LocalLookupVariable variable)
protected static bool HasAssignment(MethodDeclaration method, Variable variable)
{ {
HasAssignmentsVisitor hav = new HasAssignmentsVisitor(variable.Name, variable.TypeRef, variable.StartPos, variable.EndPos); HasAssignmentsVisitor hav = new HasAssignmentsVisitor(variable.Name, variable.Type, variable.StartPos, variable.EndPos);
method.AcceptVisitor(hav, null); method.AcceptVisitor(hav, null);
@ -291,5 +293,25 @@ namespace SharpRefactoring
public abstract IOutputAstVisitor GetOutputVisitor(); public abstract IOutputAstVisitor GetOutputVisitor();
public abstract bool Extract(); public abstract bool Extract();
public abstract Dom.IResolver GetResolver();
}
public class Variable {
public TypeReference Type { get; set; }
public string Name { get; set; }
public Location StartPos { get; set; }
public Location EndPos { get; set; }
public Expression Initializer { get; set; }
public bool IsReferenceType { get; set; }
public Variable(LocalLookupVariable v)
{
this.Type = v.TypeRef;
this.Name = v.Name;
this.StartPos = v.StartPos;
this.EndPos = v.EndPos;
this.Initializer = v.Initializer;
}
} }
} }

Loading…
Cancel
Save