1
0
mirror of https://github.com/SpaceVim/SpaceVim.git synced 2025-02-03 04:40:06 +08:00

perf(SourceCounter): import sourcecounter

This commit is contained in:
Eric Wong 2024-07-24 19:12:58 +08:00
parent a974fb5646
commit 40edbe8d6c
2 changed files with 159 additions and 120 deletions

View File

@ -4,32 +4,69 @@ let s:NOTI = SpaceVim#api#import('notify')
scriptencoding utf-8 scriptencoding utf-8
let s:support_ft = ['vim', 'java', 'c', 'py', 'md', 'txt'] let s:support_ft = ['vim', 'java', 'c', 'py', 'md', 'txt']
function! SourceCounter#View(bang, ...) abort function! SourceCounter#View(bang, ...) abort
let result = [] call s:NOTI.notify(string(a:000))
if a:0 let fts = []
let fts = a:000 let dirs = []
else let result = {}
let fts = s:support_ft let argv_type = ''
for argv in a:000
if argv == '-d'
let argv_type = 'dir'
continue
elseif argv == '-ft'
let argv_type = 'filetype'
continue
endif endif
call s:NOTI.notify('counting for: ' . join(fts, ', ')) if argv_type == 'dir'
" return call add(dirs, argv)
for ft in fts elseif argv_type == 'filetype'
let _rs = s:counter(ft) call add(fts, argv)
if !empty(_rs)
call add(result, _rs)
endif endif
endfor endfor
let result = sort(deepcopy(result), function('s:compare')) call s:NOTI.notify('counting for: ' . join(fts, ', '))
" return
for dir in dirs
for ft in fts
let _rs = s:counter(ft, dir)
if !empty(_rs)
if has_key(result, ft)
let result[ft].files = result[ft].files + _rs.files
let result[ft].lines = result[ft].lines + _rs.lines
else
let result[ft] = {
\ 'files' : _rs.files,
\ 'lines' : _rs.lines,
\ }
endif
endif
endfor
endfor
let result = sort(s:build(result), function('s:compare'))
let table = s:draw_table(result) let table = s:draw_table(result)
if a:bang if a:bang
tabnew tabnew
setlocal buftype=nofile bufhidden=wipe nobuflisted nolist noswapfile nowrap cursorline nospell nonumber norelativenumber
setlocal modifiable
noautocmd normal! gg"_dG
for line in table for line in table
call append(line('$'), line) call append(line('$'), line)
endfor endfor
normal! G
setlocal nomodifiable
nnoremap <buffer><silent> q :bd<CR>
else else
call s:NOTI.notify(join(table, "\n")) call s:NOTI.notify(join(table, "\n"))
endif endif
endfunction endfunction
function! s:build(rst) abort
let rst = []
for k in keys(a:rst)
call add(rst, [k, a:rst[k].files, a:rst[k].lines])
endfor
return rst
endfunction
function! s:compare(a, b) abort function! s:compare(a, b) abort
let m = get(g:, 'source_counter_sort', 'files') let m = get(g:, 'source_counter_sort', 'files')
if m ==# 'lines' if m ==# 'lines'
@ -115,9 +152,7 @@ function! s:list_files_exit(id, data, event) abort
endfunction endfunction
function! s:counter(ft) abort function! s:counter(ft, dir) abort
let path = getcwd()
let partten = '**/*.' . a:ft
if executable('ag') if executable('ag')
if has('nvim') if has('nvim')
let files = systemlist(['ag','-g', '.' . a:ft . '$']) let files = systemlist(['ag','-g', '.' . a:ft . '$'])
@ -125,16 +160,20 @@ function! s:counter(ft) abort
let files = split(system('ag -g .'.a:ft.'$'),nr2char(10)) let files = split(system('ag -g .'.a:ft.'$'),nr2char(10))
endif endif
else else
let files = globpath(l:path, l:partten, 0, 1) let partten = '**/*.' . a:ft
let files = globpath(a:dir, l:partten, 0, 1)
endif endif
if len(files) == 0 if len(files) == 0
return [] return []
endif endif
let lines = 0 let lines = 0
let _file_count = len(files)
if has('nvim') if has('nvim')
if len(files) > 380 if len(files) > 380
while !empty(files) while !empty(files)
let lines += matchstr(systemlist(['wc', '-l'] + remove(files, 0, min([380, len(files) - 1])))[-1], '\d\+') let _fs = remove(files, 0, min([380, len(files) - 1]))
let _rst = systemlist(['wc', '-l'] + _fs)[-1]
let lines += matchstr(_rst, '\d\+')
endwhile endwhile
else else
let lines = matchstr(systemlist(['wc', '-l'] + files)[-1], '\d\+') let lines = matchstr(systemlist(['wc', '-l'] + files)[-1], '\d\+')
@ -144,7 +183,7 @@ function! s:counter(ft) abort
let lines += str2nr(matchstr(system('wc -l '. fl), '\d\+')) let lines += str2nr(matchstr(system('wc -l '. fl), '\d\+'))
endfor endfor
endif endif
return [a:ft, len(files), lines] return {'files' : _file_count, 'lines' : lines}
endfunction endfunction

View File

@ -3,9 +3,9 @@ wsdjeg *SourceCounter.vim*
============================================================================== ==============================================================================
CONTENTS *SourceCounter.vim-contents* CONTENTS *SourceCounter.vim-contents*
1. Introduction....................................|SourceCounter.vim-intro| 1. Introduction..................................... |SourceCounter.vim-intro|
2. Configuration..................................|SourceCounter.vim-config| 2. Configuration................................... |SourceCounter.vim-config|
3. Commands.....................................|SourceCounter.vim-commands| 3. Commands...................................... |SourceCounter.vim-commands|
============================================================================== ==============================================================================
INTRODUCTION *SourceCounter.vim-intro* INTRODUCTION *SourceCounter.vim-intro*