From 39bfd0f99d118efb8e7b97d7ae241ec04499fdd8 Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 27 Aug 2026 04:47:10 +0000 Subject: [PATCH 1/2] refactor: emit through ktsu.CodeBlocker.Templates [minor] The C# syntax template model moved to ktsu.CodeBlocker 1.3.0, so the local copy under Templates/ goes away and the generators describe their output with the package's model instead. The migration is mostly mechanical - the collection properties are read-only now, so object initializers use collection-initializer syntax, and a type's declaration keyword comes from ClassTemplate.Kind rather than from free text in Keywords. WithComments and WithInterfaces cover the few call sites that hold a prebuilt sequence, which a read-only property cannot be assigned. TypeKind needs a using alias because Microsoft.CodeAnalysis declares one too. One substantive change: every BodyFactory used to write a leading space (" => Create(...)") because the old model spliced the body raw with no separator. The package model supplies that separator itself, so the leading space became a double space in the output and is removed at all 16 sites. The committed output under Semantics.Quantities/Generated/ changes across 181 files, and all of it is the model doing its job: -internal static class ConversionConstants{ +internal static class ConversionConstants +{ Types now open their brace on its own line and close with "}" instead of "};", members are separated by a blank line, and doc comments sit at the member's indent instead of at column zero - the previous output had every "/// " flush against the left margin directly after the preceding member. PropertyTemplateTests is deleted rather than ported: it covered the local PropertyTemplate, and ktsu.CodeBlocker now carries far better coverage of the same behaviour, including the accessor cases this model could not express. WriteFileHeader and WriteSourceFile go back to protected - they were narrowed to private protected only because SourceFileTemplate was local, and it is public in the package. Test suite: 1015 passed, 92 failed - the same 92 Windows-path tests that fail on main under Linux. The -4 against the previous run is exactly the deleted PropertyTemplateTests. Closes #187 Co-Authored-By: Claude Opus 5 Claude-Session: https://claude.ai/code/session_015sagQjzFv3cnFNM3G271TJ --- CLAUDE.md | 4 +- Directory.Packages.props | 2 +- .../ConversionConstants.g.cs | 6 +- .../PhysicalDimensions.g.cs | 226 ++++--- .../MetricMagnitudes.g.cs | 6 +- .../PhysicalConstants.g.cs | 418 +++++++------ .../StorageTypes.g.cs | 6 +- .../AbsorbedDose.g.cs | 14 +- .../Acceleration1D.g.cs | 23 +- .../AccelerationMagnitude.g.cs | 38 +- .../AcousticImpedance.g.cs | 11 +- .../ActivationEnergy.g.cs | 26 +- .../Admittance.g.cs | 20 +- .../Airspeed.g.cs | 32 +- .../Altitude.g.cs | 53 +- .../AmountOfSubstance.g.cs | 53 +- .../Angle.g.cs | 41 +- .../AngularAcceleration1D.g.cs | 20 +- .../AngularAccelerationMagnitude.g.cs | 35 +- .../AngularJerk1D.g.cs | 14 +- .../AngularJerkMagnitude.g.cs | 17 +- .../AngularMomentum1D.g.cs | 14 +- .../AngularMomentumMagnitude.g.cs | 29 +- .../AngularSpeed.g.cs | 38 +- .../AngularVelocity1D.g.cs | 23 +- .../ApertureAngle.g.cs | 32 +- .../Area.g.cs | 80 ++- .../AtmosphericPressure.g.cs | 35 +- .../AtomicMass.g.cs | 41 +- .../Bandwidth.g.cs | 26 +- .../Bearing.g.cs | 26 +- .../BulkModulus.g.cs | 35 +- .../Capacitance.g.cs | 26 +- .../Capacity.g.cs | 50 +- .../CatalyticActivity.g.cs | 20 +- .../Charge.g.cs | 17 +- .../ChargeMagnitude.g.cs | 38 +- .../ClockSpeed.g.cs | 26 +- .../Concentration.g.cs | 38 +- .../Conductance.g.cs | 17 +- .../CrossSectionalArea.g.cs | 41 +- .../Current1D.g.cs | 26 +- .../CurrentMagnitude.g.cs | 53 +- .../DecayTime.g.cs | 44 +- .../Density.g.cs | 29 +- .../Depth.g.cs | 53 +- .../Diameter.g.cs | 59 +- .../Displacement1D.g.cs | 47 +- .../Distance.g.cs | 53 +- .../Drag.g.cs | 29 +- .../Duration.g.cs | 311 ++++++---- .../DynamicViscosity.g.cs | 23 +- .../EMF.g.cs | 23 +- .../ElectricConductivity.g.cs | 11 +- .../ElectricField1D.g.cs | 14 +- .../ElectricFieldMagnitude.g.cs | 23 +- .../ElectricFlux.g.cs | 23 +- .../ElectricPowerDensity.g.cs | 17 +- .../Energy.g.cs | 107 ++-- .../EnergyFluxDensity.g.cs | 20 +- .../Entropy.g.cs | 29 +- .../EnzymeActivity.g.cs | 23 +- .../EquivalentDose.g.cs | 14 +- .../Exposure.g.cs | 14 +- .../FieldOfView.g.cs | 32 +- .../FlowSpeed.g.cs | 32 +- .../Force1D.g.cs | 23 +- .../ForceMagnitude.g.cs | 68 ++- .../Frequency.g.cs | 29 +- .../Friction.g.cs | 29 +- .../Gain.g.cs | 47 +- .../GaugePressure.g.cs | 35 +- .../GravitationalAcceleration.g.cs | 23 +- .../GroundSpeed.g.cs | 32 +- .../GroupVelocity.g.cs | 32 +- .../HalfLife.g.cs | 44 +- .../Heading.g.cs | 26 +- .../Heat.g.cs | 44 +- .../HeatCapacity.g.cs | 20 +- .../HeatFlowRate.g.cs | 29 +- .../HeatFlux.g.cs | 20 +- .../HeatTransferCoefficient.g.cs | 11 +- .../Height.g.cs | 53 +- .../Illuminance.g.cs | 20 +- .../Impedance.g.cs | 26 +- .../Inductance.g.cs | 17 +- .../Irradiance.g.cs | 17 +- .../Jerk1D.g.cs | 20 +- .../JerkMagnitude.g.cs | 29 +- .../KinematicViscosity.g.cs | 20 +- .../KineticEnergy.g.cs | 44 +- .../Latency.g.cs | 44 +- .../Length.g.cs | 98 ++- .../Lift.g.cs | 29 +- .../Loudness.g.cs | 11 +- .../Luminance.g.cs | 23 +- .../LuminousFlux.g.cs | 23 +- .../LuminousIntensity.g.cs | 26 +- .../MachNumber.g.cs | 47 +- .../MagneticFlux.g.cs | 41 +- .../MagneticFluxDensityMagnitude.g.cs | 20 +- .../Mass.g.cs | 86 ++- .../MassFlowRate.g.cs | 17 +- .../MolarEnergy.g.cs | 23 +- .../MolarEnthalpy.g.cs | 26 +- .../MolarMass.g.cs | 23 +- .../MomentOfInertia.g.cs | 23 +- .../Momentum1D.g.cs | 14 +- .../MomentumMagnitude.g.cs | 35 +- .../NoiseReductionCoefficient.g.cs | 47 +- .../NormalForce.g.cs | 29 +- .../NuclearCrossSection.g.cs | 14 +- .../Offset.g.cs | 47 +- .../OpticalPower.g.cs | 11 +- .../Perimeter.g.cs | 53 +- .../Period.g.cs | 44 +- .../Permittivity.g.cs | 11 +- .../Phase.g.cs | 26 +- .../PhaseVelocity.g.cs | 32 +- .../Pitch.g.cs | 26 +- .../PotentialEnergy.g.cs | 44 +- .../Power.g.cs | 74 ++- .../Pressure.g.cs | 44 +- .../RadioactiveActivity.g.cs | 14 +- .../Radius.g.cs | 53 +- .../RateConstant.g.cs | 11 +- .../Ratio.g.cs | 74 ++- .../ReactionRate.g.cs | 17 +- .../ReflectionCoefficient.g.cs | 41 +- .../RefractiveIndex.g.cs | 47 +- .../Resistance.g.cs | 29 +- .../ReverberationTime.g.cs | 44 +- .../ReynoldsNumber.g.cs | 47 +- .../Rotation.g.cs | 26 +- .../SamplingRate.g.cs | 26 +- .../Sensitivity.g.cs | 17 +- .../Sharpness.g.cs | 11 +- .../ShearModulus.g.cs | 35 +- .../SignedAngle.g.cs | 26 +- .../SignedRatio.g.cs | 47 +- .../Snap1D.g.cs | 14 +- .../SnapMagnitude.g.cs | 17 +- .../SoundAbsorption.g.cs | 47 +- .../SoundIntensity.g.cs | 20 +- .../SoundPower.g.cs | 29 +- .../SoundPressure.g.cs | 35 +- .../SoundSpeed.g.cs | 32 +- .../SoundTransmissionClass.g.cs | 47 +- .../SpecificEntropy.g.cs | 20 +- .../SpecificGravity.g.cs | 47 +- .../SpecificHeat.g.cs | 17 +- .../Speed.g.cs | 65 +- .../Stress.g.cs | 35 +- .../SurfaceArea.g.cs | 41 +- .../SurfaceTension.g.cs | 20 +- .../Temperature.g.cs | 32 +- .../TemperatureDelta.g.cs | 17 +- .../TemperatureDrop.g.cs | 23 +- .../TemperatureRise.g.cs | 23 +- .../Tension.g.cs | 29 +- .../ThermalConductivity.g.cs | 11 +- .../ThermalDiffusivity.g.cs | 23 +- .../ThermalEnergy.g.cs | 44 +- .../ThermalExpansionCoefficient.g.cs | 17 +- .../ThermalResistance.g.cs | 11 +- .../Thickness.g.cs | 53 +- .../Thrust.g.cs | 29 +- .../TimeConstant.g.cs | 44 +- .../Torque1D.g.cs | 17 +- .../TorqueMagnitude.g.cs | 38 +- .../Velocity1D.g.cs | 32 +- .../Voltage.g.cs | 23 +- .../VoltageDrop.g.cs | 23 +- .../VoltageMagnitude.g.cs | 74 ++- .../Volume.g.cs | 89 ++- .../VolumetricFlowRate.g.cs | 20 +- .../Wavelength.g.cs | 53 +- .../Weight.g.cs | 29 +- .../Width.g.cs | 53 +- .../WindSpeed.g.cs | 32 +- .../Work.g.cs | 44 +- .../YoungsModulus.g.cs | 35 +- .../Units.g.cs | 573 ++++++------------ .../CodeGen/GeneratorBase.cs | 11 +- .../Generators/ConversionsGenerator.cs | 21 +- .../Generators/DimensionsGenerator.cs | 57 +- .../Generators/LogarithmicScalesGenerator.cs | 4 +- .../Generators/MagnitudesGenerator.cs | 14 +- .../Generators/PhysicalConstantsGenerator.cs | 49 +- .../Generators/PrecisionGenerator.cs | 26 +- .../Generators/QuantitiesGenerator.cs | 187 +++--- .../Generators/UnitsGenerator.cs | 62 +- .../SemanticsGenerator.cs | 10 +- .../TemplateExtensions.cs | 52 ++ .../Templates/ClassTemplate.cs | 152 ----- .../Templates/ConstructorTemplate.cs | 82 --- .../Templates/FieldTemplate.cs | 27 - .../Templates/MemberTemplate.cs | 26 - .../Templates/MethodTemplate.cs | 66 -- .../Templates/ParameterTemplate.cs | 27 - .../Templates/PropertyTemplate.cs | 89 --- .../Templates/SourceFileTemplate.cs | 63 -- .../Templates/TemplateBase.cs | 62 -- .../Quantities/PropertyTemplateTests.cs | 82 --- docs/physics-generator.md | 2 + 205 files changed, 4818 insertions(+), 3590 deletions(-) create mode 100644 Semantics.SourceGenerators/TemplateExtensions.cs delete mode 100644 Semantics.SourceGenerators/Templates/ClassTemplate.cs delete mode 100644 Semantics.SourceGenerators/Templates/ConstructorTemplate.cs delete mode 100644 Semantics.SourceGenerators/Templates/FieldTemplate.cs delete mode 100644 Semantics.SourceGenerators/Templates/MemberTemplate.cs delete mode 100644 Semantics.SourceGenerators/Templates/MethodTemplate.cs delete mode 100644 Semantics.SourceGenerators/Templates/ParameterTemplate.cs delete mode 100644 Semantics.SourceGenerators/Templates/PropertyTemplate.cs delete mode 100644 Semantics.SourceGenerators/Templates/SourceFileTemplate.cs delete mode 100644 Semantics.SourceGenerators/Templates/TemplateBase.cs delete mode 100644 Semantics.Test/Quantities/PropertyTemplateTests.cs diff --git a/CLAUDE.md b/CLAUDE.md index 25fb6734..2dae46c7 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -41,7 +41,7 @@ The opt-in lives in `.sonarlint/sonar-local.props` (analyzer package) and `.sona | `Semantics.Music` | Immutable musical value types (`Pitch`, `Interval`, `Scale`, `Chord`, `Key`, `Duration`, `TimeSignature`) plus an analysis aggregate layer (`Progression`, `Section`, `Arrangement`, `Form`) computing roman numerals, cadences, key inference, chromatic identification, and named forms. Targets `net8.0`–`net10.0` + `netstandard2.0`/`netstandard2.1`. | | `Semantics.Color` | Physically-grounded color types. Canonical linear-RGB `Color` hub plus color-space satellites (`Srgb`, `Hsl`, `Hsv`, `Oklab`, `Oklch`); every type converts to and from every other, routed through the nearest shared hub (`Srgb` within the sRGB family, `Oklab` within the perceptual family, linear `Color` across families) so no conversion takes a redundant gamma round-trip. Also WCAG accessibility tooling, HSL/perceptual adjustment operations (lighten/saturate/hue/invert), and `NamedColors`. Targets `net8.0`–`net10.0` + `netstandard2.0`/`netstandard2.1`. | | `Semantics.Quantities` | Hand-written runtime types (`PhysicalQuantity`, `IVector0`..`IVector4`, `UnitSystem`) plus generator output under `Generated/`. | -| `Semantics.SourceGenerators` | Roslyn incremental generators that emit quantity types, units, conversions, magnitudes, physical constants, and storage-type helpers from metadata. `CodeGen/` holds the parts that are not specific to physics — the metadata-driven generator base, metadata loading, the diagnostic catalogue — and is being extracted into a shared toolkit (#181). | +| `Semantics.SourceGenerators` | Roslyn incremental generators that emit quantity types, units, conversions, magnitudes, physical constants, and storage-type helpers from metadata. The C# syntax templates now come from `ktsu.CodeBlocker.Templates`; `CodeGen/` holds what is left that is not specific to physics — the metadata-driven generator base, metadata loading, the diagnostic catalogue — and is being extracted into a shared toolkit (#181). | | `Semantics.Quantities.{Double,Float,Decimal}` | Props-only satellite packages. Each ships a `buildTransitive` props file (generated by `scripts/Generate-AliasProps.ps1`) that injects global-using aliases binding every quantity to one storage type, so consumers write `Mass` instead of `Mass`. | | `Semantics.Test` | MSTest project covering all of the above. | @@ -169,6 +169,8 @@ var converted = sourceString.As(); ### Working with the source generator - A generator declares the metadata files it reads via `MetadataFileNames` and derives from `SemanticsGenerator` (one file) or `SemanticsMultiFileGenerator` (several). Neither needs to override `Initialize`. +- Emitted C# is described with the template model from `ktsu.CodeBlocker.Templates` — `SourceFileTemplate`, `ClassTemplate`, `FieldTemplate`, `MethodTemplate` and friends. The model owns punctuation, spacing, member ordering and indentation, so a `BodyFactory` writes only the body: `=> Create(value);` for an expression body, or a braced block. Do **not** prefix it with a space; the model supplies the separator. +- A type's declaration keyword comes from `ClassTemplate.Kind` (`TypeKind.Class`, `TypeKind.Record`, …), not from `Keywords`, which carries only modifiers. Collection properties are read-only, so use collection-initializer syntax (`Keywords = { "public" }`) rather than assignment, or the `WithComments`/`WithInterfaces` helpers when you already hold a sequence. - Edit `Semantics.SourceGenerators/Metadata/dimensions.json` to add a dimension, vector form, semantic overload, or relationship. - Rebuild `Semantics.SourceGenerators` and the consuming `Semantics.Quantities` project; emitted files appear in `Semantics.Quantities/Generated/Semantics.SourceGenerators//`. - Treat generator output as committed source. Diff it before commit so accidental regressions are visible. diff --git a/Directory.Packages.props b/Directory.Packages.props index c15c7ba3..6e8776c1 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -14,7 +14,7 @@ - +