Skip to content

Commit

Permalink
Merge pull request #132 from HamletTanyavong/dev
Browse files Browse the repository at this point in the history
Rename ISymbol to IIndexName
  • Loading branch information
HamletTanyavong authored Aug 27, 2024
2 parents 1e3462a + 1b445ee commit cf55f5c
Show file tree
Hide file tree
Showing 73 changed files with 650 additions and 688 deletions.
35 changes: 35 additions & 0 deletions docs/guide/diagnostic_messages/difgeo/dg0001.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# DG0001: Invalid Index Name Declaration

## Cause

An index name was defined outside the scope of a namepace.

## How to Fix Violations

Place the index name in a namespace.

## Example

The following violates the rule:
```csharp
using Mathematics.NET.Core.Attributes;

[IndexName] public partial struct Alpha;
```
The following are valid:
```csharp
using Mathematics.NET.Core.Attributes;

namespace DG0001;

[IndexName] public partial struct Alpha;
```
and
```csharp
using Mathematics.NET.Core.Attributes;

namespace DG0001
{
[IndexName] public partial struct Alpha;
}
```
35 changes: 0 additions & 35 deletions docs/guide/diagnostic_messages/symbols/sgs0001.md

This file was deleted.

6 changes: 3 additions & 3 deletions docs/guide/toc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
items:
- name: Mathematics.NET Diagnostic Messages
href: diagnostic_messages/diagnostic-messages.md
- name: Symbols Diagnostic Messages
- name: Differential Geometry
items:
- name: SGS0001
href: diagnostic_messages/symbols/sgs0001.md
- name: DG0001
href: diagnostic_messages/difgeo/dg0001.md
2 changes: 2 additions & 0 deletions docs/template/public/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ code {
#interactive-card > .flare,
#overlay {
position: absolute;
transition: ease-out;
transition-duration: 250ms;
opacity: 0;
pointer-events: none;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// <copyright file="DiagnosticMessage.cs" company="Mathematics.NET">
// <copyright file="DiagnosticMessages.cs" company="Mathematics.NET">
// Mathematics.NET
// https://github.com/HamletTanyavong/Mathematics.NET
//
Expand Down Expand Up @@ -28,17 +28,17 @@
namespace Mathematics.NET.SourceGenerators.Public;

/// <summary>A class for creating diagnostic messages for use in Mathematics.NET source generators.</summary>
internal static class DiagnosticMessage
internal static class DiagnosticMessages
{
public static DiagnosticDescriptor CreateInvalidSymbolDeclarationDiagnosticDescriptor()
public static DiagnosticDescriptor CreateInvalidIndexNameDeclarationDiagnosticDescriptor()
{
return new DiagnosticDescriptor(
id: "SGS0001",
title: "Invalid symbol declaration",
messageFormat: "Symbols must be declared in namespaces.",
category: "Symbols",
id: "DG0001",
title: "Invalid index name declaration.",
messageFormat: "Index names must be declared in namespaces.",
category: "DifGeo",
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true,
helpLinkUri: "https://mathematics.hamlettanyavong.com/guide/diagnostic_messages/symbols/sgs0001.html");
helpLinkUri: "https://mathematics.hamlettanyavong.com/guide/diagnostic_messages/difgeo/dg0001.html");
}
}
8 changes: 0 additions & 8 deletions src/Mathematics.NET.SourceGenerators.Public/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,10 @@ public static NameSyntax CreateNameSyntaxFromNamespace(this string namespaceName
public static NameSyntax? GetNamespaceNameSyntaxFromStructOrDefault(this StructDeclarationSyntax structDeclaration)
{
if (structDeclaration.Parent is FileScopedNamespaceDeclarationSyntax fileScopedNamespaceDeclaration)
{
return fileScopedNamespaceDeclaration.Name;
}

if (structDeclaration.Parent is NamespaceDeclarationSyntax namespaceDeclaration)
{
return namespaceDeclaration.Name.WithoutTrailingTrivia();
}

return default;
}
Expand All @@ -93,14 +89,10 @@ public static NameSyntax CreateNameSyntaxFromNamespace(this string namespaceName
public static string? GetNameValueOrDefault(this NameSyntax? name)
{
if (name is null)
{
return default;
}

if (name is SimpleNameSyntax simpleName)
{
return simpleName.Identifier.Text;
}

if (name is QualifiedNameSyntax qualifiedName)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// <copyright file="SymbolBuilder.cs" company="Mathematics.NET">
// <copyright file="IndexNameBuilder.cs" company="Mathematics.NET">
// Mathematics.NET
// https://github.com/HamletTanyavong/Mathematics.NET
//
Expand Down Expand Up @@ -30,19 +30,17 @@
using Microsoft.CodeAnalysis.CSharp;
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;

namespace Mathematics.NET.SourceGenerators.Public.Symbols;
namespace Mathematics.NET.SourceGenerators.Public.IndexNames;

/// <summary>Symbols builder.</summary>
internal sealed class SymbolBuilder
/// <summary>Index name builder.</summary>
internal sealed class IndexNameBuilder
{
private readonly NameSyntax _namespaceNameSyntax;
private readonly SourceProductionContext _context;
private readonly ImmutableArray<StructInformation> _structInformationArray;

public SymbolBuilder(NameSyntax namespaceNameSyntax, SourceProductionContext context, ImmutableArray<StructInformation> structInformationArray)
public IndexNameBuilder(NameSyntax namespaceNameSyntax, ImmutableArray<StructInformation> structInformationArray)
{
_namespaceNameSyntax = namespaceNameSyntax;
_context = context;
_structInformationArray = structInformationArray;
}

Expand All @@ -61,7 +59,7 @@ private CompilationUnitSyntax CreateCompilationUnit(ImmutableArray<MemberDeclara
return CompilationUnit()
.WithUsings(
List([
UsingDirective("Mathematics.NET.Symbols".CreateNameSyntaxFromNamespace())
UsingDirective("Mathematics.NET.DifferentialGeometry".CreateNameSyntaxFromNamespace())
.WithUsingKeyword(
Token(
TriviaList(
Expand Down Expand Up @@ -101,7 +99,7 @@ private ImmutableArray<MemberDeclarationSyntax> GenerateMembers()
List<MemberDeclarationSyntax> members = [];
for (int i = 0; i < _structInformationArray.Length; i++)
{
members.Add(GenerateSymbol(_structInformationArray[i].StructDeclarationSyntax, i == _structInformationArray.Length - 1));
members.Add(GenerateIndexName(_structInformationArray[i].StructDeclarationSyntax, i == _structInformationArray.Length - 1));
}
return members.ToImmutableArray();
}
Expand All @@ -110,13 +108,13 @@ private ImmutableArray<MemberDeclarationSyntax> GenerateMembers()
// Helpers
//

private StructDeclarationSyntax GenerateSymbol(StructDeclarationSyntax structDeclaration, bool isLastSymbol)
private StructDeclarationSyntax GenerateIndexName(StructDeclarationSyntax structDeclaration, bool isLastIndex)
{
var symbolName = structDeclaration.Identifier.Text;
var indexName = structDeclaration.Identifier.Text;
return StructDeclaration(
Identifier(
TriviaList(),
symbolName,
indexName,
TriviaList(Space)))
.WithKeyword(
Token(
Expand Down Expand Up @@ -144,21 +142,21 @@ private StructDeclarationSyntax GenerateSymbol(StructDeclarationSyntax structDec
IdentifierName(
Identifier(
TriviaList(),
"ISymbol",
"IIndexName",
TriviaList(CarriageReturnLineFeed))))))
.WithColonToken(
Token(
TriviaList(),
SyntaxKind.ColonToken,
TriviaList(Space))))
.WithOpenAndCloseBraceTokens(string.Empty, !isLastSymbol)
.WithOpenAndCloseBraceTokens(string.Empty, !isLastIndex)
.WithMembers(
List(new MemberDeclarationSyntax[] {
GenerateDisplayStringField(symbolName),
GenerateDisplayStringField(indexName),
GenerateDisplayStringProperty() }));
}

private FieldDeclarationSyntax GenerateDisplayStringField(string symbolName)
private FieldDeclarationSyntax GenerateDisplayStringField(string indexName)
{
return FieldDeclaration(
VariableDeclaration(
Expand All @@ -178,7 +176,7 @@ private FieldDeclarationSyntax GenerateDisplayStringField(string symbolName)
EqualsValueClause(
LiteralExpression(
SyntaxKind.StringLiteralExpression,
Literal(symbolName)))
Literal(indexName)))
.WithEqualsToken(
Token(
TriviaList(),
Expand All @@ -202,7 +200,6 @@ private FieldDeclarationSyntax GenerateDisplayStringField(string symbolName)
TriviaList(),
SyntaxKind.SemicolonToken,
TriviaList(
CarriageReturnLineFeed,
CarriageReturnLineFeed)));
}

Expand All @@ -227,7 +224,7 @@ private PropertyDeclarationSyntax GenerateDisplayStringProperty()
TriviaList(Space))))
.WithExplicitInterfaceSpecifier(
ExplicitInterfaceSpecifier(
IdentifierName("ISymbol")))
IdentifierName("IIndexName")))
.WithExpressionBody(
ArrowExpressionClause(
IdentifierName("DisplayString"))
Expand Down Expand Up @@ -265,7 +262,7 @@ private DocumentationCommentTriviaSyntax GenerateDocumentationComment()
SingletonList<XmlAttributeSyntax>(
XmlCrefAttribute(
QualifiedCref(
IdentifierName("ISymbol"),
IdentifierName("IIndexName"),
NameMemberCref(
IdentifierName("DisplayString")))))),
XmlText()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// <copyright file="SymbolGenerator.cs" company="Mathematics.NET">
// <copyright file="IndexNameGenerator.cs" company="Mathematics.NET">
// Mathematics.NET
// https://github.com/HamletTanyavong/Mathematics.NET
//
Expand Down Expand Up @@ -29,23 +29,23 @@
using System.Text;
using Mathematics.NET.SourceGenerators.Public.Models;

namespace Mathematics.NET.SourceGenerators.Public.Symbols;
namespace Mathematics.NET.SourceGenerators.Public.IndexNames;

/// <summary>A generator for mathematical symbols.</summary>
/// <summary>A generator for index names.</summary>
[Generator]
public sealed class SymbolGenerator : IIncrementalGenerator
public sealed class IndexNameGenerator : IIncrementalGenerator
{
public void Initialize(IncrementalGeneratorInitializationContext context)
{
var provider = context.SyntaxProvider
.CreateSyntaxProvider(CouldBeSymbolAttribute, GetStructInformationOrNull)
.CreateSyntaxProvider(CouldBeIndexNameAttribute, GetStructInformationOrNull)
.Where(x => x is not null);
var compilation = context.CompilationProvider.Combine(provider.Collect());
context.RegisterSourceOutput(compilation, (context, source) => GenerateCode(context, source.Right));
}

private static bool CouldBeSymbolAttribute(SyntaxNode syntaxNode, CancellationToken token)
=> syntaxNode is AttributeSyntax attributeSyntax && attributeSyntax.Name.GetLastIdentifierNameValueOrDefault() is "Symbol" or "SymbolAttribute";
private static bool CouldBeIndexNameAttribute(SyntaxNode syntaxNode, CancellationToken token)
=> syntaxNode is AttributeSyntax attributeSyntax && attributeSyntax.Name.GetLastIdentifierNameValueOrDefault() is "IndexName" or "IndexNameAttribute";

private static StructInformation GetStructInformationOrNull(GeneratorSyntaxContext context, CancellationToken token)
{
Expand All @@ -70,13 +70,13 @@ private void GenerateCode(SourceProductionContext context, ImmutableArray<Struct
foreach (var info in selectedInformation)
{
context.ReportDiagnostic(Diagnostic.Create(
DiagnosticMessage.CreateInvalidSymbolDeclarationDiagnosticDescriptor(),
DiagnosticMessages.CreateInvalidIndexNameDeclarationDiagnosticDescriptor(),
info.StructDeclarationSyntax.Identifier.GetLocation()));
}
continue;
}
var symbols = new SymbolBuilder(nameSyntax, context, selectedInformation);
context.AddSource($"Symbols.{nameSyntax.GetNameValueOrDefault()}.g.cs", symbols.GenerateSource().GetText(Encoding.UTF8).ToString());
var indexNames = new IndexNameBuilder(nameSyntax, selectedInformation);
context.AddSource($"IndexNames.{nameSyntax.GetNameValueOrDefault()}.g.cs", indexNames.GenerateSource().GetText(Encoding.UTF8).ToString());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,17 @@

<ItemGroup>
<None Include="$(OutputPath)\$(AssemblyName).dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" />
<None Include="..\..\docs\images\logo\mathematics.net.png">
<Pack>True</Pack>
<PackagePath>\</PackagePath>
</None>
<None Include="..\..\LICENSE">
<Pack>True</Pack>
<PackagePath>\</PackagePath>
</None>
<None Update="README.md">
<Pack>True</Pack>
<PackagePath>\</PackagePath>
</None>
<None Include="..\..\docs\images\logo\mathematics.net.png" Pack="true" PackagePath="\" />
<None Include="..\..\LICENSE" Pack="true" PackagePath="\" />
<None Update="README.md" Pack="true" PackagePath="\" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.4">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.10.0" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.11.0" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

namespace Mathematics.NET.SourceGenerators.Public.Models;

/// <summary>A class containing information about a symbol struct.</summary>
/// <summary>A class containing information about an index name struct.</summary>
internal sealed class StructInformation
{
public StructInformation(NameSyntax? namespaceNameSyntax, AttributeSyntax attributeSyntax, StructDeclarationSyntax structDeclarationSyntax)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@ internal sealed class NameSyntaxComparer : IEqualityComparer<NameSyntax?>
public bool Equals(NameSyntax? x, NameSyntax? y)
{
if (x is not null && y is not null)
{
return x.GetNameValueOrDefault() == y.GetNameValueOrDefault();
}
return x == y;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// <copyright file="DiagnosticMessage.cs" company="Mathematics.NET">
// <copyright file="DiagnosticMessages.cs" company="Mathematics.NET">
// Mathematics.NET
// https://github.com/HamletTanyavong/Mathematics.NET
//
Expand Down Expand Up @@ -29,7 +29,7 @@

namespace Mathematics.NET.SourceGenerators;

internal static class DiagnosticMessage
internal static class DiagnosticMessages
{
//
// Differential Geometry
Expand Down
Loading

0 comments on commit cf55f5c

Please sign in to comment.