protected override void ReParseImpl() { var outputWindow = OutputWindowService.TryGetPane(PredefinedOutputWindowPanes.TvlIntellisense); try { Stopwatch stopwatch = Stopwatch.StartNew(); var snapshot = TextBuffer.CurrentSnapshot; SnapshotCharStream input = new SnapshotCharStream(snapshot, new Span(0, snapshot.Length)); GoLexer lexer = new GoLexer(input); GoSemicolonInsertionTokenSource tokenSource = new GoSemicolonInsertionTokenSource(lexer); CommonTokenStream tokens = new CommonTokenStream(tokenSource); GoParser parser = new GoParser(tokens); List<ParseErrorEventArgs> errors = new List<ParseErrorEventArgs>(); parser.ParseError += (sender, e) => { errors.Add(e); string message = e.Message; ITextDocument document; if (TextBuffer.Properties.TryGetProperty(typeof(ITextDocument), out document) && document != null) { string fileName = document.FilePath; var line = snapshot.GetLineFromPosition(e.Span.Start); message = string.Format("{0}({1},{2}): {3}", fileName, line.LineNumber + 1, e.Span.Start - line.Start.Position + 1, message); } if (message.Length > 100) message = message.Substring(0, 100) + " ..."; if (outputWindow != null) outputWindow.WriteLine(message); if (errors.Count > 100) throw new OperationCanceledException(); }; var result = parser.compilationUnit(); OnParseComplete(new AntlrParseResultEventArgs(snapshot, errors, stopwatch.Elapsed, tokens.GetTokens(), result)); } catch (Exception e) { if (ErrorHandler.IsCriticalException(e)) throw; try { if (outputWindow != null) outputWindow.WriteLine(e.Message); } catch (Exception ex2) { if (ErrorHandler.IsCriticalException(ex2)) throw; } } }
protected override void ReParseImpl() { var outputWindow = OutputWindowService.TryGetPane(PredefinedOutputWindowPanes.TvlIntellisense); try { Stopwatch stopwatch = Stopwatch.StartNew(); var snapshot = TextBuffer.CurrentSnapshot; SnapshotCharStream input = new SnapshotCharStream(snapshot, new Span(0, snapshot.Length)); GoLexer lexer = new GoLexer(input); GoSemicolonInsertionTokenSource tokenSource = new GoSemicolonInsertionTokenSource(lexer); CommonTokenStream tokens = new CommonTokenStream(tokenSource); GoParser parser = new GoParser(tokens); List <ParseErrorEventArgs> errors = new List <ParseErrorEventArgs>(); parser.ParseError += (sender, e) => { errors.Add(e); string message = e.Message; ITextDocument document; if (TextBuffer.Properties.TryGetProperty(typeof(ITextDocument), out document) && document != null) { string fileName = document.FilePath; var line = snapshot.GetLineFromPosition(e.Span.Start); message = string.Format("{0}({1},{2}): {3}", fileName, line.LineNumber + 1, e.Span.Start - line.Start.Position + 1, message); } if (message.Length > 100) { message = message.Substring(0, 100) + " ..."; } if (outputWindow != null) { outputWindow.WriteLine(message); } if (errors.Count > 100) { throw new OperationCanceledException(); } }; var result = parser.compilationUnit(); OnParseComplete(new AntlrParseResultEventArgs(snapshot, errors, stopwatch.Elapsed, tokens.GetTokens(), result)); } catch (Exception e) { if (ErrorHandler.IsCriticalException(e)) { throw; } try { if (outputWindow != null) { outputWindow.WriteLine(e.Message); } } catch (Exception ex2) { if (ErrorHandler.IsCriticalException(ex2)) { throw; } } } }
public void AugmentQuickInfoSession(IQuickInfoSession session, IList<object> quickInfoContent, out ITrackingSpan applicableToSpan) { applicableToSpan = null; if (session == null || quickInfoContent == null) return; if (session.TextView.TextBuffer == this.TextBuffer) { ITextSnapshot currentSnapshot = this.TextBuffer.CurrentSnapshot; SnapshotPoint? triggerPoint = session.GetTriggerPoint(currentSnapshot); if (!triggerPoint.HasValue) return; #region experimental /* use the experimental model to locate and process the expression */ Stopwatch stopwatch = Stopwatch.StartNew(); // lex the entire document var input = new SnapshotCharStream(currentSnapshot, new Span(0, currentSnapshot.Length)); var lexer = new GoLexer(input); var tokenSource = new GoSemicolonInsertionTokenSource(lexer); var tokens = new CommonTokenStream(tokenSource); 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.Value.Position) break; tokens.Consume(); } switch (tokens.LA(-1)) { case GoLexer.IDENTIFIER: //case GoLexer.KW_THIS: //case GoLexer.KW_UNIV: //case GoLexer.KW_IDEN: //case GoLexer.KW_INT2: //case GoLexer.KW_SEQINT: break; default: return; } Network network = NetworkBuilder<GoSimplifiedAtnBuilder>.GetOrBuildNetwork(); RuleBinding memberSelectRule = network.GetRule(GoSimplifiedAtnBuilder.RuleNames.PrimaryExpr); #if false HashSet<Transition> memberSelectTransitions = new HashSet<Transition>(); GetReachableTransitions(memberSelectRule, memberSelectTransitions); #endif NetworkInterpreter interpreter = new NetworkInterpreter(network, tokens); interpreter.BoundaryRules.Add(memberSelectRule); interpreter.BoundaryRules.Add(network.GetRule(GoSimplifiedAtnBuilder.RuleNames.Label)); interpreter.BoundaryRules.Add(network.GetRule(GoSimplifiedAtnBuilder.RuleNames.TypeSwitchGuard)); interpreter.BoundaryRules.Add(network.GetRule(GoSimplifiedAtnBuilder.RuleNames.FieldName)); interpreter.BoundaryRules.Add(network.GetRule(GoSimplifiedAtnBuilder.RuleNames.Receiver)); interpreter.BoundaryRules.Add(network.GetRule(GoSimplifiedAtnBuilder.RuleNames.FunctionDecl)); interpreter.BoundaryRules.Add(network.GetRule(GoSimplifiedAtnBuilder.RuleNames.BaseTypeName)); interpreter.BoundaryRules.Add(network.GetRule(GoSimplifiedAtnBuilder.RuleNames.TypeSpec)); interpreter.BoundaryRules.Add(network.GetRule(GoSimplifiedAtnBuilder.RuleNames.IdentifierList)); interpreter.BoundaryRules.Add(network.GetRule(GoSimplifiedAtnBuilder.RuleNames.MethodName)); interpreter.BoundaryRules.Add(network.GetRule(GoSimplifiedAtnBuilder.RuleNames.ParameterDecl)); interpreter.BoundaryRules.Add(network.GetRule(GoSimplifiedAtnBuilder.RuleNames.FieldIdentifierList)); interpreter.BoundaryRules.Add(network.GetRule(GoSimplifiedAtnBuilder.RuleNames.PackageName)); interpreter.BoundaryRules.Add(network.GetRule(GoSimplifiedAtnBuilder.RuleNames.TypeName)); interpreter.ExcludedStartRules.Add(network.GetRule(GoSimplifiedAtnBuilder.RuleNames.Block)); while (interpreter.TryStepBackward()) { if (interpreter.Contexts.Count == 0) break; /* we want all traces to start outside the binOpExpr18 rule, which means all * traces with a transition reachable from binOpExpr18 should contain a push * transition with binOpExpr18's start state as its target. */ if (interpreter.Contexts.All(context => context.BoundedStart)) { break; } } 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> finalResult = new HashSet<string>(); SnapshotSpan? contextSpan = null; foreach (var context in interpreter.Contexts) { Span? span = null; //List<string> results = AnalyzeInterpreterTrace(context, memberSelectRule, out span); 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) contextSpan = new SnapshotSpan(currentSnapshot, span.Value); //if (results.Count > 0) //{ // finalResult.UnionWith(results); // applicableToSpan = currentSnapshot.CreateTrackingSpan(span, SpanTrackingMode.EdgeExclusive); //} } foreach (var result in finalResult) { quickInfoContent.Add(result); } #endregion #if false var selection = session.TextView.Selection.StreamSelectionSpan; if (selection.IsEmpty || !selection.Contains(new VirtualSnapshotPoint(triggerPoint.Value))) { SnapshotSpan? expressionSpan = Provider.IntellisenseCache.GetExpressionSpan(triggerPoint.Value); if (expressionSpan.HasValue) selection = new VirtualSnapshotSpan(expressionSpan.Value); } #endif VirtualSnapshotSpan selection = new VirtualSnapshotSpan(); if (contextSpan.HasValue) selection = new VirtualSnapshotSpan(contextSpan.Value); if (!selection.IsEmpty && selection.Contains(new VirtualSnapshotPoint(triggerPoint.Value))) { applicableToSpan = selection.Snapshot.CreateTrackingSpan(selection.SnapshotSpan, SpanTrackingMode.EdgeExclusive); quickInfoContent.Add(selection.GetText()); //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); //} } } }
public void AugmentQuickInfoSession(IQuickInfoSession session, IList <object> quickInfoContent, out ITrackingSpan applicableToSpan) { applicableToSpan = null; if (session == null || quickInfoContent == null) { return; } if (session.TextView.TextBuffer == this.TextBuffer) { ITextSnapshot currentSnapshot = this.TextBuffer.CurrentSnapshot; SnapshotPoint?triggerPoint = session.GetTriggerPoint(currentSnapshot); if (!triggerPoint.HasValue) { return; } #region experimental /* use the experimental model to locate and process the expression */ Stopwatch stopwatch = Stopwatch.StartNew(); // lex the entire document var input = new SnapshotCharStream(currentSnapshot, new Span(0, currentSnapshot.Length)); var lexer = new GoLexer(input); var tokenSource = new GoSemicolonInsertionTokenSource(lexer); var tokens = new CommonTokenStream(tokenSource); 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.Value.Position) { break; } tokens.Consume(); } switch (tokens.LA(-1)) { case GoLexer.IDENTIFIER: //case GoLexer.KW_THIS: //case GoLexer.KW_UNIV: //case GoLexer.KW_IDEN: //case GoLexer.KW_INT2: //case GoLexer.KW_SEQINT: break; default: return; } Network network = NetworkBuilder <GoSimplifiedAtnBuilder> .GetOrBuildNetwork(); RuleBinding memberSelectRule = network.GetRule(GoSimplifiedAtnBuilder.RuleNames.PrimaryExpr); #if false HashSet <Transition> memberSelectTransitions = new HashSet <Transition>(); GetReachableTransitions(memberSelectRule, memberSelectTransitions); #endif NetworkInterpreter interpreter = new NetworkInterpreter(network, tokens); interpreter.BoundaryRules.Add(memberSelectRule); interpreter.BoundaryRules.Add(network.GetRule(GoSimplifiedAtnBuilder.RuleNames.Label)); interpreter.BoundaryRules.Add(network.GetRule(GoSimplifiedAtnBuilder.RuleNames.TypeSwitchGuard)); interpreter.BoundaryRules.Add(network.GetRule(GoSimplifiedAtnBuilder.RuleNames.FieldName)); interpreter.BoundaryRules.Add(network.GetRule(GoSimplifiedAtnBuilder.RuleNames.Receiver)); interpreter.BoundaryRules.Add(network.GetRule(GoSimplifiedAtnBuilder.RuleNames.FunctionDecl)); interpreter.BoundaryRules.Add(network.GetRule(GoSimplifiedAtnBuilder.RuleNames.BaseTypeName)); interpreter.BoundaryRules.Add(network.GetRule(GoSimplifiedAtnBuilder.RuleNames.TypeSpec)); interpreter.BoundaryRules.Add(network.GetRule(GoSimplifiedAtnBuilder.RuleNames.IdentifierList)); interpreter.BoundaryRules.Add(network.GetRule(GoSimplifiedAtnBuilder.RuleNames.MethodName)); interpreter.BoundaryRules.Add(network.GetRule(GoSimplifiedAtnBuilder.RuleNames.ParameterDecl)); interpreter.BoundaryRules.Add(network.GetRule(GoSimplifiedAtnBuilder.RuleNames.FieldIdentifierList)); interpreter.BoundaryRules.Add(network.GetRule(GoSimplifiedAtnBuilder.RuleNames.PackageName)); interpreter.BoundaryRules.Add(network.GetRule(GoSimplifiedAtnBuilder.RuleNames.TypeName)); interpreter.ExcludedStartRules.Add(network.GetRule(GoSimplifiedAtnBuilder.RuleNames.Block)); while (interpreter.TryStepBackward()) { if (interpreter.Contexts.Count == 0) { break; } /* we want all traces to start outside the binOpExpr18 rule, which means all * traces with a transition reachable from binOpExpr18 should contain a push * transition with binOpExpr18's start state as its target. */ if (interpreter.Contexts.All(context => context.BoundedStart)) { break; } } 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> finalResult = new HashSet <string>(); SnapshotSpan? contextSpan = null; foreach (var context in interpreter.Contexts) { Span?span = null; //List<string> results = AnalyzeInterpreterTrace(context, memberSelectRule, out span); 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) { contextSpan = new SnapshotSpan(currentSnapshot, span.Value); } //if (results.Count > 0) //{ // finalResult.UnionWith(results); // applicableToSpan = currentSnapshot.CreateTrackingSpan(span, SpanTrackingMode.EdgeExclusive); //} } foreach (var result in finalResult) { quickInfoContent.Add(result); } #endregion #if false var selection = session.TextView.Selection.StreamSelectionSpan; if (selection.IsEmpty || !selection.Contains(new VirtualSnapshotPoint(triggerPoint.Value))) { SnapshotSpan?expressionSpan = Provider.IntellisenseCache.GetExpressionSpan(triggerPoint.Value); if (expressionSpan.HasValue) { selection = new VirtualSnapshotSpan(expressionSpan.Value); } } #endif VirtualSnapshotSpan selection = new VirtualSnapshotSpan(); if (contextSpan.HasValue) { selection = new VirtualSnapshotSpan(contextSpan.Value); } if (!selection.IsEmpty && selection.Contains(new VirtualSnapshotPoint(triggerPoint.Value))) { applicableToSpan = selection.Snapshot.CreateTrackingSpan(selection.SnapshotSpan, SpanTrackingMode.EdgeExclusive); quickInfoContent.Add(selection.GetText()); //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) when (!ErrorHandler.IsCriticalException(ex)) //{ // quickInfoContent.Add(ex.Message); //} } } }