Worker.cs 6.6 KB
Newer Older
P
Pilchie 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
// Copyright (c) Microsoft Open Technologies, Inc.  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.Collections.Generic;
using System.Linq;
using System.Threading;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Classification;
using Microsoft.CodeAnalysis.CSharp.Extensions;
using Microsoft.CodeAnalysis.CSharp.Symbols;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Shared.Collections;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.Text;
using Roslyn.Utilities;

namespace Microsoft.CodeAnalysis.CSharp.Classification
{
    /// <summary>
    /// Worker is an utility class that can classify a list of tokens or a tree within a
    /// requested span The implementation is generic and can produce any kind of classification
    /// artifacts T T is normally either ClassificationSpan or a Tuple (for testing purposes) 
    /// and constructed via provided factory.
    /// </summary>
    internal partial class Worker
    {
#if DEBUG
        /// <summary>
        /// nonOverlappingSpans spans used for Debug validation that spans that worker produces
        /// are not mutually overlapping.
        /// </summary>
        private SimpleIntervalTree<TextSpan> nonOverlappingSpans;
#endif

        private readonly TextSpan textSpan;
        private readonly List<ClassifiedSpan> result;
        private readonly CancellationToken cancellationToken;

        private Worker(TextSpan textSpan, List<ClassifiedSpan> result, CancellationToken cancellationToken)
        {
            this.result = result;
            this.textSpan = textSpan;
            this.cancellationToken = cancellationToken;
        }

        internal static void CollectClassifiedSpans(
            IEnumerable<SyntaxToken> tokens, TextSpan textSpan, List<ClassifiedSpan> result, CancellationToken cancellationToken)
        {
            var worker = new Worker(textSpan, result, cancellationToken);
            foreach (var tk in tokens)
            {
                worker.ClassifyToken(tk);
            }
        }

        internal static void CollectClassifiedSpans(SyntaxNode
            node, TextSpan textSpan, List<ClassifiedSpan> result, CancellationToken cancellationToken)
        {
            var worker = new Worker(textSpan, result, cancellationToken);
            worker.ClassifyNode(node);
        }

        [System.Diagnostics.Conditional("DEBUG")]
        private void Validate(TextSpan textSpan)
        {
#if DEBUG
            if (nonOverlappingSpans == null)
            {
                nonOverlappingSpans = SimpleIntervalTree.Create(TextSpanIntervalIntrospector.Instance);
            }

            // new span should not overlap with any span that we already have.
            Contract.Requires(!nonOverlappingSpans.GetOverlappingIntervals(textSpan.Start, textSpan.Length).Any());

            nonOverlappingSpans = nonOverlappingSpans.AddInterval(textSpan);
#endif
        }

        private void AddClassification(TextSpan span, string type)
        {
            Validate(span);
            this.result.Add(new ClassifiedSpan(type, span));
        }

        private void AddClassification(SyntaxTrivia trivia, string type)
        {
            if (trivia.Width() > 0 && textSpan.OverlapsWith(trivia.Span))
            {
                AddClassification(trivia.Span, type);
            }
        }

        private void AddClassification(SyntaxToken token, string type)
        {
            if (token.Width() > 0 && textSpan.OverlapsWith(token.Span))
            {
                AddClassification(token.Span, type);
            }
        }

        private void ClassifyNodeOrToken(SyntaxNodeOrToken nodeOrToken)
        {
            if (nodeOrToken.IsToken)
            {
                ClassifyToken(nodeOrToken.AsToken());
                return;
            }

            ClassifyNode(nodeOrToken.AsNode());
        }

        private void ClassifyNode(SyntaxNode node)
        {
            foreach (var token in node.DescendantTokens(span: this.textSpan, descendIntoTrivia: false))
            {
                cancellationToken.ThrowIfCancellationRequested();
                ClassifyToken(token);
            }
        }

        private void ClassifyToken(SyntaxToken token)
        {
            var span = token.Span;
            if (span.Length != 0 && textSpan.OverlapsWith(span))
            {
                var type = ClassificationHelpers.GetClassification(token);

                if (type != null)
                {
                    AddClassification(span, type);
                }
            }

            foreach (var trivia in token.LeadingTrivia)
            {
                cancellationToken.ThrowIfCancellationRequested();
                ClassifyTrivia(trivia);
            }

            foreach (var trivia in token.TrailingTrivia)
            {
                cancellationToken.ThrowIfCancellationRequested();
                ClassifyTrivia(trivia);
            }
        }

        private void ClassifyTrivia(SyntaxTrivia trivia)
        {
149
            if (trivia.Kind() == SyntaxKind.SingleLineCommentTrivia || trivia.Kind() == SyntaxKind.MultiLineCommentTrivia)
P
Pilchie 已提交
150 151 152
            {
                AddClassification(trivia, ClassificationTypeNames.Comment);
            }
153
            else if (trivia.Kind() == SyntaxKind.DisabledTextTrivia)
P
Pilchie 已提交
154 155 156
            {
                AddClassification(trivia, ClassificationTypeNames.ExcludedCode);
            }
157
            else if (trivia.Kind() == SyntaxKind.SkippedTokensTrivia)
P
Pilchie 已提交
158 159 160 161 162 163 164
            {
                ClassifySkippedTokens((SkippedTokensTriviaSyntax)trivia.GetStructure());
            }
            else if (trivia.IsDocComment())
            {
                ClassifyDocumentationComment((DocumentationCommentTriviaSyntax)trivia.GetStructure());
            }
165
            else if (trivia.Kind() == SyntaxKind.DocumentationCommentExteriorTrivia)
P
Pilchie 已提交
166 167 168
            {
                AddClassification(trivia, ClassificationTypeNames.XmlDocCommentDelimiter);
            }
169
            else if (SyntaxFacts.IsPreprocessorDirective(trivia.Kind()))
P
Pilchie 已提交
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
            {
                ClassifyPreprocessorDirective((DirectiveTriviaSyntax)trivia.GetStructure());
            }
        }

        private void ClassifySkippedTokens(SkippedTokensTriviaSyntax skippedTokens)
        {
            if (!textSpan.OverlapsWith(skippedTokens.Span))
            {
                return;
            }

            var tokens = skippedTokens.Tokens;
            foreach (var tk in tokens)
            {
                ClassifyToken(tk);
            }
        }
    }
}