Skip to content

Commit 674ad89

Browse files
Faramoz161aliev.faramoz
andauthored
Code clean up (#6)
* code clean up Co-authored-by: aliev.faramoz <[email protected]>
1 parent 856c724 commit 674ad89

22 files changed

Lines changed: 232 additions & 193 deletions

DevExpress.Mvvm.CodeGenerators.Tests/FrameworkTests/FrameworkTests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
<PackageReference Include="DevExpressMvvm" Version="20.2.3" />
5454
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="3.9.0" />
5555
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.2" />
56-
<PackageReference Include="NUnit" Version="3.13.1" />
56+
<PackageReference Include="NUnit" Version="3.13.2" />
5757
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="5.0.0" />
5858
</ItemGroup>
5959
<ItemGroup>

DevExpress.Mvvm.CodeGenerators.Tests/NetCoreTests/NetCoreTests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="3.9.0" />
2525
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.2" />
2626
<PackageReference Include="NUnit" Version="3.13.2" />
27-
<PackageReference Include="NUnit3TestAdapter" Version="3.17.0" />
27+
<PackageReference Include="NUnit3TestAdapter" Version="4.0.0" />
2828
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.4" />
2929
</ItemGroup>
3030

DevExpress.Mvvm.CodeGenerators.Tests/TestSource/GenerationTests.cs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,14 +123,22 @@ partial class Example {
123123
/// <summary>
124124
/// Ignorable comment
125125
/// </summary>
126-
126+
127127
/// <summary>
128128
/// Test property comment
129129
/// </summary>
130130
// Ordinary comment
131131
[GenerateProperty]
132132
int property;
133-
133+
134+
/**
135+
<summary>
136+
MultiLine Comment
137+
</summary>
138+
*/
139+
[GenerateProperty]
140+
int property2;
141+
134142
/// <summary>
135143
/// Test command comment
136144
/// </summary>
@@ -143,12 +151,19 @@ public void Method(int arg) { }
143151
@" /// <summary>
144152
/// Test command comment
145153
/// </summary>";
154+
var property2Comment =
155+
@" /**
156+
<summary>
157+
MultiLine Comment
158+
</summary>
159+
*/";
146160
var commandComment =
147161
@" /// <summary>
148162
/// Test property comment
149163
/// </summary>";
150164
var generatedCode = GenerateCode(source);
151165
StringAssert.Contains(propertyComment, generatedCode);
166+
StringAssert.Contains(property2Comment, generatedCode);
152167
StringAssert.Contains(commandComment, generatedCode);
153168
StringAssert.DoesNotContain("Ignorable comment", generatedCode);
154169
StringAssert.DoesNotContain("Ordinary comment", generatedCode);

DevExpress.Mvvm.CodeGenerators.Tests/WinUITest/WinUITests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="3.9.0" />
2727
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.2" />
2828
<PackageReference Include="NUnit" Version="3.13.2" />
29-
<PackageReference Include="NUnit3TestAdapter" Version="3.17.0" />
29+
<PackageReference Include="NUnit3TestAdapter" Version="4.0.0" />
3030
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.4" />
3131
<PackageReference Include="System.ComponentModel.Annotations" Version="5.0.0" />
3232
</ItemGroup>

DevExpress.Mvvm.CodeGenerators/Diagnostics/GeneratorDiagnostics.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,18 @@ public static void ReportMVVMNotAvailable(this GeneratorExecutionContext context
2020
context.ReportDiagnostic(MVVMNotAvailable, SymbolNameLocation(classSymbol), mvvmComponents.ConcatToString(", "), classSymbol.Name);
2121
public static void ReportInvalidPropertyName(this GeneratorExecutionContext context, IFieldSymbol fieldSymbol, string propertyName) =>
2222
context.ReportDiagnostic(InvalidPropertyName, SymbolNameLocation(fieldSymbol), propertyName);
23-
public static void ReportOnChangedMethodNotFound(this GeneratorExecutionContext context, IFieldSymbol fieldSymbol, string? methodName, string parameterType, IEnumerable<IMethodSymbol> candidates) =>
23+
public static void ReportOnChangedMethodNotFound(this GeneratorExecutionContext context, IFieldSymbol fieldSymbol, string methodName, string parameterType, IEnumerable<IMethodSymbol> candidates) =>
2424
context.ReportDiagnostic(OnChangedMethodNotFound, SymbolNameLocation(fieldSymbol), methodName, parameterType, CandidatesMessage(candidates));
2525
public static void ReportIncorrectCommandSignature(this GeneratorExecutionContext context, IMethodSymbol methodSymbol) =>
2626
context.ReportDiagnostic(IncorrectCommandSignature, SymbolNameLocation(methodSymbol), methodSymbol.ReturnType.ToDisplayStringNullable(), methodSymbol.Name, ParameterTypesToDisplayString(methodSymbol));
27-
public static void ReportCanExecuteMethodNotFound(this GeneratorExecutionContext context, IMethodSymbol methodSymbol, string? canExecuteMethodName, string parameterType, IEnumerable<IMethodSymbol> candidates) =>
27+
public static void ReportCanExecuteMethodNotFound(this GeneratorExecutionContext context, IMethodSymbol methodSymbol, string canExecuteMethodName, string parameterType, IEnumerable<IMethodSymbol> candidates) =>
2828
context.ReportDiagnostic(CanExecuteMethodNotFound, SymbolNameLocation(methodSymbol, AttributesGenerator.CanExecuteMethod), canExecuteMethodName, parameterType, CandidatesMessage(candidates));
2929
public static void ReportRaiseMethodNotFound(this GeneratorExecutionContext context, INamedTypeSymbol classSymbol, string end) =>
3030
context.ReportDiagnostic(RaiseMethodNotFound, SymbolNameLocation(classSymbol), end);
31-
public static void ReportTwoSuitableMethods(this GeneratorExecutionContext context, INamedTypeSymbol classSymbol, IFieldSymbol fieldSymbol, string? methodName, string parameterType) =>
31+
public static void ReportTwoSuitableMethods(this GeneratorExecutionContext context, INamedTypeSymbol classSymbol, IFieldSymbol fieldSymbol, string methodName, string parameterType) =>
3232
context.ReportDiagnostic(TwoSuitableMethods, SymbolNameLocation(fieldSymbol), classSymbol.Name, methodName, parameterType);
3333

34-
static void ReportDiagnostic(this GeneratorExecutionContext context, DiagnosticDescriptor descriptor, Location location, params object?[] messageArgs) =>
34+
static void ReportDiagnostic(this GeneratorExecutionContext context, DiagnosticDescriptor descriptor, Location location, params object[] messageArgs) =>
3535
context.ReportDiagnostic(Diagnostic.Create(descriptor, location, messageArgs));
3636
static DiagnosticDescriptor CreateDiagnosticDescriptor(string id, string title, string messageFormat, DiagnosticSeverity diagnosticSeverity = DiagnosticSeverity.Error) =>
3737
new DiagnosticDescriptor(id, title, messageFormat, category, diagnosticSeverity, isEnabledByDefault);

DevExpress.Mvvm.CodeGenerators/Generators/ClassGenerator.cs

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,11 @@
22
using System;
33
using System.Collections.Generic;
44
using System.Linq;
5-
using System.Text;
65

76
namespace DevExpress.Mvvm.CodeGenerators {
8-
enum ChangeEventRaiseMode {
9-
EventArgs, PropertyName
10-
}
7+
enum ChangeEventRaiseMode { EventArgs, PropertyName }
118
static class ClassGenerator {
12-
static readonly string defaultUsings =
9+
const string defaultUsings =
1310
@"using System.Collections.Generic;
1411
using System.ComponentModel;";
1512

@@ -38,13 +35,14 @@ public static void GenerateSourceCode(SourceBuilder source, ContextInfo contextI
3835
}
3936
if(implISPVM) {
4037
mvvmComponentsList.Add("ISupportParentViewModel");
41-
bool shouldGenerateChangedMethod = ClassHelper.ShouldGenerateISPVMChangedMethod(classSymbol);
42-
if(contextInfo.IsMvvmAvailable && contextInfo.ISPVMSymbol != null && !ClassHelper.IsInterfaceImplemented(classSymbol, contextInfo.ISPVMSymbol, contextInfo))
38+
if(contextInfo.IsMvvmAvailable && !ClassHelper.IsInterfaceImplemented(classSymbol, contextInfo.ISPVMSymbol!, contextInfo)) {
39+
bool shouldGenerateChangedMethod = ClassHelper.ContainISPVMChangedMethod(classSymbol);
4340
interfaces.Add(new ISupportParentViewModelGenerator(shouldGenerateChangedMethod));
41+
}
4442
}
4543
if(implISS) {
4644
mvvmComponentsList.Add("ISupportServices");
47-
if(contextInfo.IsMvvmAvailable && contextInfo.ISSSymbol != null && !ClassHelper.IsInterfaceImplementedInCurrentClass(classSymbol, contextInfo.ISSSymbol))
45+
if(contextInfo.IsMvvmAvailable && !ClassHelper.IsInterfaceImplementedInCurrentClass(classSymbol, contextInfo.ISSSymbol!))
4846
interfaces.Add(new ISupportServicesGenerator());
4947
}
5048

@@ -55,18 +53,17 @@ public static void GenerateSourceCode(SourceBuilder source, ContextInfo contextI
5553

5654
Dictionary<string, TypeKind> outerClasses = ClassHelper.GetOuterClasses(classSymbol);
5755

58-
source = GenerateHeader(classSymbol, interfaces,
59-
impelementRaiseChangedMethod ? inpcedInfo.RaiseMethodImplementation : null,
60-
impelementRaiseChangingMethod ? inpcingInfo.RaiseMethodImplementation : null,
61-
genericTypes, outerClasses, source,
62-
addDevExpressUsing: contextInfo.IsMvvmAvailable);
56+
source = GenerateHeader(source, classSymbol, interfaces,
57+
impelementRaiseChangedMethod ? inpcedInfo.RaiseMethodImplementation : null,
58+
impelementRaiseChangingMethod ? inpcingInfo.RaiseMethodImplementation : null,
59+
genericTypes, outerClasses, contextInfo.IsMvvmAvailable);
6360

6461

6562
bool needStaticChangedEventArgs = inpcedInfo.HasRaiseMethodWithEventArgsParameter || impelementRaiseChangedMethod;
6663
bool needStaticChangingEventArgs = inpcingInfo.HasAttribute && (inpcingInfo.HasRaiseMethodWithEventArgsParameter || impelementRaiseChangingMethod);
67-
IReadOnlyList<string> propertyNames = GenerateProperties(contextInfo, classSymbol, inpcedInfo, inpcingInfo, needStaticChangedEventArgs, needStaticChangingEventArgs, source);
64+
IReadOnlyList<string> propertyNames = GenerateProperties(source, contextInfo, classSymbol, inpcedInfo, inpcingInfo, needStaticChangedEventArgs, needStaticChangingEventArgs);
6865

69-
GenerateCommands(contextInfo, classSymbol, contextInfo.IsMvvmAvailable, source, out bool hasCommands);
66+
GenerateCommands(source, contextInfo, classSymbol, contextInfo.IsMvvmAvailable, out bool hasCommands);
7067
if(hasCommands)
7168
mvvmComponentsList.Add("Commands");
7269

@@ -80,7 +77,7 @@ public static void GenerateSourceCode(SourceBuilder source, ContextInfo contextI
8077
contextInfo.Context.ReportMVVMNotAvailable(classSymbol, mvvmComponentsList);
8178
}
8279

83-
static SourceBuilder GenerateHeader(INamedTypeSymbol classSymbol, List<IInterfaceGenerator> interfaces, string? raiseChangedMethod, string? raiseChangingMethod, List<ITypeSymbol> genericTypes, Dictionary<string, TypeKind> outerClasses, SourceBuilder source, bool addDevExpressUsing) {
80+
static SourceBuilder GenerateHeader(SourceBuilder source, INamedTypeSymbol classSymbol, List<IInterfaceGenerator> interfaces, string? raiseChangedMethod, string? raiseChangingMethod, List<ITypeSymbol> genericTypes, Dictionary<string, TypeKind> outerClasses, bool addDevExpressUsing) {
8481
source.AppendLine(defaultUsings);
8582
if(addDevExpressUsing)
8683
source.AppendLine("using DevExpress.Mvvm;");
@@ -126,16 +123,16 @@ static void AppendGenericArguments(SourceBuilder source, List<ITypeSymbol> gener
126123
source.Append('>');
127124
}
128125
}
129-
static IReadOnlyList<string> GenerateProperties(ContextInfo contextInfo, INamedTypeSymbol classSymbol, INPCInfo inpcedInfo, INPCInfo inpcingInfo, bool needStaticChangedEventArgs, bool needStaticChangingEventArgs, SourceBuilder source) {
130-
ChangeEventRaiseMode? changedRaiseMode = needStaticChangedEventArgs
131-
? ChangeEventRaiseMode.EventArgs
132-
: inpcedInfo.HasRaiseMethodWithStringParameter
133-
? ChangeEventRaiseMode.PropertyName
126+
static IReadOnlyList<string> GenerateProperties(SourceBuilder source, ContextInfo contextInfo, INamedTypeSymbol classSymbol, INPCInfo inpcedInfo, INPCInfo inpcingInfo, bool needStaticChangedEventArgs, bool needStaticChangingEventArgs) {
127+
ChangeEventRaiseMode? changedRaiseMode = needStaticChangedEventArgs
128+
? ChangeEventRaiseMode.EventArgs
129+
: inpcedInfo.HasRaiseMethodWithStringParameter
130+
? ChangeEventRaiseMode.PropertyName
134131
: default(ChangeEventRaiseMode?);
135-
ChangeEventRaiseMode? changingRaiseMode = needStaticChangingEventArgs
136-
? ChangeEventRaiseMode.EventArgs
137-
: inpcingInfo.HasAttribute && inpcingInfo.HasRaiseMethodWithStringParameter
138-
? ChangeEventRaiseMode.PropertyName
132+
ChangeEventRaiseMode? changingRaiseMode = needStaticChangingEventArgs
133+
? ChangeEventRaiseMode.EventArgs
134+
: inpcingInfo.HasAttribute && inpcingInfo.HasRaiseMethodWithStringParameter
135+
? ChangeEventRaiseMode.PropertyName
139136
: default(ChangeEventRaiseMode?);
140137
bool generateProperties = true;
141138
List<string> propertyNames = new();
@@ -160,7 +157,7 @@ static IReadOnlyList<string> GenerateProperties(ContextInfo contextInfo, INamedT
160157
return propertyNames;
161158
}
162159

163-
static void GenerateCommands(ContextInfo contextInfo, INamedTypeSymbol classSymbol, bool isMvvmAvailable, SourceBuilder source, out bool hasCommands) {
160+
static void GenerateCommands(SourceBuilder source, ContextInfo contextInfo, INamedTypeSymbol classSymbol, bool isMvvmAvailable, out bool hasCommands) {
164161
IEnumerable<IMethodSymbol> commandCandidates = ClassHelper.GetCommandCandidates(classSymbol, contextInfo.CommandAttributeSymbol);
165162
hasCommands = commandCandidates.Any();
166163
if(isMvvmAvailable) {

DevExpress.Mvvm.CodeGenerators/Generators/CommandGenerator.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using Microsoft.CodeAnalysis.CSharp;
33
using System.Collections.Generic;
44
using System.Linq;
5-
using System.Text;
65

76
namespace DevExpress.Mvvm.CodeGenerators {
87
static class CommandGenerator {
@@ -29,7 +28,7 @@ public static void Generate(SourceBuilder source, ContextInfo info, INamedTypeSy
2928
}
3029
}
3130

32-
string name = CommandHelper.GetCommandName(methodSymbol, info.CommandAttributeSymbol, methodSymbol.Name)!;
31+
string name = CommandHelper.GetCommandName(methodSymbol, info.CommandAttributeSymbol, methodSymbol.Name);
3332
string genericArgumentType = parameterType?.ToDisplayStringNullable() ?? string.Empty;
3433
source.AppendCommandGenericType(isCommand, genericArgumentType).Append("? ").AppendFirstToLowerCase(name).AppendLine(";");
3534

@@ -47,7 +46,7 @@ static void AppendGetter(SourceBuilder source, ContextInfo info, IMethodSymbol m
4746
source.AppendLine(");");
4847
}
4948

50-
static void AppendParametersList(this SourceBuilder source, IMethodSymbol methodSymbol, INamedTypeSymbol? commandAttributeSymbol, string canExecuteMethodName, bool isCommand, string executeMethod, bool isWinUI) {
49+
static void AppendParametersList(this SourceBuilder source, IMethodSymbol methodSymbol, INamedTypeSymbol commandAttributeSymbol, string canExecuteMethodName, bool isCommand, string executeMethod, bool isWinUI) {
5150
source.Append(executeMethod).Append(", ").Append(canExecuteMethodName);
5251
if(!isCommand) {
5352
string allowMultipleExecution = CommandHelper.GetAllowMultipleExecutionValue(methodSymbol, commandAttributeSymbol).BoolToStringValue();

DevExpress.Mvvm.CodeGenerators/Generators/EventArgsGenerator.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System.Collections.Generic;
2-
using System.Text;
32

43
namespace DevExpress.Mvvm.CodeGenerators {
54
static class EventArgsGenerator {

DevExpress.Mvvm.CodeGenerators/Generators/GeneratorCore.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
using Microsoft.CodeAnalysis;
2-
using Microsoft.CodeAnalysis.Text;
32
using Microsoft.CodeAnalysis.CSharp;
3+
using Microsoft.CodeAnalysis.CSharp.Syntax;
4+
using Microsoft.CodeAnalysis.Text;
45
using System.Collections.Generic;
5-
using System.Text;
6-
using System.Linq;
7-
using System.Runtime.InteropServices;
86
using System.Diagnostics;
9-
using Microsoft.CodeAnalysis.CSharp.Syntax;
7+
using System.Linq;
8+
using System.Text;
109

1110
namespace DevExpress.Mvvm.CodeGenerators {
1211
static class ViewModelGeneratorCore {

DevExpress.Mvvm.CodeGenerators/Generators/InterfaceGenerator.cs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
using System.ComponentModel;
2-
using System.Globalization;
3-
using System.Text;
42

53
namespace DevExpress.Mvvm.CodeGenerators {
64
interface IInterfaceGenerator {
@@ -10,12 +8,12 @@ interface IInterfaceGenerator {
108

119
class INPCedInterfaceGenerator : IInterfaceGenerator {
1210
public string GetName() => nameof(INotifyPropertyChanged);
13-
public void AppendImplementation(SourceBuilder source)
14-
=> source.AppendLine("public event PropertyChangedEventHandler? PropertyChanged;");
11+
public void AppendImplementation(SourceBuilder source) =>
12+
source.AppendLine("public event PropertyChangedEventHandler? PropertyChanged;");
1513
}
1614
class INPCingInterfaceGenerator : IInterfaceGenerator {
1715
public string GetName() => nameof(INotifyPropertyChanging);
18-
public void AppendImplementation(SourceBuilder source) =>
16+
public void AppendImplementation(SourceBuilder source) =>
1917
source.AppendLine("public event PropertyChangingEventHandler? PropertyChanging;");
2018
}
2119
class IDataErrorInfoGenerator : IInterfaceGenerator {
@@ -26,9 +24,7 @@ class IDataErrorInfoGenerator : IInterfaceGenerator {
2624
}
2725
class ISupportParentViewModelGenerator : IInterfaceGenerator {
2826
readonly bool generateChangedMethod;
29-
public ISupportParentViewModelGenerator(bool shouldGenerateChangedMethod) {
30-
generateChangedMethod = shouldGenerateChangedMethod;
31-
}
27+
public ISupportParentViewModelGenerator(bool shouldGenerateChangedMethod) => generateChangedMethod = shouldGenerateChangedMethod;
3228
public string GetName() => "ISupportParentViewModel";
3329
public void AppendImplementation(SourceBuilder source) {
3430
source.AppendMultipleLines(@"object? parentViewModel;

0 commit comments

Comments
 (0)