From 4edc074e4684a06df218c828b431a6f4e096ca9a Mon Sep 17 00:00:00 2001 From: Cyrus Najmabadi Date: Tue, 20 Jul 2021 14:28:05 -0700 Subject: [PATCH] Fix 'keyword recommenders' with file scoped namespaces --- .../AbstractKeywordRecommenderTests.cs | 10 ++++++++-- .../ClassKeywordRecommenderTests.cs | 17 ++++++++--------- .../DelegateKeywordRecommenderTests.cs | 10 ++++++++-- .../InterfaceKeywordRecommenderTests.cs | 10 ++++++++-- .../InternalKeywordRecommenderTests.cs | 10 ++++++++-- .../NewKeywordRecommenderTests.cs | 10 ++++++++-- .../PartialKeywordRecommenderTests.cs | 10 ++++++++-- .../PrivateKeywordRecommenderTests.cs | 10 ++++++++-- .../ProtectedKeywordRecommenderTests.cs | 9 +++++++-- .../PublicKeywordRecommenderTests.cs | 10 ++++++++-- .../ReadOnlyKeywordRecommenderTests.cs | 10 ++++++++-- .../RecordKeywordRecommenderTests.cs | 10 ++++++++-- .../RefKeywordRecommenderTests.cs | 18 ++++++++++++++++-- .../SealedKeywordRecommenderTests.cs | 10 ++++++++-- .../StaticKeywordRecommenderTests.cs | 10 ++++++++-- .../StructKeywordRecommenderTests.cs | 10 ++++++++-- .../UnsafeKeywordRecommenderTests.cs | 10 ++++++++-- .../SemanticFacts/CSharpSemanticFacts.cs | 6 +++--- .../Services/SyntaxFacts/CSharpSyntaxFacts.cs | 11 +++++------ .../ContextQuery/SyntaxTokenExtensions.cs | 5 +++-- .../ContextQuery/SyntaxTreeExtensions.cs | 8 +------- .../CSharp/Extensions/ITypeSymbolExtensions.cs | 6 +++--- 22 files changed, 158 insertions(+), 62 deletions(-) diff --git a/src/EditorFeatures/CSharpTest2/Recommendations/AbstractKeywordRecommenderTests.cs b/src/EditorFeatures/CSharpTest2/Recommendations/AbstractKeywordRecommenderTests.cs index 67cd2e565fefc..f28bbf5baa11b 100644 --- a/src/EditorFeatures/CSharpTest2/Recommendations/AbstractKeywordRecommenderTests.cs +++ b/src/EditorFeatures/CSharpTest2/Recommendations/AbstractKeywordRecommenderTests.cs @@ -2,8 +2,6 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -#nullable disable - using System.Threading.Tasks; using Microsoft.CodeAnalysis.Test.Utilities; using Roslyn.Test.Utilities; @@ -104,6 +102,14 @@ await VerifyKeywordAsync( $$"); } + [Fact, Trait(Traits.Feature, Traits.Features.KeywordRecommending)] + public async Task TestAfterFileScopedNamespace() + { + await VerifyKeywordAsync( +@"namespace N; +$$"); + } + [Fact, Trait(Traits.Feature, Traits.Features.KeywordRecommending)] public async Task TestAfterTypeDeclaration() { diff --git a/src/EditorFeatures/CSharpTest2/Recommendations/ClassKeywordRecommenderTests.cs b/src/EditorFeatures/CSharpTest2/Recommendations/ClassKeywordRecommenderTests.cs index d1fe367290ccb..7967c43c04b4f 100644 --- a/src/EditorFeatures/CSharpTest2/Recommendations/ClassKeywordRecommenderTests.cs +++ b/src/EditorFeatures/CSharpTest2/Recommendations/ClassKeywordRecommenderTests.cs @@ -2,8 +2,6 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -#nullable disable - using System.Threading.Tasks; using Microsoft.CodeAnalysis.Test.Utilities; using Roslyn.Test.Utilities; @@ -104,6 +102,14 @@ await VerifyKeywordAsync( $$"); } + [Fact, Trait(Traits.Feature, Traits.Features.KeywordRecommending)] + public async Task TestAfterFileScopedNamespace() + { + await VerifyKeywordAsync( +@"namespace N; +$$"); + } + [Fact, Trait(Traits.Feature, Traits.Features.KeywordRecommending)] public async Task TestAfterTypeDeclaration() { @@ -402,13 +408,6 @@ await VerifyKeywordAsync( @"record $$"); } - [Fact, Trait(Traits.Feature, Traits.Features.KeywordRecommending)] - public async Task TestAfterFileScopedNamespace() - { - await VerifyKeywordAsync( -@"namespace NS; $$"); - } - [Fact, Trait(Traits.Feature, Traits.Features.KeywordRecommending)] public async Task TestAfterAttributeFileScopedNamespace() { diff --git a/src/EditorFeatures/CSharpTest2/Recommendations/DelegateKeywordRecommenderTests.cs b/src/EditorFeatures/CSharpTest2/Recommendations/DelegateKeywordRecommenderTests.cs index 4fa5865ab645c..2f132c0a1dc3b 100644 --- a/src/EditorFeatures/CSharpTest2/Recommendations/DelegateKeywordRecommenderTests.cs +++ b/src/EditorFeatures/CSharpTest2/Recommendations/DelegateKeywordRecommenderTests.cs @@ -2,8 +2,6 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -#nullable disable - using System.Threading.Tasks; using Microsoft.CodeAnalysis.Test.Utilities; using Roslyn.Test.Utilities; @@ -122,6 +120,14 @@ await VerifyKeywordAsync( $$"); } + [Fact, Trait(Traits.Feature, Traits.Features.KeywordRecommending)] + public async Task TestAfterFileScopedNamespace() + { + await VerifyKeywordAsync( +@"namespace N; +$$"); + } + [Fact, Trait(Traits.Feature, Traits.Features.KeywordRecommending)] public async Task TestAfterTypeDeclaration() { diff --git a/src/EditorFeatures/CSharpTest2/Recommendations/InterfaceKeywordRecommenderTests.cs b/src/EditorFeatures/CSharpTest2/Recommendations/InterfaceKeywordRecommenderTests.cs index 3576789a7d5d8..2c0f2c1f03f17 100644 --- a/src/EditorFeatures/CSharpTest2/Recommendations/InterfaceKeywordRecommenderTests.cs +++ b/src/EditorFeatures/CSharpTest2/Recommendations/InterfaceKeywordRecommenderTests.cs @@ -2,8 +2,6 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -#nullable disable - using System.Threading.Tasks; using Microsoft.CodeAnalysis.Test.Utilities; using Roslyn.Test.Utilities; @@ -104,6 +102,14 @@ await VerifyKeywordAsync( $$"); } + [Fact, Trait(Traits.Feature, Traits.Features.KeywordRecommending)] + public async Task TestAfterFileScopedNamespace() + { + await VerifyKeywordAsync( +@"namespace N; +$$"); + } + [Fact, Trait(Traits.Feature, Traits.Features.KeywordRecommending)] public async Task TestAfterTypeDeclaration() { diff --git a/src/EditorFeatures/CSharpTest2/Recommendations/InternalKeywordRecommenderTests.cs b/src/EditorFeatures/CSharpTest2/Recommendations/InternalKeywordRecommenderTests.cs index 5ad8dacf660fe..394a24dcc0902 100644 --- a/src/EditorFeatures/CSharpTest2/Recommendations/InternalKeywordRecommenderTests.cs +++ b/src/EditorFeatures/CSharpTest2/Recommendations/InternalKeywordRecommenderTests.cs @@ -2,8 +2,6 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -#nullable disable - using System.Threading.Tasks; using Microsoft.CodeAnalysis.Test.Utilities; using Roslyn.Test.Utilities; @@ -104,6 +102,14 @@ await VerifyKeywordAsync( $$"); } + [Fact, Trait(Traits.Feature, Traits.Features.KeywordRecommending)] + public async Task TestAfterFileScopedNamespace() + { + await VerifyKeywordAsync( +@"namespace N; +$$"); + } + [Fact, Trait(Traits.Feature, Traits.Features.KeywordRecommending)] public async Task TestAfterTypeDeclaration() { diff --git a/src/EditorFeatures/CSharpTest2/Recommendations/NewKeywordRecommenderTests.cs b/src/EditorFeatures/CSharpTest2/Recommendations/NewKeywordRecommenderTests.cs index 5fac7b143f454..7fec7cd53a7a5 100644 --- a/src/EditorFeatures/CSharpTest2/Recommendations/NewKeywordRecommenderTests.cs +++ b/src/EditorFeatures/CSharpTest2/Recommendations/NewKeywordRecommenderTests.cs @@ -2,8 +2,6 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -#nullable disable - using System.Threading.Tasks; using Microsoft.CodeAnalysis.Test.Utilities; using Roslyn.Test.Utilities; @@ -698,6 +696,14 @@ await VerifyKeywordAsync(@"namespace N {} $$"); } + [Fact, Trait(Traits.Feature, Traits.Features.KeywordRecommending)] + public async Task TestAfterFileScopedNamespace() + { + await VerifyKeywordAsync( +@"namespace N; +$$"); + } + [Fact, Trait(Traits.Feature, Traits.Features.KeywordRecommending)] public async Task TestAfterDelegateDeclaration() { diff --git a/src/EditorFeatures/CSharpTest2/Recommendations/PartialKeywordRecommenderTests.cs b/src/EditorFeatures/CSharpTest2/Recommendations/PartialKeywordRecommenderTests.cs index 1a03583b02f8d..869b01ceb8c89 100644 --- a/src/EditorFeatures/CSharpTest2/Recommendations/PartialKeywordRecommenderTests.cs +++ b/src/EditorFeatures/CSharpTest2/Recommendations/PartialKeywordRecommenderTests.cs @@ -2,8 +2,6 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -#nullable disable - using System.Threading.Tasks; using Microsoft.CodeAnalysis.Test.Utilities; using Roslyn.Test.Utilities; @@ -104,6 +102,14 @@ await VerifyKeywordAsync( $$"); } + [Fact, Trait(Traits.Feature, Traits.Features.KeywordRecommending)] + public async Task TestAfterFileScopedNamespace() + { + await VerifyKeywordAsync( +@"namespace N; +$$"); + } + [Fact, Trait(Traits.Feature, Traits.Features.KeywordRecommending)] public async Task TestAfterTypeDeclaration() { diff --git a/src/EditorFeatures/CSharpTest2/Recommendations/PrivateKeywordRecommenderTests.cs b/src/EditorFeatures/CSharpTest2/Recommendations/PrivateKeywordRecommenderTests.cs index 4e93e4a655619..39f3dded6dd0a 100644 --- a/src/EditorFeatures/CSharpTest2/Recommendations/PrivateKeywordRecommenderTests.cs +++ b/src/EditorFeatures/CSharpTest2/Recommendations/PrivateKeywordRecommenderTests.cs @@ -2,8 +2,6 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -#nullable disable - using System.Threading.Tasks; using Microsoft.CodeAnalysis.Test.Utilities; using Roslyn.Test.Utilities; @@ -118,6 +116,14 @@ await VerifyAbsenceAsync(SourceCodeKind.Regular, @"namespace N {} $$"); } + [Fact, Trait(Traits.Feature, Traits.Features.KeywordRecommending)] + public async Task TestNotAfterFileScopedNamespace() + { + await VerifyAbsenceAsync(SourceCodeKind.Regular, +@"namespace N; +$$"); + } + [Fact, Trait(Traits.Feature, Traits.Features.KeywordRecommending)] public async Task TestNotAfterTypeDeclaration() { diff --git a/src/EditorFeatures/CSharpTest2/Recommendations/ProtectedKeywordRecommenderTests.cs b/src/EditorFeatures/CSharpTest2/Recommendations/ProtectedKeywordRecommenderTests.cs index b1113b7d5feac..e3ab9f94d6226 100644 --- a/src/EditorFeatures/CSharpTest2/Recommendations/ProtectedKeywordRecommenderTests.cs +++ b/src/EditorFeatures/CSharpTest2/Recommendations/ProtectedKeywordRecommenderTests.cs @@ -2,8 +2,6 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -#nullable disable - using System.Threading.Tasks; using Microsoft.CodeAnalysis.Test.Utilities; using Xunit; @@ -96,6 +94,13 @@ await VerifyAbsenceAsync(@"namespace N {} $$"); } + [Fact, Trait(Traits.Feature, Traits.Features.KeywordRecommending)] + public async Task TestNotAfterFileScopedNamespace() + { + await VerifyAbsenceAsync(@"namespace N; +$$"); + } + [Fact, Trait(Traits.Feature, Traits.Features.KeywordRecommending)] public async Task TestNotAfterTypeDeclaration() { diff --git a/src/EditorFeatures/CSharpTest2/Recommendations/PublicKeywordRecommenderTests.cs b/src/EditorFeatures/CSharpTest2/Recommendations/PublicKeywordRecommenderTests.cs index daca25e5712c7..04abaf09b63f1 100644 --- a/src/EditorFeatures/CSharpTest2/Recommendations/PublicKeywordRecommenderTests.cs +++ b/src/EditorFeatures/CSharpTest2/Recommendations/PublicKeywordRecommenderTests.cs @@ -2,8 +2,6 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -#nullable disable - using System.Threading.Tasks; using Microsoft.CodeAnalysis.Test.Utilities; using Roslyn.Test.Utilities; @@ -104,6 +102,14 @@ await VerifyKeywordAsync( $$"); } + [Fact, Trait(Traits.Feature, Traits.Features.KeywordRecommending)] + public async Task TestAfterFileScopedNamespace() + { + await VerifyKeywordAsync( +@"namespace N; +$$"); + } + [Fact, Trait(Traits.Feature, Traits.Features.KeywordRecommending)] public async Task TestAfterTypeDeclaration() { diff --git a/src/EditorFeatures/CSharpTest2/Recommendations/ReadOnlyKeywordRecommenderTests.cs b/src/EditorFeatures/CSharpTest2/Recommendations/ReadOnlyKeywordRecommenderTests.cs index f901bb2b41a0e..8b2f1de6802dd 100644 --- a/src/EditorFeatures/CSharpTest2/Recommendations/ReadOnlyKeywordRecommenderTests.cs +++ b/src/EditorFeatures/CSharpTest2/Recommendations/ReadOnlyKeywordRecommenderTests.cs @@ -2,8 +2,6 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -#nullable disable - using System.Threading.Tasks; using Microsoft.CodeAnalysis.Test.Utilities; using Roslyn.Test.Utilities; @@ -93,6 +91,14 @@ await VerifyKeywordAsync(@"namespace N {} $$"); } + [Fact, Trait(Traits.Feature, Traits.Features.KeywordRecommending)] + public async Task TestAfterFileScopedNamespace() + { + await VerifyKeywordAsync( +@"namespace N; +$$"); + } + [Fact, Trait(Traits.Feature, Traits.Features.KeywordRecommending)] public async Task TestAfterTypeDeclaration() { diff --git a/src/EditorFeatures/CSharpTest2/Recommendations/RecordKeywordRecommenderTests.cs b/src/EditorFeatures/CSharpTest2/Recommendations/RecordKeywordRecommenderTests.cs index be370faab5655..dcf0aa2c2d6f5 100644 --- a/src/EditorFeatures/CSharpTest2/Recommendations/RecordKeywordRecommenderTests.cs +++ b/src/EditorFeatures/CSharpTest2/Recommendations/RecordKeywordRecommenderTests.cs @@ -2,8 +2,6 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -#nullable disable - using System.Threading.Tasks; using Microsoft.CodeAnalysis.CSharp.Test.Utilities; using Microsoft.CodeAnalysis.Test.Utilities; @@ -105,6 +103,14 @@ await VerifyKeywordAsync( $$"); } + [Fact, Trait(Traits.Feature, Traits.Features.KeywordRecommending)] + public async Task TestAfterFileScopedNamespace() + { + await VerifyKeywordAsync( +@"namespace N; +$$"); + } + [Fact, Trait(Traits.Feature, Traits.Features.KeywordRecommending)] public async Task TestAfterTypeDeclaration() { diff --git a/src/EditorFeatures/CSharpTest2/Recommendations/RefKeywordRecommenderTests.cs b/src/EditorFeatures/CSharpTest2/Recommendations/RefKeywordRecommenderTests.cs index 76c396ccc1786..a8671038ddd56 100644 --- a/src/EditorFeatures/CSharpTest2/Recommendations/RefKeywordRecommenderTests.cs +++ b/src/EditorFeatures/CSharpTest2/Recommendations/RefKeywordRecommenderTests.cs @@ -2,8 +2,6 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -#nullable disable - using System.Threading.Tasks; using Microsoft.CodeAnalysis.Test.Utilities; using Roslyn.Test.Utilities; @@ -963,5 +961,21 @@ class C {{ delegate*<{modifier} $$"); } + + [Fact, Trait(Traits.Feature, Traits.Features.KeywordRecommending)] + public async Task TestAfterNamespace() + { + await VerifyKeywordAsync( +@"namespace N { } +$$"); + } + + [Fact, Trait(Traits.Feature, Traits.Features.KeywordRecommending)] + public async Task TestAfterFileScopedNamespace() + { + await VerifyKeywordAsync( +@"namespace N; +$$"); + } } } diff --git a/src/EditorFeatures/CSharpTest2/Recommendations/SealedKeywordRecommenderTests.cs b/src/EditorFeatures/CSharpTest2/Recommendations/SealedKeywordRecommenderTests.cs index ec808ebd39fd3..bbe3db1011a93 100644 --- a/src/EditorFeatures/CSharpTest2/Recommendations/SealedKeywordRecommenderTests.cs +++ b/src/EditorFeatures/CSharpTest2/Recommendations/SealedKeywordRecommenderTests.cs @@ -2,8 +2,6 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -#nullable disable - using System.Threading.Tasks; using Microsoft.CodeAnalysis.Test.Utilities; using Roslyn.Test.Utilities; @@ -104,6 +102,14 @@ await VerifyKeywordAsync( $$"); } + [Fact, Trait(Traits.Feature, Traits.Features.KeywordRecommending)] + public async Task TestAfterFileScopedNamespace() + { + await VerifyKeywordAsync( +@"namespace N; +$$"); + } + [Fact, Trait(Traits.Feature, Traits.Features.KeywordRecommending)] public async Task TestAfterTypeDeclaration() { diff --git a/src/EditorFeatures/CSharpTest2/Recommendations/StaticKeywordRecommenderTests.cs b/src/EditorFeatures/CSharpTest2/Recommendations/StaticKeywordRecommenderTests.cs index 31603af3711b2..b19c1dfb70526 100644 --- a/src/EditorFeatures/CSharpTest2/Recommendations/StaticKeywordRecommenderTests.cs +++ b/src/EditorFeatures/CSharpTest2/Recommendations/StaticKeywordRecommenderTests.cs @@ -2,8 +2,6 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -#nullable disable - using System.Threading.Tasks; using Microsoft.CodeAnalysis.Test.Utilities; using Roslyn.Test.Utilities; @@ -105,6 +103,14 @@ await VerifyKeywordAsync( $$"); } + [Fact, Trait(Traits.Feature, Traits.Features.KeywordRecommending)] + public async Task TestAfterFileScopedNamespace() + { + await VerifyKeywordAsync( +@"namespace N; +$$"); + } + [Fact, Trait(Traits.Feature, Traits.Features.KeywordRecommending)] public async Task TestAfterTypeDeclaration() { diff --git a/src/EditorFeatures/CSharpTest2/Recommendations/StructKeywordRecommenderTests.cs b/src/EditorFeatures/CSharpTest2/Recommendations/StructKeywordRecommenderTests.cs index ac56796e1cfae..24942f334ea4a 100644 --- a/src/EditorFeatures/CSharpTest2/Recommendations/StructKeywordRecommenderTests.cs +++ b/src/EditorFeatures/CSharpTest2/Recommendations/StructKeywordRecommenderTests.cs @@ -2,8 +2,6 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -#nullable disable - using System.Threading.Tasks; using Microsoft.CodeAnalysis.Test.Utilities; using Roslyn.Test.Utilities; @@ -104,6 +102,14 @@ await VerifyKeywordAsync( $$"); } + [Fact, Trait(Traits.Feature, Traits.Features.KeywordRecommending)] + public async Task TestAfterFileScopedNamespace() + { + await VerifyKeywordAsync( +@"namespace N; +$$"); + } + [Fact, Trait(Traits.Feature, Traits.Features.KeywordRecommending)] public async Task TestAfterTypeDeclaration() { diff --git a/src/EditorFeatures/CSharpTest2/Recommendations/UnsafeKeywordRecommenderTests.cs b/src/EditorFeatures/CSharpTest2/Recommendations/UnsafeKeywordRecommenderTests.cs index cd19af5e899ca..9eae4d5134868 100644 --- a/src/EditorFeatures/CSharpTest2/Recommendations/UnsafeKeywordRecommenderTests.cs +++ b/src/EditorFeatures/CSharpTest2/Recommendations/UnsafeKeywordRecommenderTests.cs @@ -2,8 +2,6 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -#nullable disable - using System.Threading.Tasks; using Microsoft.CodeAnalysis.Test.Utilities; using Roslyn.Test.Utilities; @@ -104,6 +102,14 @@ await VerifyKeywordAsync( $$"); } + [Fact, Trait(Traits.Feature, Traits.Features.KeywordRecommending)] + public async Task TestAfterFileScopedNamespace() + { + await VerifyKeywordAsync( +@"namespace N; +$$"); + } + [Fact, Trait(Traits.Feature, Traits.Features.KeywordRecommending)] public async Task TestAfterTypeDeclaration() { diff --git a/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/CSharp/Services/SemanticFacts/CSharpSemanticFacts.cs b/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/CSharp/Services/SemanticFacts/CSharpSemanticFacts.cs index 5bdfd4b869e29..be88a4e2fc1c7 100644 --- a/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/CSharp/Services/SemanticFacts/CSharpSemanticFacts.cs +++ b/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/CSharp/Services/SemanticFacts/CSharpSemanticFacts.cs @@ -133,7 +133,7 @@ public ImmutableHashSet GetAliasNameSet(SemanticModel model, Cancellatio var builder = ImmutableHashSet.CreateBuilder(StringComparer.Ordinal); AppendAliasNames(root.Usings, builder); - AppendAliasNames(root.Members.OfType(), builder, cancellationToken); + AppendAliasNames(root.Members.OfType(), builder, cancellationToken); return builder.ToImmutable(); } @@ -151,14 +151,14 @@ private static void AppendAliasNames(SyntaxList usings, Im } } - private void AppendAliasNames(IEnumerable namespaces, ImmutableHashSet.Builder builder, CancellationToken cancellationToken) + private void AppendAliasNames(IEnumerable namespaces, ImmutableHashSet.Builder builder, CancellationToken cancellationToken) { foreach (var @namespace in namespaces) { cancellationToken.ThrowIfCancellationRequested(); AppendAliasNames(@namespace.Usings, builder); - AppendAliasNames(@namespace.Members.OfType(), builder, cancellationToken); + AppendAliasNames(@namespace.Members.OfType(), builder, cancellationToken); } } diff --git a/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/CSharp/Services/SyntaxFacts/CSharpSyntaxFacts.cs b/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/CSharp/Services/SyntaxFacts/CSharpSyntaxFacts.cs index 8cd64fc4f4660..d00b48b661d7f 100644 --- a/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/CSharp/Services/SyntaxFacts/CSharpSyntaxFacts.cs +++ b/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/CSharp/Services/SyntaxFacts/CSharpSyntaxFacts.cs @@ -814,7 +814,7 @@ public string GetDisplayName(SyntaxNode? node, DisplayNameOptions options, strin // containing namespace(s) in source (if any) if ((options & DisplayNameOptions.IncludeNamespaces) != 0) { - while (parent != null && parent.Kind() == SyntaxKind.NamespaceDeclaration) + while (parent != null && parent is BaseNamespaceDeclarationSyntax) { names.Add(GetName(parent, options)); parent = parent.Parent; @@ -948,18 +948,16 @@ public bool IsClassDeclaration([NotNullWhen(true)] SyntaxNode? node) => node?.Kind() == SyntaxKind.ClassDeclaration; public bool IsNamespaceDeclaration([NotNullWhen(true)] SyntaxNode? node) - => node?.Kind() == SyntaxKind.NamespaceDeclaration; + => node is BaseNamespaceDeclarationSyntax; public SyntaxNode? GetNameOfNamespaceDeclaration(SyntaxNode? node) - => node is NamespaceDeclarationSyntax namespaceDeclaration - ? namespaceDeclaration.Name - : null; + => (node as BaseNamespaceDeclarationSyntax)?.Name; public SyntaxList GetMembersOfTypeDeclaration(SyntaxNode typeDeclaration) => ((TypeDeclarationSyntax)typeDeclaration).Members; public SyntaxList GetMembersOfNamespaceDeclaration(SyntaxNode namespaceDeclaration) - => ((NamespaceDeclarationSyntax)namespaceDeclaration).Members; + => ((BaseNamespaceDeclarationSyntax)namespaceDeclaration).Members; public SyntaxList GetMembersOfCompilationUnit(SyntaxNode compilationUnit) => ((CompilationUnitSyntax)compilationUnit).Members; @@ -1967,6 +1965,7 @@ public override DeclarationKind GetDeclarationKind(SyntaxNode declaration) case SyntaxKind.CompilationUnit: return DeclarationKind.CompilationUnit; case SyntaxKind.NamespaceDeclaration: + case SyntaxKind.FileScopedNamespaceDeclaration: return DeclarationKind.Namespace; case SyntaxKind.UsingDirective: return DeclarationKind.NamespaceImport; diff --git a/src/Workspaces/SharedUtilitiesAndExtensions/Workspace/CSharp/Extensions/ContextQuery/SyntaxTokenExtensions.cs b/src/Workspaces/SharedUtilitiesAndExtensions/Workspace/CSharp/Extensions/ContextQuery/SyntaxTokenExtensions.cs index ffd0182429f7b..e6b9bef519f7a 100644 --- a/src/Workspaces/SharedUtilitiesAndExtensions/Workspace/CSharp/Extensions/ContextQuery/SyntaxTokenExtensions.cs +++ b/src/Workspaces/SharedUtilitiesAndExtensions/Workspace/CSharp/Extensions/ContextQuery/SyntaxTokenExtensions.cs @@ -222,9 +222,10 @@ public static bool IsBeginningOfGlobalStatementContext(this SyntaxToken token) { var globalStatement = token.GetAncestor(); if (globalStatement != null && globalStatement.GetLastToken(includeZeroWidth: true) == token) - { return true; - } + + if (token.Parent is FileScopedNamespaceDeclarationSyntax namespaceDeclaration && namespaceDeclaration.SemicolonToken == token) + return true; var memberDeclaration = token.GetAncestor(); if (memberDeclaration != null && memberDeclaration.GetLastToken(includeZeroWidth: true) == token && diff --git a/src/Workspaces/SharedUtilitiesAndExtensions/Workspace/CSharp/Extensions/ContextQuery/SyntaxTreeExtensions.cs b/src/Workspaces/SharedUtilitiesAndExtensions/Workspace/CSharp/Extensions/ContextQuery/SyntaxTreeExtensions.cs index 094c5da3f33a3..dd579bd03a177 100644 --- a/src/Workspaces/SharedUtilitiesAndExtensions/Workspace/CSharp/Extensions/ContextQuery/SyntaxTreeExtensions.cs +++ b/src/Workspaces/SharedUtilitiesAndExtensions/Workspace/CSharp/Extensions/ContextQuery/SyntaxTreeExtensions.cs @@ -646,7 +646,7 @@ public static bool IsTypeDeclarationContext( if (container.IsKind(SyntaxKind.IncompleteMember, out IncompleteMemberSyntax? incompleteMember)) return incompleteMember.Type.IsKind(SyntaxKind.RefType); - if (container is CompilationUnitSyntax or NamespaceDeclarationSyntax or TypeDeclarationSyntax) + if (container is CompilationUnitSyntax or BaseNamespaceDeclarationSyntax or TypeDeclarationSyntax) return true; } @@ -1961,12 +1961,6 @@ public static bool IsStatementContext(this SyntaxTree syntaxTree, int position, public static bool IsGlobalStatementContext(this SyntaxTree syntaxTree, int position, CancellationToken cancellationToken) { -#if false - if (syntaxTree.IsInPreprocessorDirectiveContext(position, cancellationToken)) - { - return false; - } -#endif var token = syntaxTree.FindTokenOnLeftOfPosition(position, cancellationToken) .GetPreviousTokenIfTouchingWord(position); diff --git a/src/Workspaces/SharedUtilitiesAndExtensions/Workspace/CSharp/Extensions/ITypeSymbolExtensions.cs b/src/Workspaces/SharedUtilitiesAndExtensions/Workspace/CSharp/Extensions/ITypeSymbolExtensions.cs index 73c1a9ff9649d..f56cd5bc16ec5 100644 --- a/src/Workspaces/SharedUtilitiesAndExtensions/Workspace/CSharp/Extensions/ITypeSymbolExtensions.cs +++ b/src/Workspaces/SharedUtilitiesAndExtensions/Workspace/CSharp/Extensions/ITypeSymbolExtensions.cs @@ -148,9 +148,9 @@ public static bool ContainingTypesOrSelfHasUnsafeKeyword(this ITypeSymbol contai private static IEnumerable GetApplicableUsings(int position, SyntaxNode root) { - var namespaceUsings = root.FindToken(position).Parent!.GetAncestors().SelectMany(n => n.Usings); - var allUsings = root is CompilationUnitSyntax - ? ((CompilationUnitSyntax)root).Usings.Concat(namespaceUsings) + var namespaceUsings = root.FindToken(position).Parent!.GetAncestors().SelectMany(n => n.Usings); + var allUsings = root is CompilationUnitSyntax compilationUnit + ? compilationUnit.Usings.Concat(namespaceUsings) : namespaceUsings; return allUsings.Where(u => u.Alias != null); }