Skip to content

Commit

Permalink
clean code
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikita Malyavin committed Nov 18, 2012
1 parent 6897534 commit 0894e5b
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 61 deletions.
85 changes: 49 additions & 36 deletions clavim.py
Expand Up @@ -13,25 +13,24 @@
import clang.cindex import clang.cindex
import vim import vim



def clavim_init(): def clavim_init():
sys.argv[0] = ''
sys.path.append(vim.eval('s:plugin_path'))
global index global index
index = clang.cindex.Index.create() index = clang.cindex.Index.create()
global translationUnits global translationUnits
translationUnits = dict() translationUnits = {}
global update
update = False


def get_current_file(): def get_current_file():
file = "\n".join(vim.eval("getline(1, '$')")) file = "\n".join(vim.eval("getline(1, '$')"))
return (vim.current.buffer.name, file) return (vim.current.buffer.name, file)


def get_current_translation_unit(update = False): def get_current_translation_unit(current_file):
current_file = get_current_file()
filename = vim.current.buffer.name filename = vim.current.buffer.name
if filename in translationUnits: if filename in translationUnits:
tu = translationUnits[filename] tu = translationUnits[filename]
if update: tu.reparse([current_file])
tu.reparse([current_file])
return tu return tu


tu = index.parse(filename) tu = index.parse(filename)
Expand All @@ -41,40 +40,57 @@ def get_current_translation_unit(update = False):
return None return None


translationUnits[filename] = tu translationUnits[filename] = tu
tu.reparse([current_file])
return tu return tu


def cursorvisit_callback(node, parent, userdata): def cursorvisit_callback(node, parent, data):
if node.kind == userdata['kind']: f = node.extent.start.file
if node.extent.start.file is None: lib = clang.cindex.conf.lib
return 2 if node.kind != data['kind']:
lib = clang.cindex.conf.lib return 2

if f is None:
my_node = dict() return 2
my_node['name'] = lib.clang_getCursorDisplayName(node)
my_node['kind'] = node.kind.name converted_node = {
my_node['file'] = node.extent.start.file.name 'name': lib.clang_getCursorDisplayName(node),
my_node['line'] = node.location.line 'kind': node.kind.name,
my_node['start'] = node.extent.start.column 'file': f.name,
my_node['end'] = node.extent.end.column 'line': node.location.line,
userdata['nodes'].append(my_node) 'start': node.extent.start.column,
'end': node.extent.end.column,
}
data['nodes'].append(converted_node)
return 2 return 2




def find_cursors(tu, kind): def find_cursors(tu, kind):
nodes = [] callback_data = {
userdata = dict() 'nodes': [],

'kind': kind,
userdata['nodes'] = nodes }
userdata['kind'] = kind


# visit children # visit children
clang.cindex.conf.lib.clang_visitChildren( clang.cindex.conf.lib.clang_visitChildren(
tu.cursor, tu.cursor,
clang.cindex.callbacks['cursor_visit'](cursorvisit_callback), clang.cindex.callbacks['cursor_visit'](cursorvisit_callback),
userdata) callback_data
)

return callback_data['nodes']

def highlight_expressions():
global cursors
# find all clang cursors
tu = get_current_translation_unit(get_current_file)
MEMBER_REF_EXPR = clang.cindex.CursorKind.MEMBER_REF_EXPR
cursors = find_cursors(tu, MEMBER_REF_EXPR)

keys = 'line start end'.split()
syn_match = r"syn match clavimMember /\%%%sl\%%%sc.*\%%%sc/"
for x in cursors:
if x['file'] == vim.current.buffer.name:
t = tuple(str(x[k]) for k in keys)
vim.command(syn_match % t)


return nodes


def main(): def main():
index = clang.cindex.Index.create() index = clang.cindex.Index.create()
Expand All @@ -84,11 +100,8 @@ def main():
nodes = find_cursors(tu, kind) nodes = find_cursors(tu, kind)


print 'nodes (%s):' % (len(nodes)) print 'nodes (%s):' % (len(nodes))
keys = 'name kind file line start end'.split()
template = ' '.join(['%1s = %%(%1s)s' % k for k in keys])
for x in nodes: for x in nodes:
print 'name=%s kind=%s file=%s line=%s start=%s end=%s' % ( print template % x
x['name'],
x['kind'],
x['file'],
x['line'],
x['start'],
x['end'])
28 changes: 3 additions & 25 deletions clavim.vim
Expand Up @@ -6,9 +6,6 @@ hi clavimMember ctermbg=Cyan ctermfg=Black guibg=#8CCBEA guifg=Black
hi clavimError ctermbg=Red ctermfg=Black guibg=Red guifg=Black hi clavimError ctermbg=Red ctermfg=Black guibg=Red guifg=Black


function! s:ClavimInit() function! s:ClavimInit()
python import sys
python sys.argv[0] = ''
exe 'python sys.path = ["' . s:plugin_path . '"] + sys.path'
exe 'pyfile ' . s:plugin_path . '/clavim.py' exe 'pyfile ' . s:plugin_path . '/clavim.py'
set ut=100 " the time in milliseconds after a keystroke when you want to reparse the AST set ut=100 " the time in milliseconds after a keystroke when you want to reparse the AST
python clavim_init() python clavim_init()
Expand All @@ -18,29 +15,10 @@ function! s:ClavimInit()
endfunction endfunction


function! s:ClavimHighlightMemberExpressions() function! s:ClavimHighlightMemberExpressions()
python << endpython call <SID>ClavimClearHighlights()
global update python highlight_expressions()
global cursors

if update:
vim.command("syn clear clavimMember")

cursors = find_cursors(get_current_translation_unit(update), clang.cindex.CursorKind.MEMBER_REF_EXPR)
keys = 'line start end'.split()
for x in cursors:
t = tuple(str(x[k]) for k in keys)
if x['file'] == vim.current.buffer.name:
vim.command(r"syn match clavimMember /\%%%sl\%%%sc.*\%%%sc/" % t)

update = True

endpython

endfunction endfunction


function! s:ClavimClearHighlights() function! s:ClavimClearHighlights()
python << endpython syn clear clavimMember
global cursors
vim.command("syn clear clavimMember")
endpython
endfunction endfunction

0 comments on commit 0894e5b

Please sign in to comment.