Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change enum prefix detection strategy #145

Merged
merged 20 commits into from
Oct 6, 2023

Conversation

PadraigK
Copy link
Contributor

@PadraigK PadraigK commented Oct 1, 2023

Fixes issue: #144

The generator will now find the common prefix among all case names instead of relying on the enum definition name.

You can see the changes to the generated API in this gist: https://gist.github.com/PadraigK/af78939c3e597635d6db5dc98e7b6c64

It will now find the common prefix among all case names instead of relying on the enum definition name.
@PadraigK
Copy link
Contributor Author

PadraigK commented Oct 1, 2023

This needs a few more tweaks to compile, I'll get to them later on today.

@PadraigK
Copy link
Contributor Author

PadraigK commented Oct 1, 2023

Ok, this should be ready for a review now. I think this is a nice improvement in the naming of enums but it impacts a lot of stuff.

It introduces one slight naming regression for MethodFlags cases because the inconsistent prefix on that enum means we don't identify a great prefix (more context here: #23) — 

Relevant part of the diff:

-    public static let methodFlagNormal = MethodFlags (rawValue: 1)
-    public static let methodFlagEditor = MethodFlags (rawValue: 2)
-    public static let methodFlagConst = MethodFlags (rawValue: 4)
-    public static let methodFlagVirtual = MethodFlags (rawValue: 8)
-    public static let methodFlagVararg = MethodFlags (rawValue: 16)
-    public static let tatic = MethodFlags (rawValue: 32)
-    public static let methodFlagObjectCore = MethodFlags (rawValue: 64)
-    public static let `default` = MethodFlags (rawValue: 1)
+    public static let flagNormal = MethodFlags (rawValue: 1)
+    public static let flagEditor = MethodFlags (rawValue: 2)
+    public static let flagConst = MethodFlags (rawValue: 4)
+    public static let flagVirtual = MethodFlags (rawValue: 8)
+    public static let flagVararg = MethodFlags (rawValue: 16)
+    public static let flagStatic = MethodFlags (rawValue: 32)
+    public static let flagObjectCore = MethodFlags (rawValue: 64)
+    public static let flagsDefault = MethodFlags (rawValue: 1)

The impact is that the default case is called flagsDefault. I think we should just special-case this enum. I'd like to handle that separately though, but if its a blocker I can do it on this PR.

@migueldeicaza
Copy link
Owner

I think it is ok to special-case the default case to get that name in place.

I also think that we should special case the keys, because I am not a fan of "_1" as an enum value vs say "key1".

Adjust linker settings since dynamic_lookup is macOS-specific
@PadraigK
Copy link
Contributor Author

PadraigK commented Oct 2, 2023

Sounds good, I'll get those changes in soon.

Instead of prefixing with "_" when the name would be invalid, lets keep the original prefix.
@PadraigK
Copy link
Contributor Author

PadraigK commented Oct 2, 2023

Ok should be good now; anytime there would be an invalid swift name, it does the same thing it would have done before, so there is no change on those cases; e.g. we get key1 instead of _1

Here's the diff in API: https://gist.github.com/PadraigK/af78939c3e597635d6db5dc98e7b6c64

Comment on lines +82 to +84
func commonPrefix() -> String {
map(\.name).commonPrefix()?.dropAfterLastUnderscore() ?? ""
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is commonPrefix often invoked to warrant providing this in an extension?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is used in 2 or 3 places. I did it like this to ensure consistent use at call sites, since things like default method parameter mappings need to follow the same pattern as the case definition.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No idea how to mark this conversation as resolved since the button to do so isn’t on my end, but consider this conversation “resolved” for all intents and purposes.

Generator/Generator/StringOperations.swift Outdated Show resolved Hide resolved
Generator/Generator/StringOperations.swift Outdated Show resolved Hide resolved
PadraigK and others added 2 commits October 5, 2023 14:38
Co-authored-by: Marquis Kurt <software@marquiskurt.net>
@migueldeicaza
Copy link
Owner

Thank you so much for this contribution, this a significant usability win. Merging it.

@migueldeicaza migueldeicaza merged commit 4e8492c into migueldeicaza:main Oct 6, 2023
1 check passed
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.

4 participants