public void HandleMatchBracesResponse()

in vsintegration/src/FSharp.LanguageService.Base/Source.cs [1293:1394]


        public void HandleMatchBracesResponse(BackgroundRequest_DEPRECATED req)
        {
            try
            {
                if (this.service == null || req.Timestamp != this.ChangeCount)
                    return;

                // save request so it can later be reused in GetPairExtent
                HandleGetPairExtentResponse(req);
#if LANGTRACE
                Trace.WriteLine("HandleMatchBracesResponse");
#endif
                // Filter matching braces and find spans to highlight - we want to highlight 
                // matches only before opening and after closing brace
                int line;
                int idx;
                NativeMethods.ThrowOnFailure(req.View.GetCaretPos(out line, out idx));

                // Get all braces from language service and filter them
                List<BraceMatch_DEPRECATED> braces = new List<BraceMatch_DEPRECATED>();
                foreach (BraceMatch_DEPRECATED m in req.ResultSink.Braces) braces.Add(m);
                braces.RemoveAll(delegate(BraceMatch_DEPRECATED match)
                {
                    if (match.a.iStartLine == line && match.a.iStartIndex == idx) return false;
                    if (match.b.iEndLine == line && match.b.iEndIndex == idx) return false;
                    return true;
                });

                // Transform collection of braces into an array of spans
                List<TextSpan> spans = new List<TextSpan>();
                foreach (BraceMatch_DEPRECATED m in braces)
                {
                    spans.Add(m.a); spans.Add(m.b);
                }

                // Highlight
                if (spans.Count == 0) return;
                NativeMethods.ThrowOnFailure(req.View.HighlightMatchingBrace((uint)this.service.Preferences.HighlightMatchingBraceFlags, (uint)spans.Count, spans.ToArray()));

                //try to show the matching line in the statusbar
                if (spans.Count > 0 && this.service.Preferences.EnableShowMatchingBrace)
                {
                    IVsStatusbar statusBar = (IVsStatusbar)service.Site.GetService(typeof(SVsStatusbar));
                    if (statusBar != null)
                    {
                        TextSpan span = spans[0];
                        bool found = false;
                        // Gather up the other side of the brace match so we can 
                        // display the text in the status bar. There could be more than one
                        // if MatchTriple was involved, in which case we merge them.
                        for (int i = 0, n = spans.Count; i < n; i++)
                        {
                            TextSpan brace = spans[i];
                            if (brace.iStartLine != req.Line)
                            {
                                if (brace.iEndLine != brace.iStartLine)
                                {
                                    brace.iEndLine = brace.iStartLine;
                                    brace.iEndIndex = this.GetLineLength(brace.iStartLine);
                                }
                                if (!found)
                                {
                                    span = brace;
                                }
                                else if (brace.iStartLine == span.iStartLine)
                                {
                                    span = TextSpanHelper.Merge(span, brace);
                                }
                                found = true;
                            }
                        }
                        if (found)
                        {
                            Debug.Assert(TextSpanHelper.IsPositive(span));
                            string text = this.GetText(span);

                            int start;
                            int len = text.Length;

                            for (start = 0; start < len && Char.IsWhiteSpace(text[start]); start++) ;

                            if (start < span.iEndIndex)
                            {
                                if (text.Length > 80)
                                {
                                    text = String.Format(CultureInfo.CurrentUICulture, SR.GetString(SR.Truncated), text.Substring(0, 80));
                                }
                                text = String.Format(CultureInfo.CurrentUICulture, SR.GetString(SR.BraceMatchStatus), text);
                                NativeMethods.ThrowOnFailure(statusBar.SetText(text));
                            }
                        }
                    }
                }
#if LANGTRACE
            } catch (Exception e) {
                Trace.WriteLine("HandleMatchBracesResponse exception: " + e.Message);
#endif
            }
            catch
            {
            }
        }