import { VimMode , HyperspaceCoordinates } from '../neovim/types' ; import { BufferInfo } from '../../common/types' ; import { onFnCall } from '../../common/utils' ; import { Worker } from '../workers/messaging/worker' ; import { BrowserWindow } from 'electron' ; import { Functions } from '../neovim/function-types' ; import { WindowMetadata } from '../../common/types' ; import { GitStatus } from '../workers/git' ; import NeovimState from '../neovim/state' ; import { EventEmitter } from 'events' ; import { clipboard } from 'electron' ; import { Events } from '../../common/ipc' ; const InstanceApi = ( workerInstanceRef , winRef ) => { const ee = new EventEmitter ( ) ; const { state , watchState , onStateValue , onStateChange , untilStateValue } = NeovimState ( 'nvim-mirror' ) ; const actionRegistrations = [ ] ; if ( actionRegistrations . length ) actionRegistrations . forEach ( ( name ) => workerInstanceRef . call . onAction ( name ) ) ; workerInstanceRef . on . nvimStateUpdate ( ( stateDiff ) => { Object . assign ( state , stateDiff ) ; } ) ; workerInstanceRef . on . showNeovimMessage ( ( ... a ) => winRef . webContents . send ( Events . nvimShowMessage , a ) ) ; workerInstanceRef . on . showStatusBarMessage ( ( message ) => { winRef . webContents . send ( Events . nvimMessageStatus , message ) ; } ) ; workerInstanceRef . on . vimrcLoaded ( ( ) => ee . emit ( 'nvim.load' , false ) ) ; workerInstanceRef . on . gitStatus ( ( status ) => ee . emit ( 'git.status' , status ) ) ; workerInstanceRef . on . gitBranch ( ( branch ) => ee . emit ( 'git.branch' , branch ) ) ; workerInstanceRef . on . actionCalled ( ( name , args ) => ee . emit ( `action.${ name }` , ... args ) ) ; workerInstanceRef . on . clipboardRead ( async ( ) => clipboard . readText ( ) ) ; workerInstanceRef . on . clipboardWrite ( ( text ) => clipboard . writeText ( text ) ) ; return { state , watchState , onStateValue , onStateChange , untilStateValue , getBufferInfo : ( ) : Promise < BufferInfo > => workerInstanceRef . request . getBufferInfo ( ) , setMode : ( mode ) => { Object . assign ( state , { mode } ) ; workerInstanceRef . call . setNvimMode ( mode ) ; } , getWindowMetadata : ( ) : Promise < WindowMetadata [ ] > => { return workerInstanceRef . request . getWindowMetadata ( ) ; } , onAction : ( name , fn ) => { if ( typeof fn !== 'function' ) throw new Error ( `nvim.onAction needs a function for event ${ name }` ) ; actionRegistrations . push ( name ) ; ee . on ( `action.${ name }` , fn ) ; try { workerInstanceRef . call . onAction ( name ) ; } catch ( _ ) { } } , gitOnStatus : ( fn ) => ee . on ( 'git.status' , fn ) , gitOnBranch : ( fn ) => ee . on ( 'git.branch' , fn ) , bufferSearch : ( file , query ) => workerInstanceRef . request . bufferSearch ( file , query ) , bufferSearchVisible : ( query ) => workerInstanceRef . request . bufferSearchVisible ( query ) , onNvimLoaded : ( fn ) => ee . on ( 'nvim.load' , fn ) , nvimGetVar : ( key ) => workerInstanceRef . request . nvimGetVar ( key ) , nvimCommand : ( command ) => workerInstanceRef . call . nvimCommand ( command ) , nvimFeedkeys : ( keys , mode = 'm' ) => workerInstanceRef . call . nvimFeedkeys ( keys , mode ) , nvimExpr : ( expr ) => workerInstanceRef . request . nvimExpr ( expr ) , nvimCall : onFnCall ( ( name , a ) => workerInstanceRef . request . nvimCall ( name , a ) ) as Functions , nvimJumpTo : ( coords ) => workerInstanceRef . call . nvimJumpTo ( coords ) , nvimGetKeymap : ( ) => workerInstanceRef . request . nvimGetKeymap ( ) , nvimGetColorByName : ( name ) => workerInstanceRef . request . nvimGetColorByName ( name ) , nvimSaveCursor : async ( ) => { const instance = workerInstanceRef ; const pos = await workerInstanceRef . request . nvimSaveCursor ( ) ; return ( ) => instance . call . nvimRestoreCursor ( pos ) ; } , getHighlightByName : ( name , isRgb ? ) : Promise < { foreground ? : number ; background ? : number ; reverse ? : boolean ; } > => workerInstanceRef . request . nvimGetHighlightByName ( name , isRgb ) , nvimHighlightSearchPattern : ( pattern , id ? ) : Promise < number > => workerInstanceRef . request . nvimHighlightSearchPattern ( pattern , id ) , nvimRemoveHighlightSearch : ( id , pattern ? ) : Promise < boolean > => workerInstanceRef . request . nvimRemoveHighlightSearch ( id , pattern ) , onInputRemapModifiersDidChange : ( fn ) => ee . on ( 'input.remap.modifiers' , fn ) , onInputKeyTransformsDidChange : ( fn ) => ee . on ( 'input.key.transforms' , fn ) , nvimOption : ( opt ) : Promise < any > => workerInstanceRef . request . nvimOption ( opt ) , } ; } ; export default InstanceApi ; export type InstanceApi = ReturnType < typeof InstanceApi > ;