private Snippet[] GetAllSnippets(SyntaxTree syntaxTree) { var snippets = new List <Snippet>(); var directiveWalker = new DirectiveWalker(); directiveWalker.Visit(syntaxTree.GetRoot()); foreach (var region in directiveWalker.Regions) { var leadingTrivia = region.Item1.EndOfDirectiveToken.LeadingTrivia; if (!leadingTrivia.Any()) { // Skip unnamed regions continue; } var syntaxTrivia = leadingTrivia.First(t => t.IsKind(SyntaxKind.PreprocessingMessageTrivia)); var fromBounds = TextSpan.FromBounds( region.Item1.GetLocation().SourceSpan.End, region.Item2.GetLocation().SourceSpan.Start); var regionName = syntaxTrivia.ToString(); if (regionName.StartsWith(_snippetPrefix)) { snippets.Add(new Snippet(syntaxTrivia.ToString(), syntaxTree.GetText().GetSubText(fromBounds), syntaxTree.FilePath)); } } return(snippets.ToArray()); }
private static DirectiveInfo GetDirectiveInfo(SyntaxNode node, CancellationToken cancellationToken) { var root = node.GetAbsoluteRoot(); var info = rootToDirectiveInfo.GetValue(root, r => { var directiveMap = new Dictionary<DirectiveTriviaSyntax, DirectiveTriviaSyntax>( DirectiveSyntaxEqualityComparer.Instance); var conditionalMap = new Dictionary<DirectiveTriviaSyntax, IEnumerable<DirectiveTriviaSyntax>>( DirectiveSyntaxEqualityComparer.Instance); var walker = new DirectiveWalker(directiveMap, conditionalMap, cancellationToken); walker.Visit(r); return new DirectiveInfo(directiveMap, conditionalMap, inactiveRegionLines: null); }); return info; }
private static DirectiveInfo GetDirectiveInfo(SyntaxNode node, CancellationToken cancellationToken) { var root = node.GetAbsoluteRoot(); var info = rootToDirectiveInfo.GetValue(root, r => { var directiveMap = new Dictionary <DirectiveTriviaSyntax, DirectiveTriviaSyntax>( DirectiveSyntaxEqualityComparer.Instance); var conditionalMap = new Dictionary <DirectiveTriviaSyntax, IEnumerable <DirectiveTriviaSyntax> >( DirectiveSyntaxEqualityComparer.Instance); var walker = new DirectiveWalker(directiveMap, conditionalMap, cancellationToken); walker.Visit(r); return(new DirectiveInfo(directiveMap, conditionalMap, inactiveRegionLines: null)); }); return(info); }
private async Task <Snippet[]> GetAllSnippetsAsync(SyntaxTree syntaxTree) { var snippets = new List <Snippet>(); var directiveWalker = new DirectiveWalker(); directiveWalker.Visit(await syntaxTree.GetRootAsync()); foreach (var region in directiveWalker.Regions) { var syntaxTrivia = region.Item1.EndOfDirectiveToken.LeadingTrivia.First(t => t.IsKind(SyntaxKind.PreprocessingMessageTrivia)); var fromBounds = TextSpan.FromBounds( region.Item1.GetLocation().SourceSpan.End, region.Item2.GetLocation().SourceSpan.Start); snippets.Add(new Snippet(syntaxTrivia.ToString(), syntaxTree.GetText().GetSubText(fromBounds))); } return(snippets.ToArray()); }
protected override void ExecuteCore(RazorCodeDocument codeDocument, DocumentIRNode irDocument) { var parserOptions = irDocument.Options; var designTime = parserOptions.DesignTime; var walker = new DirectiveWalker(); walker.VisitDocument(irDocument); var classNode = walker.ClassNode; foreach (var node in walker.FunctionsDirectiveNodes) { node.Parent.Children.Remove(node); foreach (var child in node.Children.Except(node.Tokens)) { child.Parent = classNode; classNode.Children.Add(child); } } foreach (var node in walker.InheritsDirectiveNodes.Reverse()) { node.Parent.Children.Remove(node); var token = node.Tokens.FirstOrDefault(); if (token != null) { classNode.BaseType = token.Content; break; } } var sectionsMethodNode = new CSharpStatementIRNode(); RazorIRBuilder.Create(sectionsMethodNode) .Add(new RazorIRToken() { Kind = RazorIRToken.TokenKind.CSharp, Content = "public override void DefineSections() {" }); classNode.Children.Add(sectionsMethodNode); foreach (var node in walker.SectionDirectiveNodes) { //var sectionIndex = node.Parent.Children.IndexOf(node); node.Parent.Children.Remove(node); var lambdaContent = designTime ? "__razor_section_writer" : "builder"; var sectionName = node.Tokens.FirstOrDefault()?.Content; var defineSectionStartStatement = new CSharpStatementIRNode(); RazorIRBuilder.Create(defineSectionStartStatement) .Add(new RazorIRToken() { Kind = RazorIRToken.TokenKind.CSharp, Content = $"DefineSection(\"{sectionName}\", async ({lambdaContent}) => {{" }); classNode.Children.Add(defineSectionStartStatement); foreach (var child in node.Children.Except(node.Tokens)) { classNode.Children.Add(child); } var defineSectionEndStatement = new CSharpStatementIRNode(); RazorIRBuilder.Create(defineSectionEndStatement) .Add(new RazorIRToken() { Kind = RazorIRToken.TokenKind.CSharp, Content = "});" }); classNode.Children.Add(defineSectionEndStatement); } var sectionsMethodNodeEnd = new CSharpStatementIRNode(); RazorIRBuilder.Create(sectionsMethodNodeEnd) .Add(new RazorIRToken() { Kind = RazorIRToken.TokenKind.CSharp, Content = "}" }); classNode.Children.Add(sectionsMethodNodeEnd); }