Skip to content

Commit dc28131

Browse files
committed
新增(generator): 支持多占位符及命名空间解析
新增[Program.cs]: 增加多参数RegionInject特性及Show1方法 新增[Directory.Build.props]: 更新版本号至1.0.3 新增[CodeInjectIncrementalSourceGenerator.cs]: - 引入System.Diagnostics命名空间 - 支持params string[]占位符参数 - 增加文件作用域命名空间解析逻辑 - 增加空内容及空类检查逻辑 修复[CodeInjectIncrementalSourceGenerator.cs]: 修复classSymbol空检查逻辑 优化[CodeInjectIncrementalSourceGenerator.cs]: 优化占位符提取逻辑
1 parent 25ef2b1 commit dc28131

File tree

4 files changed

+49
-17
lines changed

4 files changed

+49
-17
lines changed

Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<Project>
22
<PropertyGroup>
3-
<Version>1.0.2</Version>
3+
<Version>1.0.3</Version>
44
<CheckForOverflowUnderflow>false</CheckForOverflowUnderflow>
55
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
66
<LangVersion>preview</LangVersion>

demo/CodeRegionExamplesConsoleApp/Program.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@
1616
namespace CodeRegionExamplesConsoleApp;
1717

1818
[RegionInject("./CodeRegionTemplateConsoleApp/Program.cs", "Show")]
19+
[RegionInject("./CodeRegionTemplateConsoleApp/Program.cs", "Show1","Show123","Show222")]
1920
internal partial class ExampleProgram
2021
{
2122
static void Main(string[] args)
2223
{
2324
Show();
25+
Show1();
2426
}
2527
}

demo/CodeRegionTemplateConsoleApp/Program.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,11 @@ public static void Show()
2626
}
2727
#endregion
2828

29+
30+
#region Show1
31+
public static void Show1()
32+
{
33+
Console.WriteLine("Show123");
34+
}
35+
#endregion
2936
}

src/CodeInjectSourceGenerator/CodeInjectIncrementalSourceGenerator.cs

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
using System;
1818
using System.Collections.Generic;
1919
using System.Collections.Immutable;
20+
using System.Diagnostics;
2021
using System.IO;
2122
using System.Linq;
2223
using System.Reflection;
@@ -130,8 +131,10 @@ private static ClassToGenerate GetSemanticTargetForGeneration(GeneratorSyntaxCon
130131
var classSymbol = semanticModel.GetDeclaredSymbol(classDeclarationSyntax);
131132

132133
if (classSymbol is null)
134+
{
133135
return null;
134-
136+
}
137+
135138
var attributes = new List<RegionInjectData>();
136139

137140
foreach (var attributeData in classSymbol.GetAttributes())
@@ -157,33 +160,43 @@ private static ClassToGenerate GetSemanticTargetForGeneration(GeneratorSyntaxCon
157160
var placeholders = new List<string>();
158161

159162
// 从构造函数参数获取占位符
160-
if (attributeData.ConstructorArguments.Length > 2)
163+
// 检查是否使用了带 params string[] 的构造函数
164+
if (attributeData.ConstructorArguments.Length >= 3)
161165
{
162-
for (var i = 2; i < attributeData.ConstructorArguments.Length; i++)
166+
// 第3个参数是 params string[] placeholders
167+
var placeholdersArg = attributeData.ConstructorArguments[2];
168+
if (placeholdersArg.Kind == TypedConstantKind.Array && !placeholdersArg.IsNull)
163169
{
164-
var value = attributeData.ConstructorArguments[i].Value?.ToString();
165-
if (!string.IsNullOrEmpty(value))
170+
var arrayValues = placeholdersArg.Values;
171+
foreach (var item in arrayValues)
166172
{
167-
placeholders.Add(value);
173+
var stringValue = item.Value?.ToString();
174+
if (!string.IsNullOrEmpty(stringValue))
175+
{
176+
placeholders.Add(stringValue);
177+
}
168178
}
169179
}
170180
}
171181

172-
// Placeholders 属性获取占位符
173-
foreach (var namedArgument in attributeData.NamedArguments)
182+
// 如果构造函数中没有占位符,再从 Placeholders 属性获取
183+
if (placeholders.Count == 0)
174184
{
175-
if (namedArgument.Key == "Placeholders")
185+
foreach (var namedArgument in attributeData.NamedArguments)
176186
{
177-
var value = namedArgument.Value;
178-
if (value.Kind == TypedConstantKind.Array && !value.IsNull)
187+
if (namedArgument.Key == "Placeholders")
179188
{
180-
var arrayValues = value.Values;
181-
foreach (var item in arrayValues)
189+
var value = namedArgument.Value;
190+
if (value.Kind == TypedConstantKind.Array && !value.IsNull)
182191
{
183-
var stringValue = item.Value?.ToString();
184-
if (!string.IsNullOrEmpty(stringValue))
192+
var arrayValues = value.Values;
193+
foreach (var item in arrayValues)
185194
{
186-
placeholders.Add(stringValue);
195+
var stringValue = item.Value?.ToString();
196+
if (!string.IsNullOrEmpty(stringValue))
197+
{
198+
placeholders.Add(stringValue);
199+
}
187200
}
188201
}
189202
}
@@ -217,19 +230,25 @@ private static string GetNamespace(ClassDeclarationSyntax classDeclaration)
217230
{
218231
var namespaceDeclaration = classDeclaration.Ancestors().OfType<NamespaceDeclarationSyntax>().FirstOrDefault();
219232
if (namespaceDeclaration != null)
233+
{
220234
return namespaceDeclaration.Name.ToString();
235+
}
221236

222237
var fileScopedNamespace = classDeclaration.Ancestors().OfType<FileScopedNamespaceDeclarationSyntax>().FirstOrDefault();
223238
if (fileScopedNamespace != null)
239+
{
224240
return fileScopedNamespace.Name.ToString();
241+
}
225242

226243
return string.Empty;
227244
}
228245

229246
private static void Execute(ImmutableArray<ClassToGenerate> classes, ImmutableArray<AdditionalText> additionalFiles, SourceProductionContext context)
230247
{
231248
if (classes.IsDefaultOrEmpty)
249+
{
232250
return;
251+
}
233252

234253
foreach (var classToGenerate in classes)
235254
{
@@ -285,7 +304,9 @@ private static string GenerateClass(ClassToGenerate classInfo, ImmutableArray<Ad
285304
private static string FormatInjectedCode(string code, string indentation)
286305
{
287306
if (string.IsNullOrEmpty(code))
307+
{
288308
return string.Empty;
309+
}
289310

290311
var lines = code.Split(new[] { '\r', '\n' }, StringSplitOptions.None);
291312
var formattedLines = new List<string>();
@@ -453,7 +474,9 @@ private static string ExtractRegion(string fileContent, string regionName)
453474
private static string ProcessPlaceholders(string content, string[] placeholders)
454475
{
455476
if (placeholders.Length == 0)
477+
{
456478
return content;
479+
}
457480

458481
var result = content;
459482

0 commit comments

Comments
 (0)