Skip to content

Commit

Permalink
Use virtualtext on nvim to display results (#21)
Browse files Browse the repository at this point in the history
![](https://i.imgur.com/2rbeRZm.png)


With this PR (neovim/neovim#8180) neovim has support to display text that is not part of the file in a buffer. Using this instead of opening a new split will be a better way to display the size.

> Docs need to be added.


### Two new variables
`g:import_cost_virtualtext_hl_group` : set highlight group (default LineNr)
`g:import_cost_virtualtext_prefix`: prefix for virtualtext ( default > )
  • Loading branch information
meain authored and yardnsm committed Dec 8, 2018
1 parent 0bdc59a commit 404e964
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 16 deletions.
69 changes: 53 additions & 16 deletions autoload/import_cost.vim
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ function! s:EchoError(msg)
echohl None
endfunction

" Check if virtualtext is supported
function! import_cost#IsVirtualTextSupported()
return has('nvim-0.3.2')
endfunction

" Pretty format a size in bytes
function! s:PrettyFormatSize(size)
let l:pretty_size = a:size / 1000.0
Expand Down Expand Up @@ -154,7 +159,7 @@ function! s:FillScratchBuffer(imports, start_line, num_lines)

" Appending the imports
for import in a:imports
call append(import['line'] + a:start_line - 1, s:CreateImportString(import))
call append(import['line'] + a:start_line - 1, s:CreateImportString(import, 0))
endfor

" Clear extra blank lines
Expand Down Expand Up @@ -190,11 +195,15 @@ function! s:ParseSingleImport(key, val)
endfunction

" Create an import string from an import data
function! s:CreateImportString(import)
function! s:CreateImportString(import, mini)
let l:raw_size = s:PrettyFormatSize(a:import['size'])
let l:gzipped_size = s:PrettyFormatSize(a:import['gzip'])

let l:str = a:import['name'] . ': ' . l:raw_size
if a:mini
let l:str = l:raw_size
else
let l:str = a:import['name'] . ': ' . l:raw_size
endif

if g:import_cost_show_gzipped == 1
let l:str .= ' (gzipped: ' . l:gzipped_size . ')'
Expand All @@ -203,6 +212,7 @@ function! s:CreateImportString(import)
return l:str
endfunction


" This function takes the imports data and returns a string containing data
" about the total size
function! s:CreateTotalSizeString(imports)
Expand All @@ -218,14 +228,34 @@ function! s:CreateTotalSizeString(imports)
\ 'name': 'Total size',
\ 'size': l:size,
\ 'gzip': l:gzip,
\ })
\ }, 0)
endfunction


function! s:ShowVirtualTextMessage(imports, range_start_line, buffer_lines) abort
let l:hl_group = get(g:, 'import_cost_virtualtext_hl_group', 'LineNr')
let l:prefix = get(g:, 'import_cost_virtualtext_prefix', ' > ')

let l:buffer = bufnr('')
call nvim_buf_clear_highlight(l:buffer, 1000, 0, -1)

for import in a:imports
let l:message = s:CreateImportString(import, 1)
let l:line = import['line'] + a:range_start_line - 1
call nvim_buf_set_virtual_text(l:buffer, 1000, l:line, [[l:prefix.l:message, l:hl_group]], {})
endfor
endfunction

function! s:OnScriptFinish()

" Check for errors
if len(s:import_cost_stderr)
call s:EchoError(s:import_cost_stderr)
if !(import_cost#IsVirtualTextSupported() && g:import_cost_virtualtext)
call s:EchoError(s:import_cost_stderr)
else
let l:buffer = bufnr('')
call nvim_buf_clear_highlight(l:buffer, 1000, 0, -1)
endif
return
endif

Expand All @@ -243,28 +273,35 @@ function! s:OnScriptFinish()
" If we've got a single import, echo it instead of creating a new scratch
" buffer (if needed)
if l:imports_length == 1 && g:import_cost_always_open_split != 1
echom s:CreateImportString(l:imports[0])
echom s:CreateImportString(l:imports[0], 0)
return
endif

" Create a new scratch buffer and fill it
" Keep the focus on the currently opened buffer
if l:imports_length > 0
let l:current_buffer_name = bufname('%')
normal m'
if import_cost#IsVirtualTextSupported() && g:import_cost_virtualtext
call s:ShowVirtualTextMessage(l:imports, s:range_start_line, s:buffer_lines)
else
let l:current_buffer_name = bufname('%')
normal m'

call s:CreateScratchBuffer()
call s:FillScratchBuffer(l:imports, s:range_start_line, s:buffer_lines)

call s:CreateScratchBuffer()
call s:FillScratchBuffer(l:imports, s:range_start_line, s:buffer_lines)
" We'll keep the total size string within the scratch buffer
let b:total_size_string = s:CreateTotalSizeString(l:imports)
let l:result_message .= ' ' . b:total_size_string

" We'll keep the total size string within the scratch buffer
let b:total_size_string = s:CreateTotalSizeString(l:imports)
let l:result_message .= ' ' . b:total_size_string
execute bufwinnr(l:current_buffer_name) . 'wincmd w'
normal ''
endif

execute bufwinnr(l:current_buffer_name) . 'wincmd w'
normal ''
endif

echom l:result_message
if !(import_cost#IsVirtualTextSupported() && g:import_cost_virtualtext)
echom l:result_message
endif
endfunction

" }}}
Expand Down
38 changes: 38 additions & 0 deletions doc/import_cost.txt
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,44 @@ enabled by default. >
Value: 0 or 1
Default: 0 (use asynchronous execution)

------------------------------------------------------------------------------
g:import_cost_virtualtext *g:import_cost_virtualtext*

When this option is on, it will disable the use of virtualtext feature that
will show the result to the right of the import and instead display the
result in a split.

If you are running a version without virtualtext, this option is assumed to be
set to false.
>
let g:import_cost_virtualtext = 0
<
Value: 0 or 1
Default: 1 (use virtualtext insted of split if available)

------------------------------------------------------------------------------
g:import_cost_virtualtext_hl_group *g:import_cost_virtualtext_hl_group*

This options control what highlight group will be used to show the info.
>
let g:import_cost_virtualtext_hl_group = "LineNr"
<
Value: string
Default: "LineNr"

------------------------------------------------------------------------------
g:import_cost_virtualtext_prefix *g:import_cost_virtualtext_prefix*

This options sets the prefix used to when displaying the import cost info.
>
let g:import_cost_virtualtext_prefix = " :: "
<
Value: string
Default: ">"

==============================================================================
LICENSE *import-cost-license*

Expand Down
10 changes: 10 additions & 0 deletions plugin/import_cost.vim
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ let s:default_settings = {
\ 'split_size': 50,
\ 'split_pos': 'left',
\ 'disable_async': 0,
\ 'virtualtext': 1,
\ }

call s:InitSettings(s:default_settings)
Expand All @@ -45,6 +46,15 @@ call s:InitSettings(s:default_settings)
function! s:InitCommands()
command! -buffer -range=0 ImportCost call import_cost#ImportCost(<count>, <line1>, <line2>)
command! -buffer ImportCostSingle call import_cost#ImportCost(1, <line1>, <line1>)

if import_cost#IsVirtualTextSupported() && g:import_cost_virtualtext && !g:import_cost_disable_async
augroup import_cost_auto_run
autocmd!
autocmd InsertLeave * call import_cost#ImportCost(0, 0 ,0)
autocmd BufEnter * call import_cost#ImportCost(0, 0 ,0)
autocmd CursorHold * call import_cost#ImportCost(0, 0 ,0)
augroup END
endif
endfunction

" }}}
Expand Down

0 comments on commit 404e964

Please sign in to comment.