Validate names for invalid for C# namespaces in cmdpal (#39401)

## Summary of the Pull Request

The Command Palette allows users to create extensions with dashes in their names, which is invalid for C# namespaces. This causes projects to fail during build because the name cannot be used as a valid namespace.

## Changes
- Updated the validation regex in `NewExtensionForm.cs` to only allow valid C# identifiers
  - Now only allows names starting with a letter or underscore, followed by letters, numbers, or underscores
  - Explicitly prevents dashes, spaces, and other special characters
- Improved the error message to clearly explain the requirements for valid C# identifiers

## Before
Previously, the input only validated that no spaces were used:
```csharp
"regex": "^[^\\s]+$"
```

## After
Now the input properly validates for C# namespace compatibility:
```csharp
"regex": "^[a-zA-Z_][a-zA-Z0-9_]*$"
```

This ensures that users cannot create extension projects with names that would fail to build.![image](https://github.com/user-attachments/assets/c2fb5108-b32b-4411-84a8-45ef0c621372)

## PR Checklist

- [ ] **Closes:** https://github.com/microsoft/PowerToys/issues/38522
This commit is contained in:
Clint Rutkas
2025-05-14 20:21:42 +00:00
committed by GitHub
parent fce3c2d537
commit 9bcb140af1
3 changed files with 3 additions and 3 deletions

View File

@@ -55,7 +55,7 @@ internal sealed partial class NewExtensionForm : NewExtensionFormBase
"errorMessage": {{FormatJsonString(Properties.Resources.builtin_create_extension_name_required)}},
"id": "ExtensionName",
"placeholder": "ExtensionName",
"regex": "^[^\\s]+$"
"regex": "^[a-zA-Z_][a-zA-Z0-9_]*$"
},
{
"type": "TextBlock",

View File

@@ -178,7 +178,7 @@ namespace Microsoft.CmdPal.UI.ViewModels.Properties {
}
/// <summary>
/// Looks up a localized string similar to Extension name is required, without spaces.
/// Looks up a localized string similar to Extension name is required and must be a valid C# identifier (start with a letter or underscore, followed by letters, numbers, or underscores).
/// </summary>
public static string builtin_create_extension_name_required {
get {

View File

@@ -195,7 +195,7 @@
<value>Extension name</value>
</data>
<data name="builtin_create_extension_name_required" xml:space="preserve">
<value>Extension name is required, without spaces</value>
<value>Extension name is required and must be a valid C# identifier (start with a letter or underscore, followed by letters, numbers, or underscores)</value>
</data>
<data name="builtin_create_extension_display_name_header" xml:space="preserve">
<value>Display name</value>