import debounce from 'lodash/debounce' ; import type { Nvim , Transport } from '@vvim/nvim' ; type BooleanSetting = 0 | 1 ; export type Settings = { fullscreen : BooleanSetting ; simplefullscreen : BooleanSetting ; bold : BooleanSetting ; italic : BooleanSetting ; underline : BooleanSetting ; undercurl : BooleanSetting ; strikethrough : BooleanSetting ; fontfamily : string ; fontsize : string ; lineheight : string ; letterspacing : string ; reloadchanged : BooleanSetting ; quitoncloselastwindow : BooleanSetting ; autoupdateinterval : string ; openInProject : BooleanSetting ; } ; export type SettingsCallback = ( newSettings : Partial < Settings > , allSettings : Settings ) => void ; export const getDefaultSettings = ( ) : Settings => ( { fullscreen : 0 , simplefullscreen : 1 , bold : 1 , italic : 1 , underline : 1 , undercurl : 1 , strikethrough : 1 , fontfamily : 'monospace' , fontsize : '12' , lineheight : '1.25' , letterspacing : '0' , reloadchanged : 0 , quitoncloselastwindow : 0 , autoupdateinterval : '1440' , openInProject : 0 , } ) ; let hasCustomConfig = false ; const initSettings = ( { nvim , args , transport , } ) : void => { hasCustomConfig = args ?. indexOf ( '-u' ) !== - 1 ; let initialSettings = getDefaultSettings ( ) ; let settings = getDefaultSettings ( ) ; let newSettings = { } ; const applyAllSettings = async ( ) => { settings = { ... settings , ... newSettings , } ; if ( initialSettings && ! hasCustomConfig ) { newSettings = Object . keys ( settings ) . reduce < Partial < Settings > > ( ( result , key ) => { if ( initialSettings [ key ] !== settings [ key ] ) { return { ... result , [ key ] : settings [ key ] , } ; } return result ; } , { } ) ; initialSettings = null ; } transport . send ( 'updateSettings' , newSettings , settings ) ; newSettings = { } ; } ; const debouncedApplyAllSettings = debounce ( applyAllSettings , 10 ) ; const applySetting = < K extends keyof Settings > ( [ option , props ] ) => { if ( props !== null ) { newSettings [ option ] = props ; debouncedApplyAllSettings ( ) ; } } ; nvim . on ( 'vv:set' , applySetting ) ; } ; export default initSettings ;