Skip to content

Commit

Permalink
More efficiently generate SourceTexts
Browse files Browse the repository at this point in the history
  • Loading branch information
canton7 committed Mar 10, 2023
1 parent f7ef434 commit 8bbaba7
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 6 deletions.
39 changes: 37 additions & 2 deletions src/PropertyChanged.SourceGenerator/BetterIndentedTextWriter.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Runtime.CompilerServices;
using System.Text;
using Microsoft.CodeAnalysis.Text;

namespace PropertyChanged.SourceGenerator;

Expand All @@ -16,6 +15,42 @@ public BetterIndentedTextWriter() : base(new StringWriter())

public void Write([InterpolatedStringHandlerArgument("")] ref AppendInterpolatedStringHandler handler) { }
public void WriteLine([InterpolatedStringHandlerArgument("")] ref AppendInterpolatedStringHandler handler) => this.WriteLine();

public SourceText ToSourceText()
{
var stringWriter = (StringWriter)this.InnerWriter;
var stringBuilder = stringWriter.GetStringBuilder();
stringWriter.Close(); // Attempts to interact with it after this point will fail
return new StringBuilderText(stringBuilder);
}

private class StringBuilderText : SourceText
{
private readonly StringBuilder stringBuilder;

public StringBuilderText(StringBuilder stringBuilder)
{
this.stringBuilder = stringBuilder;
}
public override Encoding? Encoding => Encoding.UTF8;

public override char this[int position] => this.stringBuilder[position];

public override int Length => this.stringBuilder.Length;

public override void CopyTo(int sourceIndex, char[] destination, int destinationIndex, int count) =>
this.stringBuilder.CopyTo(sourceIndex, destination, destinationIndex, count);

public override string ToString(TextSpan span)
{
if (span.End > this.stringBuilder.Length)
{
throw new ArgumentOutOfRangeException(nameof(span));
}

return this.stringBuilder.ToString(span.Start, span.Length);
}
}
}

[InterpolatedStringHandler]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Collections.Generic;
using System.IO;
using System.Text;
using Microsoft.CodeAnalysis.Text;
using PropertyChanged.SourceGenerator;
using PropertyChanged.SourceGenerator.EventArgs;

Expand Down Expand Up @@ -55,5 +56,5 @@ private static string EscapeString(string? str)
: "@\"" + str.Replace("\"", "\"\"") + "\"";
}

public override string ToString() => this.writer.InnerWriter.ToString();
public SourceText ToSourceText() => this.writer.ToSourceText();
}
3 changes: 2 additions & 1 deletion src/PropertyChanged.SourceGenerator/Generator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Linq;
using System.Text;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Text;
using PropertyChanged.SourceGenerator.Analysis;
using PropertyChanged.SourceGenerator.EventArgs;

Expand Down Expand Up @@ -567,5 +568,5 @@ private static string EscapeString(string? str)
: "@\"" + str.Replace("\"", "\"\"") + "\"";
}

public override string ToString() => this.writer.InnerWriter.ToString();
public SourceText ToSourceText() => this.writer.ToSourceText();
}
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
var (typeAnalysis, eventArgsCacheLookup) = pair;
var generator = new Generator(eventArgsCacheLookup);
generator.Generate(typeAnalysis);
ctx.AddSource(typeAnalysis.TypeNameForGeneratedFileName + ".g", generator.ToString());
ctx.AddSource(typeAnalysis.TypeNameForGeneratedFileName + ".g", generator.ToSourceText());
});

context.RegisterSourceOutput(eventArgsCacheSource, (ctx, eventArgsCache) =>
Expand All @@ -127,7 +127,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
{
var generator = new EventArgsCacheGenerator(eventArgsCache);
generator.GenerateNameCache();
ctx.AddSource("PropertyChanged.SourceGenerator.Internal.EventArgsCache.g", generator.ToString());
ctx.AddSource("PropertyChanged.SourceGenerator.Internal.EventArgsCache.g", generator.ToSourceText());
}
});
}
Expand Down

0 comments on commit 8bbaba7

Please sign in to comment.