github.com/jmigpin/editor@v1.6.0/util/uiutil/widget/offsetscrollutil.go (about)

     1  package widget
     2  
     3  //godebug:annotatefile
     4  
     5  // offset, start, deletedn, insertedn
     6  func StableOffsetScroll(o int, s, dn, in int) int {
     7  	// TODO: need to know if the insertion/delete is on the same line to try to keep the line percentage (smooth scrolling)
     8  
     9  	ed := s + dn // end of deletes
    10  	ei := s + in // end of inserts
    11  
    12  	if o <= s { // o<s<={ed,ei}
    13  		return o
    14  	}
    15  	if o <= ed { // s<o<=ed
    16  		if o <= ei { // s<o<={ed,ei}
    17  			return o // inserts cover the deletes
    18  		} else { // s<ei<o<=ed
    19  			// add missing to cover the deletes
    20  			return o - (o - ei)
    21  		}
    22  	}
    23  	if o <= ei { // s<ed<o<=ei
    24  		// inserts cover the deletes
    25  		return o
    26  	}
    27  	// s<{ed,ei}<o
    28  	o += in - dn // add missing bytes to reach old offset
    29  	return o
    30  }