Skip to content

Commit f483741

Browse files
committed
More renames
1 parent ed3226c commit f483741

File tree

2 files changed

+18
-12
lines changed

2 files changed

+18
-12
lines changed

src/StronglyTypedIds/SourceGenerationHelper.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,21 @@ namespace StronglyTypedIds
66
internal static class SourceGenerationHelper
77
{
88
public static string CreateId(
9-
string idNamespace,
9+
string targetNamespace,
10+
string targetName,
1011
string idName,
1112
ParentClass? parentClass,
1213
string template,
1314
bool addDefaultAttributes,
1415
bool addGeneratedCodeAttribute,
1516
StringBuilder? sb)
1617
{
17-
if (string.IsNullOrEmpty(idName))
18+
if (string.IsNullOrEmpty(targetName))
1819
{
19-
throw new ArgumentException("Value cannot be null or empty.", nameof(idName));
20+
throw new ArgumentException("Value cannot be null or empty.", nameof(targetName));
2021
}
2122

22-
var hasNamespace = !string.IsNullOrEmpty(idNamespace);
23+
var hasNamespace = !string.IsNullOrEmpty(targetNamespace);
2324

2425
var parentsCount = 0;
2526

@@ -38,7 +39,7 @@ public static string CreateId(
3839
{
3940
sb
4041
.Append("namespace ")
41-
.Append(idNamespace)
42+
.Append(targetNamespace)
4243
.AppendLine(@"
4344
{");
4445
}
@@ -85,6 +86,7 @@ public static string CreateId(
8586

8687
sb.AppendLine(template);
8788

89+
sb.Replace("TARGETTYPE", targetName);
8890
sb.Replace("PLACEHOLDERID", idName);
8991

9092
for (int i = 0; i < parentsCount; i++)

src/StronglyTypedIds/StronglyTypedIdGenerator.cs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,10 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
8585

8686
// Output
8787
context.RegisterSourceOutput(idsWithDefaultsAndTemplates,
88-
static (spc, source) => Execute(source.Left.Left, source.Left.Right, source.Right, spc));
88+
static (spc, source) => GenerateIds(source.Left.Left, source.Left.Right, source.Right, spc));
8989
}
90-
private static void Execute(
90+
91+
private static void GenerateIds(
9192
StructToGenerate idToGenerate,
9293
ImmutableArray<(string Path, string Name, string? Content)> templates,
9394
(EquatableArray<(string Name, string Content)>, bool IsValid, DiagnosticInfo? Diagnostic) defaults,
@@ -99,7 +100,7 @@ private static void Execute(
99100
context.ReportDiagnostic(diagnostic);
100101
}
101102

102-
if (!TryGetTemplateContent(idToGenerate, templates, defaults, in context, out var templateContents))
103+
if (!TryGetTemplateContent(idToGenerate.Template, idToGenerate.TemplateNames, idToGenerate.TemplateLocation, templates, defaults, in context, out var templateContents))
103104
{
104105
return;
105106
}
@@ -111,6 +112,7 @@ private static void Execute(
111112
var result = SourceGenerationHelper.CreateId(
112113
idToGenerate.NameSpace,
113114
idToGenerate.Name,
115+
idToGenerate.Name, // same type
114116
idToGenerate.Parent,
115117
content,
116118
addDefaultAttributes: string.IsNullOrEmpty(name),
@@ -190,26 +192,28 @@ private static (EquatableArray<(string Name, string Content)>, bool, DiagnosticI
190192
}
191193

192194
private static bool TryGetTemplateContent(
193-
in StructToGenerate idToGenerate,
195+
Template? selectedTemplate,
196+
EquatableArray<string> selectedTemplateNames,
197+
LocationInfo? attributeLocation,
194198
in ImmutableArray<(string Path, string Name, string? Content)> templates,
195199
(EquatableArray<(string Name, string Content)> Contents, bool IsValid, DiagnosticInfo? Diagnostics) defaults,
196200
in SourceProductionContext context,
197201
[NotNullWhen(true)] out (string Name, string Content)[]? templateContents)
198202
{
199203
(string, string)? builtIn = null;
200-
if (idToGenerate.Template is { } templateId)
204+
if (selectedTemplate is { } templateId)
201205
{
202206
// built-in template specified
203207
var content = EmbeddedSources.GetTemplate(templateId);
204208
builtIn = (string.Empty, content);
205209
}
206210

207-
if (idToGenerate.TemplateNames.GetArray() is {Length: > 0} templateNames)
211+
if (selectedTemplateNames.GetArray() is {Length: > 0} templateNames)
208212
{
209213
// custom template specified
210214
if (GetContent(
211215
templateNames,
212-
idToGenerate.TemplateLocation,
216+
attributeLocation,
213217
builtIn.HasValue,
214218
in templates,
215219
out templateContents,

0 commit comments

Comments
 (0)