Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

File types IDE changes #62215

Merged
merged 20 commits into from
Jul 5, 2022
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,8 @@ INamedTypeSymbol INamedTypeSymbol.TupleUnderlyingType

bool INamedTypeSymbol.IsSerializable => UnderlyingNamedTypeSymbol.IsSerializable;

bool INamedTypeSymbol.IsFile => UnderlyingNamedTypeSymbol.AssociatedSyntaxTree is not null;

INamedTypeSymbol INamedTypeSymbol.NativeIntegerUnderlyingType => UnderlyingNamedTypeSymbol.NativeIntegerUnderlyingType.GetPublicSymbol();

#region ISymbol Members
Expand Down
5 changes: 5 additions & 0 deletions src/Compilers/Core/Portable/Symbols/INamedTypeSymbol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ public interface INamedTypeSymbol : ITypeSymbol
/// </summary>
bool IsComImport { get; }

/// <summary>
/// Indicates the type is declared in source and is only visible in the file it is declared in.
/// </summary>
bool IsFile { get; }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 For the record, the API review for this is tracked by #62254


/// <summary>
/// Returns collection of names of members declared within this type.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1216,6 +1216,12 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Symbols
End Get
End Property

Private ReadOnly Property INamedTypeSymbol_IsFile As Boolean Implements INamedTypeSymbol.IsFile
Get
Return False
End Get
End Property

Private ReadOnly Property INamedTypeSymbol_NativeIntegerUnderlyingType As INamedTypeSymbol Implements INamedTypeSymbol.NativeIntegerUnderlyingType
Get
Return Nothing
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -684,5 +684,130 @@ class C

await VerifyItemIsAbsentAsync(markup, "required");
}

[Fact]
public async Task SuggestFileOnTypes()
{
var markup = $$"""
$$ class C { }
""";

await VerifyItemExistsAsync(markup, "file");
}

[Fact]
public async Task DoNotSuggestFileAfterFile()
{
var markup = $$"""
file $$
""";

await VerifyItemIsAbsentAsync(markup, "file");
}

[Fact]
public async Task SuggestFileBeforeFileType()
{
var markup = $$"""
$$

file class C { }
""";

// it might seem like we want to prevent 'file file class',
// but it's likely the user is declaring a file type above an existing file type here.
await VerifyItemExistsAsync(markup, "file");
}

[Fact]
public async Task DoNotSuggestFileOnNestedTypes()
{
var markup = $$"""
class Outer
{
$$ class C { }
}
""";

await VerifyItemIsAbsentAsync(markup, "file");
}

[Fact]
public async Task DoNotSuggestFileOnNonTypeMembers()
{
var markup = $$"""
class C
{
$$
}
""";

await VerifyItemIsAbsentAsync(markup, "file");
}

[Theory]
[InlineData("public")]
[InlineData("internal")]
[InlineData("protected")]
[InlineData("private")]
public async Task DoNotSuggestFileAfterFilteredKeywords(string keyword)
{
var markup = $$"""
{{keyword}} $$
""";

await VerifyItemIsAbsentAsync(markup, "file");
}

[Theory]
[InlineData("public")]
[InlineData("internal")]
[InlineData("protected")]
[InlineData("private")]
public async Task DoNotSuggestFilteredKeywordsAfterFile(string keyword)
{
var markup = $$"""
file $$
""";

await VerifyItemIsAbsentAsync(markup, keyword);
}

[Fact]
public async Task SuggestFileInFileScopedNamespace()
{
var markup = $$"""
namespace NS;

$$
""";

await VerifyItemExistsAsync(markup, "file");
}

[Fact]
public async Task SuggestFileInNamespace()
{
var markup = $$"""
namespace NS
{
$$
}
""";

await VerifyItemExistsAsync(markup, "file");
}

[Fact]
public async Task SuggestFileAfterClass()
{
var markup = $$"""
file class C { }

$$
""";

await VerifyItemExistsAsync(markup, "file");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,98 @@ public void M<Z>(List<Z> list)
ResolveAndVerifySymbolList(members1, members2, comp1);
}

[Fact]
public void FileType1()
{
var src1 = @"using System;

namespace N1.N2
{
file class C { }
}
";
var originalComp = CreateCompilation(src1, assemblyName: "Test");
var newComp = CreateCompilation(src1, assemblyName: "Test");

var originalSymbols = GetSourceSymbols(originalComp, SymbolCategory.DeclaredType | SymbolCategory.DeclaredNamespace).OrderBy(s => s.Name).ToArray();
var newSymbols = GetSourceSymbols(newComp, SymbolCategory.DeclaredType | SymbolCategory.DeclaredNamespace).OrderBy(s => s.Name).ToArray();

Assert.Equal(3, originalSymbols.Length);
ResolveAndVerifySymbolList(newSymbols, originalSymbols, originalComp);
}

[Fact]
public void FileType2()
{
var src1 = @"using System;

namespace N1.N2
{
file class C<T> { }
}
";
var originalComp = CreateCompilation(src1, assemblyName: "Test");
var newComp = CreateCompilation(src1, assemblyName: "Test");

var originalSymbols = GetSourceSymbols(originalComp, SymbolCategory.DeclaredType | SymbolCategory.DeclaredNamespace).OrderBy(s => s.Name).ToArray();
var newSymbols = GetSourceSymbols(newComp, SymbolCategory.DeclaredType | SymbolCategory.DeclaredNamespace).OrderBy(s => s.Name).ToArray();

Assert.Equal(3, originalSymbols.Length);
ResolveAndVerifySymbolList(newSymbols, originalSymbols, originalComp);
}

[Fact]
public void FileType3()
{
var src1 = @"using System;

namespace N1.N2
{
file class C { }
}
";
// this should result in two entirely separate file symbols.
// note that the IDE can only distinguish file type symbols with the same name when they have distinct file paths.
// We are OK with this as we will require file types with identical names to have distinct file paths later in the preview.
// See https://github.com/dotnet/roslyn/issues/61999
var originalComp = CreateCompilation(new[] { SyntaxFactory.ParseSyntaxTree(src1, path: "file1.cs"), SyntaxFactory.ParseSyntaxTree(src1, path: "file2.cs") }, assemblyName: "Test");
var newComp = CreateCompilation(new[] { SyntaxFactory.ParseSyntaxTree(src1, path: "file1.cs"), SyntaxFactory.ParseSyntaxTree(src1, path: "file2.cs") }, assemblyName: "Test");

var originalSymbols = GetSourceSymbols(originalComp, SymbolCategory.DeclaredType | SymbolCategory.DeclaredNamespace).OrderBy(s => s.Name).ToArray();
var newSymbols = GetSourceSymbols(newComp, SymbolCategory.DeclaredType | SymbolCategory.DeclaredNamespace).OrderBy(s => s.Name).ToArray();

Assert.Equal(3, originalSymbols.Length);
ResolveAndVerifySymbolList(newSymbols, originalSymbols, originalComp);
}

[Fact]
public void FileType4()
{
// we should be able to distinguish a file type and non-file type when they have the same source name.
var src1 = SyntaxFactory.ParseSyntaxTree(@"using System;

namespace N1.N2
{
file class C { }
}
", path: "File1.cs");

var src2 = SyntaxFactory.ParseSyntaxTree(@"
namespace N1.N2
{
class C { }
}
", path: "File2.cs");
var originalComp = CreateCompilation(new[] { src1, src2 }, assemblyName: "Test");
var newComp = CreateCompilation(new[] { src1, src2 }, assemblyName: "Test");

var originalSymbols = GetSourceSymbols(originalComp, SymbolCategory.DeclaredType | SymbolCategory.DeclaredNamespace).OrderBy(s => s.Name).ToArray();
var newSymbols = GetSourceSymbols(newComp, SymbolCategory.DeclaredType | SymbolCategory.DeclaredNamespace).OrderBy(s => s.Name).ToArray();

Assert.Equal(3, originalSymbols.Length);
ResolveAndVerifySymbolList(newSymbols, originalSymbols, originalComp);
}

#endregion

#region "Change to symbol"
Expand Down
105 changes: 105 additions & 0 deletions src/EditorFeatures/CSharpTest/SymbolKey/SymbolKeyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,111 @@ namespace Microsoft.CodeAnalysis.Editor.CSharp.UnitTests.SymbolId
[UseExportProvider]
public class SymbolKeyTests
{
[Fact]
public async Task FileType_01()
{
var typeSource = @"
file class C1
{
public static void M() { }
}
";

var workspaceXml = @$"
<Workspace>
<Project Language=""C#"">
<CompilationOptions Nullable=""Enable""/>
<Document FilePath=""C.cs"">
{typeSource}
</Document>
</Project>
</Workspace>
";
using var workspace = TestWorkspace.Create(workspaceXml);

var solution = workspace.CurrentSolution;
var project = solution.Projects.Single();

var compilation = await project.GetCompilationAsync();
var type = compilation.GetTypeByMetadataName("<C>F0__C1");
Assert.NotNull(type);
var symbolKey = SymbolKey.Create(type);
var resolved = symbolKey.Resolve(compilation).Symbol;
Assert.Same(type, resolved);
}

[Fact]
public async Task FileType_02()
{
var workspaceXml = $$"""
<Workspace>
<Project Language="C#">
<CompilationOptions Nullable="Enable"/>
<Document FilePath="File0.cs">
file class C
{
public static void M() { }
}
</Document>
<Document FilePath="File1.cs">
file class C
{
public static void M() { }
}
</Document>
</Project>
</Workspace>
""";
using var workspace = TestWorkspace.Create(workspaceXml);

var solution = workspace.CurrentSolution;
var project = solution.Projects.Single();

var compilation = await project.GetCompilationAsync();

var type = compilation.GetTypeByMetadataName("<File1>F1__C");
Assert.NotNull(type);
var symbolKey = SymbolKey.Create(type);
var resolved = symbolKey.Resolve(compilation).Symbol;
Assert.Same(type, resolved);

type = compilation.GetTypeByMetadataName("<File0>F0__C");
Assert.NotNull(type);
symbolKey = SymbolKey.Create(type);
resolved = symbolKey.Resolve(compilation).Symbol;
Assert.Same(type, resolved);
}

[Fact]
public async Task FileType_03()
{
var workspaceXml = $$"""
<Workspace>
<Project Language="C#">
<CompilationOptions Nullable="Enable"/>
<Document FilePath="File0.cs">
file class C
{
public class Inner { }
}
</Document>
</Project>
</Workspace>
""";
using var workspace = TestWorkspace.Create(workspaceXml);

var solution = workspace.CurrentSolution;
var project = solution.Projects.Single();

var compilation = await project.GetCompilationAsync();

var type = compilation.GetTypeByMetadataName("<File0>F0__C+Inner");
Assert.NotNull(type);
var symbolKey = SymbolKey.Create(type);
var resolved = symbolKey.Resolve(compilation).Symbol;
Assert.Same(type, resolved);
}

[Fact, WorkItem(45437, "https://github.com/dotnet/roslyn/issues/45437")]
public async Task TestGenericsAndNullability()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public KeywordCompletionProvider()
new ExternKeywordRecommender(),
new FalseKeywordRecommender(),
new FieldKeywordRecommender(),
new FileKeywordRecommender(),
new FinallyKeywordRecommender(),
new FixedKeywordRecommender(),
new FloatKeywordRecommender(),
Expand Down
Loading