Skip to content

Commit 8a9fc91

Browse files
committed
Add support for "On" prefix for command methods
1 parent d4a8971 commit 8a9fc91

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

CommunityToolkit.Mvvm.SourceGenerators/Input/ICommandGenerator.Execute.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,17 @@ public static (string FieldName, string PropertyName) GetGeneratedFieldAndProper
350350
{
351351
string propertyName = methodSymbol.Name;
352352

353+
// Strip the "On" prefix, if present. Only do this if the name is longer than 2 characters,
354+
// and if the 3rd character is not a lowercase letter. This is needed to avoid accidentally
355+
// stripping fales positives for "On" prefixes in names such as "Onboard".
356+
if (propertyName.Length > 2 &&
357+
propertyName.StartsWith("On") &&
358+
!char.IsLower(propertyName[2]))
359+
{
360+
propertyName = propertyName.Substring(2);
361+
}
362+
363+
// Strip the "Async" suffix for methods returning a Task type
353364
if (methodSymbol.Name.EndsWith("Async") &&
354365
(methodSymbol.ReturnType.HasFullyQualifiedName("global::System.Threading.Tasks.Task") ||
355366
methodSymbol.ReturnType.InheritsFromFullyQualifiedName("global::System.Threading.Tasks.Task")))

tests/CommunityToolkit.Mvvm.UnitTests/Test_ICommandAttribute.cs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,34 @@ public void Test_ICommandAttribute_CultureAwareCommandName()
472472
model.InitializeCommand.Execute(null);
473473
}
474474

475+
// See https://github.com/CommunityToolkit/dotnet/issues/162
476+
[TestMethod]
477+
public async Task Test_ICommandAttribute_WithOnPrefixes()
478+
{
479+
ModelWithCommandMethodsWithOnPrefixes model = new();
480+
481+
Assert.IsFalse(model.HasOnCommandRun);
482+
Assert.IsFalse(model.HasOnboardCommandRun);
483+
Assert.IsFalse(model.HasSubmitCommandRun);
484+
Assert.IsFalse(model.HasDownloadCommandRun);
485+
486+
model.OnCommand.Execute(null);
487+
488+
Assert.IsTrue(model.HasOnCommandRun);
489+
490+
model.OnboardCommand.Execute(null);
491+
492+
Assert.IsTrue(model.HasOnboardCommandRun);
493+
494+
model.SubmitCommand.Execute(null);
495+
496+
Assert.IsTrue(model.HasSubmitCommandRun);
497+
498+
await model.DownloadCommand.ExecuteAsync(null);
499+
500+
Assert.IsTrue(model.HasDownloadCommandRun);
501+
}
502+
475503
#region Region
476504
public class Region
477505
{
@@ -806,4 +834,41 @@ private void Initialize()
806834
{
807835
}
808836
}
837+
838+
partial class ModelWithCommandMethodsWithOnPrefixes
839+
{
840+
public bool HasOnCommandRun { get; private set; }
841+
842+
public bool HasOnboardCommandRun { get; private set; }
843+
844+
public bool HasSubmitCommandRun { get; private set; }
845+
846+
public bool HasDownloadCommandRun { get; private set; }
847+
848+
[ICommand]
849+
private void On()
850+
{
851+
HasOnCommandRun = true;
852+
}
853+
854+
[ICommand]
855+
private void Onboard()
856+
{
857+
HasOnboardCommandRun = true;
858+
}
859+
860+
[ICommand]
861+
private void OnSubmit()
862+
{
863+
HasSubmitCommandRun = true;
864+
}
865+
866+
[ICommand]
867+
private async Task OnDownloadAsync()
868+
{
869+
await Task.Delay(100);
870+
871+
HasDownloadCommandRun = true;
872+
}
873+
}
809874
}

0 commit comments

Comments
 (0)