]> granicus.if.org Git - clang/commitdiff
python bindings: Get the string representation of a CompletionChunk
authorTobias Grosser <grosser@fim.uni-passau.de>
Sat, 5 Feb 2011 17:54:07 +0000 (17:54 +0000)
committerTobias Grosser <grosser@fim.uni-passau.de>
Sat, 5 Feb 2011 17:54:07 +0000 (17:54 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@124958 91177308-0d34-0410-b5e6-96231b3b80d8

bindings/python/clang/cindex.py

index 1982f6c1c4bc43ab0e59180477d1c8be3af41b2e..ae4432831c335e950f995dacceba0672adbe750c 100644 (file)
@@ -720,13 +720,78 @@ _clang_getDiagnosticFixIt.errcheck = _CXString.from_result
 
 ###
 
+class CodeCompletionChunk:
+    def __init__(self, completionString, key):
+        self.cs = completionString
+        self.key = key
+
+    def __repr__(self):
+        return self.spelling
+
+    @property
+    def spelling(self):
+        return _clang_getCompletionChunkText(self.cs, self.key).spelling
+
+    @property
+    def kind(self):
+        return _clang_getCompletionChunkKind(self.cs, self.key)
+
+    @property
+    def string(self):
+        res = _clang_getCompletionChunkCompletionString(self.cs, self.key)
+
+        if (res):
+          return CompletionString(res)
+        else:
+          None
+
+class CompletionString(ClangObject):
+    def __len__(self):
+        return _clang_getNumCompletionChunks(self.obj)
+
+    def __getitem__(self, key):
+        if len(self) <= key:
+            raise IndexError
+        return CodeCompletionChunk(self.obj, key)
+
+    @property
+    def priority(self):
+        return _clang_getCompletionPriority(self.obj)
+
+    @property
+    def availability(self):
+        return _clang_getCompletionAvailability(self.obj)
+
+    def __repr__(self):
+        return "".join([str(a) for a in self])
+
 class CodeCompletionResult(Structure):
-    _fields_ = [('cursorKind', c_int), ('completionString', c_void_p)]
+    _fields_ = [('cursorKind', c_int), ('completionString', c_object_p)]
+
+    def __repr__(self):
+        return str(CompletionString(self.completionString))
+
+    @property
+    def kind(self):
+        return CursorKind.from_id(self.cursorKind)
+
+    @property
+    def string(self):
+        return CompletionString(self.completionString)
 
 class CCRStructure(Structure):
     _fields_ = [('results', POINTER(CodeCompletionResult)),
                 ('numResults', c_int)]
 
+    def __len__(self):
+        return self.numResults
+
+    def __getitem__(self, key):
+        if len(self) <= key:
+            raise IndexError
+
+        return self.results[key]
+
 class CodeCompletionResults(ClangObject):
     def __init__(self, ptr):
         assert isinstance(ptr, POINTER(CCRStructure)) and ptr
@@ -740,14 +805,7 @@ class CodeCompletionResults(ClangObject):
 
     @property
     def results(self):
-        class ResultsItr:
-            def __init__(self, ccr):
-                self.ccr = ccr
-
-            def __len__(self):
-                return self.ccr.ptr.contents.numResults
-
-        return ResultsItr(self)
+        return self.ptr.contents
 
     @property
     def diagnostics(self):
@@ -1147,6 +1205,23 @@ _clang_codeCompleteGetDiagnostic = lib.clang_codeCompleteGetDiagnostic
 _clang_codeCompleteGetDiagnostic.argtypes = [CodeCompletionResults, c_int]
 _clang_codeCompleteGetDiagnostic.restype = Diagnostic
 
+_clang_getCompletionChunkText = lib.clang_getCompletionChunkText
+_clang_getCompletionChunkText.argtypes = [c_void_p, c_int]
+_clang_getCompletionChunkText.restype = _CXString
+
+_clang_getNumCompletionChunks = lib.clang_getNumCompletionChunks
+_clang_getNumCompletionChunks.argtypes = [c_void_p]
+_clang_getNumCompletionChunks.restype = c_int
+
+_clang_getCompletionAvailability = lib.clang_getCompletionAvailability
+_clang_getCompletionAvailability.argtypes = [c_void_p]
+_clang_getCompletionAvailability.restype = c_int
+
+_clang_getCompletionPriority = lib.clang_getCompletionPriority
+_clang_getCompletionPriority.argtypes = [c_void_p]
+_clang_getCompletionPriority.restype = c_int
+
+
 ###
 
 __all__ = ['Index', 'TranslationUnit', 'Cursor', 'CursorKind',