vim9: Rewrite tabline#formatters#default to Vim9Script

This commit is contained in:
Christian Brabandt 2021-05-08 00:48:18 +02:00
parent 725e881ac9
commit e608a0c016
No known key found for this signature in database
GPG Key ID: F3F92DA383FDDE09
1 changed files with 72 additions and 33 deletions

View File

@ -1,8 +1,9 @@
" MIT License. Copyright (c) 2013-2021 Bailey Ling et al. " MIT License. Copyright (c) 2013-2021 Bailey Ling et al.
" vim: et ts=2 sts=2 sw=2 " vim: et ts=2 sts=2 sw=2 et
scriptencoding utf-8 scriptencoding utf-8
if !exists(":def") || (exists(":def") && get(g:, "airline_experimental", 0) == 0)
function! airline#extensions#tabline#formatters#default#format(bufnr, buffers) function! airline#extensions#tabline#formatters#default#format(bufnr, buffers)
let fnametruncate = get(g:, 'airline#extensions#tabline#fnametruncate', 0) let fnametruncate = get(g:, 'airline#extensions#tabline#fnametruncate', 0)
let fmod = get(g:, 'airline#extensions#tabline#fnamemod', ':~:.') let fmod = get(g:, 'airline#extensions#tabline#fnamemod', ':~:.')
@ -10,7 +11,7 @@ function! airline#extensions#tabline#formatters#default#format(bufnr, buffers)
let name = bufname(a:bufnr) let name = bufname(a:bufnr)
if empty(name) if empty(name)
let _ .= '[No Name]' let _ = '[No Name]'
elseif name =~ 'term://' elseif name =~ 'term://'
" Neovim Terminal " Neovim Terminal
let _ = substitute(name, '\(term:\)//.*:\(.*\)', '\1 \2', '') let _ = substitute(name, '\(term:\)//.*:\(.*\)', '\1 \2', '')
@ -18,9 +19,9 @@ function! airline#extensions#tabline#formatters#default#format(bufnr, buffers)
if get(g:, 'airline#extensions#tabline#fnamecollapse', 1) if get(g:, 'airline#extensions#tabline#fnamecollapse', 1)
" Does not handle non-ascii characters like Cyrillic: 'D/Учёба/t.c' " Does not handle non-ascii characters like Cyrillic: 'D/Учёба/t.c'
"let _ .= substitute(fnamemodify(name, fmod), '\v\w\zs.{-}\ze(\\|/)', '', 'g') "let _ .= substitute(fnamemodify(name, fmod), '\v\w\zs.{-}\ze(\\|/)', '', 'g')
let _ .= pathshorten(fnamemodify(name, fmod)) let _ = pathshorten(fnamemodify(name, fmod))
else else
let _ .= fnamemodify(name, fmod) let _ = fnamemodify(name, fmod)
endif endif
if a:bufnr != bufnr('%') && fnametruncate && strlen(_) > fnametruncate if a:bufnr != bufnr('%') && fnametruncate && strlen(_) > fnametruncate
let _ = airline#util#strcharpart(_, 0, fnametruncate) let _ = airline#util#strcharpart(_, 0, fnametruncate)
@ -33,13 +34,51 @@ endfunction
function! airline#extensions#tabline#formatters#default#wrap_name(bufnr, buffer_name) function! airline#extensions#tabline#formatters#default#wrap_name(bufnr, buffer_name)
let buf_nr_format = get(g:, 'airline#extensions#tabline#buffer_nr_format', '%s: ') let buf_nr_format = get(g:, 'airline#extensions#tabline#buffer_nr_format', '%s: ')
let buf_nr_show = get(g:, 'airline#extensions#tabline#buffer_nr_show', 0) let buf_nr_show = get(g:, 'airline#extensions#tabline#buffer_nr_show', 0)
let buf_modified_symbol = g:airline_symbols.modified
let _ = buf_nr_show ? printf(buf_nr_format, a:bufnr) : '' let _ = buf_nr_show ? printf(buf_nr_format, a:bufnr) : ''
let _ .= substitute(a:buffer_name, '\\', '/', 'g') let _ .= substitute(a:buffer_name, '\\', '/', 'g')
if getbufvar(a:bufnr, '&modified') == 1 if getbufvar(a:bufnr, '&modified') == 1
let _ .= buf_modified_symbol let _ .= g:airline_symbols.modified
endif endif
return _ return _
endfunction endfunction
else
" Vim9 Script implementation
def airline#extensions#tabline#formatters#default#format(bufnr: number, buffers: list<number>): string
var fnametruncate = get(g:, 'airline#extensions#tabline#fnametruncate', 0)
var fmod = get(g:, 'airline#extensions#tabline#fnamemod', ':~:.')
var result = ''
var name = bufname(bufnr)
if empty(name)
result = '[No Name]'
elseif name =~ 'term://'
# Neovim Terminal
result = substitute(name, '\(term:\)//.*:\(.*\)', '\1 \2', '')
else
if get(g:, 'airline#extensions#tabline#fnamecollapse', 1)
result = pathshorten(fnamemodify(name, fmod))
else
result = fnamemodify(name, fmod)
endif
if bufnr != bufnr('%') && fnametruncate && strlen(result) > fnametruncate
result = airline#util#strcharpart(result, 0, fnametruncate)
endif
endif
return airline#extensions#tabline#formatters#default#wrap_name(bufnr, result)
enddef
def airline#extensions#tabline#formatters#default#wrap_name(bufnr: number, buffer_name: string): string
var buf_nr_format = get(g:, 'airline#extensions#tabline#buffer_nr_format', '%s: ')
var buf_nr_show = get(g:, 'airline#extensions#tabline#buffer_nr_show', 0)
var result = buf_nr_show ? printf(buf_nr_format, bufnr) : ''
result ..= substitute(buffer_name, '\\', '/', 'g')
if getbufvar(bufnr, '&modified') == 1
result ..= g:airline_symbols.modified
endif
return result
enddef
endif