|
3 | 3 | // See the LICENSE file in the project root for more information. |
4 | 4 |
|
5 | 5 | using System.Collections.Generic; |
| 6 | +using System.Collections.Immutable; |
6 | 7 | using System.Linq; |
7 | 8 | using System.Threading; |
8 | 9 | using Microsoft.CodeAnalysis.CSharp.Syntax; |
9 | 10 | using Microsoft.CodeAnalysis.ExtractMethod; |
10 | 11 |
|
11 | 12 | namespace Microsoft.CodeAnalysis.CSharp.ExtractMethod; |
12 | 13 |
|
13 | | -internal sealed partial class CSharpMethodExtractor |
| 14 | +internal sealed partial class CSharpExtractMethodService |
14 | 15 | { |
15 | | - private sealed class CSharpAnalyzer(CSharpSelectionResult selectionResult, bool localFunction, CancellationToken cancellationToken) : Analyzer(selectionResult, localFunction, cancellationToken) |
| 16 | + internal sealed partial class CSharpMethodExtractor |
16 | 17 | { |
17 | | - private static readonly HashSet<int> s_nonNoisySyntaxKindSet = [(int)SyntaxKind.WhitespaceTrivia, (int)SyntaxKind.EndOfLineTrivia]; |
18 | | - |
19 | | - public static AnalyzerResult Analyze(CSharpSelectionResult selectionResult, bool localFunction, CancellationToken cancellationToken) |
| 18 | + private sealed class CSharpAnalyzer(CSharpSelectionResult selectionResult, bool localFunction, CancellationToken cancellationToken) : Analyzer(selectionResult, localFunction, cancellationToken) |
20 | 19 | { |
21 | | - var analyzer = new CSharpAnalyzer(selectionResult, localFunction, cancellationToken); |
22 | | - return analyzer.Analyze(); |
23 | | - } |
| 20 | + public static AnalyzerResult Analyze(CSharpSelectionResult selectionResult, bool localFunction, CancellationToken cancellationToken) |
| 21 | + { |
| 22 | + var analyzer = new CSharpAnalyzer(selectionResult, localFunction, cancellationToken); |
| 23 | + return analyzer.Analyze(); |
| 24 | + } |
24 | 25 |
|
25 | | - protected override bool TreatOutAsRef |
26 | | - => false; |
| 26 | + protected override bool TreatOutAsRef |
| 27 | + => false; |
27 | 28 |
|
28 | | - protected override bool IsInPrimaryConstructorBaseType() |
29 | | - => this.SelectionResult.GetContainingScopeOf<PrimaryConstructorBaseTypeSyntax>() != null; |
| 29 | + protected override bool IsInPrimaryConstructorBaseType() |
| 30 | + => this.SelectionResult.GetContainingScopeOf<PrimaryConstructorBaseTypeSyntax>() != null; |
30 | 31 |
|
31 | | - protected override VariableInfo CreateFromSymbol( |
32 | | - ISymbol symbol, ITypeSymbol type, VariableStyle style, bool variableDeclared) |
33 | | - { |
34 | | - return CreateFromSymbolCommon<LocalDeclarationStatementSyntax>(symbol, type, style, s_nonNoisySyntaxKindSet); |
35 | | - } |
| 32 | + protected override VariableInfo CreateFromSymbol( |
| 33 | + ISymbol symbol, ITypeSymbol type, VariableStyle style, bool variableDeclared) |
| 34 | + { |
| 35 | + return CreateFromSymbolCommon(symbol, type, style); |
| 36 | + } |
36 | 37 |
|
37 | | - protected override ITypeSymbol? GetRangeVariableType(SemanticModel model, IRangeVariableSymbol symbol) |
38 | | - { |
39 | | - var info = model.GetSpeculativeTypeInfo(SelectionResult.FinalSpan.Start, SyntaxFactory.ParseName(symbol.Name), SpeculativeBindingOption.BindAsExpression); |
40 | | - if (info.Type is IErrorTypeSymbol) |
41 | | - return null; |
| 38 | + protected override ITypeSymbol? GetRangeVariableType(IRangeVariableSymbol symbol) |
| 39 | + { |
| 40 | + var info = this.SemanticModel.GetSpeculativeTypeInfo(SelectionResult.FinalSpan.Start, SyntaxFactory.ParseName(symbol.Name), SpeculativeBindingOption.BindAsExpression); |
| 41 | + if (info.Type is IErrorTypeSymbol) |
| 42 | + return null; |
42 | 43 |
|
43 | | - return info.Type == null || info.Type.SpecialType == SpecialType.System_Object |
44 | | - ? info.Type |
45 | | - : info.ConvertedType; |
46 | | - } |
| 44 | + return info.Type == null || info.Type.SpecialType == SpecialType.System_Object |
| 45 | + ? info.Type |
| 46 | + : info.ConvertedType; |
| 47 | + } |
47 | 48 |
|
48 | | - protected override bool ContainsReturnStatementInSelectedCode(IEnumerable<SyntaxNode> jumpOutOfRegionStatements) |
49 | | - => jumpOutOfRegionStatements.Where(n => n is ReturnStatementSyntax).Any(); |
| 49 | + protected override bool ContainsReturnStatementInSelectedCode(ImmutableArray<SyntaxNode> exitPoints) |
| 50 | + => exitPoints.Any(n => n is ReturnStatementSyntax); |
50 | 51 |
|
51 | | - protected override bool ReadOnlyFieldAllowed() |
52 | | - { |
53 | | - var scope = SelectionResult.GetContainingScopeOf<ConstructorDeclarationSyntax>(); |
54 | | - return scope == null; |
55 | | - } |
| 52 | + protected override bool ReadOnlyFieldAllowed() |
| 53 | + { |
| 54 | + var scope = SelectionResult.GetContainingScopeOf<ConstructorDeclarationSyntax>(); |
| 55 | + return scope == null; |
| 56 | + } |
56 | 57 |
|
57 | | - protected override bool IsReadOutside(ISymbol symbol, HashSet<ISymbol> readOutsideMap) |
58 | | - { |
59 | | - if (!base.IsReadOutside(symbol, readOutsideMap)) |
60 | | - return false; |
| 58 | + protected override bool IsReadOutside(ISymbol symbol, HashSet<ISymbol> readOutsideMap) |
| 59 | + { |
| 60 | + if (!base.IsReadOutside(symbol, readOutsideMap)) |
| 61 | + return false; |
61 | 62 |
|
62 | | - // Special case `using var v = ...` where the selection grabs the last statement that follows the local |
63 | | - // declaration. The compiler here considers the local variable 'read outside' since it makes it to the |
64 | | - // implicit 'dispose' call that comes after the last statement. However, as that implicit dispose would |
65 | | - // move if we move the `using var v` entirely into the new method, then it's still safe to move as there's |
66 | | - // no actual "explicit user read" that happens in the outer caller at all. |
67 | | - if (!this.SelectionResult.SelectionInExpression && |
68 | | - symbol is ILocalSymbol { IsUsing: true, DeclaringSyntaxReferences: [var reference] } && |
69 | | - reference.GetSyntax(this.CancellationToken) is VariableDeclaratorSyntax |
70 | | - { |
71 | | - Parent: VariableDeclarationSyntax |
| 63 | + // Special case `using var v = ...` where the selection grabs the last statement that follows the local |
| 64 | + // declaration. The compiler here considers the local variable 'read outside' since it makes it to the |
| 65 | + // implicit 'dispose' call that comes after the last statement. However, as that implicit dispose would |
| 66 | + // move if we move the `using var v` entirely into the new method, then it's still safe to move as there's |
| 67 | + // no actual "explicit user read" that happens in the outer caller at all. |
| 68 | + if (!this.SelectionResult.IsExtractMethodOnExpression && |
| 69 | + symbol is ILocalSymbol { IsUsing: true, DeclaringSyntaxReferences: [var reference] } && |
| 70 | + reference.GetSyntax(this.CancellationToken) is VariableDeclaratorSyntax |
72 | 71 | { |
73 | | - Parent: LocalDeclarationStatementSyntax |
| 72 | + Parent: VariableDeclarationSyntax |
74 | 73 | { |
75 | | - Parent: BlockSyntax { Statements: [.., var lastBlockStatement] }, |
76 | | - }, |
77 | | - } |
78 | | - }) |
79 | | - { |
80 | | - var lastStatement = this.SelectionResult.GetLastStatement(); |
81 | | - if (lastStatement == lastBlockStatement) |
82 | | - return false; |
83 | | - } |
| 74 | + Parent: LocalDeclarationStatementSyntax |
| 75 | + { |
| 76 | + Parent: BlockSyntax { Statements: [.., var lastBlockStatement] }, |
| 77 | + }, |
| 78 | + } |
| 79 | + }) |
| 80 | + { |
| 81 | + var lastStatement = this.SelectionResult.GetLastStatement(); |
| 82 | + if (lastStatement == lastBlockStatement) |
| 83 | + return false; |
| 84 | + } |
84 | 85 |
|
85 | | - return true; |
| 86 | + return true; |
| 87 | + } |
86 | 88 | } |
87 | 89 | } |
88 | 90 | } |
0 commit comments