over 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 dumpnode(node, indent):
    print ' ' * indent, node.kind, node.spelling
    for i in node.get_children():
        dumpnode(i, indent+2)

def srcrangestr(x):
    return '%s:%d:%d - %s:%d:%d' % (x.start.file, x.start.line, x.start.column, x.end.file, x.end.line, x.end.column)

def dumptoken(node):
    for x in node.get_tokens():
        print x.kind
        print "  " + srcrangestr(x.extent)
        print "  '" + str(x.spelling) + "'"

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'])

    dumpnode(tu.cursor, 0)
    dumptoken(tu.cursor)

if __name__ == '__main__':
    main()
    

玩法:

python xxx.py MyObject.m

就可以看到parse後的node和token名稱了

← YouCompleteMe for iOS 折騰筆記 Get Kong折騰筆記 →
 
comments powered by Disqus