-
Notifications
You must be signed in to change notification settings - Fork 15.2k
Use existing AvailabilityKind enum for code completion availability #160296
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
base: main
Are you sure you want to change the base?
Conversation
|
@llvm/pr-subscribers-clang Author: Jannick Kremer (DeinAlptraum) ChangesThis adresses point 4 from #156680. This is a necessary step before The Full diff: https://github.com/llvm/llvm-project/pull/160296.diff 1 Files Affected:
diff --git a/clang/bindings/python/clang/cindex.py b/clang/bindings/python/clang/cindex.py
index 13a91d83ede1c..7f2c2183ec1bb 100644
--- a/clang/bindings/python/clang/cindex.py
+++ b/clang/bindings/python/clang/cindex.py
@@ -3039,6 +3039,16 @@ class _CXUnsavedFile(Structure):
}
+# Converting the new enum names (full upper-case, underscore separated)
+# to the old ones (separated by capitalization), e.g. RESULT_TYPE -> ResultType
+def _kind_to_old_name(kind: BaseEnumeration):
+ # Remove underscores
+ components = kind.name.split("_")
+ # Upper-camel case each split component
+ components = [component.lower().capitalize() for component in components]
+ return "".join(components)
+
+
class CompletionChunk:
class Kind:
def __init__(self, name: str):
@@ -3165,9 +3175,9 @@ def priority(self) -> int:
return conf.lib.clang_getCompletionPriority(self.obj) # type: ignore [no-any-return]
@property
- def availability(self) -> CompletionChunk.Kind:
+ def availability(self) -> AvailabilityKind:
res = conf.lib.clang_getCompletionAvailability(self.obj)
- return availabilityKinds[res]
+ return AvailabilityKind.from_id(res)
@property
def briefComment(self) -> str:
@@ -3179,20 +3189,12 @@ def __repr__(self) -> str:
+ " || Priority: "
+ str(self.priority)
+ " || Availability: "
- + str(self.availability)
+ + _kind_to_old_name(self.availability)
+ " || Brief comment: "
+ str(self.briefComment)
)
-availabilityKinds = {
- 0: CompletionChunk.Kind("Available"),
- 1: CompletionChunk.Kind("Deprecated"),
- 2: CompletionChunk.Kind("NotAvailable"),
- 3: CompletionChunk.Kind("NotAccessible"),
-}
-
-
class CodeCompletionResult(Structure):
_fields_ = [("cursorKind", c_int), ("completionString", c_object_p)]
|
|
@Endilll about
This is not entirely possible: since we want to switch to returning the proper As mentioned above, I changed the |
Endilll
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you also add a test to ensure that nothing breaks?
Is there anything preventing us from providing our own |
There are currently two ways to get AvailabilityKinds out of the API (except for intializing them yourself): accessing the Changing the |
What would you like to be tested?
which I used to ensure that the format is correct (I kept adapting the converter function above until these tests passed) |
Co-authored-by: Vlad Serebrennikov <[email protected]>
|
I think it's also worth asking whether we want to go with this approach at all: this is already a breaking change w.r.t. the string representations of the |
| to the old ones (separated by capitalization), e.g. RESULT_TYPE -> ResultType | ||
| """ | ||
| # Remove underscores | ||
| components = kind.name.split("_") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now that this function has a new name, I found myself confused why it takes an enumerator and extracts a string out of it. I think it should just take a string instead.
Fair.
I believe that today there are only three ways to work with the return value of
(Note that they are not even equality comparable, so they have to be converted to strings: This third usage is the one we should care about, and the one I'd like to be tested.
Consequently, I don't think we need to be concerned with the exact output of either
I think we need
|
This adresses point 4 from #156680. This is a necessary step before
CompletionChunk.Kindcan be removed.The
ChunkCompletion.Kindimplements__str__and__repr__differently from our other enum classes. I have adapted the__repr__ofCompletionStringto stringify the availability of the chunk differently so that it still looks the same as before.