Skip to content

Commit

Permalink
Moarstuff
Browse files Browse the repository at this point in the history
  • Loading branch information
Dave Dash committed Aug 29, 2012
1 parent d9a9a40 commit 60c0fa0
Show file tree
Hide file tree
Showing 18 changed files with 3,002 additions and 0 deletions.
321 changes: 321 additions & 0 deletions .vim/ftplugin/python/coveragepy.vim
@@ -0,0 +1,321 @@
" File: coveragepy.vim
" Description: Displays coverage reports from Ned Batchelder's excellent
" coverage.py tool
" (see: http://nedbatchelder.com/code/coverage )
" Maintainer: Alfredo Deza <alfredodeza AT gmail.com>
" License: MIT
"============================================================================


if exists("g:loaded_coveragepy") || &cp
finish
endif

function! s:HasCoverage() abort
if (executable("coverage") == 0)
echoerr("This plugin needs coverage.py installed and accessible")
finish
endif
endfunction

" Global variables for registering next/previous error
let g:coveragepy_last_session = ""
let g:coveragepy_marks = []
let g:coveragepy_session_map = {}
let g:coveragepy_is_displaying = 0


function! s:ToggleSigns()
if exists("g:coveragepy_is_displaying") && g:coveragepy_is_displaying
call s:ClearSigns()
let g:coveragepy_is_displaying = 0
else
call s:HighlightMissing()
endif
endfunction


function! s:CoveragepySyntax() abort
let b:current_syntax = 'Coveragepy'
syn match CoveragepyTitleDecoration "\v\-{2,}"
syn match CoveragepyHeaders '\v(^Name\s+|\s*Stmts\s*|\s*Miss\s+|Cover|Missing$)'
syn match CoveragepyDelimiter "\v^(\-\-)\s+"
syn match CoveragepyPercent "\v(\d+\%\s+)"
syn match CoveragepyLineNumbers "\v(\s*\d+,|\d+-\d+,|\d+-\d+$|\d+$)"

hi def link CoveragepyFiles Number
hi def link CoveragepyHeaders Comment
hi def link CoveragepyTitleDecoration Keyword
hi def link CoveragepyDelimiter Comment
hi def link CoveragepyPercent Boolean
hi def link CoveragepyLineNumbers Error
endfunction


function! s:Echo(msg, ...) abort
redraw!
let x=&ruler | let y=&showcmd
set noruler noshowcmd
if (a:0 == 1)
echo a:msg
else
echohl WarningMsg | echo a:msg | echohl None
endif

let &ruler=x | let &showcmd=y
endfun

function! s:FindCoverage() abort
let found = findfile(".coverage", ".;")
if (found !~ '.coverage')
return ""
endif
" Return the actual directory where .coverage is found
return fnamemodify(found, ":h")
endfunction


function! s:ClearSigns() abort
exe ":sign unplace *"
endfunction

function! s:SetHighlight()
hi SignColumn guifg=#004400 guibg=green ctermfg=40 ctermbg=40
hi uncovered guifg=#ff2222 guibg=red ctermfg=1 ctermbg=1
hi covered guifg=#004400 guibg=green ctermfg=40 ctermbg=40
sign define uncovered text=XX texthl=uncovered
sign define covered text=XX texthl=covered
endfunction

function! s:HighlightMissing() abort
call s:SetHighlight()
let g:coveragepy_is_displaying = 1
if (g:coveragepy_session_map == {})
call s:CoveragepyReport()
endif
call s:ClearSigns()

let current_buffer = matchlist(expand("%:p"), '\v(.*)(.py)')[1]

for path in keys(g:coveragepy_session_map)
echo "Current buffer " . current_buffer
echo "path " . path
if current_buffer =~ path
for position in g:coveragepy_session_map[path]
execute(":sign place ". position ." line=". position ." name=uncovered buffer=".bufnr("%"))
endfor
execute g:coveragepy_session_map[path][0]
redraw!
return
endif
endfor
execute(":sign place 1 line=1 name=covered buffer=".bufnr("%"))
call s:Echo("Coveragepy ==> 100% covered", 1)
endfunction


function! s:Strip(input_string) abort
return split(a:input_string, " ")[0]
endfunction


function! s:Roulette(direction) abort
let orig_line = line('.')
let last_line = line('$') - 3

" if for some reason there is not enough
" coverage output return
if last_line < 3
return
endif

" Move to the line we need
let move_to = orig_line + a:direction

if move_to > last_line
let move_to = 3
exe move_to
elseif (move_to < 3) && (a:direction == -1)
let move_to = last_line
exe move_to
elseif (move_to < 3) && (a:direction == 1)
let move_to = 3
exe move_to
else
exe move_to
endif

if move_to == 1
let _num = move_to
else
let _num = move_to - 1
endif
endfunction


function! s:CoveragepyReport() abort
" Run a report, ignore errors and show missing lines,
" which is what we are interested after all :)
let original_dir = getcwd()
" First try to see if we actually have a .coverage file
" to work with
let has_coverage = s:FindCoverage()
if (has_coverage == "")
return 0
else
" set the original directory path
" as a global
let g:coveragepy_path = has_coverage
" change dir to where coverage is
" and do all the magic we need
exe "cd " . has_coverage
call s:ClearSigns()
let g:coveragepy_last_session = ""
let cmd = "coverage report -m -i"
let out = system(cmd)
let g:coveragepy_last_session = out
call s:ReportParse()

" Finally get back to where we initially where
exe "cd " . original_dir
return 1
endif
endfunction


function! s:ReportParse() abort
" After coverage runs, parse the content so we can get
" line numbers mapped to files
let path_to_lines = {}
for line in split(g:coveragepy_last_session, '\n')
if (line =~ '\v(\s*\d+,|\d+-\d+,|\d+-\d+$|\d+$)') && line !~ '\v(100\%)'
let path = split(line, ' ')[0]
let match_split = split(line, '%')
let line_nos = match_split[-1]
let all_line_nos = s:LineNumberParse(line_nos)
let path_to_lines[path] = all_line_nos
endif
endfor
let g:coveragepy_session_map = path_to_lines
endfunction


function! s:LineNumberParse(numbers) abort
" Line numbers will come with a possible comma in them
" and lots of extra space. Let's remove them and strip them
let parsed_list = []
let splitted = split(a:numbers, ',')
for line_no in splitted
if line_no =~ '-'
let split_nos = split(line_no, '-')
let first = s:Strip(split_nos[0])
let second = s:Strip(split_nos[1])
for range_no in range(first, second)
call add(parsed_list, range_no)
endfor
else
call add(parsed_list, s:Strip(line_no))
endif
endfor
return parsed_list
endfunction


function! s:ClearAll() abort
let bufferL = ['LastSession.coveragepy']
for b in bufferL
let _window = bufwinnr(b)
if (_window != -1)
silent! execute _window . 'wincmd w'
silent! execute 'q'
endif
endfor
endfunction


function! s:LastSession() abort
call s:ClearAll()
let winnrback = bufwinnr(expand("%"))
if (len(g:coveragepy_last_session) == 0)
call s:CoveragepyReport()
endif
let winnr = bufwinnr('LastSession.coveragepy')
silent! execute winnr < 0 ? 'botright new ' . 'LastSession.coveragepy' : winnr . 'wincmd w'
setlocal buftype=nowrite bufhidden=wipe nobuflisted noswapfile nowrap number filetype=coveragepy
let session = split(g:coveragepy_last_session, '\n')
call append(0, session)
silent! execute 'resize ' . line('$')
silent! execute 'normal gg'
silent! execute 'nnoremap <silent> <buffer> q :q! <CR>'
nnoremap <silent><script> <buffer> <C-n> :call <sid>Roulette(1)<CR>
nnoremap <silent><script> <buffer> <down> :call <sid>Roulette(1)<CR>
nnoremap <silent><script> <buffer> j :call <sid>Roulette(1)<CR>
nnoremap <silent><script> <buffer> <C-p> :call <sid>Roulette(-1)<CR>
nnoremap <silent><script> <buffer> <up> :call <sid>Roulette(-1)<CR>
nnoremap <silent><script> <buffer> k :call <sid>Roulette(-1)<CR>
nnoremap <silent> <buffer> <Enter> :call <sid>OpenBuffer()<CR>
call s:CoveragepySyntax()
exe winnrback . 'wincmd w'
call s:ClearAll()
endfunction


function! s:OpenBuffer() abort
let path = split(getline('.'), ' ')[0] . '.py'
let absolute_path = g:coveragepy_path . '/' . path
if filereadable(absolute_path)
execute 'wincmd p'
silent! execute ":e " . absolute_path
call s:HighlightMissing()
execute 'wincmd p'
call s:CoveragepySyntax()
else
call s:Echo("Could not load file: " . path)
endif
endfunction


function! s:Version() abort
call s:Echo("coveragepy version 1.1dev", 1)
endfunction


function! s:Completion(ArgLead, CmdLine, CursorPos) abort
let actions = "report\nshow\nnoshow\nsession\n"
let extras = "version\n"
return actions . extras
endfunction


function! s:Proxy(action, ...) abort
" Make sure that if we are called, we have coverage installed
call s:HasCoverage()
if (a:action == "show")
call s:ToggleSigns()
elseif (a:action == "noshow")
call s:ClearSigns()
elseif (a:action == "session")
let winnr = bufwinnr('LastSession.coveragepy')
if (winnr != -1)
silent! execute 'wincmd b'
silent! execute 'q'
return
else
call s:LastSession()
endif
elseif (a:action == "report")
let report = s:CoveragepyReport()
if report == 1
call s:LastSession()
call s:HighlightMissing()
else
call s:Echo("No .coverage was found in current or parent dirs")
endif
elseif (a:action == "version")
call s:Version()
endif
endfunction


command! -nargs=+ -complete=custom,s:Completion Coveragepy call s:Proxy(<f-args>)

56 changes: 56 additions & 0 deletions .vim/ftplugin/python/pep8.vim
@@ -0,0 +1,56 @@
" To change mapping, just put
" let g:pep8_map='whatever'
" in your .vimrc
" To change the color of
function! <SID>Pep8()
set lazyredraw
" Close any existing cwindows.
cclose
let l:grepformat_save = &grepformat
let l:grepprogram_save = &grepprg
set grepformat&vim
set grepformat&vim
let &grepformat = '%f:%l:%m'
let &grepprg = 'pep8 --repeat'
if &readonly == 0 | update | endif
silent! grep! %
let &grepformat = l:grepformat_save
let &grepprg = l:grepprogram_save
let l:mod_total = 0
let l:win_count = 1
" Determine correct window height
windo let l:win_count = l:win_count + 1
if l:win_count <= 2 | let l:win_count = 4 | endif
windo let l:mod_total = l:mod_total + winheight(0)/l:win_count |
\ execute 'resize +'.l:mod_total
" Open cwindow
execute 'belowright copen '.l:mod_total
nnoremap <buffer> <silent> c :cclose<CR>
set nolazyredraw
redraw!
let tlist=getqflist() ", 'get(v:val, ''bufnr'')')
if empty(tlist)
if !hlexists('GreenBar')
hi GreenBar term=reverse ctermfg=white ctermbg=darkgreen guifg=white guibg=darkgreen
endif
echohl GreenBar
echomsg "PEP8 correct"
echohl None
cclose
endif
endfunction

if !exists('g:pep8_map')
let g:pep8_map='<F5>'
endif
if ( !hasmapto('<SID>PEP8()') && (maparg(g:pep8_map) == '') )
exe 'nnoremap <silent> '. g:pep8_map .' :call <SID>Pep8()<CR>'
" map <F5> :call <SID>Pep8()<CR>
" map! <F5> :call <SID>Pep8()<CR>
else
if ( !has("gui_running") || has("win32") )
echo "Python PEP8 Error: No Key mapped.\n".
\ g:pep8_map ." is taken and a replacement was not assigned."
endif
endif

0 comments on commit 60c0fa0

Please sign in to comment.