Skip to content

Commit

Permalink
reworked code generation
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelstaib committed Feb 22, 2022
1 parent 097fb4c commit a6c20ed
Show file tree
Hide file tree
Showing 30 changed files with 1,321 additions and 1,440 deletions.
Expand Up @@ -14,6 +14,7 @@ public class PropertyBuilder : ICodeBuilder
private string? _value;
private string? _interface;
private bool _isStatic;
private bool _isOverride;

public static PropertyBuilder New() => new();

Expand All @@ -29,6 +30,12 @@ public PropertyBuilder SetStatic()
return this;
}

public PropertyBuilder SetOverride()
{
_isOverride = true;
return this;
}

public PropertyBuilder AsLambda(string resolveCode)
{
_lambdaResolver = CodeInlineBuilder.From(resolveCode);
Expand Down Expand Up @@ -119,6 +126,12 @@ public void Build(CodeWriter writer)
writer.Write("static");
writer.WriteSpace();
}

if (_isOverride)
{
writer.Write("override");
writer.WriteSpace();
}
}

_type.Build(writer);
Expand Down
Expand Up @@ -10,6 +10,8 @@
using HotChocolate.Validation;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Formatting;
using Microsoft.CodeAnalysis.Options;
using StrawberryShake.CodeGeneration.Analyzers;
using StrawberryShake.CodeGeneration.Analyzers.Models;
using StrawberryShake.CodeGeneration.CSharp.Generators;
Expand All @@ -20,6 +22,7 @@
using static HotChocolate.Language.Utf8GraphQLParser;
using static StrawberryShake.CodeGeneration.Utilities.DocumentHelper;
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
using static Microsoft.CodeAnalysis.Formatting.FormattingOptions;

namespace StrawberryShake.CodeGeneration.CSharp
{
Expand Down Expand Up @@ -341,6 +344,12 @@ public static class CSharpGenerator
SourceDocumentKind kind,
ICollection<SourceDocument> documents)
{
var workspace = new AdhocWorkspace();
OptionSet options = workspace.Options
.WithChangedOption(IndentationSize, LanguageNames.CSharp, 4)
.WithChangedOption(SmartIndent, LanguageNames.CSharp, IndentStyle.Smart)
.WithChangedOption(UseTabs, LanguageNames.CSharp, false);

foreach (var group in results.GroupBy(t => t.Result.Namespace).OrderBy(t => t.Key))
{
foreach (var item in group)
Expand All @@ -358,7 +367,7 @@ public static class CSharpGenerator
NamespaceDeclaration(IdentifierName(group.Key)).AddMembers(
typeDeclaration));

compilationUnit = compilationUnit.NormalizeWhitespace(elasticTrivia: true);
SyntaxNode formatted = Formatter.Format(compilationUnit, workspace, options);

var code = new StringBuilder();

Expand All @@ -369,7 +378,7 @@ public static class CSharpGenerator
code.AppendLine("#nullable enable");

code.AppendLine();
code.AppendLine(compilationUnit.ToFullString());
code.AppendLine(formatted.ToFullString());

documents.Add(new(
item.Result.FileName,
Expand Down

0 comments on commit a6c20ed

Please sign in to comment.