Skip to content

Commit 61296c8

Browse files
authored
Merge pull request #73 from Ted-Jin-Lab/development
Release
2 parents 099df9e + 0a7aa16 commit 61296c8

6 files changed

Lines changed: 38 additions & 22 deletions

File tree

Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<Project>
33
<PropertyGroup>
4-
<Version>2025.11.24.3</Version>
4+
<Version>2025.11.25.0</Version>
55
<ImplicitUsings>enable</ImplicitUsings>
66
<Nullable>enable</Nullable>
77
<LangVersion>preview</LangVersion>

Tests/TedToolkit.Console/Absorptance.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
namespace TedToolkit.Quantities;
22

3+
[QuantityDisplayUnit<AbsorbedDoseUnit>(AbsorbedDoseUnit.Centigray)]
34
[QuantityImplicitFromValueType]
45
[QuantityImplicitToValueType]
56
[QuantityOperator<AbsorbedDose, AbsorbedDose, double>(Operator.Divide)]

src/analyzers/TedToolkit.Quantities.Analyzer/QuantitiesAttributeGenerator.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public readonly record struct QuantityUnit(Quantity Quantity, string Unit)
2020
var unit = q.Units
2121
.Select(u => data.Units[u])
2222
.OrderBy(u => u.DistanceToDefault)
23+
.ThenByDescending(u => u.ApplicableSystem)
2324
.First().GetUnitName(data.Units.Values);
2425
return new QuantityUnit(q, unit);
2526
});

src/analyzers/TedToolkit.Quantities.Analyzer/QuantityStructGenerator.cs

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ public void GenerateCode(SourceProductionContext context)
329329
ReturnStatement(BinaryExpression(SyntaxKind.AddExpression, BinaryExpression(
330330
SyntaxKind.AddExpression, InvocationExpression(MemberAccessExpression(
331331
SyntaxKind.SimpleMemberAccessExpression,
332-
IdentifierName("Value"),
332+
IdentifierName(UnitString is {} argText ? $"As({argText})" : "Value"),
333333
IdentifierName("ToString")))
334334
.WithArgumentList(ArgumentList(
335335
[
@@ -950,6 +950,23 @@ MemberDeclarationSyntax CreateEnumerableMethod(string methodName, string xml)
950950
}
951951
}
952952

953+
private string? UnitString
954+
{
955+
get
956+
{
957+
if (quantitySymbol?.GetAttributes().FirstOrDefault(a =>
958+
a.AttributeClass is { IsGenericType: true } attributeClass
959+
&& attributeClass.ConstructUnboundGenericType().GetName().FullName is
960+
"global::TedToolkit.Quantities.QuantityDisplayUnitAttribute<>") is not
961+
{ } displayUnitAttribute) return null;
962+
963+
var syntax = displayUnitAttribute.ApplicationSyntaxReference?.GetSyntax()
964+
as AttributeSyntax;
965+
var argSyntax = syntax?.ArgumentList?.Arguments[0].Expression;
966+
return argSyntax?.ToString();
967+
}
968+
}
969+
953970
private ExpressionSyntax GetSystemUnitName()
954971
{
955972
var allUnits = quantity.Units
@@ -963,32 +980,25 @@ private ExpressionSyntax GetSystemUnitName()
963980
Literal(quantity.Name));
964981
}
965982

966-
if (quantitySymbol?.GetAttributes().FirstOrDefault(a =>
967-
a.AttributeClass is { IsGenericType: true } attributeClass
968-
&& attributeClass.ConstructUnboundGenericType().GetName().FullName is
969-
"global::TedToolkit.Quantities.QuantityDisplayUnitAttribute<>") is { } displayUnitAttribute)
983+
if (UnitString is { } argText)
970984
{
971-
var syntax = displayUnitAttribute.ApplicationSyntaxReference?.GetSyntax()
972-
as AttributeSyntax;
973-
var argSyntax = syntax?.ArgumentList?.Arguments[0].Expression;
974-
var argText = argSyntax?.ToString();
975-
if (argText is not null)
976-
return InvocationExpression(
977-
MemberAccessExpression(
978-
SyntaxKind.SimpleMemberAccessExpression,
979-
IdentifierName(argText),
980-
IdentifierName("ToString")))
981-
.WithArgumentList(ArgumentList(
982-
[
983-
Argument(IdentifierName("index")),
984-
Argument(IdentifierName("formatProvider"))
985-
]));
985+
return InvocationExpression(
986+
MemberAccessExpression(
987+
SyntaxKind.SimpleMemberAccessExpression,
988+
IdentifierName(argText),
989+
IdentifierName("ToString")))
990+
.WithArgumentList(ArgumentList(
991+
[
992+
Argument(IdentifierName("index")),
993+
Argument(IdentifierName("formatProvider"))
994+
]));
986995
}
987996

988997
if (quantity.IsNoDimensions)
989998
{
990999
var memberName = allUnits
9911000
.OrderBy(u => u.DistanceToDefault)
1001+
.ThenByDescending(u => u.ApplicableSystem)
9921002
.First().GetUnitName(data.Units.Values);
9931003
return FromMember(quantity.UnitName, memberName);
9941004
}
@@ -1010,6 +1020,7 @@ private ExpressionSyntax GetSystemUnitName()
10101020
return true;
10111021
})
10121022
.OrderBy(u => u.DistanceToDefault)
1023+
.ThenByDescending(u => u.ApplicableSystem)
10131024
.FirstOrDefault();
10141025

10151026
if (!string.IsNullOrEmpty(choiceUnit.Name))

src/analyzers/TedToolkit.Quantities.Analyzer/UnitSystem.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public Unit GetUnit(string key)
2626

2727
return quantityUnits
2828
.OrderBy(i => i.DistanceToDefault)
29+
.ThenByDescending(i => i.ApplicableSystem)
2930
.First();
3031
}
3132
}

src/libraries/TedToolkit.Quantities.Data/Unit.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ static string MakeSafe(string value)
3535
}
3636

3737
[JsonIgnore] public Conversion Conversion => new(EDecimal.FromString(Multiplier), EDecimal.FromString(Offset));
38+
39+
3840
[JsonIgnore]
3941
public double DistanceToDefault
4042
{
@@ -65,7 +67,7 @@ public double DistanceToDefault
6567
}
6668
}
6769

68-
return result - ApplicableSystem;
70+
return result;
6971
}
7072
}
7173
}

0 commit comments

Comments
 (0)