FluentSyntaxRewriter 0.5.5

There is a newer version of this package available.
See the version list below for details.
dotnet add package FluentSyntaxRewriter --version 0.5.5                
NuGet\Install-Package FluentSyntaxRewriter -Version 0.5.5                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="FluentSyntaxRewriter" Version="0.5.5" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add FluentSyntaxRewriter --version 0.5.5                
#r "nuget: FluentSyntaxRewriter, 0.5.5"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install FluentSyntaxRewriter as a Cake Addin
#addin nuget:?package=FluentSyntaxRewriter&version=0.5.5

// Install FluentSyntaxRewriter as a Cake Tool
#tool nuget:?package=FluentSyntaxRewriter&version=0.5.5                

FluentSyntaxRewriter

NuGet Version Build Status GitHub Sponsors

A library to help you use the Roslyn APIs for analysing and rewriting the .NET programming language in a fluent way.

Minimum Requirements

  • Requires a platform with .NET Standard 2.0 or later, and Windows Vista+, Windows Server 2008+
    • Supported .NET Version: .NET Core 2.0+, .NET 5+, .NET Framework 4.6.1+, Mono 5.4+, UWP 10.0.16299+, Unity 2018.1+

How to use

Rename existing member method and add statements

var parsedCode = CSharpSyntaxTree.ParseText("""
namespace MyNamespace {
	using System;
	
	public struct MyExtension {
		public static int Hello() {
		}
		public static int Say() { return 1; }
	}
}
""").GetRoot();

var modifiedCode = FluentCSharpSyntaxRewriter.Define()
    .WithVisitMethodDeclaration((_, d) =>
    {
        if (d.GetContainingNamespace() != "MyNamespace")
            return d;
        if (d.GetContainingTypeName() != "MyExtension")
            return d;
        if (d.GetMemberName() != "Hello")
            return d;
        return d
            .RenameMember(s => $"Modified_{s}")
            .AddStatements("return 123;");
    })
    .RewriteCSharp(parsedCode);

Console.Out.WriteLine(modifiedCode);

Replace Identifier Token

var parsedCode = CSharpSyntaxTree.ParseText("""
/// <summary>
/// Indicates that a specific value entry should be deleted from the registry.
/// </summary>
public sealed class __TypeName__ {
	private static readonly Lazy<__TypeName__> _instanceOf = new Lazy<__TypeName__>(
		() => new __TypeName__(),
		LazyThreadSafetyMode.None);
	
	public static __TypeName__ Instance => _instanceOf.Value;
	
	internal __TypeName__() :
		base()
	{ }
}
""").GetRoot();

var modifiedCode = FluentCSharpSyntaxRewriter
	.Define()
	.WithVisitToken((_, token) =>
	{
		if (token.IsKind(SyntaxKind.IdentifierToken) &&
			string.Equals(token.ValueText, "__TypeName__", StringComparison.Ordinal))
			return SyntaxFactory.Identifier("AType").WithTriviaFrom(token);
		return token;
	})
	.Visit(parsedCode)
	.ToFullStringCSharp();

Console.Out.WriteLine(modifiedCode);

Add Using References, Rename Member

var namespaceCode = CSharpSyntaxTree.ParseText(
	"""
	namespace __ProjectNamespace__ {
	}
	""").GetRoot();

var modifiedNamespaceCode = FluentCSharpSyntaxRewriter
	.Define()
	.WithVisitNamespaceDeclaration((_, ns) =>
	{
	    ns = ns.AddUsings("System", "System.Collections.Generic").OrderUsings().DistinctUsings().RenameMember(_ => "TheProject");
	    ns = ns.AddMembers(modifiedClassCode);
	    return ns;
	})
	.Visit(namespaceCode)
	.ToFullStringCSharp();

Console.Out.WriteLine(modifiedNamespaceCode);

Add XML Documentation to Member

var field = SyntaxFactory.ParseMemberDeclaration(
	"""
	static int z = 0;
	""");

var code = field.AddXmlDocumentation(summary: "Test")
.ToFullStringCSharp();

Console.Out.WriteLine(code);

Get Compilation Unit

var s = CSharpSyntaxTree.ParseText(
	"""
	namespace A
	{
	    public class B
	    {
	    }
	}
	""");

var unit1 = s.GetCompilationUnitRoot();
var unit2 = await s.GetCompilationUnitSyntaxAsync();
var result = s.TryGetCompilationUnitSyntax(out var unit3);

Chained method calls

var typeName = "DeleteValueType";

var template = SyntaxFactory.ParseMemberDeclaration(
	"""
	public sealed class __TypeName__ {
		private static readonly Lazy<__TypeName__> _instanceOf = new Lazy<__TypeName__>(
			() => new __TypeName__(),
			LazyThreadSafetyMode.None);
				
		public static __TypeName__ Instance => _instanceOf.Value;
				
		internal __TypeName__() :
			base()
		{ }
	}
	""");

var code = FluentCSharpSyntaxRewriter
	.Define()
	.WithVisitToken((_, token) =>
	{
		if (token.IsKind(SyntaxKind.IdentifierToken) &&
			string.Equals(token.ValueText, "__TypeName__", StringComparison.Ordinal))
			return SyntaxFactory.Identifier(typeName).WithTriviaFrom(token);

		return token;
	})
	.WithVisitClassDeclaration((_, token) =>
	{
		token = FluentSyntaxRewriter.CSharpSyntaxTreeExtension.AddXmlDocumentation(token,
			summary: "Indicates that a specific value entry should be deleted from the registry.");
		return token;
	})
	.Visit(template)
	.ToFullStringCSharp();

Console.Out.WriteLine(code);

License

This library follows Apache-2.0 license. See LICENSE file for more information.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.5.7 112 6/17/2024
0.5.6 93 6/17/2024
0.5.5 96 6/16/2024
0.5.4 96 6/14/2024
0.5.3 92 6/14/2024
0.5.2 96 6/14/2024
0.5.1 89 6/13/2024
0.5.0 94 6/13/2024