Skip to content

Commit

Permalink
File types IDE changes (#62215)
Browse files Browse the repository at this point in the history
Co-authored-by: Cyrus Najmabadi <cyrusn@microsoft.com>
Co-authored-by: Youssef Victor <youssefvictor00@gmail.com>
  • Loading branch information
3 people committed Jul 5, 2022
1 parent a540733 commit 53b193b
Show file tree
Hide file tree
Showing 18 changed files with 523 additions and 22 deletions.
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; }

/// <summary>
/// Returns collection of names of members declared within this type.
/// </summary>
Expand Down
6 changes: 6 additions & 0 deletions src/Compilers/VisualBasic/Portable/Symbols/NamedTypeSymbol.vb
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");
}

[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.
// 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.
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

0 comments on commit 53b193b

Please sign in to comment.