提交 7d5d6fcc 编写于 作者: S Sam Harwell

Simplify checks for showing properties in the object initializer syntax

上级 c7601c0f
......@@ -698,8 +698,9 @@ static void Main(string[] args)
}
}";
await VerifyItemIsAbsentAsync(markup, "S");
await VerifyItemIsAbsentAsync(markup, "D");
// Can't use S={3}, but the object initializer syntax S={} is still valid
await VerifyItemExistsAsync(markup, "S");
await VerifyItemExistsAsync(markup, "D");
}
[WorkItem(13158, "https://github.com/dotnet/roslyn/issues/13158")]
......@@ -1015,7 +1016,7 @@ static void Main(string[] args)
}
}";
await VerifyItemIsAbsentAsync(markup, "PropB");
await VerifyItemExistsAsync(markup, "PropB");
}
private async Task VerifyExclusiveAsync(string markup, bool exclusive)
......
......@@ -3,11 +3,13 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.Text;
using Roslyn.Utilities;
namespace Microsoft.CodeAnalysis.Completion.Providers
{
......@@ -78,7 +80,10 @@ protected override Task<CompletionDescription> GetDescriptionWorkerAsync(Documen
protected abstract Task<bool> IsExclusiveAsync(Document document, int position, CancellationToken cancellationToken);
private bool IsLegalFieldOrProperty(ISymbol symbol, ISymbol within)
=> CanSupportCollectionInitializer(symbol, within) || symbol.IsWriteableFieldOrProperty() || CanSupportObjectInitializer(symbol, within);
{
return symbol.IsWriteableFieldOrProperty()
|| CanSupportObjectInitializer(symbol, within);
}
private static readonly CompletionItemRules s_rules = CompletionItemRules.Create(enterKeyRule: EnterKeyRule.Never);
......@@ -92,39 +97,18 @@ protected virtual bool IsInitializable(ISymbol member, INamedTypeSymbol containi
private static bool CanSupportObjectInitializer(ISymbol symbol, ISymbol within)
{
if (symbol is IPropertySymbol propertySymbol)
Debug.Assert(!symbol.IsWriteableFieldOrProperty(), "Assertion failed - expected writable field/property check before calling this method.");
if (symbol is IFieldSymbol fieldSymbol)
{
return propertySymbol.GetMethod != null && !propertySymbol.Type.IsStructType() && HasAccesseblePropertiesOrFields(propertySymbol.Type, within);
return !fieldSymbol.Type.IsStructType();
}
return false;
}
private static bool HasAccesseblePropertiesOrFields(ITypeSymbol type, ISymbol within)
{
var types = new HashSet<ITypeSymbol>();
return HasAccesseblePropertiesOrFields(type, types);
bool HasAccesseblePropertiesOrFields(ITypeSymbol typeToCheck, HashSet<ITypeSymbol> alreadyCheckedTypes)
else if (symbol is IPropertySymbol propertySymbol)
{
if (alreadyCheckedTypes.Contains(typeToCheck))
{
return false;
}
alreadyCheckedTypes.Add(typeToCheck);
return typeToCheck.GetBaseTypesAndThis().SelectMany(x => x.GetMembers())
.Where(member => member is IPropertySymbol || member is IFieldSymbol)
.Any(member => member.IsWriteableFieldOrProperty() ||
CanSupportCollectionInitializer(member, within) ||
HasAccesseblePropertiesOrFields(member.GetMemberType(), alreadyCheckedTypes));
return !propertySymbol.Type.IsStructType();
}
}
private static bool CanSupportCollectionInitializer(ISymbol symbol, ISymbol within)
{
var type = symbol.GetMemberType();
return type != null && type.CanSupportCollectionInitializer(within);
throw ExceptionUtilities.Unreachable;
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册