未验证 提交 7c04f241 编写于 作者: J Julien Couvreur 提交者: GitHub

Offer completion for `#nullable enable|disable` (#31171)

上级 d5f71191
......@@ -52,6 +52,8 @@ public SyntaxToken DirectiveNameToken
return ((LoadDirectiveTriviaSyntax)this).LoadKeyword;
case SyntaxKind.ShebangDirectiveTrivia:
return ((ShebangDirectiveTriviaSyntax)this).ExclamationToken;
case SyntaxKind.NullableDirectiveTrivia:
return ((NullableDirectiveTriviaSyntax)this).NullableKeyword;
default:
throw ExceptionUtilities.UnexpectedValue(this.Kind());
}
......
......@@ -247,6 +247,7 @@ public static bool IsPreprocessorDirective(SyntaxKind kind)
case SyntaxKind.LoadDirectiveTrivia:
case SyntaxKind.BadDirectiveTrivia:
case SyntaxKind.ShebangDirectiveTrivia:
case SyntaxKind.NullableDirectiveTrivia:
return true;
default:
return false;
......
......@@ -260,6 +260,9 @@ private void VerifyDirectivesSpecial(CSharpSyntaxNode node, params DirectiveInfo
Assert.Equal(exp.Text, setting.ValueText);
Assert.True(setting.Kind() == SyntaxKind.EnableKeyword || setting.Kind() == SyntaxKind.DisableKeyword);
}
Assert.Equal(SyntaxKind.NullableKeyword, nn.DirectiveNameToken.Kind());
Assert.True(SyntaxFacts.IsPreprocessorDirective(SyntaxKind.NullableDirectiveTrivia));
Assert.True(SyntaxFacts.IsPreprocessorKeyword(SyntaxKind.NullableKeyword));
break;
default:
if (null != exp.Text)
......
......@@ -2,12 +2,31 @@
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Test.Utilities;
using Roslyn.Test.Utilities;
using Xunit;
namespace Microsoft.CodeAnalysis.Editor.CSharp.UnitTests.Recommendations
{
public class DisableKeywordRecommenderTests : KeywordRecommenderTests
{
[Fact, Trait(Traits.Feature, Traits.Features.KeywordRecommending)]
[WorkItem(31130, "https://github.com/dotnet/roslyn/issues/31130")]
public async Task TestAfterNullable()
{
await VerifyKeywordAsync(@"#nullable $$");
}
[Fact]
[WorkItem(31130, "https://github.com/dotnet/roslyn/issues/31130")]
public async Task TestNotAfterNullableAndNewline()
{
await VerifyAbsenceAsync(@"
#nullable
$$
");
}
[Fact, Trait(Traits.Feature, Traits.Features.KeywordRecommending)]
public async Task TestNotAtRoot_Interactive()
{
......
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Test.Utilities;
using Roslyn.Test.Utilities;
using Xunit;
namespace Microsoft.CodeAnalysis.Editor.CSharp.UnitTests.Recommendations
{
[Trait(Traits.Feature, Traits.Features.KeywordRecommending)]
public class EnableKeywordRecommenderTests : KeywordRecommenderTests
{
[Fact]
[WorkItem(31130, "https://github.com/dotnet/roslyn/issues/31130")]
public async Task TestAfterNullable()
{
await VerifyKeywordAsync(@"#nullable $$");
}
[Fact]
[WorkItem(31130, "https://github.com/dotnet/roslyn/issues/31130")]
public async Task TestNotAfterNullableAndNewline()
{
await VerifyAbsenceAsync(@"
#nullable
$$
");
}
[Fact]
[WorkItem(31130, "https://github.com/dotnet/roslyn/issues/31130")]
public async Task TestNotAfterHash()
{
await VerifyAbsenceAsync(@"#$$");
}
[Fact]
public async Task TestNotAtRoot_Interactive()
{
await VerifyAbsenceAsync(SourceCodeKind.Script, @"$$");
}
[Fact]
public async Task TestNotAfterClass_Interactive()
{
await VerifyAbsenceAsync(SourceCodeKind.Script,
@"class C { }
$$");
}
[Fact]
public async Task TestNotAfterGlobalStatement_Interactive()
{
await VerifyAbsenceAsync(SourceCodeKind.Script,
@"System.Console.WriteLine();
$$");
}
[Fact]
public async Task TestNotAfterGlobalVariableDeclaration_Interactive()
{
await VerifyAbsenceAsync(SourceCodeKind.Script,
@"int i = 0;
$$");
}
[Fact]
public async Task TestNotInUsingAlias()
{
await VerifyAbsenceAsync(@"using Goo = $$");
}
[Fact]
public async Task TestNotInEmptyStatement()
{
await VerifyAbsenceAsync(AddInsideMethod(@"$$"));
}
[Fact]
public async Task TestNotAfterPragma()
{
await VerifyAbsenceAsync(@"#pragma $$");
}
[Fact]
public async Task TestNotAfterPragmaWarning()
{
await VerifyAbsenceAsync(@"#pragma warning $$");
}
}
}
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Test.Utilities;
using Xunit;
namespace Microsoft.CodeAnalysis.Editor.CSharp.UnitTests.Recommendations
{
[Trait(Traits.Feature, Traits.Features.KeywordRecommending)]
public class NullableKeywordRecommenderTests : KeywordRecommenderTests
{
[Fact]
public async Task TestNotAtRoot_Interactive()
{
await VerifyAbsenceAsync(SourceCodeKind.Script,
@"$$");
}
[Fact]
public async Task TestNotAfterClass_Interactive()
{
await VerifyAbsenceAsync(SourceCodeKind.Script,
@"class C { }
$$");
}
[Fact]
public async Task TestNotAfterGlobalStatement_Interactive()
{
await VerifyAbsenceAsync(SourceCodeKind.Script,
@"System.Console.WriteLine();
$$");
}
[Fact]
public async Task TestNotAfterGlobalVariableDeclaration_Interactive()
{
await VerifyAbsenceAsync(SourceCodeKind.Script,
@"int i = 0;
$$");
}
[Fact]
public async Task TestNotInUsingAlias()
{
await VerifyAbsenceAsync(
@"using Goo = $$");
}
[Fact]
public async Task TestNotInEmptyStatement()
{
await VerifyAbsenceAsync(AddInsideMethod(
@"$$"));
}
[Fact]
public async Task TestAfterHash()
{
await VerifyKeywordAsync(
@"#$$");
}
[Fact]
public async Task TestAfterHashAndSpace()
{
await VerifyKeywordAsync(
@"# $$");
}
[Fact]
public async Task TestNotAfterHashAndNullable()
{
await VerifyAbsenceAsync(
@"#nullable $$");
}
}
}
......@@ -56,6 +56,7 @@ private static ImmutableArray<IKeywordRecommender<CSharpSyntaxContext>> GetKeywo
new DynamicKeywordRecommender(),
new ElifKeywordRecommender(),
new ElseKeywordRecommender(),
new EnableKeywordRecommender(),
new EndIfKeywordRecommender(),
new EndRegionKeywordRecommender(),
new EnumKeywordRecommender(),
......@@ -96,6 +97,7 @@ private static ImmutableArray<IKeywordRecommender<CSharpSyntaxContext>> GetKeywo
new NameOfKeywordRecommender(),
new NamespaceKeywordRecommender(),
new NewKeywordRecommender(),
new NullableKeywordRecommender(),
new NullKeywordRecommender(),
new ObjectKeywordRecommender(),
new OnKeywordRecommender(),
......
......@@ -14,12 +14,20 @@ public DisableKeywordRecommender()
protected override bool IsValidContext(int position, CSharpSyntaxContext context, CancellationToken cancellationToken)
{
// # pragma warning |
// # pragma warning d|
var previousToken1 = context.TargetToken;
var previousToken2 = previousToken1.GetPreviousToken(includeSkipped: true);
var previousToken3 = previousToken2.GetPreviousToken(includeSkipped: true);
if (previousToken1.Kind() == SyntaxKind.NullableKeyword &&
previousToken2.Kind() == SyntaxKind.HashToken)
{
// # nullable |
// # nullable d|
return true;
}
// # pragma warning |
// # pragma warning d|
return
previousToken1.Kind() == SyntaxKind.WarningKeyword &&
previousToken2.Kind() == SyntaxKind.PragmaKeyword &&
......
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Threading;
using Microsoft.CodeAnalysis.CSharp.Extensions.ContextQuery;
namespace Microsoft.CodeAnalysis.CSharp.Completion.KeywordRecommenders
{
internal class EnableKeywordRecommender : AbstractSyntacticSingleKeywordRecommender
{
public EnableKeywordRecommender()
: base(SyntaxKind.EnableKeyword, isValidInPreprocessorContext: true)
{
}
protected override bool IsValidContext(int position, CSharpSyntaxContext context, CancellationToken cancellationToken)
{
var previousToken1 = context.TargetToken;
var previousToken2 = previousToken1.GetPreviousToken(includeSkipped: true);
// # nullable |
// # nullable e|
return previousToken1.Kind() == SyntaxKind.NullableKeyword &&
previousToken2.Kind() == SyntaxKind.HashToken;
}
}
}
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Threading;
using Microsoft.CodeAnalysis.CSharp.Extensions.ContextQuery;
namespace Microsoft.CodeAnalysis.CSharp.Completion.KeywordRecommenders
{
internal class NullableKeywordRecommender : AbstractSyntacticSingleKeywordRecommender
{
public NullableKeywordRecommender()
: base(SyntaxKind.NullableKeyword, isValidInPreprocessorContext: true)
{
}
protected override bool IsValidContext(int position, CSharpSyntaxContext context, CancellationToken cancellationToken)
=> context.IsPreProcessorKeywordContext;
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册