almost 6 years ago
設定方式和上一篇一樣:
把clang_complete裡的cindex.py、init.py、enumerations.py抓下來放在./clang
的資料夾下,再指定libclang.dylib的位置(下面的path變數),就可以開始玩了
import sys
from clang.cindex import Index, SourceLocation, Cursor, File, CursorKind, TypeKind, Config, LibclangError
def getQuickFix(diagnostic):
# Some diagnostics have no file, e.g. "too many errors emitted, stopping now"
if diagnostic.location.file:
filename = diagnostic.location.file.name
else:
filename = ""
if diagnostic.severity == diagnostic.Ignored:
type = 'I'
elif diagnostic.severity == diagnostic.Note:
type = 'I'
elif diagnostic.severity == diagnostic.Warning:
type = 'W'
elif diagnostic.severity == diagnostic.Error:
type = 'E'
elif diagnostic.severity == diagnostic.Fatal:
type = 'E'
else:
return None
res = dict({ 'buf' : filename,
'lnum' : diagnostic.location.line,
'col' : diagnostic.location.column,
'text' : diagnostic.spelling,
'type' : type})
return res
def getQuickFixList(tu):
return filter (None, map (getQuickFix, tu.diagnostics))
def init():
conf = Config()
# here we use the libclang.dylib from the vim plugin -- YouCompleteMe
path = "/Users/<UserName>/.vim/bundle/YouCompleteMe/third_party/ycmd"
Config.set_library_path(path)
conf.set_library_path(path)
try:
conf.get_cindex_library()
except LibclangError as e:
print "Error: " + str(e)
def main():
init()
index = Index.create()
print sys.argv[1]
tu = index.parse(sys.argv[1], args=['-x', 'objective-c'])
print getQuickFixList(tu)
if __name__ == '__main__':
main()
玩法:
python xxx.py MyObject.m
就可以看到各種編譯錯誤(如果有的話)