Skip to content

Commit a08e9e1

Browse files
Merge pull request #68432 from CyrusNajmabadi/extractMethodPerf
Lift variable for performance in extract method
2 parents 43913e6 + 75d322a commit a08e9e1

File tree

1 file changed

+20
-31
lines changed

1 file changed

+20
-31
lines changed

src/Features/Core/Portable/ExtractMethod/SelectionResult.cs

Lines changed: 20 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ public bool ShouldPutAsyncModifier()
118118
{
119119
var firstToken = GetFirstTokenInSelection();
120120
var lastToken = GetLastTokenInSelection();
121+
var syntaxFacts = SemanticDocument.Project.Services.GetService<ISyntaxFactsService>();
121122

122123
for (var currentToken = firstToken;
123124
currentToken.Span.End < lastToken.SpanStart;
@@ -129,7 +130,7 @@ public bool ShouldPutAsyncModifier()
129130
//
130131
// for the case above, even if the selection contains "await", it doesn't belong to the enclosing block
131132
// which extract method is applied to
132-
if (SemanticDocument.Project.Services.GetService<ISyntaxFactsService>().IsAwaitKeyword(currentToken)
133+
if (syntaxFacts.IsAwaitKeyword(currentToken)
133134
&& !UnderAnonymousOrLocalMethod(currentToken, firstToken, lastToken))
134135
{
135136
return true;
@@ -141,6 +142,8 @@ public bool ShouldPutAsyncModifier()
141142

142143
public bool ShouldCallConfigureAwaitFalse()
143144
{
145+
var syntaxFacts = SemanticDocument.Project.Services.GetService<ISyntaxFactsService>();
146+
144147
var firstToken = GetFirstTokenInSelection();
145148
var lastToken = GetLastTokenInSelection();
146149

@@ -149,49 +152,35 @@ public bool ShouldCallConfigureAwaitFalse()
149152
foreach (var node in SemanticDocument.Root.DescendantNodesAndSelf())
150153
{
151154
if (!node.Span.OverlapsWith(span))
152-
{
153155
continue;
154-
}
155156

156157
if (IsConfigureAwaitFalse(node) && !UnderAnonymousOrLocalMethod(node.GetFirstToken(), firstToken, lastToken))
157-
{
158158
return true;
159-
}
160159
}
161160

162161
return false;
163-
}
164162

165-
private bool IsConfigureAwaitFalse(SyntaxNode node)
166-
{
167-
var syntaxFacts = SemanticDocument.Project.Services.GetService<ISyntaxFactsService>();
168-
if (!syntaxFacts.IsInvocationExpression(node))
163+
bool IsConfigureAwaitFalse(SyntaxNode node)
169164
{
170-
return false;
171-
}
165+
if (!syntaxFacts.IsInvocationExpression(node))
166+
return false;
172167

173-
var invokedExpression = syntaxFacts.GetExpressionOfInvocationExpression(node);
174-
if (!syntaxFacts.IsSimpleMemberAccessExpression(invokedExpression))
175-
{
176-
return false;
177-
}
168+
var invokedExpression = syntaxFacts.GetExpressionOfInvocationExpression(node);
169+
if (!syntaxFacts.IsSimpleMemberAccessExpression(invokedExpression))
170+
return false;
178171

179-
var name = syntaxFacts.GetNameOfMemberAccessExpression(invokedExpression);
180-
var identifier = syntaxFacts.GetIdentifierOfSimpleName(name);
181-
if (!syntaxFacts.StringComparer.Equals(identifier.ValueText, nameof(Task.ConfigureAwait)))
182-
{
183-
return false;
184-
}
172+
var name = syntaxFacts.GetNameOfMemberAccessExpression(invokedExpression);
173+
var identifier = syntaxFacts.GetIdentifierOfSimpleName(name);
174+
if (!syntaxFacts.StringComparer.Equals(identifier.ValueText, nameof(Task.ConfigureAwait)))
175+
return false;
185176

186-
var arguments = syntaxFacts.GetArgumentsOfInvocationExpression(node);
187-
if (arguments.Count != 1)
188-
{
189-
return false;
190-
}
177+
var arguments = syntaxFacts.GetArgumentsOfInvocationExpression(node);
178+
if (arguments.Count != 1)
179+
return false;
191180

192-
var argument = arguments[0];
193-
var expression = syntaxFacts.GetExpressionOfArgument(argument);
194-
return syntaxFacts.IsFalseLiteralExpression(expression);
181+
var expression = syntaxFacts.GetExpressionOfArgument(arguments[0]);
182+
return syntaxFacts.IsFalseLiteralExpression(expression);
183+
}
195184
}
196185
}
197186
}

0 commit comments

Comments
 (0)