Commits

Ivan Vučica  committed 92e5faa

Cleaned up parsing and autocompleting a bit. Printing compile diagnostic to the console.

  • Participants
  • Parent commits e63286b

Comments (0)

Files changed (2)

File IDE/ZCTextEditorViewController.h

 }
 @property (nonatomic, retain) NSTextView *textView;
 
+#if HAVE_LIBCLANG
 // code completion stuff should be kept and ran in the project, not in the text editor.
 // in text editor, we may refer to the data stored in the project, but it should not
 // be the primary source of info.
 
 @property (nonatomic, assign) CXIndex codeCompletionIndex; 
-@property (nonatomic, assign) CXTranslationUnit codeCompletionTranslationUnit; 
+@property (nonatomic, assign) CXTranslationUnit codeCompletionTranslationUnit;
+#endif
+
 @end

File IDE/ZCTextEditorViewController.m

  */
 
 #import "ZCTextEditorViewController.h"
+#define DEFINE_ARGS const char* args[] = { \
+    "-x", "objective-c", \
+    "-isysroot", "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk", \
+    "-I", "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk/usr/include/c++/4.2.1/tr1", \
+    "-mmacosx-version-min=10.6", \
+    "-I", [[self.fileURL path] stringByDeletingLastPathComponent].UTF8String \
+    };
 
+@interface ZCTextEditorViewController ()
+@property (nonatomic, retain, readonly) NSURL * fileURL;
+@end
 
 @implementation ZCTextEditorViewController
 @synthesize textView;
     [textView setDelegate:self];
 #if HAVE_LIBCLANG
     self.codeCompletionIndex = clang_createIndex(0, 0);
+
+    DEFINE_ARGS;
     
-    struct CXUnsavedFile unsavedFile;
-    unsavedFile.Contents = [[self.textView.textStorage string] UTF8String];
-    unsavedFile.Filename = [self.fileName UTF8String];
-    unsavedFile.Length = [[self.textView.textStorage string] length];
-    self.codeCompletionTranslationUnit = clang_parseTranslationUnit(self.codeCompletionIndex, [self.fileName UTF8String], (const char*[]){"codecompletion", "-x","objective-c","-isysroot","/Xcode4/SDKs/MacOSX10.6.sdk","-mmacosx-version-min=10.6","-I",[self.fileName stringByDeletingLastPathComponent].UTF8String}, 8, &unsavedFile, 1, clang_defaultEditingTranslationUnitOptions());
+    self.codeCompletionTranslationUnit = clang_parseTranslationUnit(self.codeCompletionIndex, [self.fileName UTF8String], args, 8, NULL, 0, clang_defaultEditingTranslationUnitOptions());
+    [self printDiagnostics];
     
 #endif
 }
     unsavedFile.Contents = [[self.textView.textStorage string] UTF8String];
     unsavedFile.Filename = [self.fileName UTF8String];
     unsavedFile.Length = [[self.textView.textStorage string] length];
-    return clang_reparseTranslationUnit(self.codeCompletionTranslationUnit, 1, &unsavedFile, clang_defaultReparseOptions(self.codeCompletionTranslationUnit) | CXTranslationUnit_CacheCompletionResults);
+    int returnValue = clang_reparseTranslationUnit(self.codeCompletionTranslationUnit, 1, &unsavedFile, clang_defaultReparseOptions(self.codeCompletionTranslationUnit) | CXTranslationUnit_CacheCompletionResults);
+    [self printDiagnostics];
+    return returnValue;
+}
+-(void)printDiagnostics
+{
+    if(!self.codeCompletionTranslationUnit)
+    {
+        NSLog(@"No file parsed ok, cannot code-complete");
+        return;
+    }
+    int num = clang_getNumDiagnostics(self.codeCompletionTranslationUnit);
+    for(int i = 0; i < num; i++)
+    {
+        CXDiagnostic d = clang_getDiagnostic(self.codeCompletionTranslationUnit, i);
+        CXString s = clang_formatDiagnostic(d,                                          clang_defaultDiagnosticDisplayOptions());
+        fprintf(stderr, "%s\n", clang_getCString(s));
+        clang_disposeString(s);
+    }
 }
 #endif
 -(void)dealloc
 }
 -(void)_loadFile
 {
-    NSString *fileContents = [NSString stringWithContentsOfFile:fileName];
+    NSString *fileContents = [NSString stringWithContentsOfFile:fileName encoding:NSUTF8StringEncoding error:NULL];
     
     if(!fileContents)
         return;
     //[self reparseTranslationUnit];
 }
 #endif
+
+-(NSURL *)fileURL
+{
+    return [NSURL fileURLWithPath:self.fileName];
+}
 @end