Do not update statisitics if buffer has not changed

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
This commit is contained in:
Jean-Marc Lasgouttes 2024-07-24 22:19:32 +02:00
parent 823d290036
commit 0d50a8417f
2 changed files with 29 additions and 17 deletions

View File

@ -13,11 +13,13 @@
#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;
}

View 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;
};
}