Skip to content

Commit 67fded2

Browse files
authored
Merge pull request #7 from WCKYWCKF/bugfix
修复大量细微BUG
2 parents cbc33d2 + 10647f9 commit 67fded2

File tree

8 files changed

+77
-51
lines changed

8 files changed

+77
-51
lines changed

WCKYWCKF.Model2ViewModel.Editor/ViewModels/MainWindowViewModel.cs

Lines changed: 45 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -465,44 +465,52 @@ private async Task PreviewGeneratedCode()
465465
})
466466
.OrderBy(x => x.FileName)
467467
.ToList());
468-
var operations = ModelMetadata.Operations.Select(x =>
469-
{
470-
return
471-
$"""[M2VMPropertyOrFieldOperationInfo(TargetTypeFQType = {x.TargetTypeTypeofInfo.Sample}, TargetMemberName = "{x.TargetMemberName}", TargetOperation = {GetOperation(x.TargetOperation)})]""";
472-
473-
string GetOperation(PropertyOrFieldOperationKind operationKind)
468+
var count = 0;
469+
var all = ModelMetadata.Replaces.Concat<M2VMPropertyOrFieldOperationBase>(ModelMetadata.Operations)
470+
.OrderBy(x => x.TargetTypeFQType)
471+
.ThenBy(x => x.TargetMemberName)
472+
.ThenByDescending(x => x is M2VMPropertyOrFieldOperationInfo)
473+
.Select(x =>
474474
{
475-
string[] str =
476-
[
477-
operationKind.HasFlag(PropertyOrFieldOperationKind.IgnoreProperty)
478-
? "PropertyOrFieldOperationKind.IgnoreProperty"
479-
: "",
480-
operationKind.HasFlag(PropertyOrFieldOperationKind.DoNotReplaceTargetType)
481-
? "PropertyOrFieldOperationKind.DoNotReplaceTargetType"
482-
: "",
483-
operationKind.HasFlag(PropertyOrFieldOperationKind.IncludePropertyOrField)
484-
? "PropertyOrFieldOperationKind.IncludePropertyOrField"
485-
: "",
486-
operationKind.HasFlag(PropertyOrFieldOperationKind.TypeIsNotNullable)
487-
? "PropertyOrFieldOperationKind.TypeIsNotNullable"
488-
: "",
489-
operationKind.HasFlag(PropertyOrFieldOperationKind.TypeIsNullable)
490-
? "PropertyOrFieldOperationKind.TypeIsNullable"
491-
: ""
492-
];
493-
StringBuilder builder = new();
494-
var distinct = str.Where(y => !string.IsNullOrEmpty(y)).ToList();
495-
builder.Append(distinct.FirstOrDefault());
496-
foreach (var item in distinct.Skip(1)) builder.Append(" | " + item);
497-
498-
return builder.ToString();
499-
}
500-
});
501-
var replaces = ModelMetadata.Replaces.Select(x =>
502-
$"""[M2VMReplaceGenerationInfo(TargetTypeFQType = {x.TargetTypeTypeofInfo.Sample}, TargetMemberName = "{x.TargetMemberName}", ReplaceFQType = {x.ReplaceTypeTypeofInfo.Sample})]""");
475+
if (x is M2VMPropertyOrFieldOperationInfo info)
476+
{
477+
return
478+
$"""[M2VMPropertyOrFieldOperationInfo(TargetTypeFQType = {info.TargetTypeTypeofInfo.Sample}, TargetMemberName = "{info.TargetMemberName}", TargetOperation = {GetOperation(info.TargetOperation)})]""";
479+
480+
string GetOperation(PropertyOrFieldOperationKind operationKind)
481+
{
482+
string[] str =
483+
[
484+
operationKind.HasFlag(PropertyOrFieldOperationKind.IgnoreProperty)
485+
? "PropertyOrFieldOperationKind.IgnoreProperty"
486+
: "",
487+
operationKind.HasFlag(PropertyOrFieldOperationKind.DoNotReplaceTargetType)
488+
? "PropertyOrFieldOperationKind.DoNotReplaceTargetType"
489+
: "",
490+
operationKind.HasFlag(PropertyOrFieldOperationKind.IncludePropertyOrField)
491+
? "PropertyOrFieldOperationKind.IncludePropertyOrField"
492+
: "",
493+
operationKind.HasFlag(PropertyOrFieldOperationKind.TypeIsNotNullable)
494+
? "PropertyOrFieldOperationKind.TypeIsNotNullable"
495+
: "",
496+
operationKind.HasFlag(PropertyOrFieldOperationKind.TypeIsNullable)
497+
? "PropertyOrFieldOperationKind.TypeIsNullable"
498+
: ""
499+
];
500+
StringBuilder builder = new();
501+
var distinct = str.Where(y => !string.IsNullOrEmpty(y)).ToList();
502+
builder.Append(distinct.FirstOrDefault());
503+
foreach (var item in distinct.Skip(1)) builder.Append(" | " + item);
504+
505+
return builder.ToString();
506+
}
507+
}
508+
509+
return
510+
$"""[M2VMReplaceGenerationInfo(TargetTypeFQType = {x.TargetTypeTypeofInfo.Sample}, TargetMemberName = "{x.TargetMemberName}", ReplaceFQType = {(x as M2VMReplaceGenerationInfo)!.ReplaceTypeTypeofInfo.Sample})]""";
511+
});
503512
var rootCode = await Task.Run(() => $"""
504-
{string.Join("\n", operations)}
505-
{string.Join("\n", replaces)}
513+
{string.Join("\n", all)}
506514
public partial class {ModelMetadata.ClassName};
507515
""");
508516
SummaryGeneratedBorderMdDocument = await Task.Run(() => Markdown.Parse(
@@ -567,7 +575,7 @@ private void ConfirmBatchModify()
567575
[ReactiveCommand]
568576
private void CancelCurrentSelection()
569577
{
570-
foreach (var metadataTdgItemViewModel in _selection)
578+
foreach (var metadataTdgItemViewModel in _selection.ToList())
571579
metadataTdgItemViewModel.IsSelected = false;
572580
}
573581

WCKYWCKF.Model2ViewModel.Sample/Examples.cs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.ComponentModel;
34
using CommunityToolkit.Mvvm.ComponentModel;
45
using EmmyLua.LanguageServer.Framework.Protocol.Capabilities.Client.ClientCapabilities;
6+
using ReactiveUI;
57

68
namespace WCKYWCKF.Model2ViewModel.Sample;
79

810
public class ComplexModel
911
{
10-
private int TestPrivateField;
12+
private int? TestPrivateField;
1113
public string TestPublicField;
1214

1315
public List<Dictionary<string, int[]>>? NestedCollection { get; set; }
@@ -28,4 +30,15 @@ public class DDS
2830
// [M2VMGenerationInfo(TargetTypeFQType = typeof(ClientCapabilities))]
2931
[M2VMGenerationInfo(TargetTypeFQType = typeof(ComplexModel))]
3032
[M2VMSaveGenerationInfo(SaveFilePath = @"D:\Temp\test.json")]
31-
public partial class TF : ObservableObject;
33+
[M2VMUseAutoField]
34+
public partial class TF;
35+
36+
[M2VMPropertyOrFieldOperationInfo(TargetTypeFQType = typeof(string), TargetMemberName = "TestPublicField", TargetOperation = PropertyOrFieldOperationKind.IncludePropertyOrField)]
37+
[M2VMPropertyOrFieldOperationInfo(TargetTypeFQType = typeof(int), TargetMemberName = "TestPrivate", TargetOperation = PropertyOrFieldOperationKind.IncludePropertyOrField)]
38+
[M2VMPropertyOrFieldOperationInfo(TargetTypeFQType = typeof(IEnumerable<DateTime>), TargetMemberName = "Dates", TargetOperation = PropertyOrFieldOperationKind.IncludePropertyOrField)]
39+
[M2VMPropertyOrFieldOperationInfo(TargetTypeFQType = typeof(string), TargetMemberName = "TestProtected", TargetOperation = PropertyOrFieldOperationKind.IncludePropertyOrField)]
40+
[M2VMPropertyOrFieldOperationInfo(TargetTypeFQType = typeof(string), TargetMemberName = "Str2", TargetOperation = PropertyOrFieldOperationKind.IgnoreProperty)]
41+
[M2VMPropertyOrFieldOperationInfo(TargetTypeFQType = typeof(int), TargetMemberName = "TestPrivateField", TargetOperation = PropertyOrFieldOperationKind.IncludePropertyOrField | PropertyOrFieldOperationKind.TypeIsNullable)]
42+
[M2VMReplaceGenerationInfo(TargetTypeFQType = typeof(IEnumerable<DateTime>), TargetMemberName = "Dates", ReplaceFQType = typeof(List<DateTime>))]
43+
[M2VMReplaceGenerationInfo(TargetTypeFQType = typeof(string), TargetMemberName = "TestProtected", ReplaceFQType = typeof(double))]
44+
public partial class TF;

WCKYWCKF.Model2ViewModel.Sample/WCKYWCKF.Model2ViewModel.Sample.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
<PropertyGroup>
44
<TargetFramework>net8.0</TargetFramework>
55
<Nullable>enable</Nullable>
6+
<LangVersion>preview</LangVersion>
67
</PropertyGroup>
78

89
<ItemGroup>

WCKYWCKF.Model2ViewModel.Share/BuildInfo/PropertyBuildInfo.cs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ public record PropertyBuildInfo
1212

1313
public string CreatePropertyCode(GenerateMode generateMode, bool useAutoField)
1414
{
15-
var fieldName = useAutoField ? "filed" : GetFieldName(PropertyName);
16-
var propertyType = GlobalPropertyFQType + (IsNullable ? "?" : "");
15+
var fieldName = useAutoField ? "field" : GetFieldName(PropertyName);
16+
var propertyType = GlobalPropertyFQType.Replace("?", "") + (IsNullable ? "?" : "");
1717
return $$"""
1818
{{(useAutoField ? "" : $"{TabStr}private {propertyType} {fieldName};")}}
1919
{{TabStr}}public {{propertyType}} {{PropertyName}} {
@@ -22,18 +22,22 @@ public string CreatePropertyCode(GenerateMode generateMode, bool useAutoField)
2222
{{GetPropertySetterTemplate()}}
2323
{{TabStr}}{{TabStr}}}
2424
{{TabStr}}}
25-
{{TabStr}}partial void On{{PropertyName}}Changing({{propertyType}} value);
26-
{{TabStr}}partial void On{{PropertyName}}Changing({{propertyType}} oldValue, {{propertyType}} newValue);
27-
{{TabStr}}partial void On{{PropertyName}}Changed({{propertyType}} value);
28-
{{TabStr}}partial void On{{PropertyName}}Changed({{propertyType}} oldValue, {{propertyType}} newValue);
29-
""";
25+
""" + (generateMode is GenerateMode.CommunityMvvm
26+
? $$"""
27+
28+
{{TabStr}}partial void On{{PropertyName}}Changing({{propertyType}} value);
29+
{{TabStr}}partial void On{{PropertyName}}Changing({{propertyType}} oldValue, {{propertyType}} newValue);
30+
{{TabStr}}partial void On{{PropertyName}}Changed({{propertyType}} value);
31+
{{TabStr}}partial void On{{PropertyName}}Changed({{propertyType}} oldValue, {{propertyType}} newValue);
32+
"""
33+
: "");
3034

3135
string GetPropertySetterTemplate()
3236
{
3337
return generateMode switch
3438
{
3539
GenerateMode.RxUI =>
36-
$$"""{{TabStr}}{{TabStr}}{{TabStr}}this.RaiseAndSetIfChanged(ref {{{fieldName}}}, value);""",
40+
$$"""{{TabStr}}{{TabStr}}{{TabStr}}this.RaiseAndSetIfChanged(ref {{fieldName}}, value);""",
3741
GenerateMode.CommunityMvvm =>
3842
$$"""
3943
{{TabStr}}{{TabStr}}{{TabStr}}if (!global::System.Collections.Generic.EqualityComparer<{{propertyType}}>.Default.Equals({{fieldName}}, value))

WCKYWCKF.Model2ViewModel.Share/BuildInfo/ViewModelBuildInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ protected virtual void OnPropertyChanged([CallerMemberName] string? propertyName
1515
1616
protected bool SetField<T>(ref T field, T value, [CallerMemberName] string? propertyName = null)
1717
{
18-
if (EqualityComparer<T>.Default.Equals(field, value)) return false;
18+
if (!global::System.Collections.Generic.EqualityComparer<T>.Default.Equals(field, value)) return false;
1919
field = value;
2020
OnPropertyChanged(propertyName);
2121
return true;

WCKYWCKF.Model2ViewModel.Share/M2VMHelper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ public static string GetTypeofTargetTypeFQType(ITypeSymbol typeSymbol, bool fQTy
130130
{
131131
return $"typeof({typeSymbol.ToDisplayString(fQType
132132
? GlobalSymbolDisplayFormat
133-
: SymbolDisplayFormat.MinimallyQualifiedFormat)})";
133+
: SymbolDisplayFormat.MinimallyQualifiedFormat).Replace(typeSymbol.IsValueType ? "" : "?", "")})";
134134
}
135135

136136
public static string GetTypeofTargetTypeFQType(string typeName)

WCKYWCKF.Model2ViewModel.Share/SourceInfo/M2VMTypeMemberInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public M2VMTypeMemberInfo(ISymbol symbol)
2727
SetterAccessibility = propertySymbol.SetMethod?.DeclaredAccessibility;
2828
}
2929

30-
MemberTypeInfo = new TypeofInfo(typeSymbol);
30+
MemberTypeInfo = new TypeofInfo(symbol.ContainingType);
3131
IsMemberTypeSystem = M2VMHelper.IsSystemType(typeSymbol);
3232
}
3333

WCKYWCKF.Model2ViewModel/WCKYWCKF.Model2ViewModel.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
<RootNamespace>WCKYWCKF.RxUI.Model2ViewModel</RootNamespace>
1313
<PackageId>WCKYWCKF.Model2ViewModel</PackageId>
14-
<Version>0.12.1-pre</Version>
14+
<Version>0.12.5-pre</Version>
1515
<Title>WCKYWCKF.Model2ViewModel</Title>
1616
<Authors>https://github.com/WCKYWCKF</Authors>
1717
<Description>这是一个源生成器,旨在帮助你在MVVM设计模式中,将复杂且庞大的Model配置类生成一个ViewModel版本。</Description>

0 commit comments

Comments
 (0)