public override void ExitCatchBlock([NotNull] XSharpParser.CatchBlockContext context) { // Here will have all the informations that are AFTER the CATCH keyword, but we need to include the keyword XSharpParser.TryStmtContext tryStmt = (XSharpParser.TryStmtContext)context.Parent; // Search the CatchBlock int i = 0; LanguageService.SyntaxTree.Tree.IParseTree token = null; for (i = 0; i < tryStmt.ChildCount; i++) { token = tryStmt.GetChild(i); if (token == context) { // Ok, we found the current CatchBlock, so the Catch is the previous one if (i > 0) { token = tryStmt.GetChild(i - 1); break; } } } // if (token != null) { LanguageService.SyntaxTree.IToken sym = ((LanguageService.SyntaxTree.Tree.TerminalNodeImpl)token).Symbol; var tokenSpan = new TextSpan(sym.StartIndex, sym.StopIndex - sym.StartIndex + 1); _regionTags.Add(tokenSpan.ToClassificationSpan(_snapshot, xsharpRegionStartType)); // XSharpParser.CatchBlockContext lastTokenInContext = context as LanguageService.CodeAnalysis.XSharp.SyntaxParser.XSharpParser.CatchBlockContext; tokenSpan = new TextSpan(lastTokenInContext.Stop.StopIndex - 1, 1); _regionTags.Add(tokenSpan.ToClassificationSpan(_snapshot, xsharpRegionStopType)); } }
public override void ExitTryStmt([NotNull] XSharpParser.TryStmtContext context) { TagRegion(context, context.ChildCount - 1); if (context.FinBlock != null) { // Search the ELSE block, if Any int i = 0; LanguageService.SyntaxTree.Tree.IParseTree token = null; for (i = 0; i < context.ChildCount; i++) { token = context.GetChild(i); String tokenText = token.GetText().ToUpper(); if (tokenText == "FINALLY") { break; } else { token = null; } } // if (token is LanguageService.SyntaxTree.Tree.TerminalNodeImpl) { LanguageService.SyntaxTree.IToken sym = ((LanguageService.SyntaxTree.Tree.TerminalNodeImpl)token).Symbol; var tokenSpan = new TextSpan(sym.StartIndex, sym.StopIndex - sym.StartIndex + 1); _regionTags.Add(tokenSpan.ToClassificationSpan(_snapshot, xsharpRegionStartType)); // XSharpParser.StatementBlockContext lastTokenInContext = context.FinBlock as LanguageService.CodeAnalysis.XSharp.SyntaxParser.XSharpParser.StatementBlockContext; tokenSpan = new TextSpan(lastTokenInContext.Stop.StopIndex - 1, 1); _regionTags.Add(tokenSpan.ToClassificationSpan(_snapshot, xsharpRegionStopType)); } } }
public override void ExitEveryRule([NotNull] ParserRuleContext context) { base.ExitEveryRule(context); if ((context is XSharpParser.Namespace_Context) || (context is XSharpParser.Class_Context) || (context is XSharpParser.PropertyContext) || (context is XSharpParser.PropertyAccessorContext)) { // already done // BEGIN NAMESPACE .... END NAMESPACE // TagRegion(context, context.ChildCount - 2); } else if ((context is XSharpParser.FunctionContext) || (context is XSharpParser.ProcedureContext) || (context is XSharpParser.MethodContext) || (context is XSharpParser.ConstructorContext) || (context is XSharpParser.DestructorContext)) { // Put a region up to the end of the Entity TagRegion(context, context.ChildCount - 1); } else if (context is XSharpParser.IdentifierContext) { LanguageService.SyntaxTree.IToken sym = context.Start; TextSpan tokenSpan; tokenSpan = new TextSpan(sym.StartIndex, sym.StopIndex - sym.StartIndex + 1); tags.Add(tokenSpan.ToTagSpan(Snapshot, xsharpIdentifierType)); } }
public override void ExitIfStmt([NotNull] XSharpParser.IfStmtContext context) { TagRegion(context, context.ChildCount - 2); // try { XSharpParser.StatementBlockContext elseBlock = null; XSharpParser.IfElseBlockContext stmt = null; if (context.IfStmt.ElseBlock != null) { elseBlock = context.IfStmt.ElseBlock; stmt = context.IfStmt; } else if (context.IfStmt.ElseIfBlock != null) { if (context.IfStmt.ElseIfBlock.ElseBlock != null) { elseBlock = context.IfStmt.ElseIfBlock.ElseBlock; stmt = context.IfStmt.ElseIfBlock; } } // if (elseBlock != null) { // Search the ELSE block, if Any int i = 0; LanguageService.SyntaxTree.Tree.IParseTree token = null; for (i = 0; i < stmt.ChildCount; i++) { token = stmt.GetChild(i); String tokenText = token.GetText().ToUpper(); if (tokenText == "ELSE") { break; } else { token = null; } } // if (token is LanguageService.SyntaxTree.Tree.TerminalNodeImpl) { LanguageService.SyntaxTree.IToken sym = ((LanguageService.SyntaxTree.Tree.TerminalNodeImpl)token).Symbol; var tokenSpan = new TextSpan(sym.StartIndex, sym.StopIndex - sym.StartIndex + 1); _regionTags.Add(tokenSpan.ToClassificationSpan(_snapshot, xsharpRegionStartType)); // XSharpParser.StatementBlockContext lastTokenInContext = elseBlock as LanguageService.CodeAnalysis.XSharp.SyntaxParser.XSharpParser.StatementBlockContext; tokenSpan = new TextSpan(lastTokenInContext.Stop.StopIndex - 1, 1); _regionTags.Add(tokenSpan.ToClassificationSpan(_snapshot, xsharpRegionStopType)); } } } catch (Exception e) { XSharpClassifier.Debug("Tagregion failed: " + e.Message); } }
public override void ExitStatementBlock([NotNull] XSharpParser.StatementBlockContext context) { if (!(context.Parent is XSharpParser.IfElseBlockContext) && !(context.Parent is XSharpParser.CaseBlockContext) && !(context.Parent is XSharpParser.TryStmtContext) && !(context.Parent is XSharpParser.CatchBlockContext) && !(context.Parent is XSharpParser.PropertyAccessorContext)) { TagRegion(context.Parent, context.Parent.ChildCount - 1); } else if (context.Parent is XSharpParser.IfElseBlockContext) { var ctxt = context.Parent as XSharpParser.IfElseBlockContext; // if (ctxt.ElseIfBlock != null) { // we have Count >= 3 // 0 : BinaryExpr // 1 : EOS // 2 : StatementBlock //TagRegion(ctxt.ElseIfBlock, 2); // //// Search the ELSEIF block, if Any int i = 0; LanguageService.SyntaxTree.Tree.IParseTree token = null; for (i = 0; i < ctxt.ChildCount; i++) { token = ctxt.GetChild(i); String tokenText = token.GetText().ToUpper(); if (tokenText == "ELSEIF") { break; } else { token = null; } } // if (token is LanguageService.SyntaxTree.Tree.TerminalNodeImpl) { LanguageService.SyntaxTree.IToken sym = ((LanguageService.SyntaxTree.Tree.TerminalNodeImpl)token).Symbol; var tokenSpan = new TextSpan(sym.StartIndex, sym.StopIndex - sym.StartIndex + 1); _regionTags.Add(tokenSpan.ToClassificationSpan(_snapshot, xsharpRegionStartType)); // var endToken = ctxt.ElseIfBlock.GetChild(2); XSharpParser.StatementBlockContext lastTokenInContext = endToken as LanguageService.CodeAnalysis.XSharp.SyntaxParser.XSharpParser.StatementBlockContext; tokenSpan = new TextSpan(lastTokenInContext.Stop.StopIndex - 1, 1); _regionTags.Add(tokenSpan.ToClassificationSpan(_snapshot, xsharpRegionStopType)); } } } }
private void TagRegion(RuleContext _context, int endChild) { try { var context = _context as XSharpParserRuleContext; var endToken = context.GetChild(endChild); if (endToken is LanguageService.SyntaxTree.Tree.TerminalNodeImpl) { LanguageService.SyntaxTree.IToken sym = ((LanguageService.SyntaxTree.Tree.TerminalNodeImpl)endToken).Symbol; var tokenSpan = new TextSpan(context.Start.StartIndex, 1); // Attribute ? if (context.Start.Text == "[") { // Skip it int newStart = getStatementForAttribute(_snapshot, context.Start.StartIndex); tokenSpan = new TextSpan(newStart, 1); } _regionTags.Add(tokenSpan.ToClassificationSpan(_snapshot, xsharpRegionStartType)); tokenSpan = new TextSpan(sym.StartIndex, sym.StopIndex - sym.StartIndex + 1); _regionTags.Add(tokenSpan.ToClassificationSpan(_snapshot, xsharpRegionStopType)); } else if (endToken is LanguageService.CodeAnalysis.XSharp.SyntaxParser.XSharpParser.StatementBlockContext) { XSharpParser.StatementBlockContext lastTokenInContext = endToken as LanguageService.CodeAnalysis.XSharp.SyntaxParser.XSharpParser.StatementBlockContext; var tokenSpan = new TextSpan(context.Start.StartIndex, 1); // Attribute on top of Function/Method/... ? if (context.Start.Text == "[") { // Skip it int newStart = getStatementForAttribute(_snapshot, context.Start.StartIndex); tokenSpan = new TextSpan(newStart, 1); } // _regionTags.Add(tokenSpan.ToClassificationSpan(_snapshot, xsharpRegionStartType)); tokenSpan = new TextSpan(lastTokenInContext.Stop.StopIndex - 1, 1); _regionTags.Add(tokenSpan.ToClassificationSpan(_snapshot, xsharpRegionStopType)); } else if (endToken is ParserRuleContext) { var lastTokenInContext = endToken as ParserRuleContext; var tokenSpan = new TextSpan(context.Start.StartIndex, 1); _regionTags.Add(tokenSpan.ToClassificationSpan(_snapshot, xsharpRegionStartType)); tokenSpan = new TextSpan(lastTokenInContext.Stop.StartIndex - 1, 1); _regionTags.Add(tokenSpan.ToClassificationSpan(_snapshot, xsharpRegionStopType)); } } catch (Exception e) { XSharpClassifier.Debug("Tagregion failed: " + e.Message); } }
private void TagRegion(ParserRuleContext context, int endChild) { var endToken = context.GetChild(endChild); if (endToken is LanguageService.SyntaxTree.Tree.TerminalNodeImpl) { LanguageService.SyntaxTree.IToken sym = ((LanguageService.SyntaxTree.Tree.TerminalNodeImpl)endToken).Symbol; var tokenSpan = new TextSpan(context.Start.StartIndex, 1); tags.Add(tokenSpan.ToTagSpan(Snapshot, xsharpRegionStartType)); tokenSpan = new TextSpan(sym.StartIndex, sym.StopIndex - sym.StartIndex + 1); tags.Add(tokenSpan.ToTagSpan(Snapshot, xsharpRegionStopType)); } else if (endToken is LanguageService.CodeAnalysis.XSharp.SyntaxParser.XSharpParser.StatementBlockContext) { XSharpParser.StatementBlockContext lastTokenInContext = endToken as LanguageService.CodeAnalysis.XSharp.SyntaxParser.XSharpParser.StatementBlockContext; var tokenSpan = new TextSpan(context.Start.StartIndex, 1); tags.Add(tokenSpan.ToTagSpan(Snapshot, xsharpRegionStartType)); tokenSpan = new TextSpan(lastTokenInContext.Stop.StartIndex - 1, 1); tags.Add(tokenSpan.ToTagSpan(Snapshot, xsharpRegionStopType)); } }