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 17 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
Original file line number Diff line number Diff line change
Expand Up @@ -3245,9 +3245,22 @@ void M(C c)
var tree = comp.SyntaxTrees[0];
var model = comp.GetSemanticModel(tree);
var node = tree.GetRoot().DescendantNodes().OfType<ParameterSyntax>().Single();
var type = model.GetTypeInfo(node.Type!).Type;
var type = (INamedTypeSymbol)model.GetTypeInfo(node.Type!).Type!;
Assert.Equal("C@<tree 0>", type.ToTestDisplayString());
Assert.Equal(tree, type.GetSymbol<NamedTypeSymbol>()!.AssociatedSyntaxTree);
Assert.Equal(tree, type.GetSymbol()!.AssociatedSyntaxTree);
Assert.True(type.IsFile);

var referencingMetadataComp = CreateCompilation("", new[] { comp.ToMetadataReference() });
type = ((Compilation)referencingMetadataComp).GetTypeByMetadataName("<>F0__C")!;
Assert.Equal("C@<tree 0>", type.ToTestDisplayString());
Assert.Equal(tree, type.GetSymbol()!.AssociatedSyntaxTree);
Assert.True(type.IsFile);

var referencingImageComp = CreateCompilation("", new[] { comp.EmitToImageReference() });
type = ((Compilation)referencingImageComp).GetTypeByMetadataName("<>F0__C")!;
Assert.Equal("<>F0__C", type.ToTestDisplayString());
Assert.Null(type.GetSymbol()!.AssociatedSyntaxTree);
Assert.False(type.IsFile);
}

[Fact]
Expand All @@ -3267,9 +3280,10 @@ void M(C c)
var tree = comp.SyntaxTrees[0];
var model = comp.GetSemanticModel(tree);
var node = tree.GetRoot().DescendantNodes().OfType<ParameterSyntax>().Single();
var type = model.GetTypeInfo(node.Type!).Type;
var type = (INamedTypeSymbol)model.GetTypeInfo(node.Type!).Type!;
Assert.Equal("C", type.ToTestDisplayString());
Assert.Null(type.GetSymbol<NamedTypeSymbol>()!.AssociatedSyntaxTree);
Assert.Null(type.GetSymbol()!.AssociatedSyntaxTree);
Assert.False(type.IsFile);
}

[Fact]
Expand All @@ -3289,8 +3303,9 @@ void M(C<int> c)
var tree = comp.SyntaxTrees[0];
var model = comp.GetSemanticModel(tree);
var node = tree.GetRoot().DescendantNodes().OfType<ParameterSyntax>().Single();
var type = model.GetTypeInfo(node.Type!).Type;
var type = (INamedTypeSymbol)model.GetTypeInfo(node.Type!).Type!;
Assert.Equal("C<System.Int32>@<tree 0>", type.ToTestDisplayString());
Assert.Equal(tree, type.GetSymbol<NamedTypeSymbol>()!.AssociatedSyntaxTree);
Assert.Equal(tree, type.GetSymbol()!.AssociatedSyntaxTree);
Assert.True(type.IsFile);
}
}
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

RikkiGibson marked this conversation as resolved.
Show resolved Hide resolved

/// <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,151 @@ 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");
RikkiGibson marked this conversation as resolved.
Show resolved Hide resolved
}

[Fact]
public async Task SuggestFileAfterReadonly()
{
// e.g. 'readonly file struct X { }'
var markup = $$"""
readonly $$
""";

await VerifyItemExistsAsync(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 SuggestFileBeforeDelegate()
{
var markup = $$"""
$$ delegate
""";

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.
RikkiGibson marked this conversation as resolved.
Show resolved Hide resolved
// 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(4, 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.
RikkiGibson marked this conversation as resolved.
Show resolved Hide resolved
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(4, originalSymbols.Length);
ResolveAndVerifySymbolList(newSymbols, originalSymbols, originalComp);
}

#endregion

#region "Change to symbol"
Expand Down
Loading