Welcome to mirror list, hosted at ThFree Co, Russian Federation.

PatternMatch.cs « PatternMatching « TextLogic « Def « Text « src - github.com/microsoft/vs-editor-api.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: dd907d0e2009692b658f043511740e02824c4134 (plain)
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
using System;
using System.Collections.Immutable;
using System.Linq;

namespace Microsoft.VisualStudio.Text.PatternMatching
{
    public struct PatternMatch : IComparable<PatternMatch>
    {
        /// <summary>
        /// True if this was a case sensitive match.
        /// </summary>
        public bool IsCaseSensitive { get; }

        /// <summary>
        /// The type of match that occurred.
        /// </summary>
        public PatternMatchKind Kind { get; }

        /// <summary>
        /// The spans in the original text that were matched.  Only returned if the 
        /// pattern matcher is asked to collect these spans.
        /// </summary>
        public ImmutableArray<Span> MatchedSpans { get; }

        /// <summary>
        /// True if punctuation was removed for this match.
        /// </summary>
        public bool IsPunctuationStripped { get; }

        /// <summary>
        /// Creates a PatternMatch object with an optional single span.
        /// </summary>
        /// <param name="resultType">How is this match categorized?</param>
        /// <param name="punctuationStripped">Was punctuation removed?</param>
        /// <param name="isCaseSensitive">Was this a case sensitive match?</param>
        public PatternMatch(
            PatternMatchKind resultType,
            bool punctuationStripped,
            bool isCaseSensitive)
            : this(resultType, punctuationStripped, isCaseSensitive, ImmutableArray<Span>.Empty)
        {
        }

        /// <summary>
        /// Creates a PatternMatch object with a set of spans
        /// </summary>
        /// <param name="resultType">How is this match categorized?</param>
        /// <param name="punctuationStripped">Was punctuation removed?</param>
        /// <param name="isCaseSensitive">Was this a case sensitive match?</param>
        /// <param name="matchedSpans">What spans of the candidate were matched? An empty array signifies no span information is given.</param>
        public PatternMatch(
            PatternMatchKind resultType,
            bool punctuationStripped,
            bool isCaseSensitive,
            ImmutableArray<Span> matchedSpans)
            : this()
        {
            this.Kind = resultType;
            this.IsCaseSensitive = isCaseSensitive;
            this.MatchedSpans = matchedSpans;
            this.IsPunctuationStripped = punctuationStripped;
        }

        /// <summary>
        /// Get a PatternMatch object with additional spans added to it. This is an optimization to avoid having to call the whole constructor.
        /// </summary>
        /// <param name="matchedSpans">Spans to associate with this PatternMatch.</param>
        /// <returns>A new instance of a PatternMatch with the specified spans.</returns>
        public PatternMatch WithMatchedSpans(ImmutableArray<Span> matchedSpans)
            => new PatternMatch(Kind, IsPunctuationStripped, IsCaseSensitive, matchedSpans);

        /// <summary>
        /// Compares two PatternMatch objects.
        /// </summary>
        public int CompareTo(PatternMatch other)
            => CompareTo(other, ignoreCase: false);

        /// <summary>
        /// Compares two PatternMatch objects with the specified behavior for ignoring capitalization.
        /// </summary>
        /// <param name="ignoreCase">Should case be ignored?</param>
        public int CompareTo(PatternMatch other, bool ignoreCase)
        {
            int diff;
            if ((diff = CompareType(this, other)) != 0 ||
                (diff = CompareCase(this, other, ignoreCase)) != 0 ||
                (diff = ComparePunctuation(this, other)) != 0)
            {
                return diff;
            }

            return 0;
        }

        private static int ComparePunctuation(PatternMatch result1, PatternMatch result2)
        {
            // Consider a match to be better if it was successful without stripping punctuation
            // versus a match that had to strip punctuation to succeed.
            if (result1.IsPunctuationStripped != result2.IsPunctuationStripped)
            {
                return result1.IsPunctuationStripped ? 1 : -1;
            }

            return 0;
        }

        private static int CompareCase(PatternMatch result1, PatternMatch result2, bool ignoreCase)
        {
            if (!ignoreCase)
            {
                if (result1.IsCaseSensitive != result2.IsCaseSensitive)
                {
                    return result1.IsCaseSensitive ? -1 : 1;
                }
            }

            return 0;
        }

        private static int CompareType(PatternMatch result1, PatternMatch result2)
            => result1.Kind.CompareTo(result2.Kind);
    }
}