|
| 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