import { isEqual , pick } from "lodash/fp" ; import CodeMirror from "codemirror" ; const getPaddingText = ( pos1 , pos2 , lastPos , ch ) : string => { const pickPos = pick ( [ "line" , "ch" ] ) ; if ( isEqual ( pickPos ( pos2 ) , pickPos ( lastPos ) ) ) return " " ; if ( isEqual ( pickPos ( pos1 ) , pickPos ( pos2 ) ) ) return "" ; if ( ch === " " ) return "" ; return " " ; } ; export const getPaddedTextToInsert = ( doc , textToInsert ) : string => { const _doc = doc . copy ( false ) ; const from = _doc . getCursor ( "from" ) ; const to = _doc . getCursor ( "to" ) ; _doc . extendSelection ( { ... from , ch : from . ch - 1 } , { ... to , ch : to . ch + 1 } ) ; const extSelectedText = _doc . getSelection ( ) ; const extFrom = _doc . getCursor ( "from" ) ; const extTo = _doc . getCursor ( "to" ) ; const lastLine = _doc . lastLine ( ) ; const lastPos = { line : lastLine , ch : _doc . getLine ( lastLine ) . length , } ; const leftCh = getPaddingText ( from , extFrom , lastPos , extSelectedText . slice ( 0 , 1 ) ) ; const rightCh = getPaddingText ( to , extTo , lastPos , extSelectedText . slice ( - 1 ) ) ; const paddedTextToInsert = `${ leftCh }${ textToInsert }${ rightCh }` ; return paddedTextToInsert ; } ;