import { BaseBuffer } from "./base" ; import { Neovim } from "neovim" ; import { CtrlbBufferType } from "./type" ; import { BufferContainer } from "./container" ; import { ListBuffer } from "./list" ; import { HighlightRepository } from "../repository/highlight" ; import { TabRepository , Tab } from "../repository/tab" ; import { EventRegisterer } from "./event" ; export class TabListItem { constructor ( protected readonly tab ) { } public toString ( ) { if ( this . tab . active ) { return "|" + this . tab . title + "\t" + this . tab . url ; } return " " + this . tab . title + "\t" + this . tab . url ; } public get value ( ) : Tab { return this . tab ; } } export class TabList extends BaseBuffer { public static readonly type = CtrlbBufferType . tabList ; protected readonly options = { buftype : "nofile" , buflisted : true , swapfile : false , modifiable : false , undolevels : - 1 , } ; constructor ( protected readonly vim , protected readonly bufferContainer , protected readonly listBuffer , protected readonly eventRegisterer , protected readonly highlightRepository , protected readonly tabRepository ) { super ( vim , bufferContainer , eventRegisterer ) ; this . actions [ "debug" ] = async ( ) => this . debug ( await this . listBuffer . getCurrent ( ) ) ; this . actions [ "zoomUp" ] = ( firstLine , lastLine ) => this . zoomUp ( firstLine , lastLine ) ; this . actions [ "zoomDown" ] = ( firstLine , lastLine ) => this . zoomDown ( firstLine , lastLine ) ; this . actions [ "zoomReset" ] = ( firstLine , lastLine ) => this . zoomReset ( firstLine , lastLine ) ; this . actions [ "activate" ] = ( ) => this . activate ( ) ; this . actions [ "reload" ] = ( firstLine , lastLine ) => this . reload ( firstLine , lastLine ) ; this . actions [ "close" ] = ( firstLine , lastLine ) => this . close ( firstLine , lastLine ) ; this . actions [ "duplicate" ] = ( firstLine , lastLine ) => this . duplicate ( firstLine , lastLine ) ; this . actions [ "read" ] = ( ) => this . read ( ) ; this . actions [ "highlight" ] = ( ) => this . highlight ( ) ; } protected async setup ( ) { await this . highlightRepository . link ( "CtrlbTabListUrl" , "Underlined" ) ; await this . highlightRepository . link ( "CtrlbTabListActive" , "Title" ) ; await this . highlight ( ) ; const p = await this . tabRepository . onChanged ( data => this . read ( ) ) ; this . eventRegisterer . subscribe ( p ) ; await this . bufferContainer . defineReadAction ( "read" ) ; await this . bufferContainer . defineEnableHighlightAction ( "highlight" ) ; this . read ( ) ; } protected async highlight ( ) { await this . highlightRepository . match ( "CtrlbTabListActive" , "^|.*" , [ "CtrlbTabListUrl" , ] ) ; await this . highlightRepository . match ( "CtrlbTabListUrl" , "[[:tab:]]\\zs.*$" ) ; } protected async read ( ) { const [ tabs , error ] = await this . tabRepository . getListAll ( ) ; if ( error !== null ) { return ; } const items = tabs . map ( tab => { return new TabListItem ( tab ) ; } ) ; await this . listBuffer . set ( items ) ; } public async activate ( ) { const tab = await this . listBuffer . getCurrent ( ) ; if ( tab === null ) { return ; } const id = tab . id ; if ( id === undefined ) { return ; } this . tabRepository . activate ( id ) ; } public async close ( firstLine , lastLine ) { const ids = await this . getIds ( firstLine , lastLine ) ; await this . tabRepository . close ( ids ) ; } public async duplicate ( firstLine , lastLine ) { const ids = await this . getIds ( firstLine , lastLine ) ; await this . tabRepository . duplicate ( ids ) ; } public async reload ( firstLine , lastLine ) { const ids = await this . getIds ( firstLine , lastLine ) ; await this . tabRepository . reload ( ids ) ; } public async zoomUp ( firstLine , lastLine ) { const ids = await this . getIds ( firstLine , lastLine ) ; await this . tabRepository . zoomUp ( ids ) ; } public async zoomDown ( firstLine , lastLine ) { const ids = await this . getIds ( firstLine , lastLine ) ; await this . tabRepository . zoomDown ( ids ) ; } public async zoomReset ( firstLine , lastLine ) { const ids = await this . getIds ( firstLine , lastLine ) ; await this . tabRepository . zoomReset ( ids ) ; } protected async getIds ( firstLine , lastLine ) { return ( await this . listBuffer . getRangeModels ( firstLine , lastLine ) ) . map ( tab => tab . id ) . filter ( ( id ) : id is number => id !== undefined ) ; } }