]> git.lyx.org Git - lyx.git/commitdiff
Make BufferView::singeParUpdate more robust
authorJean-Marc Lasgouttes <lasgouttes@lyx.org>
Mon, 1 Jul 2024 21:56:33 +0000 (23:56 +0200)
committerJean-Marc Lasgouttes <lasgouttes@lyx.org>
Wed, 10 Jul 2024 15:18:48 +0000 (17:18 +0200)
In some cases, it might happen that this method is called with no
metrics or position known for the current paragraph.

Take care of these cases to avoid assertions.

Remove setting of inset positions in the method, but make sure that
updateMetrics(false) is always called to get everything right.

In the new code, updateMetrics(bool) is the method that sets
everything right with minimal effort.

(cherry picked from commit 89ab9eb569ec0eea87d9a8c269eb87507934e1c5)

src/BufferView.cpp

index 428c03299f00288e27bc35f8834fd3e01b7cfeff..1fdfa78e2a2c6059a1c459c2b6e02117c002fbc5 100644 (file)
@@ -564,10 +564,12 @@ void BufferView::processUpdateFlags(Update::flags flags)
        // are not already redrawing all).
        // We handle this before FitCursor because the later will require
        // correct metrics at cursor position.
-       if (!(flags & Update::ForceDraw)
-                       && (flags & Update::SinglePar)
-                       && !singleParUpdate())
-               updateMetrics(true);
+       if (!(flags & Update::ForceDraw) && (flags & Update::SinglePar)) {
+               if (singleParUpdate())
+                       updateMetrics(false);
+               else
+                       updateMetrics(true);
+       }
 
        // Then make sure that the screen contains the cursor if needed
        if (flags & Update::FitCursor) {
@@ -3132,6 +3134,10 @@ bool BufferView::singleParUpdate()
        if (d->inlineCompletionPos_.fixIfBroken())
                d->inlineCompletionPos_ = DocIterator();
 
+       if (!tm.contains(pit)) {
+               LYXERR(Debug::PAINTING, "SinglePar optimization failed: no known metrics");
+               return false;
+       }
        /* Try to rebreak only the paragraph containing the cursor (if
         * this paragraph contains insets etc., rebreaking will
         * recursively descend). We need a full redraw if either
@@ -3149,18 +3155,19 @@ bool BufferView::singleParUpdate()
        tm.redoParagraph(pit);
        ParagraphMetrics & pm = tm.parMetrics(pit);
        if (pm.height() != old_dim.height()
-               || (pm.width() != old_dim.width() && old_dim.width() == tm.width())) {
+            || (pm.width() != old_dim.width() && old_dim.width() == tm.width())) {
                // Paragraph height or width has changed so we cannot proceed
                // to the singlePar optimisation.
-               LYXERR(Debug::PAINTING, "SinglePar optimization failed.");
+               LYXERR(Debug::PAINTING, "SinglePar optimization failed: paragraph metrics changed");
                return false;
        }
        // Since position() points to the baseline of the first row, we
        // may have to update it. See ticket #11601 for an example where
        // the height does not change but the ascent does.
-       pm.setPosition(pm.position() - old_dim.ascent() + pm.ascent());
-
-       tm.updatePosCache(pit);
+       if (pm.hasPosition())
+               pm.setPosition(pm.position() - old_dim.ascent() + pm.ascent());
+       else
+               LYXERR0("SinglePar optimization succeeded, but no position to update");
 
        LYXERR(Debug::PAINTING, "\ny1: " << pm.top() << " y2: " << pm.bottom()
                << " pit: " << pit << " singlepar: 1");