Skip to content

Add --with-enum-member-strip for per-enum member name stripping - #770

Merged
tannergooding merged 2 commits into
dotnet:mainfrom
tannergooding:tannergooding-enum-member-strip-modes
Jul 14, 2026
Merged

Add --with-enum-member-strip for per-enum member name stripping#770
tannergooding merged 2 commits into
dotnet:mainfrom
tannergooding:tannergooding-enum-member-strip-modes

Conversation

@tannergooding

Copy link
Copy Markdown
Member

Note

This PR body was drafted by Copilot.

Resolves #461 -- adds a single, mode-based option for stripping a common prefix or suffix from an enum's member names, so enum abc_some_enum { abc_some_enum_key1, ... } can generate first/second/... instead of the fully-qualified member names.

--with-enum-member-strip <enum-name>=<mode> (alias -wems) is a per-enum knob keyed by the remapped enum name, reusing the existing with-* dictionary plumbing: * sets a global default and per-enum entries override it. Modes:

  • none -- leave the members alone.
  • common-prefix -- auto-detect the longest common prefix of the members.
  • common-suffix -- same, for the "common POSTfix instead of prefix" case.
  • type-name -- strip the enum type name (identical to strip-enum-member-type-name).
  • prefix:<str> / suffix:<str> -- strip an explicit string.

Honored edge cases for the auto-detect modes:

  • The detected prefix/suffix is trimmed back to the nearest _ token boundary so a non-obvious prefix never cuts mid-token (abc_some_enum_key1/key2 -> key1/key2, not 1/2).
  • A leading digit after stripping is guarded with _ since it isn't a valid C# identifier start (abc_version_1_0 -> _1_0).
  • Stripping is all-or-nothing per enum: if any member would become empty or two would collide, the whole enum keeps its original names and an Info diagnostic is emitted naming the enum and the reason, so the member set stays internally consistent.
  • The strip is applied identically at the declaration and at every sibling reference in initializer expressions (e.g. primary = vulkan | metal), so the generated bindings still compile.

This composes with, and falls back to, the existing behavior: when no mode resolves for an enum, the legacy strip-enum-member-type-name / --typePrefixStrip handling runs unchanged. using static <EnumType>; also remains available as a zero-config workaround.

Added golden tests covering common-prefix/-suffix, explicit prefix:/suffix:, the digit guard, single-member no-op, collision abandonment with the diagnostic, type-name matching the legacy behavior, the * default with a per-enum override, and decl/ref consistency for sibling references.

tannergooding and others added 2 commits July 14, 2026 09:59
Adds a per-enum option keyed by the remapped enum name (with a * global
default) selecting how member names are stripped: none, common-prefix,
common-suffix, type-name, prefix:<str>, or suffix:<str>. Auto-detected
prefixes/suffixes are trimmed to a _ token boundary and applied
all-or-nothing per enum, abandoning the strip with an Info diagnostic when it
would empty or collide members. Legacy strip-enum-member-type-name and
--typePrefixStrip remain the fallback when no mode resolves.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Covers common-prefix/-suffix, explicit prefix:/suffix:, the leading-digit
guard, single-member no-op, collision abandonment with an Info diagnostic,
type-name matching the legacy behavior, the '*' default with a per-enum
override, and decl/ref consistency for sibling references.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@tannergooding
tannergooding merged commit b951c1f into dotnet:main Jul 14, 2026
14 checks passed
@tannergooding
tannergooding deleted the tannergooding-enum-member-strip-modes branch July 14, 2026 19:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Enum key stripping

1 participant