From f108c906434b0c5eb3e2159f877d39a97853f078 Mon Sep 17 00:00:00 2001 From: Dmitry Maluka Date: Thu, 22 Oct 2020 22:29:16 +0200 Subject: [PATCH] hltrailingws: improve updateTrailingWs logic Handle the case when the cursor itself hasn't really moved to another line, but its line number has changed due to insert or remove of some lines above. In this case, if the cursor is still at its new trailingws, we should not reset NewTrailingWsY to -1 but update it to the new line number. A scenario exemplifying this issue: Bind some key, e.g. Alt-r, to such a lua function: function insertNewlineAbove(bp) bp.Buf:Insert(buffer.Loc(0, bp.Cursor.Y), "\n") end Then in a file containing these lines: aaa bbb ccc insert a space at the end of bbb line, and then press Alt-r. bbb and ccc are moved one line down, but also the trailing space after bbb becomes highlighted, which isn't what we expect. This commit fixes that. --- internal/buffer/eventhandler.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/internal/buffer/eventhandler.go b/internal/buffer/eventhandler.go index 66c44dba..6dfb32a0 100644 --- a/internal/buffer/eventhandler.go +++ b/internal/buffer/eventhandler.go @@ -391,5 +391,11 @@ func (eh *EventHandler) updateTrailingWs(t *TextEvent) { } else if !removedAfterWs { c.NewTrailingWsY = -1 } + } else if c.NewTrailingWsY != -1 && start.Y != end.Y && c.Loc.GreaterThan(start) && + ((t.EventType == TextEventInsert && c.Y == c.NewTrailingWsY+(end.Y-start.Y)) || + (t.EventType == TextEventRemove && c.Y == c.NewTrailingWsY-(end.Y-start.Y))) { + // The cursor still has its new trailingws + // but its line number was shifted by insert or remove of lines above + c.NewTrailingWsY = c.Y } }