summaryrefslogtreecommitdiff
path: root/Source/DafnyExtension/BraceMatching.cs
blob: 50d264efd92cb613df9db7a7f6b8c34c50eeaf22 (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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using Microsoft.VisualStudio.Text;
using Microsoft.VisualStudio.Text.Editor;
using Microsoft.VisualStudio.Text.Tagging;
using Microsoft.VisualStudio.Utilities;


namespace DafnyLanguage
{

  #region Provider

  [Export(typeof(IViewTaggerProvider))]
  [ContentType("dafny")]
  [TagType(typeof(TextMarkerTag))]
  internal class BraceMatchingTaggerProvider : IViewTaggerProvider
  {
    [Import]
    internal IBufferTagAggregatorFactoryService AggregatorFactory = null;

    public ITagger<T> CreateTagger<T>(ITextView textView, ITextBuffer buffer) where T : ITag {
      if (textView == null)
        return null;

      //provide highlighting only on the top-level buffer
      if (textView.TextBuffer != buffer)
        return null;

      ITagAggregator<DafnyTokenTag> tagAggregator = AggregatorFactory.CreateTagAggregator<DafnyTokenTag>(buffer);
      return new BraceMatchingTagger(textView, buffer, tagAggregator) as ITagger<T>;
    }
  }

  #endregion


  #region Tagger

  internal abstract class TokenBasedTagger
  {
    ITagAggregator<DafnyTokenTag> _aggregator;

    internal TokenBasedTagger(ITagAggregator<DafnyTokenTag> tagAggregator) {
      _aggregator = tagAggregator;
    }

    public bool InsideComment(SnapshotPoint pt) {
      SnapshotSpan span = new SnapshotSpan(pt, 1);
      foreach (var tagSpan in this._aggregator.GetTags(span)) {
        switch (tagSpan.Tag.Kind) {
          case DafnyTokenKind.Comment:
          case DafnyTokenKind.String:
            foreach (var s in tagSpan.Span.GetSpans(pt.Snapshot)) {
              if (s.Contains(span))
                return true;
            }
            break;
          default:
            break;
        }
      }
      return false;
    }
  }


  internal class BraceMatchingTagger : TokenBasedTagger, ITagger<TextMarkerTag>
  {
    ITextView View { get; set; }
    ITextBuffer SourceBuffer { get; set; }
    SnapshotPoint? CurrentChar { get; set; }
    private char[] openBraces;
    private char[] closeBraces;

    static TextMarkerTag Blue = new TextMarkerTag("blue");

    internal BraceMatchingTagger(ITextView view, ITextBuffer sourceBuffer, ITagAggregator<DafnyTokenTag> tagAggregator)
      : base(tagAggregator)
    {
      //here the keys are the open braces, and the values are the close braces
      openBraces =  new char[] { '(', '{', '[' };
      closeBraces = new char[] { ')', '}', ']' };
      this.View = view;
      this.SourceBuffer = sourceBuffer;
      this.CurrentChar = null;

      this.View.Caret.PositionChanged += CaretPositionChanged;
      this.View.LayoutChanged += ViewLayoutChanged;
    }

    public event EventHandler<SnapshotSpanEventArgs> TagsChanged;

    void ViewLayoutChanged(object sender, TextViewLayoutChangedEventArgs e) {
      if (e.NewSnapshot != e.OldSnapshot) //make sure that there has really been a change
      {
        UpdateAtCaretPosition(View.Caret.Position);
      }
    }

    void CaretPositionChanged(object sender, CaretPositionChangedEventArgs e) {
      UpdateAtCaretPosition(e.NewPosition);
    }

    void UpdateAtCaretPosition(CaretPosition caretPosition) {
      CurrentChar = caretPosition.Point.GetPoint(SourceBuffer, caretPosition.Affinity);

      if (!CurrentChar.HasValue)
        return;

      var chngd = TagsChanged;
      if (chngd != null)
        chngd(this, new SnapshotSpanEventArgs(new SnapshotSpan(SourceBuffer.CurrentSnapshot, 0, SourceBuffer.CurrentSnapshot.Length)));
    }

    public IEnumerable<ITagSpan<TextMarkerTag>> GetTags(NormalizedSnapshotSpanCollection spans) {
      if (spans.Count == 0)   //there is no content in the buffer
        yield break;

      //don't do anything if the current SnapshotPoint is not initialized
      if (!CurrentChar.HasValue)
        yield break;

      //hold on to a snapshot of the current character
      SnapshotPoint currentChar = CurrentChar.Value;

      //if the requested snapshot isn't the same as the one the brace is on, translate our spans to the expected snapshot
      if (spans[0].Snapshot != currentChar.Snapshot) {
        currentChar = currentChar.TranslateTo(spans[0].Snapshot, PointTrackingMode.Positive);
      }

      if (currentChar.Position < spans[0].Snapshot.Length) {
        // Check if the current character is an open brace
        char ch = currentChar.GetChar();
        char closeCh;
        if (MatchBrace(currentChar, ch, true, out closeCh)) {
          SnapshotSpan pairSpan;
          if (FindMatchingCloseChar(currentChar, ch, closeCh, View.TextViewLines.Count, out pairSpan)) {
            yield return new TagSpan<TextMarkerTag>(new SnapshotSpan(currentChar, 1), Blue);
            yield return new TagSpan<TextMarkerTag>(pairSpan, Blue);
          }
        }
      }

      if (0 < currentChar.Position) {
        // Check if the previous character is a close brace (note, caret may be between a close brace and an open brace, in which case we'll tag two pairs)
        SnapshotPoint prevChar = currentChar - 1;
        char ch = prevChar.GetChar();
        char openCh;
        if (MatchBrace(prevChar, ch, false, out openCh)) {
          SnapshotSpan pairSpan;
          if (FindMatchingOpenChar(prevChar, openCh, ch, View.TextViewLines.Count, out pairSpan)) {
            yield return new TagSpan<TextMarkerTag>(new SnapshotSpan(prevChar, 1), Blue);
            yield return new TagSpan<TextMarkerTag>(pairSpan, Blue);
          }
        }
      }
    }

    private bool MatchBrace(SnapshotPoint pt, char query, bool sourceIsOpen, out char match) {
      if (!InsideComment(pt)) {
        char[] source = sourceIsOpen ? openBraces : closeBraces;
        int i = 0;
        foreach (char ch in source) {
          if (ch == query) {
            char[] dest = sourceIsOpen ? closeBraces : openBraces;
            match = dest[i];
            return true;
          }
          i++;
        }
      }
      match = query;  // satisfy compiler
      return false;
    }

    private bool FindMatchingCloseChar(SnapshotPoint startPoint, char open, char close, int linesViewed, out SnapshotSpan pairSpan) {
      ITextSnapshotLine line = startPoint.GetContainingLine();
      int lineNumber = line.LineNumber;
      int offset = startPoint.Position - line.Start.Position + 1;

      int lineNumberLimit = Math.Min(startPoint.Snapshot.LineCount, lineNumber + linesViewed);

      int openCount = 0;
      while (true) {
        string lineText = line.GetText();

        //walk the entire line
        for (; offset < line.Length; offset++) {
          char currentChar = lineText[offset];
          if (currentChar == open || currentChar == close) {
            if (!InsideComment(new SnapshotPoint(line.Snapshot, line.Start.Position + offset))) {
              if (currentChar == open) {
                openCount++;
              } else if (0 < openCount) {
                openCount--;
              } else {
                //found the matching close
                pairSpan = new SnapshotSpan(startPoint.Snapshot, line.Start + offset, 1);
                return true;
              }
            }
          }
        }

        //move on to the next line
        lineNumber++;
        if (lineNumberLimit <= lineNumber)
          break;

        line = line.Snapshot.GetLineFromLineNumber(lineNumber);
        offset = 0;
      }

      pairSpan = new SnapshotSpan(startPoint, startPoint);  // satisfy the compiler
      return false;
    }

    private bool FindMatchingOpenChar(SnapshotPoint startPoint, char open, char close, int linesViewed, out SnapshotSpan pairSpan) {
      ITextSnapshotLine line = startPoint.GetContainingLine();
      int lineNumber = line.LineNumber;
      int offset = startPoint.Position - line.Start.Position - 1; //move the offset to the character before this one

      int lineNumberLimit = Math.Max(0, lineNumber - linesViewed);

      int closeCount = 0;
      while (true) {
        string lineText = line.GetText();

        //walk the entire line
        for (; 0 <= offset; offset--) {
          char currentChar = lineText[offset];
          if (currentChar == open || currentChar == close) {
            if (!InsideComment(new SnapshotPoint(line.Snapshot, line.Start.Position + offset))) {
              if (currentChar == close) {
                closeCount++;
              } else if (0 < closeCount) {
                closeCount--;
              } else {
                // We've found the open character
                pairSpan = new SnapshotSpan(line.Start + offset, 1); //we just want the character itself
                return true;
              }
            }
          }
        }

        // Move to the previous line
        lineNumber--;
        if (lineNumber < lineNumberLimit)
          break;

        line = line.Snapshot.GetLineFromLineNumber(lineNumber);
        offset = line.Length - 1;
      }

      pairSpan = new SnapshotSpan(startPoint, startPoint);  // satisfy the compiler
      return false;
    }
  }

  #endregion

}