Permalink
Browse files

member variables are now highlighted... i think?

  • Loading branch information...
1 parent f4f2bbc commit 4341ff34bef01cd180a2f0135ca8d49f07601a82 @rdavison rdavison committed Jul 30, 2011
View
22 README
@@ -2,3 +2,25 @@ clavim (latin acc. for "key")
clavim is (going to be) a better C, C++, and Obj-C syntax highlighter for vim
based on libclang from the Clang project in the LLVM tree
+
+To run the plugin, you need the latest libclang you can get your hands on. To
+do that, follow the instructions at:
+
+ http://clang.llvm.org/get_started.html
+
+Once you have libclang in your path, copy:
+ clang/
+ clavim.py
+ clavim.vim
+
+to your vim's plugins folder (on mine it's $HOME/.vim/plugin)
+
+Also, your vim needs to be compiled in such a way that you can script it with
+python. I'm using the vim I found in Debian's apt repository, and it came
+precompiled with python support.
+
+TODO:
+ - find a way to track member variables once they've been highlighted so as
+ not to have to have to reparse the AST every time I want to update the
+ highlight's absolute position
+ - perhaps use libclang to highlight lines that have syntax errors?
View
Binary file not shown.
View
Binary file not shown.
View
@@ -1,7 +1,7 @@
10
dir
-134871
+136229
http://llvm.org/svn/llvm-project/cfe/trunk/bindings/python/clang
http://llvm.org/svn/llvm-project
@@ -32,7 +32,7 @@ file
-2011-07-10T17:04:40.485920Z
+2011-07-27T18:23:27.444758Z
5be284784bf9e68e69fc61168617d25d
2011-07-06T03:00:34.349957Z
134460
@@ -66,7 +66,7 @@ file
-2011-07-10T17:04:40.485920Z
+2011-07-27T18:23:27.468757Z
ec11eb50097b2b6c72c855afb3b66907
2010-01-25T00:43:08.146480Z
94392
View
Binary file not shown.
View
Binary file not shown.
View
@@ -1,7 +1,11 @@
#include "Dog.hpp"
+int algo() {
+ char *name = "LOL";
+}
+
Dog::Dog(char *aName) {
- name = aName;
+ name = "something";
}
char *Dog::getName() {
View
@@ -0,0 +1,92 @@
+#!/usr/bin/env python
+
+# steps
+# 1. load file and contents into a tuple
+# 2. pass file contents to libclang to parse an AST
+# 3. search for member functions and variables
+# 4. return a list of them to vim
+# 5. highlight them sons of bitches!
+# 6. ???
+# 7. profit!!1
+
+import sys
+import clang.cindex
+import vim
+
+def clavim_init():
+ global index
+ index = Index.create()
+ global translationUnits
+ translationUnits = dict()
+ global update
+ update = False
+
+def get_current_file():
+ file = "\n".join(vim.eval("getline(1, '$')"))
+ return (vim.current.buffer.name, file)
+
+def get_current_translation_unit(update = False):
+ current_file = get_current_file()
+ filename = vim.current.buffer.name
+ if filename in translationUnits:
+ tu = translationUnits[filename]
+ if update:
+ tu.reparse([current_file])
+ return tu
+
+ tu = index.parse(filename)
+
+ if tu is None:
+ print 'Error: tu is None'
+ return None
+
+ translationUnits[filename] = tu
+ tu.reparse([current_file])
+ return tu
+
+def cursorvisit_callback(node, parent, userdata):
+ if node.kind == userdata['kind']:
+ if node.extent.start.file is None:
+ return 2
+ my_node = dict()
+ my_node['name'] = clang.cindex.Cursor_displayname(node)
+ my_node['kind'] = node.kind.name
+ my_node['file'] = node.extent.start.file.name
+ my_node['line'] = node.location.line
+ my_node['start'] = node.extent.start.column
+ my_node['end'] = node.extent.end.column
+ userdata['nodes'].append(my_node)
+ return 2
+
+
+def find_cursors(tu, kind):
+ nodes = []
+ userdata = dict()
+
+ userdata['nodes'] = nodes
+ userdata['kind'] = kind
+
+ # visit children
+ clang.cindex.Cursor_visit(
+ tu.cursor,
+ clang.cindex.Cursor_visit_callback(cursorvisit_callback),
+ userdata)
+
+ return nodes
+
+def main():
+ index = clang.cindex.Index.create()
+ fname = sys.argv[1]
+ tu = index.parse(fname)
+ kind = clang.cindex.CursorKind.MEMBER_REF_EXPR
+ nodes = find_cursors(tu, kind)
+
+ print 'nodes (%s):' % (len(nodes))
+ for x in nodes:
+ print 'name=%s kind=%s file=%s line=%s start=%s end=%s' % (
+ x['name'],
+ x['kind'],
+ x['file'],
+ x['line'],
+ x['start'],
+ x['end'])
View
@@ -0,0 +1,47 @@
+au FileType c,cpp,h,hpp call <SID>ClavimInit()
+
+let s:plugin_path = escape(expand('<sfile>:p:h'), '\')
+
+hi clavimMember ctermbg=Cyan ctermfg=Black guibg=#8CCBEA guifg=Black
+hi clavimError ctermbg=Red ctermfg=Black guibg=Red guifg=Black
+
+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'
+ set ut=10 " the time in milliseconds after a keystroke when you want to reparse the AST
+ python global update
+ python update = False
+ call s:ClavimHighlightMemberExpressions()
+ au CursorHold,CursorHoldI <buffer> call <SID>ClavimHighlightMemberExpressions()
+ " au InsertChange,InsertEnter,InsertLeave <buffer> call <SID>ClavimHighlightMemberExpressions()
+endfunction
+
+function! s:ClavimHighlightMemberExpressions()
+python << endpython
+
+global update
+global cursors
+
+if update == True:
+ vim.command("syn clear clavimMember")
+
+cursors = []
+cursors = find_cursors(get_current_translation_unit(update), clang.cindex.CursorKind.MEMBER_REF_EXPR)
+for x in cursors:
+ vim.command("syn match clavimMember /\\%"+str(x['line'])+"l\\%"+str(x['start'])+"c.*\\%"+str(x['end'])+"c/")
+
+if update == False:
+ update = True
+
+endpython
+endfunction
+
+function! s:ClavimClearHighlights()
+python << endpython
+ global cursors
+ for x in cursors:
+ vim.command("syn clear clavimMember")
+endpython
+endfunction
View
@@ -1,45 +0,0 @@
-#!/usr/bin/env python
-
-# steps
-# 1. load file and contents into a tuple
-# 2. pass file contents to libclang to parse an AST
-# 3. search for member functions and variables
-# 4. return a list of them to vim
-# 5. highlight them sons of bitches!
-# 6. ???
-# 7. profit!!1
-
-
-import sys
-import clang.cindex
-
-class Highlighter:
- def __init__(self, filename):
- self.nodes = []
- self.filename = filename
- self.index = clang.cindex.Index.create()
- self.tu = self.index.parse(filename)
-
-def cursorvisit_callback(node, parent, userdata):
- #if node.kind == clang.cindex.CursorKind.MEMBER_REF_EXPR:
- userdata['object'].nodes.append(node)
- return 2
-
-
-def main():
- highlighter = Highlighter(sys.argv[1])
-
- userdata = dict()
- userdata['object'] = highlighter
- # visit children
- clang.cindex.Cursor_visit(
- highlighter.tu.cursor,
- clang.cindex.Cursor_visit_callback(cursorvisit_callback),
- userdata)
-
- print 'nodes (%s):' % (len(highlighter.nodes))
- for x in highlighter.nodes:
- if not x.extent.start.file is None:
- print 'name=%s kind=%s file=%s line=%s col=%s' % (clang.cindex.Cursor_displayname(x), x.kind.name, x.extent.start.file.name, x.location.line, x.location.column)
-
-main()
View
@@ -1 +0,0 @@
-pyfile highlighter.py

0 comments on commit 4341ff3

Please sign in to comment.