-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathJavaQuickInfoSource.cs
More file actions
405 lines (345 loc) · 17.2 KB
/
Copy pathJavaQuickInfoSource.cs
File metadata and controls
405 lines (345 loc) · 17.2 KB
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
397
398
399
400
401
402
403
404
405
namespace Tvl.VisualStudio.Language.Java
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Antlr.Runtime;
using Antlr.Runtime.Tree;
using Microsoft.VisualStudio.Text;
using Microsoft.VisualStudio.Text.Tagging;
using Tvl.VisualStudio.Language.Java.Experimental;
using Tvl.VisualStudio.Language.Parsing.Experimental.Atn;
using CancellationToken = System.Threading.CancellationToken;
using Contract = System.Diagnostics.Contracts.Contract;
using IOutputWindowPane = Tvl.VisualStudio.OutputWindow.Interfaces.IOutputWindowPane;
using IQuickInfoSession = Microsoft.VisualStudio.Language.Intellisense.IQuickInfoSession;
using IQuickInfoSource = Microsoft.VisualStudio.Language.Intellisense.IQuickInfoSource;
using ITextBuffer = Microsoft.VisualStudio.Text.ITextBuffer;
using ITextSnapshot = Microsoft.VisualStudio.Text.ITextSnapshot;
using ITrackingSpan = Microsoft.VisualStudio.Text.ITrackingSpan;
using IWpfTextView = Microsoft.VisualStudio.Text.Editor.IWpfTextView;
using JavaAtnBuilder = Tvl.VisualStudio.Language.Java.Experimental.JavaAtnBuilder;
using JavaSimplifiedAtnBuilder = Tvl.VisualStudio.Language.Java.Experimental.JavaSimplifiedAtnBuilder;
using NetworkInterpreter = Tvl.VisualStudio.Language.Parsing.Experimental.Interpreter.NetworkInterpreter;
using PredefinedOutputWindowPanes = Tvl.VisualStudio.OutputWindow.Interfaces.PredefinedOutputWindowPanes;
using SnapshotCharStream = Tvl.VisualStudio.Language.Parsing.SnapshotCharStream;
using Stopwatch = System.Diagnostics.Stopwatch;
internal class JavaQuickInfoSource : IQuickInfoSource
{
private readonly ITextBuffer _textBuffer;
private readonly JavaQuickInfoSourceProvider _provider;
private readonly object _contentUpdateLock = new object();
private SnapshotPoint? _triggerPoint;
private ITrackingSpan _applicableToSpan;
private IList<object> _quickInfoContent = new object[0];
public JavaQuickInfoSource(ITextBuffer textBuffer, JavaQuickInfoSourceProvider provider)
{
Contract.Requires<ArgumentNullException>(textBuffer != null, "textBuffer");
Contract.Requires<ArgumentNullException>(provider != null, "provider");
_textBuffer = textBuffer;
_provider = provider;
}
public ITextBuffer TextBuffer
{
get
{
return _textBuffer;
}
}
public JavaQuickInfoSourceProvider Provider
{
get
{
return _provider;
}
}
public void AugmentQuickInfoSession(IQuickInfoSession session, IList<object> quickInfoContent, out ITrackingSpan applicableToSpan)
{
applicableToSpan = null;
if (session == null || quickInfoContent == null)
return;
Provider.IntelliSenseCache.BeginReferenceSourceParsing();
if (session.TextView.TextBuffer == this.TextBuffer)
{
ITextSnapshot currentSnapshot = this.TextBuffer.CurrentSnapshot;
SnapshotPoint? triggerPoint = session.GetTriggerPoint(currentSnapshot);
if (!triggerPoint.HasValue)
return;
lock (_contentUpdateLock)
{
if (triggerPoint == this._triggerPoint)
{
foreach (var content in _quickInfoContent)
quickInfoContent.Add(content);
applicableToSpan = _applicableToSpan;
return;
}
}
BeginUpdateQuickInfoContent(session, triggerPoint.Value);
}
}
private void BeginUpdateQuickInfoContent(IQuickInfoSession session, SnapshotPoint triggerPoint)
{
Contract.Requires(session != null);
Action updateAction = () => UpdateQuickInfoContent(session, triggerPoint);
Task.Factory.StartNew(updateAction, CancellationToken.None, TaskCreationOptions.None, Provider.PriorityIntelliSenseTaskScheduler).HandleNonCriticalExceptions();
}
private void UpdateQuickInfoContent(IQuickInfoSession session, SnapshotPoint triggerPoint)
{
/* use the experimental model to locate and process the expression */
Stopwatch stopwatch = Stopwatch.StartNew();
// lex the entire document
var currentSnapshot = triggerPoint.Snapshot;
var input = new SnapshotCharStream(currentSnapshot, new Span(0, currentSnapshot.Length));
var unicodeInput = new JavaUnicodeStream(input);
var lexer = new Java2Lexer(unicodeInput);
var tokens = new CommonTokenStream(lexer);
tokens.Fill();
// locate the last token before the trigger point
while (true)
{
IToken nextToken = tokens.LT(1);
if (nextToken.Type == CharStreamConstants.EndOfFile)
break;
if (nextToken.StartIndex > triggerPoint.Position)
break;
tokens.Consume();
}
IToken triggerToken = tokens.LT(-1);
if (triggerToken == null)
return;
switch (triggerToken.Type)
{
// symbol references
case Java2Lexer.IDENTIFIER:
case Java2Lexer.THIS:
case Java2Lexer.SUPER:
// primitive types
case Java2Lexer.BOOLEAN:
case Java2Lexer.CHAR:
case Java2Lexer.BYTE:
case Java2Lexer.SHORT:
case Java2Lexer.INT:
case Java2Lexer.LONG:
case Java2Lexer.FLOAT:
case Java2Lexer.DOUBLE:
// literals
case Java2Lexer.INTLITERAL:
case Java2Lexer.LONGLITERAL:
case Java2Lexer.FLOATLITERAL:
case Java2Lexer.DOUBLELITERAL:
case Java2Lexer.CHARLITERAL:
case Java2Lexer.STRINGLITERAL:
case Java2Lexer.TRUE:
case Java2Lexer.FALSE:
case Java2Lexer.NULL:
break;
default:
return;
}
NetworkInterpreter interpreter = CreateNetworkInterpreter(tokens);
while (interpreter.TryStepBackward())
{
if (interpreter.Contexts.Count == 0 || interpreter.Contexts.Count > 400)
break;
if (interpreter.Contexts.All(context => context.BoundedStart))
break;
}
interpreter.Contexts.RemoveAll(i => !i.BoundedStart);
interpreter.CombineBoundedStartContexts();
IOutputWindowPane pane = Provider.OutputWindowService.TryGetPane(PredefinedOutputWindowPanes.TvlIntellisense);
if (pane != null)
{
pane.WriteLine(string.Format("Located {0} QuickInfo expression(s) in {1}ms.", interpreter.Contexts.Count, stopwatch.ElapsedMilliseconds));
}
HashSet<string> intermediateResult = new HashSet<string>();
HashSet<string> finalResult = new HashSet<string>();
List<object> quickInfoContent = new List<object>();
foreach (var context in interpreter.Contexts)
{
Span? span = null;
foreach (var transition in context.Transitions)
{
if (!transition.Transition.IsMatch)
continue;
IToken token = transition.Token;
Span tokenSpan = new Span(token.StartIndex, token.StopIndex - token.StartIndex + 1);
if (span == null)
span = tokenSpan;
else
span = Span.FromBounds(Math.Min(span.Value.Start, tokenSpan.Start), Math.Max(span.Value.End, tokenSpan.End));
}
if (span.HasValue && !span.Value.IsEmpty)
{
string text = currentSnapshot.GetText(span.Value);
if (!intermediateResult.Add(text))
continue;
AstParserRuleReturnScope<CommonTree, CommonToken> result = null;
try
{
var expressionInput = new ANTLRStringStream(text);
var expressionUnicodeInput = new JavaUnicodeStream(expressionInput);
var expressionLexer = new Java2Lexer(expressionUnicodeInput);
var expressionTokens = new CommonTokenStream(expressionLexer);
var expressionParser = new Java2Parser(expressionTokens);
result = expressionParser.primary();
// anchors experiment
Contract.Assert(TextBuffer.CurrentSnapshot == triggerPoint.Snapshot);
ClassAnchorTracker tracker = new ClassAnchorTracker(TextBuffer, null);
SnapshotSpan trackedSpan = new SnapshotSpan(triggerPoint.Snapshot, 0, triggerPoint.Position);
ITagSpan<ScopeAnchorTag>[] tags = tracker.GetTags(new NormalizedSnapshotSpanCollection(trackedSpan)).ToArray();
text = result.Tree.ToStringTree();
}
catch (RecognitionException)
{
text = "Could not parse: " + text;
}
text = text.Replace("\n", "\\n").Replace("\r", "\\r");
finalResult.Add(text);
//if (Regex.IsMatch(text, @"^[A-Za-z_]+(?:\.\w+)*$"))
//{
// NameResolutionContext resolutionContext = NameResolutionContext.Global(Provider.IntelliSenseCache);
// resolutionContext = resolutionContext.Filter(text, null, true);
// CodeElement[] matching = resolutionContext.GetMatchingElements();
// if (matching.Length > 0)
// {
// foreach (var element in matching)
// {
// element.AugmentQuickInfoSession(quickInfoContent);
// }
// }
// else
// {
// // check if this is a package
// CodePhysicalFile[] files = Provider.IntelliSenseCache.GetPackageFiles(text, true);
// if (files.Length > 0)
// {
// finalResult.Add(string.Format("package {0}", text));
// }
// else
// {
// // check if this is a type
// string typeName = text.Substring(text.LastIndexOf('.') + 1);
// CodeType[] types = Provider.IntelliSenseCache.GetTypes(typeName, true);
// foreach (var type in types)
// {
// if (type.FullName == text)
// finalResult.Add(string.Format("{0}: {1}", type.GetType().Name, type.FullName));
// }
// }
// }
//}
//else
//{
// finalResult.Add(text);
//}
}
}
ITrackingSpan applicableToSpan = null;
foreach (var result in finalResult)
{
quickInfoContent.Add(result);
}
applicableToSpan = currentSnapshot.CreateTrackingSpan(new Span(triggerToken.StartIndex, triggerToken.StopIndex - triggerToken.StartIndex + 1), SpanTrackingMode.EdgeExclusive);
//try
//{
// Expression currentExpression = Provider.IntellisenseCache.ParseExpression(selection);
// if (currentExpression != null)
// {
// SnapshotSpan? span = currentExpression.Span;
// if (span.HasValue)
// applicableToSpan = span.Value.Snapshot.CreateTrackingSpan(span.Value, SpanTrackingMode.EdgeExclusive);
// quickInfoContent.Add(currentExpression.ToString());
// }
// else
// {
// quickInfoContent.Add("Could not parse expression.");
// }
//}
//catch (Exception ex)
//{
// if (ErrorHandler.IsCriticalException(ex))
// throw;
// quickInfoContent.Add(ex.Message);
//}
lock (_contentUpdateLock)
{
_triggerPoint = triggerPoint;
_applicableToSpan = applicableToSpan;
_quickInfoContent = quickInfoContent;
}
IWpfTextView wpfTextView = session.TextView as IWpfTextView;
if (wpfTextView != null && wpfTextView.VisualElement != null)
{
ITrackingPoint trackingTriggerPoint = triggerPoint.Snapshot.CreateTrackingPoint(triggerPoint.Position, PointTrackingMode.Negative);
wpfTextView.VisualElement.Dispatcher.BeginInvoke((Action<IQuickInfoSession, ITrackingPoint, bool>)RetriggerQuickInfo, session, trackingTriggerPoint, true);
}
}
private void RetriggerQuickInfo(IQuickInfoSession session, ITrackingPoint triggerPoint, bool trackMouse)
{
Provider.QuickInfoBroker.TriggerQuickInfo(session.TextView, triggerPoint, true);
}
private NetworkInterpreter CreateNetworkInterpreter(CommonTokenStream tokens)
{
Network network = NetworkBuilder<JavaSimplifiedAtnBuilder>.GetOrBuildNetwork();
NetworkInterpreter interpreter = new NetworkInterpreter(network, tokens);
IToken previousToken = tokens.LT(-1);
if (previousToken == null)
return new NetworkInterpreter(network, new CommonTokenStream());
switch (previousToken.Type)
{
case Java2Lexer.IDENTIFIER:
// definitions always appear as a single identifier (at least the part of them we care about for Quick Info)
interpreter.BoundaryRules.Add(network.GetRule(JavaAtnBuilder.RuleNames.SymbolDefinitionIdentifier));
interpreter.BoundaryRules.Add(network.GetRule(JavaAtnBuilder.RuleNames.ClassOrInterfaceType));
interpreter.BoundaryRules.Add(network.GetRule(JavaAtnBuilder.RuleNames.QualifiedName));
interpreter.BoundaryRules.Add(network.GetRule(JavaAtnBuilder.RuleNames.ElementValuePair));
interpreter.BoundaryRules.Add(network.GetRule(JavaAtnBuilder.RuleNames.Statement));
interpreter.BoundaryRules.Add(network.GetRule(JavaAtnBuilder.RuleNames.Primary));
interpreter.BoundaryRules.Add(network.GetRule(JavaAtnBuilder.RuleNames.InnerCreator));
break;
case Java2Lexer.SUPER:
interpreter.BoundaryRules.Add(network.GetRule(JavaAtnBuilder.RuleNames.Primary));
interpreter.BoundaryRules.Add(network.GetRule(JavaAtnBuilder.RuleNames.TypeArgument));
interpreter.BoundaryRules.Add(network.GetRule(JavaAtnBuilder.RuleNames.ExplicitConstructorInvocation));
break;
case Java2Lexer.THIS:
interpreter.BoundaryRules.Add(network.GetRule(JavaAtnBuilder.RuleNames.Primary));
interpreter.BoundaryRules.Add(network.GetRule(JavaAtnBuilder.RuleNames.ExplicitConstructorInvocation));
break;
case Java2Lexer.CLASS:
interpreter.BoundaryRules.Add(network.GetRule(JavaAtnBuilder.RuleNames.Primary));
interpreter.ExcludedStartRules.Add(network.GetRule(JavaAtnBuilder.RuleNames.ClassHeader));
break;
case Java2Lexer.BOOLEAN:
case Java2Lexer.CHAR:
case Java2Lexer.BYTE:
case Java2Lexer.SHORT:
case Java2Lexer.INT:
case Java2Lexer.LONG:
case Java2Lexer.FLOAT:
case Java2Lexer.DOUBLE:
interpreter.BoundaryRules.Add(network.GetRule(JavaAtnBuilder.RuleNames.PrimitiveType));
break;
case Java2Lexer.INTLITERAL:
case Java2Lexer.LONGLITERAL:
case Java2Lexer.FLOATLITERAL:
case Java2Lexer.DOUBLELITERAL:
case Java2Lexer.CHARLITERAL:
case Java2Lexer.STRINGLITERAL:
case Java2Lexer.TRUE:
case Java2Lexer.FALSE:
case Java2Lexer.NULL:
interpreter.BoundaryRules.Add(network.GetRule(JavaAtnBuilder.RuleNames.Literal));
break;
default:
return new NetworkInterpreter(network, new CommonTokenStream());
}
return interpreter;
}
public void Dispose()
{
}
}
}