import { BasicList , ListContext , ListItem , Neovim } from 'coc.nvim' ; import colors from 'colors/safe' ; const regex = /^\s*(\d+)(.+?)"(.+?)".*/ ; export default class BufferList extends BasicList { public readonly name = 'buffers' ; public readonly defaultAction = 'open' ; public description = 'get buffer list' ; constructor ( nvim ) { super ( nvim ) ; this . addAction ( 'open' , async ( item ) => { let { bufnr } = item . data ; await nvim . command ( `buffer ${ bufnr }` ) ; } ) ; this . addAction ( 'drop' , async ( item ) => { let { bufnr , visible } = item . data ; if ( visible ) { let info = await nvim . call ( 'getbufinfo' , bufnr ) as any [ ] ; if ( info . length && info [ 0 ] . windows . length ) { let winid = info [ 0 ] . windows [ 0 ] ; await nvim . call ( 'win_gotoid' , winid ) ; return ; } } await nvim . command ( `buffer ${ bufnr }` ) ; } ) ; this . addAction ( 'split' , async ( item ) => { let { bufnr } = item . data ; await nvim . command ( `sb ${ bufnr }` ) ; } ) ; this . addAction ( 'tabe' , async ( item ) => { let { bufname } = item . data ; let escaped = await nvim . call ( 'fnameescape' , bufname ) ; await nvim . command ( `tabe ${ escaped }` ) ; } ) ; this . addAction ( 'vsplit' , async ( item ) => { let { bufname } = item . data ; let escaped = await nvim . call ( 'fnameescape' , bufname ) ; await nvim . command ( `vs ${ escaped }` ) ; } ) ; this . addAction ( 'delete' , async ( item ) => { let { bufnr } = item . data ; await nvim . command ( `bdelete ${ bufnr }` ) ; } , { persist : true , reload : true } ) ; this . addAction ( 'wipe' , async ( item ) => { let { bufnr } = item . data ; await nvim . command ( `bwipeout ${ bufnr }` ) ; } , { persist : true , reload : true } ) ; this . addAction ( 'preview' , async ( item , context ) => { let { nvim } = this ; let { bufname , bufnr } = item . data ; let lines = await nvim . call ( 'getbufline' , [ bufnr , 1 , 200 ] ) as string [ ] ; let filetype = await nvim . call ( 'getbufvar' , [ bufnr , '&filetype' , 'txt' ] ) as string ; if ( lines . length == 0 ) { lines = [ `Unable to get lines for buffer ${ bufname }, add 'set hidden' in your vimrc.` ] ; } await this . preview ( { filetype , bufname , lines , sketch : true } , context ) ; } ) ; } public async loadItems ( _context ) { const { nvim } = this ; const bufnrAlt = Number ( await nvim . call ( 'bufnr' , '#' ) ) ; const content = await nvim . call ( 'execute' , 'ls' ) as string ; return content . split ( /\n/ ) . reduce ( ( res , line ) => { const ms = line . match ( regex ) ; if ( ! ms ) return res ; const bufnr = Number ( ms [ 1 ] ) ; const item = { label : ` ${ colors . magenta ( ms [ 1 ] ) }${ colors . america ( ms [ 2 ] ) }${ ms [ 3 ] }` , filterText : ms [ 3 ] , sortText : ms [ 1 ] , data : { bufnr , bufname : ms [ 3 ] , visible : ms [ 2 ] . indexOf ( 'a' ) !== - 1 } } as ListItem ; return bufnr === bufnrAlt ? [ item , ... res ] : [ ... res , item ] ; } , [ ] ) ; } }