From bb9978646a9f758165294e53c1e95004277dc7d3 Mon Sep 17 00:00:00 2001 From: Pavel Krymets Date: Wed, 7 Oct 2020 08:33:48 -0700 Subject: [PATCH] Support regions without names in Update-Snippets (#15746) Fixes: https://github.com/Azure/azure-sdk-for-net/issues/15744 --- eng/SnippetGenerator/DirectoryProcessor.cs | 26 +++++++++++++++++----- 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/eng/SnippetGenerator/DirectoryProcessor.cs b/eng/SnippetGenerator/DirectoryProcessor.cs index 2f4f304458bf4..0605b8ae5d380 100644 --- a/eng/SnippetGenerator/DirectoryProcessor.cs +++ b/eng/SnippetGenerator/DirectoryProcessor.cs @@ -204,11 +204,19 @@ private List GetSnippetsInDirectory(string baseDirectory) var list = new List(); foreach (var file in Directory.GetFiles(baseDirectory, "*.cs", SearchOption.AllDirectories)) { - var syntaxTree = CSharpSyntaxTree.ParseText( - File.ReadAllText(file), - new CSharpParseOptions(LanguageVersion.Preview), - path: file); - list.AddRange(GetAllSnippets(syntaxTree)); + try + { + var syntaxTree = CSharpSyntaxTree.ParseText( + File.ReadAllText(file), + new CSharpParseOptions(LanguageVersion.Preview), + path: file); + + list.AddRange(GetAllSnippets(syntaxTree)); + } + catch (Exception e) + { + throw new InvalidOperationException($"Failed to discover snippets from file {file}", e); + } } return list; @@ -222,7 +230,13 @@ private Snippet[] GetAllSnippets(SyntaxTree syntaxTree) foreach (var region in directiveWalker.Regions) { - var syntaxTrivia = region.Item1.EndOfDirectiveToken.LeadingTrivia.First(t => t.IsKind(SyntaxKind.PreprocessingMessageTrivia)); + 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);