private static async Task <Document> GenerateCodeFixAsync(Document document, AttributeSyntax node, CancellationToken cancellationToken) { var args = node.ArgumentList !.Arguments; if ( args[0].Expression is not MemberAccessExpressionSyntax langExp || //args[1].Expression is not LiteralExpressionSyntax nameExp || args[2].Expression is not LiteralExpressionSyntax descriptionExp) { return(document); } if (!Enum.TryParse(langExp.OperatorToken.GetNextToken().ToString(), out Lang lang)) { return(document); } //string? name = nameExp.Token.Value as string; string?description = descriptionExp.Token.Value as string; var sb = SBPool.Rent(); if (TryProcessText(description, lang, sb)) { var sourceText = await descriptionExp.SyntaxTree.GetTextAsync(cancellationToken); string newText = "\"" + sb.ToString().Replace("\n", "\\n") + "\""; return(document.WithText(sourceText.WithChanges(new TextChange(descriptionExp.FullSpan, newText)))); } sb.Return(); return(document); }
public override void Execute(GeneratorExecutionContext context) { var contentBuilder = SBPool.Rent(); Generate(context, contentBuilder); if (contentBuilder.Length == 0) { goto end; } string source = @$ "{Using}namespace {_CompileSettings.Namespace} {{ {contentBuilder} }}"; // Wht not? Cuz it's f*****g suffering that using SourceGenerator In NetFX context.AddSource(UniqueName + ".cs", SourceText.From(source, Encoding.UTF8)); end: SBPool.Return(contentBuilder); }
public void Execute(GeneratorExecutionContext context) { // retrieve the receiver if (context.SyntaxContextReceiver is not LocalizationSyntaxReceiver receiver) { return; } object addSourceLock = new(); List <(string hintName, SourceText sourceText)> sources = new(50); Parallel.ForEach(receiver.localizations, info => { var sb = SBPool.Rent(); sb.AppendLine("namespace " + info.Symbol.ContainingNamespace.ToDisplayString() + "{"); sb.AppendLine("partial class " + info.Symbol.Name + "{"); Build(sb, BuildType.Name, info); Build(sb, BuildType.Description, info); sb.AppendLine("}}"); // return sb soon as possible string rawSource = sb.ToStringAndReturn(); var hintName = $"{info.Symbol.ToDisplayString()}_LocalizableImpl.cs"; var sourceText = SourceText.From(rawSource, Encoding.UTF8); lock (addSourceLock) sources.Add((hintName, sourceText)); }); foreach (var(hintName, sourceText) in sources) { context.AddSource(hintName, sourceText); } }