TodoCommentTokens.cs 1.8 KB
Newer Older
J
Jonathon Marolf 已提交
1 2 3
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
4 5 6 7

using System.Collections.Generic;
using System.Collections.Immutable;
using System.ComponentModel.Composition;
8
using System.Globalization;
J
Jason Malinowski 已提交
9
using System.Threading;
10
using Microsoft.CodeAnalysis.TodoComments;
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

namespace Microsoft.CodeAnalysis.Editor.Implementation.TodoComments
{
    /// <summary>
    /// provide comment tokens to scan
    /// 
    /// we use this indirection so that we can get different tokens based on host
    /// </summary>
    [Export]
    internal class TodoCommentTokens
    {
        [ImportingConstructor]
        public TodoCommentTokens()
        {
        }

        private class TokenInfo
        {
            internal readonly string OptionText;
            internal readonly ImmutableArray<TodoCommentDescriptor> Tokens;

            public TokenInfo(string optionText, ImmutableArray<TodoCommentDescriptor> tokens)
            {
                this.OptionText = optionText;
                this.Tokens = tokens;
            }
        }

        private TokenInfo _lastTokenInfo;

C
Cyrus Najmabadi 已提交
41
        public ImmutableArray<TodoCommentDescriptor> GetTokens(Document document)
42
        {
43
            var optionText = document.Project.Solution.Options.GetOption(TodoCommentOptions.TokenList);
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58

            var lastInfo = _lastTokenInfo;
            if (lastInfo != null && lastInfo.OptionText == optionText)
            {
                return lastInfo.Tokens;
            }

            var tokens = Parse(optionText);

            System.Threading.Interlocked.CompareExchange(ref _lastTokenInfo, new TokenInfo(optionText, tokens), lastInfo);

            return tokens;
        }
    }
}