提交 ed1ff182 编写于 作者: C CyrusNajmabadi

Add a structure provider for outlining collection initializers.

上级 e8b5adf3
......@@ -56,6 +56,7 @@
<Link>InternalUtilities\LambdaUtilities.cs</Link>
</Compile>
<Compile Include="ConvertToInterpolatedString\CSharpConvertConcatenationToInterpolatedStringRefactoringProvider.cs" />
<Compile Include="Structure\Providers\InitializerExpressionStructureProvider.cs" />
<Compile Include="UseCoalesceExpression\CSharpUseCoalesceExpressionForNullableDiagnosticAnalyzer.cs" />
<Compile Include="UseCoalesceExpression\CSharpUseCoalesceExpressionDiagnosticAnalyzer.cs" />
<Compile Include="UseExpressionBody\Accessors\UseExpressionBodyForAccessorsCodeFixProvider.cs" />
......
......@@ -7,6 +7,7 @@
namespace Microsoft.CodeAnalysis.CSharp.Structure
{
internal class CSharpBlockStructureProvider : AbstractBlockStructureProvider
{
private static ImmutableDictionary<Type, ImmutableArray<AbstractSyntaxStructureProvider>> CreateDefaultNodeProviderMap()
......@@ -29,6 +30,7 @@ internal class CSharpBlockStructureProvider : AbstractBlockStructureProvider
builder.Add<EventFieldDeclarationSyntax, EventFieldDeclarationStructureProvider, MetadataAsSource.MetadataEventFieldDeclarationStructureProvider>();
builder.Add<FieldDeclarationSyntax, FieldDeclarationStructureProvider, MetadataAsSource.MetadataFieldDeclarationStructureProvider>();
builder.Add<IndexerDeclarationSyntax, IndexerDeclarationStructureProvider, MetadataAsSource.MetadataIndexerDeclarationStructureProvider>();
builder.Add<InitializerExpressionSyntax, InitializerExpressionStructureProvider>();
builder.Add<InterfaceDeclarationSyntax, TypeDeclarationStructureProvider, MetadataAsSource.MetadataTypeDeclarationStructureProvider>();
builder.Add<MethodDeclarationSyntax, MethodDeclarationStructureProvider, MetadataAsSource.MetadataMethodDeclarationStructureProvider>();
builder.Add<NamespaceDeclarationSyntax, NamespaceDeclarationStructureProvider>();
......
// 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;
using System.Threading;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Structure;
using Microsoft.CodeAnalysis.Text;
namespace Microsoft.CodeAnalysis.CSharp.Structure
{
internal class InitializerExpressionStructureProvider : AbstractSyntaxNodeStructureProvider<InitializerExpressionSyntax>
{
protected override void CollectBlockSpans(
InitializerExpressionSyntax node,
ArrayBuilder<BlockSpan> spans,
CancellationToken cancellationToken)
{
if (node.Parent is InitializerExpressionSyntax)
{
// We have something like:
//
// new Dictionary<int, string>
// {
// ...
// {
// ...
// },
// ...
// }
//
// In this case, we want to collapse the "{ ... }," (including the comma).
var nextToken = node.CloseBraceToken.GetNextToken();
var end = nextToken.Kind() == SyntaxKind.CommaToken
? nextToken.Span.End
: node.Span.End;
spans.Add(new BlockSpan(
isCollapsible: true,
textSpan: TextSpan.FromBounds(node.SpanStart, end),
hintSpan: TextSpan.FromBounds(node.SpanStart, end),
type: BlockTypes.Expression));
}
else
{
// Parent is something like:
//
// new Dictionary<int, string> {
// ...
// }
//
// The collapsed textspan should be from the > to the }
//
// However, the hint span should be the entire object creation.
var previousToken = node.OpenBraceToken.GetPreviousToken();
spans.Add(new BlockSpan(
isCollapsible: true,
textSpan: TextSpan.FromBounds(previousToken.Span.End, node.Span.End),
hintSpan: node.Parent.Span,
type: BlockTypes.Expression));
}
}
}
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册