Skip to content

Commit 1db9bd3

Browse files
committed
Add python 3 support
1 parent 2e97eb0 commit 1db9bd3

2 files changed

Lines changed: 77 additions & 1 deletion

File tree

plugin/Phpqa.vim

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,13 @@ if 0 == has("signs")
2929
endif
3030

3131
let $CUR_DIRECTORY=expand("<sfile>:p:h")
32-
source $CUR_DIRECTORY/python/codecoverage.vim
32+
if 1 == has('python3')
33+
source $CUR_DIRECTORY/python/codecoverage_py3.vim
34+
else
35+
if 1 == has('python')
36+
source $CUR_DIRECTORY/python/codecoverage.vim
37+
endif
38+
endif
3339

3440
let g:phpqa_check = 1
3541

plugin/python/codecoverage_py3.vim

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
function! AddCodeCoverageSigns(clover)
2+
if has('python3')
3+
python3 << EOF
4+
import lxml.etree
5+
import vim
6+
import os.path
7+
import time
8+
9+
global doc
10+
global mtime
11+
12+
try:
13+
mtime
14+
except NameError:
15+
mtime = 0
16+
17+
clover = vim.eval('a:clover')
18+
buf = vim.eval('bufname("%")')
19+
fileName = vim.eval('fnamemodify("'+buf+'","%")')
20+
21+
"""
22+
XML may already be parsed and held in memory
23+
Check the file modification time, and only re-parse
24+
if it's been modified.
25+
"""
26+
try:
27+
try:
28+
doc
29+
if doc != None:
30+
cmtime = time.ctime(os.path.getmtime(clover))
31+
if cmtime > mtime:
32+
doc = None
33+
except NameError:
34+
doc = None
35+
36+
" t0 = time.time() "
37+
38+
if doc is None:
39+
doc = lxml.etree.parse(clover)
40+
mtime = time.ctime(os.path.getmtime(clover))
41+
42+
#ctxt = doc.xpathNewContext()
43+
#res = ctxt.xpathEval("//file[substring(@name, string-length(@name) - string-length('"+fileName+"') + 1)='"+fileName+"']/line[@type='stmt']")
44+
res = doc.xpath("//file[substring(@name, string-length(@name) - string-length('"+fileName+"') + 1)='"+fileName+"']/line[@type='stmt']")
45+
cur_signs = int(vim.eval('g:phpqa_num_cc_signs'))
46+
showcovered = int(vim.eval('g:phpqa_codecoverage_showcovered'))
47+
cmd_list = ''
48+
49+
for node in res:
50+
lnum = node.attrib['num']
51+
cnt = int(node.attrib['count'])
52+
if showcovered == 0 and cnt > 0:
53+
continue
54+
cur_signs += 1
55+
sign = "CodeCoverageCovered" if cnt > 0 else "CodeCoverageNotCovered"
56+
cmd_list += 'exec "sign place 4783 name='+sign+' line='+lnum+' file='+fileName+'" | '
57+
58+
vim.command(cmd_list)
59+
vim.command('let g:phpqa_num_cc_signs='+str(cur_signs))
60+
61+
except os.error:
62+
vim.command('echohl Error | echo "Missing or inaccessible code coverage file" | echohl None')
63+
except Exception as e:
64+
vim.command('echohl Error | echo "An error has occured while parsing the code coverage file" | echohl None')
65+
66+
EOF
67+
else
68+
echohl Error | echo "Code coverage support for PHPQA requires Vim with Python3" | echohl None
69+
endif
70+
endfunction

0 commit comments

Comments
 (0)