From dcbc8d5fdd2d8c9e1076d49628e88670f17e3a3f Mon Sep 17 00:00:00 2001 From: Siegfried Pammer Date: Sun, 18 Jan 2009 15:15:57 +0000 Subject: [PATCH] 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 --- .../Src/CSharpMethodExtractor.cs | 34 ++++++++++++++----- .../Src/MethodExtractorBase.cs | 30 +++++++++++++--- 2 files changed, 51 insertions(+), 13 deletions(-) diff --git a/src/AddIns/Misc/SharpRefactoring/Src/CSharpMethodExtractor.cs b/src/AddIns/Misc/SharpRefactoring/Src/CSharpMethodExtractor.cs index d8533548d2..a5270e477f 100644 --- a/src/AddIns/Misc/SharpRefactoring/Src/CSharpMethodExtractor.cs +++ b/src/AddIns/Misc/SharpRefactoring/Src/CSharpMethodExtractor.cs @@ -4,7 +4,7 @@ // // $Revision: 3287 $ // -using ICSharpCode.SharpDevelop; +using ICSharpCode.TextEditor; using System; using System.Collections.Generic; using System.Diagnostics; @@ -15,6 +15,7 @@ using ICSharpCode.NRefactory.Ast; using ICSharpCode.NRefactory.AstBuilder; using ICSharpCode.NRefactory.PrettyPrinter; using ICSharpCode.NRefactory.Visitors; +using ICSharpCode.SharpDevelop; using ICSharpCode.SharpDevelop.Dom.NRefactoryResolver; using ICSharpCode.SharpDevelop.Project; using ICSharpCode.TextEditor.Document; @@ -116,13 +117,23 @@ namespace SharpRefactoring Location end = new Location(this.currentSelection.EndPosition.Column + 1, this.currentSelection.EndPosition.Line + 1); foreach (KeyValuePair> 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) 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)) { - possibleReturnValues.Add(new VariableDeclaration(variable.Name, variable.Initializer, variable.TypeRef)); - otherReturnValues.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.Type)); } FindReferenceVisitor frv = new FindReferenceVisitor(true, variable.Name, start, end); @@ -136,18 +147,18 @@ namespace SharpRefactoring bool getsAssigned = pair.Value.Count > 0; 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 { 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 { 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 { 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 - 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(); } + + public override Dom.IResolver GetResolver() + { + return new NRefactoryResolver(Dom.LanguageProperties.CSharp); + } } } diff --git a/src/AddIns/Misc/SharpRefactoring/Src/MethodExtractorBase.cs b/src/AddIns/Misc/SharpRefactoring/Src/MethodExtractorBase.cs index 7dea6962b4..10341c6e4a 100644 --- a/src/AddIns/Misc/SharpRefactoring/Src/MethodExtractorBase.cs +++ b/src/AddIns/Misc/SharpRefactoring/Src/MethodExtractorBase.cs @@ -9,13 +9,13 @@ 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; @@ -260,7 +260,7 @@ namespace SharpRefactoring return false; } - protected bool IsInitializedVariable(bool caseSensitive, ParametrizedNode member, LocalLookupVariable variable) + protected bool IsInitializedVariable(bool caseSensitive, ParametrizedNode member, Variable variable) { if (!(variable.Initializer.IsNull)) { return true; @@ -279,9 +279,11 @@ namespace SharpRefactoring 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); @@ -291,5 +293,25 @@ namespace SharpRefactoring public abstract IOutputAstVisitor GetOutputVisitor(); 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; + } } }