Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using NUnit.Framework;
using System;
using System.Linq;
using System.Reflection;

namespace DevExpress.Mvvm.CodeGenerators.Tests {
Expand Down Expand Up @@ -38,7 +39,17 @@ void WithNullableString3(string? str) { }
void WithNullableString4(string str) { }
bool CanWithNullableString1(string str) => str?.Length > 0;
bool CanWithNullableString2(string str) => str.Length > 0;
[First]
[Second]
[Third]
[GenerateCommand]
void AttributeTest() { }
}
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Property)]
public sealed class FirstAttribute : Attribute { }
[AttributeUsage(AttributeTargets.Method)]
public sealed class SecondAttribute : Attribute { }
public sealed class ThirdAttribute : Attribute { }

[TestFixture]
public class CommandGenerationTests {
Expand Down Expand Up @@ -134,5 +145,14 @@ public void NullableReferenceType() {
Assert.IsTrue(generated.WithNullableString3Command.CanExecute(""));
Assert.IsFalse(generated.WithNullableString4Command.CanExecute(null));
}
[Test]
public void AttributeGenerationTest() {
var generated = new GenerateCommands();

var attributes = generated.GetType().GetProperty("AttributeTestCommand").GetCustomAttributes().ToList();
Assert.AreEqual(2, attributes.Count);
Assert.IsTrue(attributes[0] is FirstAttribute);
Assert.IsTrue(attributes[1] is ThirdAttribute);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
using System.Reflection;
using DevExpress.Mvvm.CodeGenerators.MvvmLight;
using GalaSoft.MvvmLight.Helpers;
using System.Linq;

#if NETCOREAPP
using GalaSoft.MvvmLight.Command;
#else
Expand Down Expand Up @@ -39,7 +41,17 @@ void WithNullableString3(string? str) { }
void WithNullableString4(string str) { }
bool CanWithNullableString1(string str) => str?.Length > 0;
bool CanWithNullableString2(string str) => str.Length > 0;
[First]
[Second]
[Third]
[GenerateCommand]
void AttributeTest() { }
}
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Property)]
public sealed class FirstAttribute : Attribute { }
[AttributeUsage(AttributeTargets.Method)]
public sealed class SecondAttribute : Attribute { }
public sealed class ThirdAttribute : Attribute { }

[TestFixture]
public class CommandGenerationTests {
Expand Down Expand Up @@ -111,5 +123,15 @@ public void NullableReferenceType() {
Assert.IsTrue(generated.WithNullableString3Command.CanExecute(""));
Assert.IsFalse(generated.WithNullableString4Command.CanExecute(null));
}

[Test]
public void AttributeGenerationTest() {
var generated = new GenerateCommands();

var attributes = generated.GetType().GetProperty("AttributeTestCommand").GetCustomAttributes().ToList();
Assert.AreEqual(2, attributes.Count);
Assert.IsTrue(attributes[0] is FirstAttribute);
Assert.IsTrue(attributes[1] is ThirdAttribute);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using DevExpress.Mvvm.CodeGenerators.Prism;
using Prism.Commands;
using System.Collections.Generic;
using System.Linq;

namespace Prism.Mvvm.Tests {
[GenerateViewModel]
Expand Down Expand Up @@ -41,7 +42,17 @@ public void WithCanExecuteAndCanExecuteProperty() { }
void WithCanExecutePropertyAndOservesProperty() { }

public void SomeMethod() { }
[First]
[Second]
[Third]
[GenerateCommand]
void AttributeTest() { }
}
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Property)]
public sealed class FirstAttribute : Attribute { }
[AttributeUsage(AttributeTargets.Method)]
public sealed class SecondAttribute : Attribute { }
public sealed class ThirdAttribute : Attribute { }

[TestFixture]
public class CommandGenerationTests {
Expand Down Expand Up @@ -120,5 +131,14 @@ public void ObservesProperties() {
Assert.AreEqual(3, expressionsWithCanExecuteProperty.Count);
Assert.IsTrue(expressionsWithCanExecuteProperty.Contains($"() => value({generated.GetType().FullName}).CanExecuteProperty"));
}
[Test]
public void AttributeGenerationTest() {
var generated = new GenerateCommands();

var attributes = generated.GetType().GetProperty("AttributeTestCommand").GetCustomAttributes().ToList();
Assert.AreEqual(2, attributes.Count);
Assert.IsTrue(attributes[0] is FirstAttribute);
Assert.IsTrue(attributes[1] is ThirdAttribute);
}
}
}
2 changes: 2 additions & 0 deletions DevExpress.Mvvm.CodeGenerators/Generators/CommandGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ public static void Generate(SourceBuilder source, ContextInfo info, INamedTypeSy
CSharpSyntaxNode commandSyntaxNode = (CSharpSyntaxNode)methodSymbol.DeclaringSyntaxReferences[0].GetSyntax();
XMLCommentHelper.AppendComment(source, commandSyntaxNode);

AttributeHelper.AppendMethodAttriutes(source, methodSymbol, info);

source.Append("public ").AppendCommandGenericType(mvvm, isCommand, genericArgumentType).Append(" ").Append(name);

switch(mvvm) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ static class PropertyGenerator {
CSharpSyntaxNode fieldSyntaxNode = (CSharpSyntaxNode)fieldSymbol.DeclaringSyntaxReferences[0].GetSyntax().Parent!.Parent!;
XMLCommentHelper.AppendComment(source, fieldSyntaxNode);

PropertyHelper.AppendAttributesList(source, fieldSymbol);
AttributeHelper.AppendFieldAttriutes(source, fieldSymbol, info);

bool isVirtual = PropertyHelper.GetIsVirtualValue(fieldSymbol, propertyAttributeSymbol);
string virtuality = isVirtual ? "virtual " : string.Empty;
Expand Down
26 changes: 25 additions & 1 deletion DevExpress.Mvvm.CodeGenerators/Helpers/AttributeHelper.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Microsoft.CodeAnalysis;
using System.Collections.Generic;
using System;
using System.Collections.Immutable;
using System.Linq;

namespace DevExpress.Mvvm.CodeGenerators {
Expand Down Expand Up @@ -30,5 +31,28 @@ public static bool HasAttribute(ISymbol sourceSymbol, INamedTypeSymbol? attribut
return null;
return sourceSymbol.GetAttributes().FirstOrDefault(ad => SymbolEqualityComparer.Default.Equals(ad.AttributeClass, attributeSymbol));
}

public static void AppendFieldAttriutes(SourceBuilder source, ISymbol symbol, ContextInfo info) {
AppendAttributesListCore(source, symbol, info, CanAppendFieldAttribute);
}
static bool CanAppendFieldAttribute(ContextInfo _, AttributeData attribute) {
return PropertyHelper.CanAppendAttribute(attribute.ToString());
}
public static void AppendMethodAttriutes(SourceBuilder source, ISymbol symbol, ContextInfo info) {
AppendAttributesListCore(source, symbol, info, CanAppendMethodAttribute);
}
static bool CanAppendMethodAttribute(ContextInfo info, AttributeData attribute) {
return CommandHelper.CanAppendAttribute(attribute, info);
}
static void AppendAttributesListCore(SourceBuilder source, ISymbol symbol, ContextInfo info, Func<ContextInfo, AttributeData, bool> predicate) {
ImmutableArray<AttributeData> attributeList = symbol.GetAttributes();
if(attributeList.Length == 1)
return;
foreach(AttributeData attribute in attributeList) {
string attributeName = attribute.ToString();
if(predicate(info, attribute))
source.Append('[').Append(attributeName).AppendLine("]");
}
}
}
}
8 changes: 8 additions & 0 deletions DevExpress.Mvvm.CodeGenerators/Helpers/CommandHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,13 @@ static bool HaveSameParametersList(ImmutableArray<IParameterSymbol> parameters,
return parameters.Count() == 0;
return parameters.Count() == 1 && PropertyHelper.IsСompatibleType(parameters.First().Type, parameterType);
}
public static bool CanAppendAttribute(AttributeData attribute, ContextInfo info) {
ImmutableArray<AttributeData> innerAttributeList = attribute.AttributeClass!.GetAttributes();
if(innerAttributeList.Length == 0)
return true;
object? attributeTargets = innerAttributeList.FirstOrDefault(attr => IsAttributeUsageSymbol(attr, info))?.ConstructorArguments[0].Value;
return attributeTargets != null && (((AttributeTargets)attributeTargets & AttributeTargets.Property) != 0);
}
static bool IsAttributeUsageSymbol(AttributeData attribute, ContextInfo info) => SymbolEqualityComparer.Default.Equals(attribute.AttributeClass, info.AttributeUsageSymbol);
}
}
16 changes: 5 additions & 11 deletions DevExpress.Mvvm.CodeGenerators/Helpers/PropertyHelper.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using Microsoft.CodeAnalysis;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;

namespace DevExpress.Mvvm.CodeGenerators {
Expand All @@ -25,16 +24,6 @@ public static string GetSetterAccessModifierValue(IFieldSymbol fieldSymbol, INam
int enumIndex = AttributeHelper.GetPropertyActualValue(fieldSymbol, propertySymbol, nameofSetterAccessModifier, 0);
return AccessModifierGenerator.GetCodeRepresentation((AccessModifier)enumIndex);
}
public static void AppendAttributesList(SourceBuilder source, IFieldSymbol fieldSymbol) {
ImmutableArray<AttributeData> attributeList = fieldSymbol.GetAttributes();
if(attributeList.Length == 1)
return;
foreach(AttributeData attribute in attributeList) {
string attributeName = attribute.ToString();
if(!(attributeName.StartsWith(AttributesGenerator.DxPropertyAttributeFullName!) || attributeName.StartsWith(AttributesGenerator.PrismPropertyAttributeFullName!) || attributeName.StartsWith(AttributesGenerator.MvvmLightPropertyAttributeFullName!)))
source.Append('[').Append(attributeName).AppendLine("]");
}
}
public static NullableAnnotation GetNullableAnnotation(ITypeSymbol type) =>
type.IsReferenceType && type.NullableAnnotation == NullableAnnotation.None
? NullableAnnotation.Annotated
Expand Down Expand Up @@ -90,5 +79,10 @@ static IEnumerable<IMethodSymbol> GetOnChangedMethods(INamedTypeSymbol classSymb
CommandHelper.GetMethods(classSymbol,
methodSymbol => methodSymbol.ReturnsVoid && methodSymbol.Name == methodName && methodSymbol.Parameters.Length < 2 &&
(methodSymbol.Parameters.Length == 0 || IsСompatibleType(methodSymbol.Parameters.First().Type, fieldType)));
public static bool CanAppendAttribute(string attributeName) {
return !(attributeName.StartsWith(AttributesGenerator.DxPropertyAttributeFullName!) ||
attributeName.StartsWith(AttributesGenerator.PrismPropertyAttributeFullName!) ||
attributeName.StartsWith(AttributesGenerator.MvvmLightPropertyAttributeFullName!));
}
}
}
2 changes: 2 additions & 0 deletions DevExpress.Mvvm.CodeGenerators/Info/ContextInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ class ContextInfo {
public INamedTypeSymbol INPCingSymbol { get; }
public INamedTypeSymbol TaskSymbol { get; }
public INamedTypeSymbol BoolSymbol { get; }
public INamedTypeSymbol AttributeUsageSymbol { get; }

public bool IsWinUI { get; }
public List<SupportedMvvm> AvailableMvvm { get; }
Expand All @@ -81,6 +82,7 @@ public ContextInfo(GeneratorExecutionContext context, Compilation compilation) {

TaskSymbol = compilation.GetTypeByMetadataName("System.Threading.Tasks.Task")!;
BoolSymbol = compilation.GetTypeByMetadataName("System.Boolean")!;
AttributeUsageSymbol = compilation.GetTypeByMetadataName("System.AttributeUsageAttribute")!;

IsWinUI = GetIsWinUI(compilation);
}
Expand Down