提交 9f202244 编写于 作者: A Allison Chou

Change name generating implementation

上级 1ea92fa2
......@@ -41,7 +41,7 @@ protected override SyntaxToken CreateMethodName(bool localFunction)
var semanticModel = this.SemanticDocument.SemanticModel;
var nameGenerator = new UniqueNameGenerator(semanticModel);
return SyntaxFactory.Identifier(nameGenerator.CreateUniqueMethodName(containingScope, methodName, localFunction));
return SyntaxFactory.Identifier(nameGenerator.CreateUniqueMethodName(containingScope, methodName));
}
private static string GetMethodNameBasedOnExpression(string methodName, SyntaxNode expression)
......
......@@ -47,7 +47,14 @@ protected override SyntaxToken CreateMethodName(bool localFunction)
var semanticModel = this.SemanticDocument.SemanticModel;
var nameGenerator = new UniqueNameGenerator(semanticModel);
var scope = this.CSharpSelectionResult.GetContainingScope();
return SyntaxFactory.Identifier(nameGenerator.CreateUniqueMethodName(scope, "NewMethod", localFunction));
// If extracting a local function, we want to ensure all local variables are considered when generating a unique name.
if (localFunction)
{
scope = this.CSharpSelectionResult.GetFirstTokenInSelection().Parent;
}
return SyntaxFactory.Identifier(nameGenerator.CreateUniqueMethodName(scope, "NewMethod"));
}
protected override IEnumerable<StatementSyntax> GetInitialStatementsForMethodDefinitions()
......
......@@ -39,7 +39,14 @@ protected override SyntaxToken CreateMethodName(bool localFunction)
var semanticModel = this.SemanticDocument.SemanticModel;
var nameGenerator = new UniqueNameGenerator(semanticModel);
var scope = this.CSharpSelectionResult.GetContainingScope();
return SyntaxFactory.Identifier(nameGenerator.CreateUniqueMethodName(scope, "NewMethod", localFunction));
// If extracting a local function, we want to ensure all local variables are considered when generating a unique name.
if (localFunction)
{
scope = this.CSharpSelectionResult.GetFirstTokenInSelection().Parent;
}
return SyntaxFactory.Identifier(nameGenerator.CreateUniqueMethodName(scope, "NewMethod"));
}
protected override IEnumerable<StatementSyntax> GetInitialStatementsForMethodDefinitions()
......
......@@ -16,23 +16,11 @@ public UniqueNameGenerator(SemanticModel semanticModel)
_semanticModel = semanticModel;
}
public string CreateUniqueMethodName(SyntaxNode contextNode, string baseName, bool localFunction = false)
public string CreateUniqueMethodName(SyntaxNode contextNode, string baseName)
{
Contract.ThrowIfNull(contextNode);
Contract.ThrowIfNull(baseName);
if (localFunction)
{
// When generating local functions, we also want to take into account the names of local variables.
var childNodes = contextNode.ChildNodes().AsArray();
if (childNodes.Length > 0)
{
// If we take the first child node, we may end up with part of the method header.
// To be safe, we use the last child node, which is guaranteed to be part of the context method's body.
contextNode = (SyntaxNode)childNodes.GetValue(childNodes.Length - 1);
}
}
return NameGenerator.GenerateUniqueName(baseName, string.Empty,
n => _semanticModel.LookupSymbols(contextNode.SpanStart, container: null, n).Length == 0);
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册