-
Notifications
You must be signed in to change notification settings - Fork 18
/
CSharpCommentTextTagger.cs
396 lines (344 loc) · 13.4 KB
/
CSharpCommentTextTagger.cs
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using Microsoft.VisualStudio.Text;
using Microsoft.VisualStudio.Text.Tagging;
namespace Microsoft.VisualStudio.Language.Spellchecker
{
/// <summary>
/// Due to issues with the built-in C# classifier, we write our own NaturalTextTagger that looks for
/// comments (single, multi-line, and doc comment) and strings (single and multi-line) and tags them
/// with NaturalTextTag.
/// </summary>
internal class CSharpCommentTextTagger : ITagger<NaturalTextTag>, IDisposable
{
ITextBuffer _buffer;
ITextSnapshot _lineCacheSnapshot = null;
List<State> _lineCache;
public CSharpCommentTextTagger(ITextBuffer buffer)
{
_buffer = buffer;
// Populate our cache initially.
ITextSnapshot snapshot = _buffer.CurrentSnapshot;
int lines = snapshot.LineCount;
_lineCache = new List<State>(snapshot.LineCount);
_lineCache.AddRange(Enumerable.Repeat(State.Default, snapshot.LineCount));
RescanLines(snapshot, startLine: 0, lastDirtyLine: snapshot.LineCount - 1);
_lineCacheSnapshot = snapshot;
// Listen for text changes so we can stay up-to-date.
_buffer.Changed += OnTextBufferChanged;
}
public void Dispose()
{
_buffer.Changed -= OnTextBufferChanged;
}
public event EventHandler<SnapshotSpanEventArgs> TagsChanged;
public IEnumerable<ITagSpan<NaturalTextTag>> GetTags(NormalizedSnapshotSpanCollection spans)
{
foreach (SnapshotSpan span in spans)
{
// If we're called on the non-current snapshot, return nothing.
if (span.Snapshot != _lineCacheSnapshot)
yield break;
SnapshotPoint lineStart = span.Start;
while (lineStart < span.End)
{
ITextSnapshotLine line = lineStart.GetContainingLine();
State state = _lineCache[line.LineNumber];
List<SnapshotSpan> naturalTextSpans = new List<SnapshotSpan>();
state = ScanLine(state, line, naturalTextSpans);
foreach (SnapshotSpan naturalTextSpan in naturalTextSpans)
{
if (naturalTextSpan.IntersectsWith(span))
yield return new TagSpan<NaturalTextTag>(naturalTextSpan, new NaturalTextTag());
}
// Advance to next line.
lineStart = line.EndIncludingLineBreak;
}
}
}
private void OnTextBufferChanged(object sender, TextContentChangedEventArgs e)
{
ITextSnapshot snapshot = e.After;
// First update _lineCache so its size matches snapshot.LineCount.
foreach (ITextChange change in e.Changes)
{
if (change.LineCountDelta > 0)
{
int line = snapshot.GetLineFromPosition(change.NewPosition).LineNumber;
_lineCache.InsertRange(line, Enumerable.Repeat(State.Default, change.LineCountDelta));
}
else if (change.LineCountDelta < 0)
{
int line = snapshot.GetLineFromPosition(change.NewPosition).LineNumber;
_lineCache.RemoveRange(line, -change.LineCountDelta);
}
}
// Now that _lineCache is the appropriate size we can safely start rescanning.
// If we hadn't updated _lineCache, then rescanning could walk off the edge.
List<SnapshotSpan> changedSpans = new List<SnapshotSpan>();
foreach (ITextChange change in e.Changes)
{
ITextSnapshotLine startLine = snapshot.GetLineFromPosition(change.NewPosition);
ITextSnapshotLine endLine = snapshot.GetLineFromPosition(change.NewPosition);
int lastUpdatedLine = RescanLines(snapshot, startLine.LineNumber, endLine.LineNumber);
changedSpans.Add(new SnapshotSpan(
startLine.Start,
snapshot.GetLineFromLineNumber(lastUpdatedLine).End));
}
_lineCacheSnapshot = snapshot;
var tagsChanged = TagsChanged;
if (tagsChanged != null)
{
foreach (SnapshotSpan span in changedSpans)
{
tagsChanged(this, new SnapshotSpanEventArgs(span));
}
}
}
// Returns last line updated (will be greater than or equal to lastDirtyLine).
private int RescanLines(ITextSnapshot snapshot, int startLine, int lastDirtyLine)
{
int currentLine = startLine;
State state = _lineCache[currentLine];
bool updatedStateForCurrentLine = true;
// Go until we have covered all of the dirty lines and we get to a line where our
// new state matches the old state.
while (currentLine < lastDirtyLine || (updatedStateForCurrentLine && currentLine < snapshot.LineCount))
{
ITextSnapshotLine line = snapshot.GetLineFromLineNumber(currentLine);
state = ScanLine(state, line);
// Advance to next line.
currentLine++;
if (currentLine < snapshot.LineCount)
{
updatedStateForCurrentLine = (state != _lineCache[currentLine]);
_lineCache[currentLine] = state;
}
}
return currentLine - 1; // last line updated.
}
private State ScanLine(State state, ITextSnapshotLine line, List<SnapshotSpan> naturalTextSpans = null)
{
LineProgress p = new LineProgress(line, state, naturalTextSpans);
while (!p.EndOfLine)
{
if (p.State == State.Default)
ScanDefault(p);
else if (p.State == State.MultiLineComment)
ScanMultiLineComment(p);
else if (p.State == State.MultiLineString)
ScanMultiLineString(p);
else
Debug.Fail("Invalid state at beginning of line.");
}
// End Of Line state must be one of these.
Debug.Assert(p.State == State.Default || p.State == State.MultiLineString || p.State == State.MultiLineComment);
return p.State;
}
private void ScanDefault(LineProgress p)
{
while (!p.EndOfLine)
{
if (p.Char() == '/' && p.NextChar() == '/' && p.NextNextChar() == '/') // doc comment.
{
p.Advance(3);
p.State = State.DocComment;
ScanDocComment(p);
}
else if (p.Char() == '/' && p.NextChar() == '/') // single line comment
{
p.Advance(2);
p.StartNaturalText();
p.AdvanceToEndOfLine();
p.EndNaturalText();
p.State = State.Default;
return;
}
else if (p.Char() == '/' && p.NextChar() == '*') // multi-line comment
{
p.Advance(2);
p.State = State.MultiLineComment;
ScanMultiLineComment(p);
}
else if (p.Char() == '@' && p.NextChar() == '"') // multi-line string
{
p.Advance(2);
p.State = State.MultiLineString;
ScanMultiLineString(p);
}
else if (p.Char() == '"') // single-line string
{
p.Advance(1);
p.State = State.String;
ScanString(p);
}
else if (p.Char() == '\'') // character literal
{
p.Advance(1);
p.State = State.Character;
ScanCharacter(p);
}
else
{
p.Advance();
}
}
}
private void ScanDocComment(LineProgress p)
{
p.StartNaturalText();
while (!p.EndOfLine)
{
if (p.Char() == '<')
{
p.EndNaturalText();
p.Advance();
p.State = State.DocCommentXml;
ScanDocCommentXml(p);
p.StartNaturalText();
}
else
{
p.Advance();
}
}
// End of line. Record what we have and revert to default state.
p.EndNaturalText();
p.State = State.Default;
}
private void ScanDocCommentXml(LineProgress p)
{
while (!p.EndOfLine)
{
if (p.Char() == '"')
{
p.Advance(1);
p.State = State.DocCommentXmlString;
ScanDocCommentXmlString(p);
}
else if (p.Char() == '>')
{
p.Advance();
p.State = State.DocComment;
return; // Done with xml tag in doc comment.
}
else
{
p.Advance();
}
}
// End of line. Never found the '>' for the tag, but whatever. We revert to default state.
p.State = State.Default;
}
private void ScanDocCommentXmlString(LineProgress p)
{
while (!p.EndOfLine)
{
if (p.Char() == '"')
{
p.Advance(1);
p.State = State.DocCommentXml;
return; // Done with string in doc comment xml.
}
else
{
p.Advance();
}
}
// End of line. Never found the '"' to close the string, but whatever. We revert to default state.
p.State = State.Default;
}
private void ScanMultiLineComment(LineProgress p)
{
p.StartNaturalText();
while (!p.EndOfLine)
{
if (p.Char() == '*' && p.NextChar() == '/') // close comment
{
p.EndNaturalText();
p.Advance(2);
p.State = State.Default;
return; // done with multi-line comment.
}
else
{
p.Advance();
}
}
// End of line. Emit as human readable, but remain in MultiLineComment state.
p.EndNaturalText();
Debug.Assert(p.State == State.MultiLineComment);
}
private void ScanMultiLineString(LineProgress p)
{
p.StartNaturalText();
while (!p.EndOfLine)
{
if (p.Char() == '"' && p.NextChar() == '"') // "" is allowed within multiline string.
{
p.Advance(2);
}
else if (p.Char() == '"') // end of multi-line string
{
p.EndNaturalText();
p.Advance();
p.State = State.Default;
return;
}
else
{
p.Advance();
}
}
// End of line. Emit as human readable, but remain in MultiLineString state.
p.EndNaturalText();
Debug.Assert(p.State == State.MultiLineString);
}
private void ScanString(LineProgress p)
{
p.StartNaturalText();
while (!p.EndOfLine)
{
if (p.Char() == '\\') // escaped character. Skip over it.
{
p.Advance(2);
}
else if (p.Char() == '"') // end of string.
{
p.EndNaturalText();
p.Advance();
p.State = State.Default;
return;
}
else
{
p.Advance();
}
}
// End of line. String wasn't closed. Oh well. Revert to Default state.
p.EndNaturalText();
p.State = State.Default;
}
private void ScanCharacter(LineProgress p)
{
if (!p.EndOfLine && p.Char() == '\\') // escaped character. Eat it.
{
p.Advance(2);
}
else if (!p.EndOfLine && p.Char() != '\'') // non-escaped character. Eat it.
{
p.Advance(1);
}
if (!p.EndOfLine && p.Char() == '\'') // closing ' for character, as expected.
{
p.Advance(1);
p.State = State.Default;
return;
}
// Didn't find closing ' for character. Oh well.
p.State = State.Default;
}
}
}