Skip to content

Commit

Permalink
Merge branch 'simpleUsing' into fileScopedNamespace
Browse files Browse the repository at this point in the history
  • Loading branch information
CyrusNajmabadi committed May 22, 2024
2 parents 077012c + 3e29648 commit a55b46a
Show file tree
Hide file tree
Showing 42 changed files with 933 additions and 558 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@ struct MyStruct
[WpfFact, Trait(Traits.Feature, Traits.Features.Completion)]
public abstract Task InsertSnippetInReadonlyStruct();

[WpfFact, Trait(Traits.Feature, Traits.Features.Completion)]
public abstract Task InsertSnippetInReadonlyStruct_ReadonlyModifierInOtherPartialDeclaration();

[WpfFact, Trait(Traits.Feature, Traits.Features.Completion)]
public abstract Task InsertSnippetInReadonlyStruct_ReadonlyModifierInOtherPartialDeclaration_MissingPartialModifier();

// This case might produce non-default results for different snippets (e.g. no `set` accessor in 'propg' snippet),
// so it is tested separately for all of them
[WpfFact, Trait(Traits.Feature, Traits.Features.Completion)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,39 @@ readonly struct MyStruct
""", "public int MyProperty { get; }");
}

public override async Task InsertSnippetInReadonlyStruct_ReadonlyModifierInOtherPartialDeclaration()
{
// Ensure we don't generate redundant `set` accessor when executed in readonly struct
await VerifyPropertyAsync("""
partial struct MyStruct
{
$$
}
readonly partial struct MyStruct
{
}
""", "public int MyProperty { get; }");
}

public override async Task InsertSnippetInReadonlyStruct_ReadonlyModifierInOtherPartialDeclaration_MissingPartialModifier()
{
// Even though there is no `partial` modifier on the first declaration
// compiler still treats the whole type as partial since it is more likely that
// the user's intent was to have a partial type and they just forgot the modifier.
// Thus we still recognize that as `readonly` context and don't generate a setter
await VerifyPropertyAsync("""
struct MyStruct
{
$$
}
readonly partial struct MyStruct
{
}
""", "public int MyProperty { get; }");
}

public override async Task InsertSnippetInInterface()
{
await VerifyDefaultPropertyAsync("""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,39 @@ readonly struct MyStruct
""", "public int MyProperty { get; }");
}

public override async Task InsertSnippetInReadonlyStruct_ReadonlyModifierInOtherPartialDeclaration()
{
// Ensure we don't generate redundant `set` accessor when executed in readonly struct
await VerifyPropertyAsync("""
partial struct MyStruct
{
$$
}
readonly partial struct MyStruct
{
}
""", "public int MyProperty { get; }");
}

public override async Task InsertSnippetInReadonlyStruct_ReadonlyModifierInOtherPartialDeclaration_MissingPartialModifier()
{
// Even though there is no `partial` modifier on the first declaration
// compiler still treats the whole type as partial since it is more likely that
// the user's intent was to have a partial type and they just forgot the modifier.
// Thus we still recognize that as `readonly` context and don't generate a setter
await VerifyPropertyAsync("""
struct MyStruct
{
$$
}
readonly partial struct MyStruct
{
}
""", "public int MyProperty { get; }");
}

public override async Task InsertSnippetInInterface()
{
// Ensure we don't generate redundant `set` accessor when executed in interface
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,34 @@ readonly struct MyStruct
""");
}

public override async Task InsertSnippetInReadonlyStruct_ReadonlyModifierInOtherPartialDeclaration()
{
await VerifyDefaultPropertyAsync("""
partial struct MyStruct
{
$$
}
readonly partial struct MyStruct
{
}
""");
}

public override async Task InsertSnippetInReadonlyStruct_ReadonlyModifierInOtherPartialDeclaration_MissingPartialModifier()
{
await VerifyDefaultPropertyAsync("""
struct MyStruct
{
$$
}
readonly partial struct MyStruct
{
}
""");
}

public override async Task InsertSnippetInInterface()
{
await VerifyDefaultPropertyAsync("""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,39 @@ class Program
await TestExtractMethodAsync(code, expected);
}

[Fact]
public async Task SelectTypeParameterWithAllowsRefStructAntiConstraint()
{
var code = """
using System;
class Program
{
void MyMethod1<TT>(TT tt) where TT : IDisposable, allows ref struct
{
[|tt.Dispose();|]
}
}
""";
var expected = """
using System;
class Program
{
void MyMethod1<TT>(TT tt) where TT : IDisposable, allows ref struct
{
NewMethod(tt);
}
private static void NewMethod<TT>(TT tt) where TT : IDisposable, allows ref struct
{
tt.Dispose();
}
}
""";

await TestExtractMethodAsync(code, expected);
}
[Fact]
public async Task SelectTypeParameter()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,30 @@ void Goo()
await TestAsync(markup, expectedOrderedItems);
}

[Fact]
public async Task DeclaringGenericTypeWithConstraintsAllowRefStruct()
{
var markup = """
class G<S> where S : allows ref struct
{ };
class C
{
void Goo()
{
[|G<$$|]>
}
}
""";

var expectedOrderedItems = new List<SignatureHelpTestItem>
{
new SignatureHelpTestItem("G<S> where S : allows ref struct", string.Empty, string.Empty, currentParameterIndex: 0)
};

await TestAsync(markup, expectedOrderedItems);
}

#endregion

#region "Generic member invocation"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Editor.Test;
using Microsoft.CodeAnalysis.Editor.UnitTests;
using Microsoft.CodeAnalysis.Editor.UnitTests.Workspaces;
using Microsoft.CodeAnalysis.Formatting;
using Microsoft.CodeAnalysis.Indentation;
using Microsoft.CodeAnalysis.Host;
using Microsoft.CodeAnalysis.Options;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.Shared.TestHooks;
Expand Down Expand Up @@ -468,10 +467,15 @@ End Class
var classC = classD.BaseType;
}

[Fact]
public async Task TestGetCompilationOnCrossLanguageDependentProjectChanged()
[Theory, CombinatorialData]
internal async Task TestGetCompilationOnCrossLanguageDependentProjectChanged(
SourceGeneratorExecutionPreference preference)
{
using var workspace = CreateWorkspace();
using var workspace = CreateWorkspace(composition: EditorTestCompositions.EditorFeatures.AddParts(typeof(TestWorkspaceConfigurationService)));

var configService = workspace.ExportProvider.GetExportedValue<TestWorkspaceConfigurationService>();
configService.Options = new WorkspaceConfigurationOptions(SourceGeneratorExecution: preference);

var solutionX = workspace.CurrentSolution;

var document1 = new EditorTestHostDocument(@"public class C { }");
Expand Down Expand Up @@ -514,7 +518,12 @@ End Class
var classDz = compilation2Z.SourceModule.GlobalNamespace.GetTypeMembers("D").Single();
var classCz = classDz.BaseType;

Assert.Equal(TypeKind.Error, classCz.TypeKind);
// In balanced mode the skeleton won't be regenerated. So the downstream project won't see the change to
// remove the class.
if (preference is SourceGeneratorExecutionPreference.Automatic)
Assert.Equal(TypeKind.Error, classCz.TypeKind);
else
Assert.Equal(TypeKind.Class, classCz.TypeKind);
}

[WpfFact]
Expand Down Expand Up @@ -575,12 +584,19 @@ End Class
}
}

[WpfFact]
public async Task TestGetCompilationOnCrossLanguageDependentProjectChangedInProgress()
[WpfTheory, CombinatorialData]
internal async Task TestGetCompilationOnCrossLanguageDependentProjectChangedInProgress(
SourceGeneratorExecutionPreference preference)
{
var composition = EditorTestCompositions.EditorFeatures.AddParts(typeof(TestDocumentTrackingService));
var composition = EditorTestCompositions.EditorFeatures.AddParts(
typeof(TestDocumentTrackingService),
typeof(TestWorkspaceConfigurationService));

using var workspace = CreateWorkspace(disablePartialSolutions: false, composition: composition);

var configService = workspace.ExportProvider.GetExportedValue<TestWorkspaceConfigurationService>();
configService.Options = new WorkspaceConfigurationOptions(SourceGeneratorExecution: preference);

var trackingService = (TestDocumentTrackingService)workspace.Services.GetRequiredService<IDocumentTrackingService>();
var solutionX = workspace.CurrentSolution;

Expand Down Expand Up @@ -662,8 +678,12 @@ End Class
}
}

// Should find now that we're going a normal compilation.
Assert.True(foundTheError, "Did not find error");
// In balanced mode the skeleton won't be regenerated. So the downstream project won't see the change to
// remove the class. So it will not find the error symbol.
if (preference is SourceGeneratorExecutionPreference.Automatic)
Assert.True(foundTheError);
else
Assert.False(foundTheError);
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -872,6 +872,33 @@ class C
Await TestAPIAndFeature(input, kind, host)
End Function

<WpfTheory, CombinatorialData>
Public Async Function TestConstructor_ImplicitAndExplicitObjectCreation_Local(kind As TestKind, host As TestHost) As Task
Dim input =
<Workspace>
<Project Language="C#" CommonReferences="true">
<Document>
class D
{
public {|Definition:$$D|}() { }
}
</Document>
<Document>
class C
{
void M()
{
D d = [|new|]();
D d2 = new [|D|]();
D d3 = [|new|]();
}
}
</Document>
</Project>
</Workspace>
Await TestAPIAndFeature(input, kind, host)
End Function

<WpfTheory, CombinatorialData>
Public Async Function TestConstructor_ImplicitObjectCreation_Local_WithArguments(kind As TestKind, host As TestHost) As Task
Dim input =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ public partial class GeneratedClass : IInterface { }
</Document>
</Project>
</Workspace>, host:=host, renameTo:="A", sourceGenerator:=New GeneratorThatImplementsInterfaceMethod())

End Using
End Sub

Expand Down
12 changes: 8 additions & 4 deletions src/EditorFeatures/Test2/Rename/RenameEngineResult.vb
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@ Imports System.Collections.Immutable
Imports System.Threading
Imports Microsoft.CodeAnalysis
Imports Microsoft.CodeAnalysis.CodeActions
Imports Microsoft.CodeAnalysis.CodeCleanup
Imports Microsoft.CodeAnalysis.Editor.UnitTests.Workspaces
Imports Microsoft.CodeAnalysis.Host
Imports Microsoft.CodeAnalysis.Options
Imports Microsoft.CodeAnalysis.Remote.Testing
Imports Microsoft.CodeAnalysis.Rename
Imports Microsoft.CodeAnalysis.Rename.ConflictEngine
Expand Down Expand Up @@ -57,18 +55,24 @@ Namespace Microsoft.CodeAnalysis.Editor.UnitTests.Rename
host As RenameTestHost,
Optional renameOptions As SymbolRenameOptions = Nothing,
Optional expectFailure As Boolean = False,
Optional sourceGenerator As ISourceGenerator = Nothing) As RenameEngineResult
Optional sourceGenerator As ISourceGenerator = Nothing,
Optional executionPreference As SourceGeneratorExecutionPreference = SourceGeneratorExecutionPreference.Automatic) As RenameEngineResult

Dim composition = EditorTestCompositions.EditorFeatures.AddParts(
GetType(NoCompilationContentTypeLanguageService),
GetType(NoCompilationContentTypeDefinitions),
GetType(WorkspaceTestLogger))
GetType(WorkspaceTestLogger),
GetType(TestWorkspaceConfigurationService))

If host = RenameTestHost.OutOfProcess_SingleCall OrElse host = RenameTestHost.OutOfProcess_SplitCall Then
composition = composition.WithTestHostParts(TestHost.OutOfProcess)
End If

Dim workspace = TestWorkspace.CreateWorkspace(workspaceXml, composition:=composition)

Dim configService = workspace.ExportProvider.GetExportedValue(Of TestWorkspaceConfigurationService)
configService.Options = New WorkspaceConfigurationOptions(SourceGeneratorExecution:=executionPreference)

workspace.Services.SolutionServices.SetWorkspaceTestOutput(helper)

If sourceGenerator IsNot Nothing Then
Expand Down
6 changes: 2 additions & 4 deletions src/EditorFeatures/Test2/Rename/RenameTestHelpers.vb
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@ Imports Microsoft.CodeAnalysis.Editor.Implementation.RenameTracking
Imports Microsoft.CodeAnalysis.Editor.Shared.Utilities
Imports Microsoft.CodeAnalysis.Editor.UnitTests.RenameTracking
Imports Microsoft.CodeAnalysis.Editor.UnitTests.Utilities.GoToHelpers
Imports Microsoft.CodeAnalysis.Editor.UnitTests.Workspaces
Imports Microsoft.CodeAnalysis.Options
Imports Microsoft.CodeAnalysis.Shared.TestHooks
Imports Microsoft.CodeAnalysis.Rename
Imports Microsoft.CodeAnalysis.Text
Imports Microsoft.CodeAnalysis.Text.Shared.Extensions
Imports Microsoft.VisualStudio.Text
Expand All @@ -22,10 +20,10 @@ Imports Microsoft.VisualStudio.Text.Tagging

Namespace Microsoft.CodeAnalysis.Editor.UnitTests.Rename
Friend Module RenameTestHelpers

Private ReadOnly s_composition As TestComposition = EditorTestCompositions.EditorFeaturesWpf.AddParts(
GetType(MockDocumentNavigationServiceFactory),
GetType(MockPreviewDialogService))
GetType(MockPreviewDialogService),
GetType(TestWorkspaceConfigurationService))

Private Function GetSessionInfo(workspace As EditorTestWorkspace) As (document As Document, textSpan As TextSpan)
Dim hostdoc = workspace.DocumentWithCursor
Expand Down
Loading

0 comments on commit a55b46a

Please sign in to comment.