From 1db9bd31c6662744df06ed8c0816134564db40f7 Mon Sep 17 00:00:00 2001 From: Mark Kimsal Date: Thu, 22 Mar 2018 16:47:02 -0400 Subject: [PATCH] Add python 3 support --- plugin/Phpqa.vim | 8 +++- plugin/python/codecoverage_py3.vim | 70 ++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 plugin/python/codecoverage_py3.vim diff --git a/plugin/Phpqa.vim b/plugin/Phpqa.vim index f7f19e3..5eba25b 100644 --- a/plugin/Phpqa.vim +++ b/plugin/Phpqa.vim @@ -29,7 +29,13 @@ if 0 == has("signs") endif let $CUR_DIRECTORY=expand(":p:h") -source $CUR_DIRECTORY/python/codecoverage.vim +if 1 == has('python3') + source $CUR_DIRECTORY/python/codecoverage_py3.vim +else + if 1 == has('python') + source $CUR_DIRECTORY/python/codecoverage.vim + endif +endif let g:phpqa_check = 1 diff --git a/plugin/python/codecoverage_py3.vim b/plugin/python/codecoverage_py3.vim new file mode 100644 index 0000000..f59e6a1 --- /dev/null +++ b/plugin/python/codecoverage_py3.vim @@ -0,0 +1,70 @@ +function! AddCodeCoverageSigns(clover) + if has('python3') +python3 << EOF +import lxml.etree +import vim +import os.path +import time + +global doc +global mtime + +try: + mtime +except NameError: + mtime = 0 + +clover = vim.eval('a:clover') +buf = vim.eval('bufname("%")') +fileName = vim.eval('fnamemodify("'+buf+'","%")') + +""" +XML may already be parsed and held in memory +Check the file modification time, and only re-parse +if it's been modified. +""" +try: + try: + doc + if doc != None: + cmtime = time.ctime(os.path.getmtime(clover)) + if cmtime > mtime: + doc = None + except NameError: + doc = None + + " t0 = time.time() " + + if doc is None: + doc = lxml.etree.parse(clover) + mtime = time.ctime(os.path.getmtime(clover)) + + #ctxt = doc.xpathNewContext() + #res = ctxt.xpathEval("//file[substring(@name, string-length(@name) - string-length('"+fileName+"') + 1)='"+fileName+"']/line[@type='stmt']") + res = doc.xpath("//file[substring(@name, string-length(@name) - string-length('"+fileName+"') + 1)='"+fileName+"']/line[@type='stmt']") + cur_signs = int(vim.eval('g:phpqa_num_cc_signs')) + showcovered = int(vim.eval('g:phpqa_codecoverage_showcovered')) + cmd_list = '' + + for node in res: + lnum = node.attrib['num'] + cnt = int(node.attrib['count']) + if showcovered == 0 and cnt > 0: + continue + cur_signs += 1 + sign = "CodeCoverageCovered" if cnt > 0 else "CodeCoverageNotCovered" + cmd_list += 'exec "sign place 4783 name='+sign+' line='+lnum+' file='+fileName+'" | ' + + vim.command(cmd_list) + vim.command('let g:phpqa_num_cc_signs='+str(cur_signs)) + +except os.error: + vim.command('echohl Error | echo "Missing or inaccessible code coverage file" | echohl None') +except Exception as e: + vim.command('echohl Error | echo "An error has occured while parsing the code coverage file" | echohl None') + +EOF + else + echohl Error | echo "Code coverage support for PHPQA requires Vim with Python3" | echohl None + endif +endfunction