Commits

Ivan Vučica committed 94d6520

Annotating tokens with cursors. Colorizing based on cursors.

Comments (0)

Files changed (1)

Editor Teksta/IVSourceCodeDocument.m

 
     CXToken * tokens;
     unsigned int numTokens;
-    
     clang_tokenize(self.codeCompletionTranslationUnit, entireFileRange, &tokens, &numTokens);
+    
+    CXCursor cursors[numTokens];
+    clang_annotateTokens(self.codeCompletionTranslationUnit, tokens, numTokens, cursors);
+    
     for(int i = 0; i < numTokens; i++)
     {
         CXToken token = tokens[i];
+        CXSourceLocation location = clang_getTokenLocation(self.codeCompletionTranslationUnit, token);
+        //CXCursor cursor = clang_getCursor(self.codeCompletionTranslationUnit, location);
+        CXCursor cursor = cursors[i];
         CXTokenKind kind = clang_getTokenKind(token);
+        
         NSColor * c = nil;
         switch(kind)
         {
                 c = [NSColor colorWithCalibratedRed:0 green:0.5 blue:0 alpha:1.];
                 break;
             case CXToken_Identifier:
+            {
                 c = [NSColor blackColor];
+#if 0
+                const char *start = NULL, *end = NULL;
+                unsigned startCol, endCol, startRow, endRow;
+                clang_getDefinitionSpellingAndExtent(cursor, &start, &end, &startCol, &endCol, &startRow, &endRow);
+                for(const char * p = start; p != end; p++)
+                {
+                    printf("%c", *p);
+                }
+                printf("\n");
+#endif
                 break;
+            }
             case CXToken_Literal:
                 c = [NSColor redColor];
                 break;
                 c = [NSColor blackColor];
                 break;
         }
+        
+        enum CXCursorKind cursorKind = clang_getCursorKind(cursor);
+    redoCursorKind:
+        NSLog(@"Kind %d\n", cursorKind);
+        switch(cursorKind)
+        {
+            case CXCursor_TypeRef:
+            {
+                /*
+                CXCursor referencedCursor = clang_getCursorReferenced(cursor);
+                if(referencedCursor != 0)
+                 */
+                c = [NSColor orangeColor];
+                break;
+            }
+            case CXCursor_FunctionDecl:
+                /*
+                 c = [NSColor redColor];
+                cursorKind = clang_getCursorKind(clang_getCursor(self.codeCompletionTranslationUnit, location));
+                CXType type = clang_getCursorResultType(cursor);
+                
+                NSLog(@"%s", clang_getCString(clang_getTypeKindSpelling(type.kind)));
+                if(cursorKind != CXCursor_FunctionDecl)
+                    goto redoCursorKind;
+                */
+                break;
+            case CXCursor_DeclRefExpr:
+                c = [NSColor blueColor];
+                break;
+            case CXCursor_PreprocessingDirective:
+                c = [NSColor brownColor];
+                break;
+            case CXCursor_FirstInvalid:
+                break;
+        }
+        if(cursorKind == CXCursor_FirstInvalid)
+        {
+           // break;
+        }
+        
         CXSourceRange range = clang_getTokenExtent(self.codeCompletionTranslationUnit, token);
      
         CXSourceLocation start = clang_getRangeStart(range);
         unsigned int endOffset;
         clang_getSpellingLocation(start, file, NULL, NULL, &startOffset);
         clang_getSpellingLocation(end, file, NULL, NULL, &endOffset);
+        NSRange nsrange = NSMakeRange(startOffset, endOffset-startOffset);
         if(c)
-            [mas addAttribute:NSForegroundColorAttributeName value:c range:NSMakeRange(startOffset, endOffset-startOffset)];
+            [mas addAttribute:NSForegroundColorAttributeName value:c range:nsrange];
+        NSLog(@"%@", [[mas string] substringWithRange:nsrange]);
     }
     
     self.content = mas;