Skip to content

Commit

Permalink
abbr is the rest column
Browse files Browse the repository at this point in the history
  • Loading branch information
Shougo committed May 17, 2023
1 parent 140be50 commit c981ac0
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 17 deletions.
10 changes: 6 additions & 4 deletions autoload/pum.vim
Original file line number Diff line number Diff line change
Expand Up @@ -259,9 +259,9 @@ endfunction

function! pum#_format_item(item, options, mode, startcol, max_columns) abort
const columns = a:item->get('columns', {})->copy()->extend(#{
\ abbr: get(a:item, 'abbr', a:item.word),
\ kind: get(a:item, 'kind', ''),
\ menu: get(a:item, 'menu', ''),
\ abbr: a:item->get('abbr', a:item.word),
\ kind: a:item->get('kind', ''),
\ menu: a:item->get('menu', ''),
\ })

let str = ''
Expand All @@ -279,7 +279,9 @@ function! pum#_format_item(item, options, mode, startcol, max_columns) abort
" Fallback to "word"
let column = a:item.word
endif
let column ..= ' '->repeat(a:max_columns[order] - strdisplaywidth(column))
" Padding
let column ..= ' '
\ ->repeat(a:max_columns[order] - column->strdisplaywidth())

let str ..= column
endfor
Expand Down
34 changes: 21 additions & 13 deletions autoload/pum/popup.vim
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,30 @@ function! pum#popup#_open(startcol, items, mode, insert) abort
" Calc max columns
let max_columns = {}
for column in options.item_orders
let max_columns[column] = items->copy()
\ ->map({ _, val ->
\ strdisplaywidth(get(get(val, 'columns', {}), column, ''))
\ })->max()
let max_column =
\ column ==# 'abbr' ? items->copy()->map({ _, val ->
\ val->get('abbr', val.word)->strdisplaywidth()})->max() :
\ column ==# 'kind' ? items->copy()->map({ _, val ->
\ val->get('kind', '')->strdisplaywidth()})->max() :
\ column ==# 'menu' ? items->copy()->map({ _, val ->
\ val->get('menu', '')->strdisplaywidth()})->max() :
\ items->copy()->map({ _, val ->
\ val->get('columns', {})->get(column, '')->strdisplaywidth()})
\ ->max()

if max_column > 0
let max_columns[column] = max_column
endif
endfor
let max_columns.abbr = items->copy()->map({ _, val ->
\ strdisplaywidth(get(val, 'abbr', val.word))
\ })->max()
let max_columns.kind = items->copy()
\ ->map({ _, val -> strdisplaywidth(get(val, 'kind', ''))})->max()
let max_columns.menu = items->copy()
\ ->map({ _, val -> strdisplaywidth(get(val, 'menu', ''))})->max()
call filter(max_columns, { _, val -> val != 0 })

" Calc width
let width = 0
for max_column in max_columns->values()
let column_length = 0
for [column, max_column] in max_columns->items()
let width += max_column
if column !=# 'abbr'
let column_length += max_column + 1
endif
endfor

" Padding
Expand All @@ -53,6 +59,8 @@ function! pum#popup#_open(startcol, items, mode, insert) abort
if options.max_width > 0
let width = [width, options.max_width]->min()
endif
" NOTE: abbr is the rest column
let max_columns.abbr = width - column_length

let lines = items->copy()->map({ _, val ->
\ pum#util#_truncate(
Expand Down

0 comments on commit c981ac0

Please sign in to comment.