]> git.lyx.org Git - lyx.git/commitdiff
Do not update statisitics if buffer has not changed
authorJean-Marc Lasgouttes <lasgouttes@lyx.org>
Wed, 24 Jul 2024 20:19:32 +0000 (22:19 +0200)
committerJean-Marc Lasgouttes <lasgouttes@lyx.org>
Wed, 24 Jul 2024 20:38:39 +0000 (22:38 +0200)
Rely on the newly-introduced Buffer::id() to skip statistics
computation if the id is the same as last time. This will reduce the
annoyance of updates triggering at random times.

Take this occasion to clean code up:

- add 'skip' parameter (true by default) to Statistics::update to indicate
  that the insets that do not produce output should be skipped.

- use a trailing underscrore for private members

src/Statistics.cpp
src/Statistics.h

index eabae8f399432ba808bab84b12879a2c5522863a..b0e109b584548e81dc941508d27cd634ddb5fe00 100644 (file)
 
 #include "Statistics.h"
 
+#include "Buffer.h"
 #include "Paragraph.h"
 #include "Text.h"
 #include "Cursor.h"
 
 #include "support/lassert.h"
+#include "support/debug.h"
 #include "support/lstrings.h"
 #include "support/textutils.h"
 
@@ -27,10 +29,17 @@ namespace lyx {
 using namespace support;
 
 
-void Statistics::update(CursorData const & cur)
+void Statistics::update(CursorData const & cur, bool skip)
 {
+       // early exit if the buffer has not changed since last time
+       if (stats_id_ == cur.buffer()->id())
+               return;
+
        // reset counts
        *this = Statistics();
+       skip_no_output_ = skip;
+       stats_id_ = cur.buffer()->id();
+
        if (cur.selection()) {
                if (cur.inMathed())
                        return;
@@ -91,15 +100,15 @@ void Statistics::update(Paragraph const & par, pos_type from, pos_type to)
                // Stuff that we skip
                if (par.isDeleted(pos))
                        continue;
-               if (ins && skip_no_output && !ins->producesOutput())
+               if (ins && skip_no_output_ && !ins->producesOutput())
                        continue;
 
                // words
                if (par.isWordSeparator(pos))
-                       inword = false;
-               else if (!inword) {
+                       inword_ = false;
+               else if (!inword_) {
                        ++word_count;
-                       inword = true;
+                       inword_ = true;
                }
 
                if (ins)
@@ -112,7 +121,7 @@ void Statistics::update(Paragraph const & par, pos_type from, pos_type to)
                                ++blank_count;
                }
        }
-       inword = false;
+       inword_ = false;
 }
 
 
index 7439de06cf9a7e3953450059b3789f13c3dac1f4..e91e9fc3405e4001da265b6ad92eb5ab5076b5b0 100644 (file)
@@ -25,22 +25,21 @@ class Paragraph;
 // Class used to compute letters/words statistics on buffer or selection
 class Statistics {
 public:
+       /// Count characters in the whole document, or in the selection if
+       /// there is one. This is the main entry point.
+       void update(CursorData const & cur, bool skip = true);
+
+       /// Helper: count chars and words in this string
+       void update(docstring const & s);
+       /// Helper: count chars and words in the paragraphs of \c text
+       void update(Text const & text);
+
        // Number of words
        int word_count = 0;
        // Number of non blank characters
        int char_count = 0;
        // Number of blank characters
        int blank_count = 0;
-       // Indicate whether parts that are not output should be counted.
-       bool skip_no_output = true;
-
-       /// Count characters in the whole document, or in the selection if
-       /// there is one. This is the main entry point.
-       void update(CursorData const & cur);
-       ///  Count chars and words in this string
-       void update(docstring const & s);
-       /// Count chars and words in the paragraphs of \c text
-       void update(Text const & text);
 
 private:
 
@@ -55,8 +54,12 @@ private:
         */
        void update(Paragraph const & par, pos_type from = 0, pos_type to = -1);
 
+       // Indicate whether parts that produce no output should be counted.
+       bool skip_no_output_;
        // Used in the code to track status
-       bool inword = false;
+       bool inword_ = false;
+       // The buffer id at last statistics computation.
+       int stats_id_ = -1;
 };
 
 }