提交 8f4011df 编写于 作者: M Mikkel Nylander Bundgaard 提交者: Ivan Basov

Escape keywords in object initializer completion (#26740)

上级 18cdf8df
......@@ -820,6 +820,36 @@ static void Main(string[] args)
await VerifyItemExistsAsync(markup, "Value");
}
[WorkItem(26560, "https://github.com/dotnet/roslyn/issues/26560")]
[Fact, Trait(Traits.Feature, Traits.Features.Completion)]
public async Task ObjectInitializerEscapeKeywords()
{
var markup = @"
class c
{
public int @new { get; set; }
public int @this { get; set; }
public int now { get; set; }
}
class d
{
static void Main(string[] args)
{
var t = new c() { $$ };
}
}";
await VerifyItemExistsAsync(markup, "@new");
await VerifyItemExistsAsync(markup, "@this");
await VerifyItemExistsAsync(markup, "now");
await VerifyItemIsAbsentAsync(markup, "new");
await VerifyItemIsAbsentAsync(markup, "this");
}
private async Task VerifyExclusiveAsync(string markup, bool exclusive)
{
using (var workspace = TestWorkspace.CreateCSharp(markup))
......
......@@ -435,6 +435,32 @@ End Program"
Await VerifySendEnterThroughToEditorAsync(code, "bar", expected:=False)
End Function
<WorkItem(26560, "https://github.com/dotnet/roslyn/issues/26560")>
<Fact, Trait(Traits.Feature, Traits.Features.Completion)>
Public Async Function TestKeywordsEscaped() As Task
Dim text = <a>Class C
Public Property [Wend] As Integer
Public Property [New] As Integer
Public Property A As Integer
End Class
Class Program
Sub Main()
Dim c As New C With { .$$ }
End Sub
End Class</a>.Value
Await VerifyItemExistsAsync(text, "[Wend]")
Await VerifyItemExistsAsync(text, "[New]")
Await VerifyItemExistsAsync(text, "A")
Await VerifyItemIsAbsentAsync(text, "Wend")
Await VerifyItemIsAbsentAsync(text, "New")
End Function
Friend Overrides Function CreateCompletionProvider() As CompletionProvider
Return New ObjectInitializerCompletionProvider()
End Function
......
......@@ -178,5 +178,10 @@ protected override bool IsInitializable(ISymbol member, INamedTypeSymbol contain
return base.IsInitializable(member, containingType);
}
protected override string EscapeIdentifier(ISymbol symbol)
{
return symbol.Name.EscapeIdentifier();
}
}
}
......@@ -15,6 +15,7 @@ internal abstract class AbstractObjectInitializerCompletionProvider : CommonComp
{
protected abstract Tuple<ITypeSymbol, Location> GetInitializedType(Document document, SemanticModel semanticModel, int position, CancellationToken cancellationToken);
protected abstract HashSet<string> GetInitializedMembers(SyntaxTree tree, int position, CancellationToken cancellationToken);
protected abstract string EscapeIdentifier(ISymbol symbol);
public override async Task ProvideCompletionsAsync(CompletionContext context)
{
......@@ -63,7 +64,7 @@ public override async Task ProvideCompletionsAsync(CompletionContext context)
foreach (var uninitializedMember in uninitializedMembers)
{
context.AddItem(SymbolCompletionItem.CreateWithSymbolId(
displayText: uninitializedMember.Name,
displayText: EscapeIdentifier(uninitializedMember),
insertionText: null,
symbols: ImmutableArray.Create(uninitializedMember),
contextPosition: initializerLocation.SourceSpan.Start,
......
......@@ -93,6 +93,10 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Completion.Providers
member.IsAccessibleWithin(containingType)
End Function
Protected Overrides Function EscapeIdentifier(symbol As ISymbol) As String
Return symbol.Name.EscapeIdentifier()
End Function
Private Function IsValidProperty(member As ISymbol) As Boolean
Dim [property] = TryCast(member, IPropertySymbol)
If [property] IsNot Nothing Then
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册