Skip to content

Commit

Permalink
Add trivia config
Browse files Browse the repository at this point in the history
  • Loading branch information
kzrnm committed Dec 15, 2020
1 parent bc00d4f commit c0f8e4d
Show file tree
Hide file tree
Showing 6 changed files with 234 additions and 38 deletions.
3 changes: 3 additions & 0 deletions Sample/_SampleLibrary2/SourceExpander.Embedder.Config.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,8 @@
"System.Diagnostics.DebuggerStepThroughAttribute",
"System.Diagnostics.DebuggerTypeProxyAttribute",
"System.Diagnostics.DebuggerVisualizerAttribute"
],
"add-trivia-kinds": [
"EqualsGreaterThanToken"
]
}
3 changes: 2 additions & 1 deletion Source/DummySourceExpanderEmbedder/DummyGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ public void Execute(GeneratorExecutionContext context)
excludeAttributes: new[] {
"System.Runtime.CompilerServices.MethodImplAttribute",
"System.Runtime.CompilerServices.CallerFilePathAttribute"
}),
},
addTriviaKinds: ImmutableHashSet<SyntaxKind>.Empty),
context.CancellationToken);

context.AddSource(
Expand Down
32 changes: 29 additions & 3 deletions Source/SourceExpander.Embedder/EmbedderConfig.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Runtime.Serialization;
using System.Threading;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Text;

namespace SourceExpander
Expand All @@ -12,21 +14,30 @@ public EmbedderConfig()
: this(
true,
EmbeddingType.GZipBase32768,
Array.Empty<string>())
Array.Empty<string>(),
ImmutableHashSet<SyntaxKind>.Empty)
{ }
public EmbedderConfig(
bool enabled,
EmbeddingType embeddingType,
string[] excludeAttributes)
string[] excludeAttributes,
IEnumerable<SyntaxKind> addTriviaKinds)
{
Enabled = enabled;
EmbeddingType = embeddingType;
ExcludeAttributes = ImmutableHashSet.Create(excludeAttributes);
AddTriviaKinds = addTriviaKinds switch
{
ImmutableHashSet<SyntaxKind> hs => hs,
ImmutableHashSet<SyntaxKind>.Builder hs => hs.ToImmutable(),
_ => ImmutableHashSet.CreateRange(addTriviaKinds),
};
}

public bool Enabled { get; }
public EmbeddingType EmbeddingType { get; }
public ImmutableHashSet<string> ExcludeAttributes { get; }
public ImmutableHashSet<SyntaxKind> AddTriviaKinds { get; }

public static EmbedderConfig Parse(SourceText? sourceText, CancellationToken cancellationToken)
{
Expand All @@ -37,13 +48,26 @@ static EmbeddingType ParseEmbeddingType(string? str)
_ => EmbeddingType.GZipBase32768,
};

static ImmutableHashSet<SyntaxKind> ParseSyntaxKinds(string[]? arr)
{
if (arr is null)
return ImmutableHashSet<SyntaxKind>.Empty;

var builder = ImmutableHashSet.CreateBuilder<SyntaxKind>();
foreach (var str in arr)
if (Enum.TryParse(str, true, out SyntaxKind kind))
builder.Add(kind);
return builder.ToImmutable();
}

try
{
if (sourceText is not null && JsonUtil.ParseJson<EmbedderConfigData>(sourceText, cancellationToken) is { } data)
return new EmbedderConfig(
enabled: data.Enabled ?? true,
embeddingType: ParseEmbeddingType(data.EmbeddingType),
excludeAttributes: data.ExcludeAttributes ?? Array.Empty<string>());
excludeAttributes: data.ExcludeAttributes ?? Array.Empty<string>(),
addTriviaKinds: ParseSyntaxKinds(data.AddTriviaKinds));
return new EmbedderConfig();
}
catch (Exception e)
Expand All @@ -61,6 +85,8 @@ private class EmbedderConfigData
public string? EmbeddingType { set; get; }
[DataMember(Name = "exclude-attributes")]
public string[]? ExcludeAttributes { set; get; }
[DataMember(Name = "add-trivia-kinds")]
public string[]? AddTriviaKinds { set; get; }

public ExtensionDataObject? ExtensionData { get; set; }
}
Expand Down
2 changes: 1 addition & 1 deletion Source/SourceExpander.Embedder/EmbeddingResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ private SourceFileInfoRaw ParseSource(SyntaxTree tree, string commonPrefix)
if (newRoot is null)
throw new Exception($"Syntax tree of {tree.FilePath} is invalid");

var minified = new TriviaRemover().Visit(newRoot.NormalizeWhitespace("", " ")).ToString();
var minified = new TriviaRemover(config).Visit(newRoot.NormalizeWhitespace("", " "))!.ToString();

if (ValidationHelpers.CompareSyntax(newRoot,
CSharpSyntaxTree.ParseText(minified,
Expand Down
19 changes: 18 additions & 1 deletion Source/SourceExpander.Embedder/Roslyn/TriviaRemover.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ namespace SourceExpander.Roslyn
{
public class TriviaRemover : CSharpSyntaxRewriter
{
public TriviaRemover()
private readonly EmbedderConfig config;
public TriviaRemover(EmbedderConfig config)
{
this.config = config;

var splitBuilder = ImmutableHashSet.CreateBuilder<SyntaxKind>();
splitBuilder.Add(SyntaxKind.TildeToken);
splitBuilder.Add(SyntaxKind.ExclamationToken);
Expand Down Expand Up @@ -67,10 +70,24 @@ public TriviaRemover()
SplitToken = splitBuilder.ToImmutable();
}
public ImmutableHashSet<SyntaxKind> SplitToken { get; }
public override SyntaxNode? Visit(SyntaxNode? node)
{
var res = base.Visit(node);
if (res is null)
return null;
if (config.AddTriviaKinds.Contains(res.Kind()))
return res.WithLeadingTrivia(SyntaxFactory.Space).WithTrailingTrivia(SyntaxFactory.Space);
return res;
}
public override SyntaxToken VisitToken(SyntaxToken token)
{
var res = base.VisitToken(token);

if (config.AddTriviaKinds.Contains(token.Kind()))
return res
.WithLeadingTrivia(SyntaxFactory.Space)
.WithTrailingTrivia(SyntaxFactory.Space);

if (SplitToken.Contains(token.Kind()))
return res.WithoutTrivia();

Expand Down
Loading

0 comments on commit c0f8e4d

Please sign in to comment.