AutoSource 1.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package AutoSource --version 1.0.0                
NuGet\Install-Package AutoSource -Version 1.0.0                
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="AutoSource" Version="1.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add AutoSource --version 1.0.0                
#r "nuget: AutoSource, 1.0.0"                
#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 AutoSource as a Cake Addin
#addin nuget:?package=AutoSource&version=1.0.0

// Install AutoSource as a Cake Tool
#tool nuget:?package=AutoSource&version=1.0.0                

AutoSource

Build Status NuGet Status

NuGet packages

https://nuget.org/packages/AutoSource/

Files included

<details> <summary><a href="https://github.com/distantcam/AutoSource/blob/main/src/AutoSource/Source/CodeBuilder.cs">CodeBuilder.cs</a></summary>

using global::Microsoft.CodeAnalysis;
using global::Microsoft.CodeAnalysis.Text;
using global::System.Text;

internal class CodeBuilder
{
    private readonly StringBuilder _stringBuilder = new();
    private int _indent = 0;

    public CodeBuilder AppendLine()
    {
        _stringBuilder.AppendLine();
        return this;
    }
    public CodeBuilder AppendLine(string line)
    {
        _stringBuilder.AppendLine(Indent + line);
        return this;
    }

    public CodeBuilder StartBlock()
    {
        AppendLine("{");
        _indent++;
        return this;
    }
    public CodeBuilder EndBlock()
    {
        _indent--;
        AppendLine("}");
        return this;
    }

    public char IndentChar { get; set; } = '\t';
    public string Indent => new string(IndentChar, _indent);

    public IDisposable StartPartialType(ITypeSymbol type)
    {
        var ns = type.ContainingNamespace.IsGlobalNamespace
                ? null
                : type.ContainingNamespace.ToString();
        var typeKeyword = type.IsRecord
            ? "record"
            : type.IsValueType
                ? "struct"
                : "class";

        if (!string.IsNullOrEmpty(ns))
        {
            AppendLine($"namespace {ns}");
            StartBlock();
        }

        var typeStack = new Stack<string>();
        var containingType = type.ContainingType;
        while (containingType is not null)
        {
            var contTypeKeyword = containingType.IsRecord
                ? "record"
                : containingType.IsValueType
                    ? "struct"
                    : "class";
            var typeName = containingType.ToDisplayString(SymbolDisplayFormat.MinimallyQualifiedFormat);
            typeStack.Push(contTypeKeyword + " " + typeName);
            containingType = containingType.ContainingType;
        }

        var nestedCount = typeStack.Count;
        while (typeStack.Count > 0)
        {
            AppendLine($"partial {typeStack.Pop()}");
            StartBlock();
        }

        AppendLine($"partial {typeKeyword} {type.ToDisplayString(SymbolDisplayFormat.MinimallyQualifiedFormat)}");
        StartBlock();

        return new CloseBlock(this, 1 + nestedCount + (ns != null ? 1 : 0));
    }

    public static implicit operator SourceText(CodeBuilder codeBuilder)
        => SourceText.From(codeBuilder._stringBuilder.ToString(), Encoding.UTF8);

    private readonly struct CloseBlock : IDisposable
    {
        private readonly CodeBuilder _codeBuilder;
        private readonly int _count;
        public CloseBlock(CodeBuilder codeBuilder, int count) { _codeBuilder = codeBuilder; _count = count; }
        public void Dispose() { for (var i = 0; i < _count; i++) _codeBuilder.EndBlock(); }
    }
}

</details>

<details> <summary><a href="https://github.com/distantcam/AutoSource/blob/main/src/AutoSource/Source/SourceDisplayFormats.cs">SourceDisplayFormats.cs</a></summary>

using global::Microsoft.CodeAnalysis;

internal static class SourceDisplayFormats
{
    public static readonly SymbolDisplayFormat FullyQualifiedParameterFormat = SymbolDisplayFormat.FullyQualifiedFormat
        .WithParameterOptions(
            SymbolDisplayParameterOptions.IncludeName |
            SymbolDisplayParameterOptions.IncludeType |
            SymbolDisplayParameterOptions.IncludeParamsRefOut
        );
}

</details>

<details> <summary><a href="https://github.com/distantcam/AutoSource/blob/main/src/AutoSource/Source/SourceTools.cs">SourceTools.cs</a></summary>

using global::Microsoft.CodeAnalysis;
using global::Microsoft.CodeAnalysis.CSharp.Syntax;

internal static class SourceTools
{
    public static bool IsCorrectAttribute(string attributeName, SyntaxNode syntaxNode, CancellationToken cancellationToken)
    {
        if (syntaxNode is not AttributeSyntax attribute) return false;
        var name = attribute.Name switch
        {
            SimpleNameSyntax ins => ins.Identifier.Text,
            QualifiedNameSyntax qns => qns.Right.Identifier.Text,
            _ => null
        };
        return name == attributeName || name == attributeName + "Attribute";
    }

    public static IMethodSymbol? GetMethodFromAttribute(GeneratorSyntaxContext context, CancellationToken cancellationToken)
    {
        var attributeSyntax = (AttributeSyntax)context.Node;
        if (attributeSyntax.Parent?.Parent is not MethodDeclarationSyntax methodNode) return null;
        if (context.SemanticModel.GetDeclaredSymbol(methodNode) is not IMethodSymbol method) return null;
        return method;
    }

    public static ITypeSymbol? GetTypeFromAttribute(GeneratorSyntaxContext context, CancellationToken cancellationToken)
    {
        var attributeSyntax = (AttributeSyntax)context.Node;

        // "attribute.Parent" is "AttributeListSyntax"
        // "attribute.Parent.Parent" is a C# fragment the attributes are applied to
        TypeDeclarationSyntax? typeNode = attributeSyntax.Parent?.Parent switch
        {
            ClassDeclarationSyntax classDeclarationSyntax => classDeclarationSyntax,
            RecordDeclarationSyntax recordDeclarationSyntax => recordDeclarationSyntax,
            StructDeclarationSyntax structDeclarationSyntax => structDeclarationSyntax,
            _ => null
        };

        if (typeNode == null) return null;
        if (context.SemanticModel.GetDeclaredSymbol(typeNode) is not ITypeSymbol type) return null;
        return type;
    }
}

</details>

There are no supported framework assets in this package.

Learn more about Target Frameworks and .NET Standard.

  • .NETStandard 2.0

    • No dependencies.

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
1.4.3 254 8/28/2023
1.4.2 113 8/28/2023
1.4.1 109 8/27/2023
1.4.0 120 8/27/2023
1.3.0 135 8/12/2023
1.2.1 196 7/9/2023
1.2.0 146 7/8/2023
1.1.0 136 7/8/2023
1.0.1 139 7/8/2023
1.0.0 137 7/8/2023