示例#1
0
        public void ColourisePattern(ICancellable cnc, ColouredSegments colouredSegments, string pattern, Segment visibleSegment)
        {
            Regex regex = ColouringRegex;

            foreach (Match m in regex.Matches(pattern))
            {
                Debug.Assert(m.Success);

                if (cnc.IsCancellationRequested)
                {
                    return;
                }

                // escapes, '\...'
                {
                    var g = m.Groups["escape"];
                    if (g.Success)
                    {
                        if (cnc.IsCancellationRequested)
                        {
                            return;
                        }

                        foreach (Capture c in g.Captures)
                        {
                            if (cnc.IsCancellationRequested)
                            {
                                return;
                            }

                            var intersection = Segment.Intersection(visibleSegment, c.Index, c.Length);

                            if (!intersection.IsEmpty)
                            {
                                colouredSegments.Escapes.Add(intersection);
                            }
                        }
                    }
                }

                if (cnc.IsCancellationRequested)
                {
                    return;
                }
            }
        }
示例#2
0
        public void ColourisePattern(ICancellable cnc, ColouredSegments colouredSegments, string pattern, Segment visibleSegment)
        {
            Regex regex = GetCachedColouringRegex( );

            foreach (Match m in regex.Matches(pattern))
            {
                Debug.Assert(m.Success);

                if (cnc.IsCancellationRequested)
                {
                    return;
                }

                // escapes, '\...'
                {
                    var g = m.Groups["escape"];
                    if (g.Success)
                    {
                        if (cnc.IsCancellationRequested)
                        {
                            return;
                        }

                        foreach (Capture c in g.Captures)
                        {
                            if (cnc.IsCancellationRequested)
                            {
                                return;
                            }

                            var intersection = Segment.Intersection(visibleSegment, c.Index, c.Length);

                            if (!intersection.IsEmpty)
                            {
                                colouredSegments.Escapes.Add(intersection);
                            }
                        }
                    }
                }

                if (cnc.IsCancellationRequested)
                {
                    return;
                }

                // class (within [...] groups), '[:...:]'
                {
                    var g = m.Groups["class"];
                    if (g.Success)
                    {
                        if (cnc.IsCancellationRequested)
                        {
                            return;
                        }

                        foreach (Capture c in g.Captures)
                        {
                            if (cnc.IsCancellationRequested)
                            {
                                return;
                            }

                            var intersection = Segment.Intersection(visibleSegment, c.Index, c.Length);

                            if (!intersection.IsEmpty)
                            {
                                colouredSegments.Escapes.Add(intersection);
                            }
                        }
                    }
                }

                if (cnc.IsCancellationRequested)
                {
                    return;
                }

                // named group, '(?P<name>...)'
                {
                    var g = m.Groups["name"];
                    if (g.Success)
                    {
                        if (cnc.IsCancellationRequested)
                        {
                            return;
                        }

                        foreach (Capture c in g.Captures)
                        {
                            if (cnc.IsCancellationRequested)
                            {
                                return;
                            }

                            var intersection = Segment.Intersection(visibleSegment, c.Index, c.Length);

                            if (!intersection.IsEmpty)
                            {
                                colouredSegments.GroupNames.Add(intersection);
                            }
                        }
                    }
                }
            }
        }
示例#3
0
        public void ColourisePattern(ICancellable cnc, ColouredSegments colouredSegments, string pattern, Segment visibleSegment)
        {
            Regex regex = GetCachedColouringRegex( );

            foreach (Match m in regex.Matches(pattern))
            {
                Debug.Assert(m.Success);

                if (cnc.IsCancellationRequested)
                {
                    return;
                }

                // escapes, '\...'
                {
                    var g = m.Groups["escape"];
                    if (g.Success)
                    {
                        if (cnc.IsCancellationRequested)
                        {
                            return;
                        }

                        foreach (Capture c in g.Captures)
                        {
                            if (cnc.IsCancellationRequested)
                            {
                                return;
                            }

                            var intersection = Segment.Intersection(visibleSegment, c.Index, c.Length);

                            if (!intersection.IsEmpty)
                            {
                                colouredSegments.Escapes.Add(intersection);
                            }
                        }

                        continue;
                    }
                }

                if (cnc.IsCancellationRequested)
                {
                    return;
                }

                // comments, '(?#...)', '#...'
                {
                    var g = m.Groups["comment"];
                    if (g.Success)
                    {
                        if (cnc.IsCancellationRequested)
                        {
                            return;
                        }

                        foreach (Capture c in g.Captures)
                        {
                            if (cnc.IsCancellationRequested)
                            {
                                return;
                            }

                            var intersection = Segment.Intersection(visibleSegment, c.Index, c.Length);

                            if (!intersection.IsEmpty)
                            {
                                colouredSegments.Comments.Add(intersection);
                            }
                        }

                        continue;
                    }
                }

                if (cnc.IsCancellationRequested)
                {
                    return;
                }

                // named groups and back references
                {
                    var g = m.Groups["name"];
                    if (g.Success)
                    {
                        if (cnc.IsCancellationRequested)
                        {
                            return;
                        }

                        foreach (Capture c in g.Captures)
                        {
                            if (cnc.IsCancellationRequested)
                            {
                                return;
                            }

                            var intersection = Segment.Intersection(visibleSegment, c.Index, c.Length);

                            if (!intersection.IsEmpty)
                            {
                                colouredSegments.GroupNames.Add(intersection);
                            }
                        }

                        continue;
                    }
                }
            }
        }
示例#4
0
        public void ColourisePattern(ICancellable cnc, ColouredSegments colouredSegments, string pattern, Segment visibleSegment)
        {
            Regex regex = GetCachedColouringRegex(OptionsControl.GetSelectedOptions( ).NativeOptions);

            foreach (Match m in regex.Matches(pattern))
            {
                Debug.Assert(m.Success);

                if (cnc.IsCancellationRequested)
                {
                    return;
                }

                // comments, '(?#...)' and '#...'
                {
                    var g = m.Groups["comment"];
                    if (g.Success)
                    {
                        if (cnc.IsCancellationRequested)
                        {
                            return;
                        }

                        var intersection = Segment.Intersection(visibleSegment, g.Index, g.Length);

                        if (!intersection.IsEmpty)
                        {
                            colouredSegments.Comments.Add(intersection);
                        }

                        continue;
                    }
                }

                if (cnc.IsCancellationRequested)
                {
                    return;
                }

                // escapes, '\...'
                {
                    var g = m.Groups["escape"];
                    if (g.Success)
                    {
                        if (cnc.IsCancellationRequested)
                        {
                            return;
                        }

                        // we need captures because of '*?'
                        foreach (Capture c in g.Captures)
                        {
                            if (cnc.IsCancellationRequested)
                            {
                                return;
                            }

                            var intersection = Segment.Intersection(visibleSegment, c.Index, c.Length);

                            if (!intersection.IsEmpty)
                            {
                                colouredSegments.Escapes.Add(intersection);
                            }
                        }

                        continue;
                    }
                }

                if (cnc.IsCancellationRequested)
                {
                    return;
                }

                // named groups, '(?<name>...' and "(?'name'...", including balancing groups
                {
                    var g = m.Groups["name"];
                    if (g.Success)
                    {
                        if (cnc.IsCancellationRequested)
                        {
                            return;
                        }

                        var intersection = Segment.Intersection(visibleSegment, g.Index, g.Length);

                        if (!intersection.IsEmpty)
                        {
                            colouredSegments.GroupNames.Add(intersection);
                        }

                        continue;
                    }
                }
            }
        }
示例#5
0
        public void ColourisePattern(ICancellable cnc, ColouredSegments colouredSegments, string pattern, Segment visibleSegment)
        {
            GrammarEnum grammar = OptionsControl.GetGrammar( );
            bool        mod_x   = OptionsControl.GetModX( );

            Regex regex = GetCachedColouringRegex(grammar, mod_x);

            foreach (Match m in regex.Matches(pattern))
            {
                Debug.Assert(m.Success);

                if (cnc.IsCancellationRequested)
                {
                    return;
                }

                // escapes, '\...'
                {
                    var g = m.Groups["escape"];
                    if (g.Success)
                    {
                        if (cnc.IsCancellationRequested)
                        {
                            return;
                        }

                        foreach (Capture c in g.Captures)
                        {
                            if (cnc.IsCancellationRequested)
                            {
                                return;
                            }

                            var intersection = Segment.Intersection(visibleSegment, c.Index, c.Length);

                            if (!intersection.IsEmpty)
                            {
                                colouredSegments.Escapes.Add(intersection);
                            }
                        }
                    }
                }

                if (cnc.IsCancellationRequested)
                {
                    return;
                }

                // comments, '(?#...)', '#...'
                {
                    var g = m.Groups["comment"];
                    if (g.Success)
                    {
                        if (cnc.IsCancellationRequested)
                        {
                            return;
                        }

                        foreach (Capture c in g.Captures)
                        {
                            if (cnc.IsCancellationRequested)
                            {
                                return;
                            }

                            var intersection = Segment.Intersection(visibleSegment, c.Index, c.Length);

                            if (!intersection.IsEmpty)
                            {
                                colouredSegments.Comments.Add(intersection);
                            }
                        }
                    }
                }

                if (cnc.IsCancellationRequested)
                {
                    return;
                }

                // class (within [...] groups), '[:...:]', '[=...=]', '[. ... .]'
                {
                    var g = m.Groups["class"];
                    if (g.Success)
                    {
                        if (cnc.IsCancellationRequested)
                        {
                            return;
                        }

                        foreach (Capture c in g.Captures)
                        {
                            if (cnc.IsCancellationRequested)
                            {
                                return;
                            }

                            var intersection = Segment.Intersection(visibleSegment, c.Index, c.Length);

                            if (!intersection.IsEmpty)
                            {
                                colouredSegments.Escapes.Add(intersection);
                            }
                        }
                    }
                }

                if (cnc.IsCancellationRequested)
                {
                    return;
                }

                // named group, '(?<name>...)' or '(?'name'...)'
                {
                    var g = m.Groups["name"];
                    if (g.Success)
                    {
                        if (cnc.IsCancellationRequested)
                        {
                            return;
                        }

                        foreach (Capture c in g.Captures)
                        {
                            if (cnc.IsCancellationRequested)
                            {
                                return;
                            }

                            var intersection = Segment.Intersection(visibleSegment, c.Index, c.Length);

                            if (!intersection.IsEmpty)
                            {
                                colouredSegments.GroupNames.Add(intersection);
                            }
                        }
                    }
                }
            }
        }
示例#6
0
        public void ColourisePattern(ICancellable cnc, ColouredSegments colouredSegments, string pattern, Segment visibleSegment)
        {
            GrammarEnum grammar = OptionsControl.GetGrammar( );

            Regex regex = GetCachedColouringRegex(grammar);

            foreach (Match m in regex.Matches(pattern))
            {
                Debug.Assert(m.Success);

                if (cnc.IsCancellationRequested)
                {
                    return;
                }

                // escapes, '\...'
                {
                    var g = m.Groups["escape"];
                    if (g.Success)
                    {
                        if (cnc.IsCancellationRequested)
                        {
                            return;
                        }

                        foreach (Capture c in g.Captures)
                        {
                            if (cnc.IsCancellationRequested)
                            {
                                return;
                            }

                            var intersection = Segment.Intersection(visibleSegment, c.Index, c.Length);

                            if (!intersection.IsEmpty)
                            {
                                colouredSegments.Escapes.Add(intersection);
                            }
                        }
                    }
                }

                if (cnc.IsCancellationRequested)
                {
                    return;
                }

                // classes within character groups, [ ... [:...:] ... ]
                {
                    var g = m.Groups["class"];
                    if (g.Success)
                    {
                        if (cnc.IsCancellationRequested)
                        {
                            return;
                        }

                        foreach (Capture c in g.Captures)
                        {
                            if (cnc.IsCancellationRequested)
                            {
                                return;
                            }

                            var intersection = Segment.Intersection(visibleSegment, c.Index, c.Length);

                            if (!intersection.IsEmpty)
                            {
                                colouredSegments.Escapes.Add(intersection);
                            }
                        }
                    }
                }
            }
        }
示例#7
0
        void RecolouringThreadProc(ICancellable cnc)
        {
            IRegexEngine regex_engine;
            string       eol;

            lock (this)
            {
                regex_engine = mRegexEngine;
                eol          = mEol;
            }

            if (regex_engine == null)
            {
                return;
            }

            TextData td           = null;
            Rect     clip_rect    = Rect.Empty;
            int      top_index    = 0;
            int      bottom_index = 0;

            UITaskHelper.Invoke(rtb, () =>
            {
                td = null;

                var start_doc = rtb.Document.ContentStart;
                var end_doc   = rtb.Document.ContentStart;

                if (!start_doc.HasValidLayout || !end_doc.HasValidLayout)
                {
                    return;
                }

                var td0 = rtb.GetTextData(eol);

                if (cnc.IsCancellationRequested)
                {
                    return;
                }

                td        = td0;
                clip_rect = new Rect(new Size(rtb.ViewportWidth, rtb.ViewportHeight));

                TextPointer top_pointer = rtb.GetPositionFromPoint(new Point(0, 0), snapToText: true).GetLineStartPosition(-1, out int _);
                if (cnc.IsCancellationRequested)
                {
                    return;
                }

                top_index = td.TextPointers.GetIndex(top_pointer, LogicalDirection.Backward);
                if (cnc.IsCancellationRequested)
                {
                    return;
                }
                if (top_index < 0)
                {
                    top_index = 0;
                }

                TextPointer bottom_pointer = rtb.GetPositionFromPoint(new Point(0, rtb.ViewportHeight), snapToText: true).GetLineStartPosition(+1, out int lines_skipped);
                if (cnc.IsCancellationRequested)
                {
                    return;
                }

                if (bottom_pointer == null || lines_skipped == 0)
                {
                    bottom_index = td.Text.Length;
                }
                else
                {
                    bottom_index = td.TextPointers.GetIndex(bottom_pointer, LogicalDirection.Forward);
                    if (cnc.IsCancellationRequested)
                    {
                        return;
                    }
                }
                if (bottom_index > td.Text.Length)
                {
                    bottom_index = td.Text.Length;
                }
                if (bottom_index < top_index)
                {
                    bottom_index = top_index;                                            // (including 'if bottom_index == 0')
                }
            });

            if (cnc.IsCancellationRequested)
            {
                return;
            }

            if (td == null)
            {
                return;
            }
            if (td.Text.Length == 0)
            {
                return;
            }

            Debug.Assert(top_index >= 0);
            Debug.Assert(bottom_index >= top_index);
            //Debug.Assert( bottom_index < td.OldPointers.Count );
            Debug.Assert(bottom_index <= td.Text.Length);

            var visible_segment       = new Segment(top_index, bottom_index - top_index + 1);
            var segments_to_colourise = new ColouredSegments( );

            regex_engine.ColourisePattern(cnc, segments_to_colourise, td.Text, visible_segment);

            if (cnc.IsCancellationRequested)
            {
                return;
            }

            int center_index = (top_index + bottom_index) / 2;

            var arranged_escapes = segments_to_colourise.Escapes
                                   .OrderBy(s => Math.Abs(center_index - (s.Index + s.Length / 2)))
                                   .ToList( );

            RtbUtilities.ApplyStyle(cnc, ChangeEventHelper, null, td, segments_to_colourise.Comments, PatternCommentStyleInfo);
            RtbUtilities.ApplyStyle(cnc, ChangeEventHelper, null, td, arranged_escapes, PatternEscapeStyleInfo);
            RtbUtilities.ApplyStyle(cnc, ChangeEventHelper, null, td, segments_to_colourise.GroupNames, PatternGroupNameStyleInfo);

            var uncovered_segments = new List <Segment> {
                new Segment(0, td.Text.Length)
            };

            foreach (var s in segments_to_colourise.All.SelectMany(s => s))
            {
                if (cnc.IsCancellationRequested)
                {
                    return;
                }

                Segment.Except(uncovered_segments, s);
            }

            Segment.Except(uncovered_segments, LeftHighlightedParenthesis);
            Segment.Except(uncovered_segments, RightHighlightedParenthesis);
            Segment.Except(uncovered_segments, LeftHighlightedBracket);
            Segment.Except(uncovered_segments, RightHighlightedBracket);
            Segment.Except(uncovered_segments, LeftHighlightedCurlyBrace);
            Segment.Except(uncovered_segments, RightHighlightedCurlyBrace);

            var segments_to_uncolour =
                uncovered_segments
                .Select(s => Segment.Intersection(s, visible_segment))
                .Where(s => !s.IsEmpty)
                .OrderBy(s => Math.Abs(center_index - (s.Index + s.Length / 2)))
                .ToList( );

            if (cnc.IsCancellationRequested)
            {
                return;
            }

            RtbUtilities.ApplyStyle(cnc, ChangeEventHelper, null, td, segments_to_uncolour, PatternNormalStyleInfo);
        }