[clang] Use existing AvailabilityKind enum for code completion availability (PR #160296)

via cfe-commits cfe-commits at lists.llvm.org
Tue Sep 23 06:40:38 PDT 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Jannick Kremer (DeinAlptraum)

<details>
<summary>Changes</summary>

This adresses point 4 from #<!-- -->156680. This is a necessary step before `CompletionChunk.Kind` can be removed.

The `ChunkCompletion.Kind` implements `__str__` and `__repr__` differently from our other enum classes. I have adapted the `__repr__` of `CompletionString` to stringify the availability of the chunk differently so that it still looks the same as before.

---
Full diff: https://github.com/llvm/llvm-project/pull/160296.diff


1 Files Affected:

- (modified) clang/bindings/python/clang/cindex.py (+13-11) 


``````````diff
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)]
 

``````````

</details>


https://github.com/llvm/llvm-project/pull/160296


More information about the cfe-commits mailing list