Smarter smartpaste (#3001) (#3002)

* smarterpaste(?)

* make it more readable

* fix edge cases

* fix paste starting with a single space

* fix single line paste
This commit is contained in:
Mikko 2024-03-13 22:16:10 +02:00 committed by GitHub
parent a01ae92541
commit bd306d67b4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1274,9 +1274,13 @@ func (h *BufPane) PastePrimary() bool {
func (h *BufPane) paste(clip string) {
if h.Buf.Settings["smartpaste"].(bool) {
if h.Cursor.X > 0 && len(util.GetLeadingWhitespace([]byte(strings.TrimLeft(clip, "\r\n")))) == 0 {
leadingWS := util.GetLeadingWhitespace(h.Buf.LineBytes(h.Cursor.Y))
clip = strings.ReplaceAll(clip, "\n", "\n"+string(leadingWS))
if h.Cursor.X > 0 {
leadingPasteWS := string(util.GetLeadingWhitespace([]byte(clip)))
if leadingPasteWS != " " && strings.Contains(clip, "\n"+leadingPasteWS) {
leadingWS := string(util.GetLeadingWhitespace(h.Buf.LineBytes(h.Cursor.Y)))
clip = strings.TrimPrefix(clip, leadingPasteWS)
clip = strings.ReplaceAll(clip, "\n"+leadingPasteWS, "\n"+leadingWS)
}
}
}