RegionDirectiveOutliner.cs 1.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// 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.Collections.Generic;
using System.Threading;
using Microsoft.CodeAnalysis.CSharp.Extensions;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Editor.Implementation.Outlining;
using Microsoft.CodeAnalysis.Text;

namespace Microsoft.CodeAnalysis.Editor.CSharp.Outlining
{
    internal class RegionDirectiveOutliner : AbstractSyntaxNodeOutliner<RegionDirectiveTriviaSyntax>
    {
        private static string GetBannerText(DirectiveTriviaSyntax simpleDirective)
        {
            var kw = simpleDirective.DirectiveNameToken;
17
            var prefixLength = kw.Span.End - simpleDirective.Span.Start;
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
            var text = simpleDirective.ToString().Substring(prefixLength).Trim();

            if (text.Length == 0)
            {
                return simpleDirective.HashToken.ToString() + kw.ToString();
            }
            else
            {
                return text;
            }
        }

        protected override void CollectOutliningSpans(
            RegionDirectiveTriviaSyntax regionDirective,
            List<OutliningSpan> spans,
            CancellationToken cancellationToken)
        {
            var match = regionDirective.GetMatchingDirective(cancellationToken);
            if (match != null)
            {
                spans.Add(new OutliningSpan(
                    TextSpan.FromBounds(regionDirective.SpanStart, match.Span.End),
                    GetBannerText(regionDirective),
41 42
                    autoCollapse: true,
                    isDefaultCollapsed: true));
43 44 45 46 47 48 49 50 51
            }
        }

        protected override bool SupportedInWorkspaceKind(string kind)
        {
            return kind != WorkspaceKind.MetadataAsSource;
        }
    }
}