Skip to content

Commit 0358e72

Browse files
committed
Merge remote-tracking branch 'upstream/main' into ReloadLSPServerAfterSolutionClose
2 parents aa2b0d8 + ad14335 commit 0358e72

File tree

78 files changed

+1469
-375
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

78 files changed

+1469
-375
lines changed

src/CodeStyle/Core/CodeFixes/LanguageServices/SemanticModelWorkspaceService/SemanticModelWorkspaceServiceFactory.SemanticModelWorkspaceService.cs

Lines changed: 0 additions & 28 deletions
This file was deleted.

src/CodeStyle/VisualBasic/Analyzers/Microsoft.CodeAnalysis.VisualBasic.CodeStyle.vbproj

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,31 +10,6 @@
1010
<!-- Package used for symbol publishing. The assembly ships in analyzer packages Microsoft.CodeAnalysis.(CSharp|VisualBasic).CodeStyle -->
1111
<IsSymbolPublishingPackage>true</IsSymbolPublishingPackage>
1212
</PropertyGroup>
13-
<ItemGroup>
14-
<Compile Include="..\..\..\Workspaces\VisualBasic\Portable\Formatting\VisualBasicSyntaxFormatting.vb" Link="Formatting\VisualBasicSyntaxFormatting.vb" />
15-
<Compile Include="..\..\..\Workspaces\VisualBasic\Portable\Formatting\Rules\StructuredTriviaFormattingRule.vb" Link="Formatting\Rules\StructuredTriviaFormattingRule.vb" />
16-
<Compile Include="..\..\..\Workspaces\VisualBasic\Portable\Formatting\Rules\NodeBasedFormattingRule.vb" Link="Formatting\Rules\NodeBasedFormattingRule.vb" />
17-
<Compile Include="..\..\..\Workspaces\VisualBasic\Portable\Formatting\Rules\ElasticTriviaFormattingRule.vb" Link="Formatting\Rules\ElasticTriviaFormattingRule.vb" />
18-
<Compile Include="..\..\..\Workspaces\VisualBasic\Portable\Formatting\Rules\BaseFormattingRule.vb" Link="Formatting\Rules\BaseFormattingRule.vb" />
19-
<Compile Include="..\..\..\Workspaces\VisualBasic\Portable\Formatting\Rules\AlignTokensFormattingRule.vb" Link="Formatting\Rules\AlignTokensFormattingRule.vb" />
20-
<Compile Include="..\..\..\Workspaces\VisualBasic\Portable\Formatting\Rules\AdjustSpaceFormattingRule.vb" Link="Formatting\Rules\AdjustSpaceFormattingRule.vb" />
21-
<Compile Include="..\..\..\Workspaces\VisualBasic\Portable\Formatting\FormattingHelpers.vb" Link="Formatting\FormattingHelpers.vb" />
22-
<Compile Include="..\..\..\Workspaces\VisualBasic\Portable\Formatting\Engine\VisualBasicStructuredTriviaFormatEngine.vb" Link="Formatting\Engine\VisualBasicStructuredTriviaFormatEngine.vb" />
23-
<Compile Include="..\..\..\Workspaces\VisualBasic\Portable\Formatting\Engine\VisualBasicFormatEngine.vb" Link="Formatting\Engine\VisualBasicFormatEngine.vb" />
24-
<Compile Include="..\..\..\Workspaces\VisualBasic\Portable\Formatting\Engine\Trivia\VisualBasicTriviaFormatter.vb" Link="Formatting\Engine\Trivia\VisualBasicTriviaFormatter.vb" />
25-
<Compile Include="..\..\..\Workspaces\VisualBasic\Portable\Formatting\Engine\Trivia\TriviaDataFactory.vb" Link="Formatting\Engine\Trivia\TriviaDataFactory.vb" />
26-
<Compile Include="..\..\..\Workspaces\VisualBasic\Portable\Formatting\Engine\Trivia\TriviaDataFactory.TriviaRewriter.vb" Link="Formatting\Engine\Trivia\TriviaDataFactory.TriviaRewriter.vb" />
27-
<Compile Include="..\..\..\Workspaces\VisualBasic\Portable\Formatting\Engine\Trivia\TriviaDataFactory.ModifiedComplexTrivia.vb" Link="Formatting\Engine\Trivia\TriviaDataFactory.ModifiedComplexTrivia.vb" />
28-
<Compile Include="..\..\..\Workspaces\VisualBasic\Portable\Formatting\Engine\Trivia\TriviaDataFactory.LineContinuationTrivia.vb" Link="Formatting\Engine\Trivia\TriviaDataFactory.LineContinuationTrivia.vb" />
29-
<Compile Include="..\..\..\Workspaces\VisualBasic\Portable\Formatting\Engine\Trivia\TriviaDataFactory.FormattedComplexTrivia.vb" Link="Formatting\Engine\Trivia\TriviaDataFactory.FormattedComplexTrivia.vb" />
30-
<Compile Include="..\..\..\Workspaces\VisualBasic\Portable\Formatting\Engine\Trivia\TriviaDataFactory.ComplexTrivia.vb" Link="Formatting\Engine\Trivia\TriviaDataFactory.ComplexTrivia.vb" />
31-
<Compile Include="..\..\..\Workspaces\VisualBasic\Portable\Formatting\Engine\Trivia\TriviaDataFactory.CodeShapeAnalyzer.vb" Link="Formatting\Engine\Trivia\TriviaDataFactory.CodeShapeAnalyzer.vb" />
32-
<Compile Include="..\..\..\Workspaces\VisualBasic\Portable\Formatting\Engine\Trivia\TriviaDataFactory.Analyzer.vb" Link="Formatting\Engine\Trivia\TriviaDataFactory.Analyzer.vb" />
33-
<Compile Include="..\..\..\Workspaces\VisualBasic\Portable\Formatting\Engine\Trivia\TriviaDataFactory.AbstractLineBreakTrivia.vb" Link="Formatting\Engine\Trivia\TriviaDataFactory.AbstractLineBreakTrivia.vb" />
34-
<Compile Include="..\..\..\Workspaces\VisualBasic\Portable\Formatting\Engine\FormattingResult.vb" Link="Formatting\Engine\FormattingResult.vb" />
35-
<Compile Include="..\..\..\Workspaces\VisualBasic\Portable\Formatting\Engine\AggregatedFormattingResult.vb" Link="Formatting\Engine\AggregatedFormattingResult.vb" />
36-
<Compile Include="..\..\..\Workspaces\VisualBasic\Portable\Formatting\DefaultOperationProvider.vb" Link="Formatting\DefaultOperationProvider.vb" />
37-
</ItemGroup>
3813
<ItemGroup>
3914
<Import Include="Microsoft.CodeAnalysis.Shared.Extensions" />
4015
<Import Include="Microsoft.CodeAnalysis.Shared.Utilities" />

src/Compilers/Test/Core/Microsoft.CodeAnalysis.Test.Utilities.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
<ProjectReference Include="..\..\..\Compilers\Core\Portable\Microsoft.CodeAnalysis.csproj" />
1717
<ProjectReference Include="..\..\..\Compilers\CSharp\Portable\Microsoft.CodeAnalysis.CSharp.csproj" />
1818
<ProjectReference Include="..\..\..\Compilers\VisualBasic\Portable\Microsoft.CodeAnalysis.VisualBasic.vbproj" />
19-
<ProjectReference Include="..\..\..\RoslynAnalyzers\Microsoft.CodeAnalysis.AnalyzerUtilities\Microsoft.CodeAnalysis.AnalyzerUtilities.csproj" />
2019
</ItemGroup>
2120
<ItemGroup>
2221
<InternalsVisibleTo Include="Microsoft.CodeAnalysis.CSharp.CommandLine.UnitTests" />

src/Features/CSharp/Portable/ConvertPrimaryToRegularConstructor/ConvertPrimaryToRegularConstructorCodeRefactoringProvider.cs

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ private static async Task<Solution> ConvertAsync(
119119
RemovePrimaryConstructorParameterList();
120120
RemovePrimaryConstructorBaseTypeArgumentList();
121121
RemovePrimaryConstructorTargetingAttributes();
122-
RemoveDirectFieldAndPropertyAssignments();
122+
await RemoveDirectFieldAndPropertyAssignmentsAsync().ConfigureAwait(false);
123123
AddNewFields();
124124
AddConstructorDeclaration();
125125
await RewritePrimaryConstructorParameterReferencesAsync().ConfigureAwait(false);
@@ -265,14 +265,16 @@ void RemovePrimaryConstructorTargetingAttributes()
265265
mainDocumentEditor.RemoveNode(attributeList);
266266
}
267267

268-
void RemoveDirectFieldAndPropertyAssignments()
268+
async Task RemoveDirectFieldAndPropertyAssignmentsAsync()
269269
{
270270
// Remove all the initializers from existing fields/props the params are assigned to.
271271
foreach (var (_, initializer) in initializedFieldsAndProperties)
272272
{
273+
var editor = await solutionEditor.GetDocumentEditorAsync(solution.GetDocumentId(initializer.SyntaxTree), cancellationToken).ConfigureAwait(false);
274+
273275
if (initializer.Parent is PropertyDeclarationSyntax propertyDeclaration)
274276
{
275-
mainDocumentEditor.ReplaceNode(
277+
editor.ReplaceNode(
276278
propertyDeclaration,
277279
propertyDeclaration
278280
.WithInitializer(null)
@@ -281,7 +283,7 @@ void RemoveDirectFieldAndPropertyAssignments()
281283
}
282284
else if (initializer.Parent is VariableDeclaratorSyntax)
283285
{
284-
mainDocumentEditor.RemoveNode(initializer);
286+
editor.RemoveNode(initializer);
285287
}
286288
else
287289
{
@@ -468,14 +470,17 @@ ConstructorDeclarationSyntax CreateConstructorDeclaration()
468470
assignmentStatements.Add(ExpressionStatement(assignment));
469471
}
470472

471-
// Next, actually assign to all the fields/properties that were previously referencing any primary
472-
// constructor parameters.
473-
foreach (var (fieldOrProperty, initializer) in initializedFieldsAndProperties.OrderBy(i => i.initializer.SpanStart))
473+
// Next, actually assign to all the fields/properties that were previously referencing any primary constructor parameters.
474+
// Chunk assignments by declarations they are in starting from the declaration with the primary constructor
475+
foreach (var location in namedType.Locations.OrderBy(l => !ReferenceEquals(l.SourceTree, typeDeclaration.SyntaxTree)))
474476
{
475-
var left = MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, ThisExpression(), fieldOrProperty.Name.ToIdentifierName())
476-
.WithAdditionalAnnotations(Simplifier.Annotation);
477-
var assignment = AssignmentExpression(SyntaxKind.SimpleAssignmentExpression, left, initializer.EqualsToken, initializer.Value);
478-
assignmentStatements.Add(ExpressionStatement(assignment));
477+
foreach (var (fieldOrProperty, initializer) in initializedFieldsAndProperties.Where(i => ReferenceEquals(i.initializer.SyntaxTree, location.SourceTree)).OrderBy(i => i.initializer.SpanStart))
478+
{
479+
var left = MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, ThisExpression(), fieldOrProperty.Name.ToIdentifierName())
480+
.WithAdditionalAnnotations(Simplifier.Annotation);
481+
var assignment = AssignmentExpression(SyntaxKind.SimpleAssignmentExpression, left, initializer.EqualsToken, initializer.Value);
482+
assignmentStatements.Add(ExpressionStatement(assignment));
483+
}
479484
}
480485

481486
var rewrittenParameters = parameterList.ReplaceNodes(

src/Features/CSharp/Portable/ConvertProgram/ConvertProgramTransform_TopLevelStatements.cs

Lines changed: 55 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
using Microsoft.CodeAnalysis.Formatting;
1818
using Microsoft.CodeAnalysis.PooledObjects;
1919
using Microsoft.CodeAnalysis.RemoveUnnecessaryImports;
20+
using Microsoft.CodeAnalysis.Shared.Collections;
2021
using Microsoft.CodeAnalysis.Shared.Extensions;
2122
using Roslyn.Utilities;
2223

@@ -33,7 +34,7 @@ public static async Task<Document> ConvertToTopLevelStatementsAsync(
3334
Contract.ThrowIfNull(typeDeclaration); // checked by analyzer
3435

3536
var generator = document.GetRequiredLanguageService<SyntaxGenerator>();
36-
var root = await document.GetRequiredSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
37+
var root = (CompilationUnitSyntax)await document.GetRequiredSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
3738

3839
var semanticModel = await document.GetRequiredSemanticModelAsync(cancellationToken).ConfigureAwait(false);
3940
var rootWithGlobalStatements = GetRootWithGlobalStatements(
@@ -97,7 +98,7 @@ private static void AddUsingDirectives(NameSyntax name, SyntaxAnnotation annotat
9798
private static SyntaxNode GetRootWithGlobalStatements(
9899
SemanticModel semanticModel,
99100
SyntaxGenerator generator,
100-
SyntaxNode root,
101+
CompilationUnitSyntax root,
101102
TypeDeclarationSyntax typeDeclaration,
102103
MethodDeclarationSyntax methodDeclaration,
103104
CancellationToken cancellationToken)
@@ -107,19 +108,17 @@ private static SyntaxNode GetRootWithGlobalStatements(
107108
semanticModel, typeDeclaration, methodDeclaration, cancellationToken);
108109

109110
var namespaceDeclaration = typeDeclaration.Parent as BaseNamespaceDeclarationSyntax;
111+
110112
if (namespaceDeclaration != null &&
111113
namespaceDeclaration.Members.Count >= 2)
112114
{
113115
// Our parent namespace has another symbol in it. Keep the namespace declaration around, removing only
114116
// the existing Program type from it.
115117
editor.RemoveNode(typeDeclaration);
116-
editor.ReplaceNode(
117-
root,
118-
(current, _) =>
119-
{
120-
var currentRoot = (CompilationUnitSyntax)current;
121-
return currentRoot.WithMembers(currentRoot.Members.InsertRange(0, globalStatements));
122-
});
118+
editor.InsertBefore(namespaceDeclaration, globalStatements);
119+
120+
// We want to place the trailing directive on the namespace declaration we're preceding.
121+
AddDirectivesToNextMemberOrEndOfFile(root.Members.IndexOf(namespaceDeclaration));
123122
}
124123
else if (namespaceDeclaration != null)
125124
{
@@ -136,18 +135,60 @@ private static SyntaxNode GetRootWithGlobalStatements(
136135
globalStatements[0].WithPrependedLeadingTrivia(fileBanner));
137136
}
138137

139-
editor.ReplaceNode(
140-
root,
141-
root.ReplaceNode(namespaceDeclaration, globalStatements));
138+
editor.ReplaceNode(namespaceDeclaration, (_, _) => globalStatements);
139+
140+
// We're removing the namespace itself. So we want to place the trailing directive on the element that follows that.
141+
AddDirectivesToNextMemberOrEndOfFile(root.Members.IndexOf(namespaceDeclaration) + 1);
142142
}
143143
else
144144
{
145145
// type wasn't in a namespace. just remove the type and replace it with the new global statements.
146-
editor.ReplaceNode(
147-
root, root.ReplaceNode(typeDeclaration, globalStatements));
146+
editor.ReplaceNode(typeDeclaration, (_, _) => globalStatements);
147+
148+
// We're removing the namespace itself. So we want to place the trailing directive on the element that follows that.
149+
AddDirectivesToNextMemberOrEndOfFile(root.Members.IndexOf(typeDeclaration) + 1);
148150
}
149151

150152
return editor.GetChangedRoot();
153+
154+
void AddDirectivesToNextMemberOrEndOfFile(int memberIndexToPlaceTrailingDirectivesOn)
155+
{
156+
// If the method has trailing directive on the close brace, move them to whatever will come after the
157+
// final global statement in the new file. That could be the next namespace/type member declaration. Or
158+
// it could be the end of file token if there are no more members in the file.
159+
if (methodDeclaration.Body is not BlockSyntax block)
160+
return;
161+
162+
var leadingCloseBraceTrivia = block.CloseBraceToken.LeadingTrivia;
163+
if (!leadingCloseBraceTrivia.Any(t => t.IsDirective))
164+
return;
165+
166+
if (memberIndexToPlaceTrailingDirectivesOn < root.Members.Count)
167+
{
168+
editor.ReplaceNode(
169+
root.Members[memberIndexToPlaceTrailingDirectivesOn],
170+
(current, _) =>
171+
{
172+
var updated = current.WithPrependedLeadingTrivia(leadingCloseBraceTrivia);
173+
updated = updated.ReplaceToken(
174+
updated.GetFirstToken(),
175+
updated.GetFirstToken().WithAdditionalAnnotations(Formatter.Annotation));
176+
return updated;
177+
});
178+
}
179+
else
180+
{
181+
editor.ReplaceNode(
182+
root,
183+
(current, _) =>
184+
{
185+
var currentRoot = (CompilationUnitSyntax)current;
186+
return currentRoot.WithEndOfFileToken(currentRoot.EndOfFileToken
187+
.WithPrependedLeadingTrivia(leadingCloseBraceTrivia)
188+
.WithAdditionalAnnotations(Formatter.Annotation));
189+
});
190+
}
191+
}
151192
}
152193

153194
private static ImmutableArray<GlobalStatementSyntax> GetGlobalStatements(

0 commit comments

Comments
 (0)