SOURCE DUMP v1

This commit is contained in:
Rian Drake 2026-10-15 14:09:11 +13:00
commit fb20299e9d
3 changed files with 768360 additions and 0 deletions

View file

@ -0,0 +1,74 @@
using System.Collections.Immutable;
using System.Composition;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
namespace Brutal.Strings.Analyzers;
[ExportCodeFixProvider(LanguageNames.CSharp, Name = nameof(PreferString8OverloadFixProvider)), Shared]
public class PreferString8OverloadFixProvider : CodeFixProvider
{
private static readonly string DiagnosticId = Resources.PreferU8OverloadBrutalCore0011.Id;
private const string Suggestion = Resources.PreferU8OverloadBrutalCore0011Suggestion;
public sealed override ImmutableArray<string> FixableDiagnosticIds { get; } = [DiagnosticId];
public override FixAllProvider GetFixAllProvider() => WellKnownFixAllProviders.BatchFixer;
public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
{
// We link only one diagnostic and assume there is only one diagnostic in the context.
var diagnostic = context.Diagnostics.Single();
// 'SourceSpan' of 'Location' is the highlighted area. We're going to use this area to find the 'SyntaxNode' to rename.
var diagnosticSpan = diagnostic.Location.SourceSpan;
// Get the root of Syntax Tree that contains the highlighted diagnostic.
var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);
// Find SyntaxNode corresponding to the diagnostic.
var diagnosticNode = root?.FindNode(diagnosticSpan);
// We expect a string literal (argument or conversion)
var literal = diagnosticNode switch
{
ArgumentSyntax { Expression: LiteralExpressionSyntax literalArg } => literalArg,
LiteralExpressionSyntax literalConversion => literalConversion,
_ => null,
};
if (literal == null)
return;
// Register a code action that will invoke the fix.
context.RegisterCodeFix(
CodeAction.Create(
title: Suggestion,
createChangedSolution: c => FixAsync(context.Document, literal, c),
equivalenceKey: nameof(DiagnosticId)
),
diagnostic
);
}
private static async Task<Solution> FixAsync(
Document document,
LiteralExpressionSyntax stringLiteral,
CancellationToken cancellationToken
)
{
var root = await document.GetSyntaxRootAsync(cancellationToken);
if (root == null)
return document.Project.Solution;
// change from "literal" to "literal"u8
var token = stringLiteral.Token;
var u8Token = SyntaxFactoryExtensions.Utf8Literal(token.Text + "u8", token.ValueText);
var u8Literal = SyntaxFactory.LiteralExpression(SyntaxKind.Utf8StringLiteralExpression, u8Token);
var newRoot = root.ReplaceNode(stringLiteral, u8Literal);
var newDocument = document.WithSyntaxRoot(newRoot);
return newDocument.Project.Solution;
}
}

View file

@ -0,0 +1,67 @@
using System.Collections.Immutable;
using System.Composition;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.CSharp;
namespace Brutal.Strings.Analyzers;
[ExportCodeFixProvider(LanguageNames.CSharp, Name = nameof(SpanToStringFixProvider)), Shared]
public class SpanToStringFixProvider : CodeFixProvider
{
// Specify the diagnostic IDs of analyzers that are expected to be linked.
public sealed override ImmutableArray<string> FixableDiagnosticIds { get; } = [Resources.BrutalCore0001.Id];
// If you don't need the 'fix all' behaviour, return null.
public override FixAllProvider? GetFixAllProvider() => null;
public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
{
// We link only one diagnostic and assume there is only one diagnostic in the context.
var diagnostic = context.Diagnostics.Single();
// 'SourceSpan' of 'Location' is the highlighted area. We're going to use this area to find the 'SyntaxNode' to rename.
var diagnosticSpan = diagnostic.Location.SourceSpan;
// Get the root of Syntax Tree that contains the highlighted diagnostic.
var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);
// Find SyntaxNode corresponding to the diagnostic.
var diagnosticNode = root?.FindNode(diagnosticSpan)!;
// Register a code action that will invoke the fix.
context.RegisterCodeFix(
CodeAction.Create(
title: Resources.BrutalCore0001Suggestion,
createChangedSolution: c => FixToStringAsync(context.Document, diagnosticNode, c),
equivalenceKey: nameof(Resources.BrutalCore0001.Id)
),
diagnostic
);
}
/// <summary>
/// Executed on the quick fix action raised by the user.
/// </summary>
/// <param name="document">Affected source file.</param>
/// <param name="methodName">The syntax node that we found in 'RegisterCodeFixesAsync'.</param>
/// <param name="cancellationToken">Any fix is cancellable by the user, so we should support the cancellation token.</param>
/// <returns>Clone of the solution with updates: renamed class.</returns>
private static async Task<Solution> FixToStringAsync(
Document document,
SyntaxNode methodName,
CancellationToken cancellationToken
)
{
// change from "ReadOnlySpan<byte>.ToString()" to "ReadOnlySpan<byte>.ToString16()"
var root = await document.GetSyntaxRootAsync(cancellationToken);
if (root == null)
return document.Project.Solution;
var newName = SyntaxFactory.IdentifierName("ToString16");
var newRoot = root.ReplaceNode(methodName, newName);
var newDocument = document.WithSyntaxRoot(newRoot);
return newDocument.Project.Solution;
}
}

View file

@ -0,0 +1,26 @@
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
namespace Brutal.Strings.Analyzers;
using static SyntaxFactory;
// TODO: move this to a shared package...
public static class SyntaxFactoryExtensions
{
/// <inheritdoc cref="Utf8Literal(string,string)" />
public static SyntaxToken Utf8Literal(string value)
{
return Utf8Literal(SymbolDisplay.FormatLiteral(value, quote: true) + "u8", value);
}
/// <summary>
/// Creates a token with kind Utf8StringLiteralToken from the text and corresponding string value.
/// </summary>
/// <param name="text">The raw text of the literal, including quotes and escape sequences.</param>
/// <param name="value">The string value to be represented by the returned token.</param>
public static SyntaxToken Utf8Literal(string text, string value)
{
return Token(SyntaxTriviaList.Empty, SyntaxKind.Utf8StringLiteralToken, text, value, SyntaxTriviaList.Empty);
}
}