diff --git a/CHANGELOG.md b/CHANGELOG.md index 9760b6601..dc3924f39 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -63,6 +63,7 @@ - Bugfix: Fixed double-click selection not working when clicking outside a message. (#5617) - Bugfix: Fixed emotes starting with ":" not tab-completing. (#5603) - Bugfix: Fixed 7TV emotes messing with Qt's HTML. (#5677) +- Bugfix: Fixed incorrect messages getting replaced visually. (#5683) - Dev: Update Windows build from Qt 6.5.0 to Qt 6.7.1. (#5420) - Dev: Update vcpkg build Qt from 6.5.0 to 6.7.0, boost from 1.83.0 to 1.85.0, openssl from 3.1.3 to 3.3.0. (#5422) - Dev: Unsingletonize `ISoundController`. (#5462) @@ -75,7 +76,7 @@ - Dev: Removed unused timegate settings. (#5361) - Dev: Add `Channel::addSystemMessage` helper function, allowing us to avoid the common `channel->addMessage(makeSystemMessage(...));` pattern. (#5500) - Dev: Unsingletonize `Resources2`. (#5460) -- Dev: All Lua globals now show in the `c2` global in the LuaLS metadata. (#5385) +- Dev: All Lua globals now show in the `c2` global in the LuaLS metadata. (#5385, #5682) - Dev: Images are now loaded in worker threads. (#5431) - Dev: Fixed broken `SignalVector::operator[]` implementation. (#5556) - Dev: Qt Creator now auto-configures Conan when loading the project and skips vcpkg. (#5305) @@ -110,7 +111,7 @@ - Dev: Twitch messages are now sent using Twitch's Helix API instead of IRC by default. (#5607) - Dev: `GIFTimer` is no longer initialized in tests. (#5608) - Dev: Emojis now use flags instead of a set of strings for capabilities. (#5616) -- Dev: Move plugins to Sol2. (#5622) +- Dev: Move plugins to Sol2. (#5622, #5682) - Dev: Refactored static `MessageBuilder` helpers to standalone functions. (#5652) - Dev: Decoupled reply parsing from `MessageBuilder`. (#5660, #5668) - Dev: Refactored IRC message building. (#5663) diff --git a/docs/plugin-meta.lua b/docs/plugin-meta.lua index 5a86efca0..27cdf8786 100644 --- a/docs/plugin-meta.lua +++ b/docs/plugin-meta.lua @@ -5,20 +5,20 @@ -- Add the folder this file is in to "Lua.workspace.library". c2 = {} ----@alias c2.LogLevel.Debug "c2.LogLevel.Debug" ----@alias c2.LogLevel.Info "c2.LogLevel.Info" ----@alias c2.LogLevel.Warning "c2.LogLevel.Warning" ----@alias c2.LogLevel.Critical "c2.LogLevel.Critical" ----@alias c2.LogLevel c2.LogLevel.Debug|c2.LogLevel.Info|c2.LogLevel.Warning|c2.LogLevel.Critical ----@type { Debug: c2.LogLevel.Debug, Info: c2.LogLevel.Info, Warning: c2.LogLevel.Warning, Critical: c2.LogLevel.Critical } -c2.LogLevel = {} +---@enum c2.LogLevel +c2.LogLevel = { + Debug = {}, ---@type c2.LogLevel.Debug + Info = {}, ---@type c2.LogLevel.Info + Warning = {}, ---@type c2.LogLevel.Warning + Critical = {}, ---@type c2.LogLevel.Critical +} -- Begin src/controllers/plugins/api/EventType.hpp ----@alias c2.EventType.CompletionRequested "c2.EventType.CompletionRequested" ----@alias c2.EventType c2.EventType.CompletionRequested ----@type { CompletionRequested: c2.EventType.CompletionRequested } -c2.EventType = {} +---@enum c2.EventType +c2.EventType = { + CompletionRequested = {}, ---@type c2.EventType.CompletionRequested +} -- End src/controllers/plugins/api/EventType.hpp @@ -38,19 +38,19 @@ c2.EventType = {} -- Begin src/common/Channel.hpp ----@alias c2.ChannelType.None "c2.ChannelType.None" ----@alias c2.ChannelType.Direct "c2.ChannelType.Direct" ----@alias c2.ChannelType.Twitch "c2.ChannelType.Twitch" ----@alias c2.ChannelType.TwitchWhispers "c2.ChannelType.TwitchWhispers" ----@alias c2.ChannelType.TwitchWatching "c2.ChannelType.TwitchWatching" ----@alias c2.ChannelType.TwitchMentions "c2.ChannelType.TwitchMentions" ----@alias c2.ChannelType.TwitchLive "c2.ChannelType.TwitchLive" ----@alias c2.ChannelType.TwitchAutomod "c2.ChannelType.TwitchAutomod" ----@alias c2.ChannelType.TwitchEnd "c2.ChannelType.TwitchEnd" ----@alias c2.ChannelType.Misc "c2.ChannelType.Misc" ----@alias c2.ChannelType c2.ChannelType.None|c2.ChannelType.Direct|c2.ChannelType.Twitch|c2.ChannelType.TwitchWhispers|c2.ChannelType.TwitchWatching|c2.ChannelType.TwitchMentions|c2.ChannelType.TwitchLive|c2.ChannelType.TwitchAutomod|c2.ChannelType.TwitchEnd|c2.ChannelType.Misc ----@type { None: c2.ChannelType.None, Direct: c2.ChannelType.Direct, Twitch: c2.ChannelType.Twitch, TwitchWhispers: c2.ChannelType.TwitchWhispers, TwitchWatching: c2.ChannelType.TwitchWatching, TwitchMentions: c2.ChannelType.TwitchMentions, TwitchLive: c2.ChannelType.TwitchLive, TwitchAutomod: c2.ChannelType.TwitchAutomod, TwitchEnd: c2.ChannelType.TwitchEnd, Misc: c2.ChannelType.Misc } -c2.ChannelType = {} +---@enum c2.ChannelType +c2.ChannelType = { + None = {}, ---@type c2.ChannelType.None + Direct = {}, ---@type c2.ChannelType.Direct + Twitch = {}, ---@type c2.ChannelType.Twitch + TwitchWhispers = {}, ---@type c2.ChannelType.TwitchWhispers + TwitchWatching = {}, ---@type c2.ChannelType.TwitchWatching + TwitchMentions = {}, ---@type c2.ChannelType.TwitchMentions + TwitchLive = {}, ---@type c2.ChannelType.TwitchLive + TwitchAutomod = {}, ---@type c2.ChannelType.TwitchAutomod + TwitchEnd = {}, ---@type c2.ChannelType.TwitchEnd + Misc = {}, ---@type c2.ChannelType.Misc +} -- End src/common/Channel.hpp @@ -174,90 +174,97 @@ function c2.Channel.by_twitch_id(id) end -- Begin src/controllers/plugins/api/HTTPResponse.hpp ----@class HTTPResponse -HTTPResponse = {} +---@class c2.HTTPResponse +c2.HTTPResponse = {} --- Returns the data. This is not guaranteed to be encoded using any --- particular encoding scheme. It's just the bytes the server returned. --- -function HTTPResponse:data() end +---@return string +---@nodiscard +function c2.HTTPResponse:data() end --- Returns the status code. --- -function HTTPResponse:status() end +---@return number|nil +---@nodiscard +function c2.HTTPResponse:status() end --- A somewhat human readable description of an error if such happened --- -function HTTPResponse:error() end +---@return string +---@nodiscard +function c2.HTTPResponse:error() end ---@return string -function HTTPResponse:__tostring() end +---@nodiscard +function c2.HTTPResponse:__tostring() end -- End src/controllers/plugins/api/HTTPResponse.hpp -- Begin src/controllers/plugins/api/HTTPRequest.hpp ----@alias HTTPCallback fun(result: HTTPResponse): nil ----@class HTTPRequest -HTTPRequest = {} +---@alias c2.HTTPCallback fun(result: c2.HTTPResponse): nil +---@class c2.HTTPRequest +c2.HTTPRequest = {} --- Sets the success callback --- ----@param callback HTTPCallback Function to call when the HTTP request succeeds -function HTTPRequest:on_success(callback) end +---@param callback c2.HTTPCallback Function to call when the HTTP request succeeds +function c2.HTTPRequest:on_success(callback) end --- Sets the failure callback --- ----@param callback HTTPCallback Function to call when the HTTP request fails or returns a non-ok status -function HTTPRequest:on_error(callback) end +---@param callback c2.HTTPCallback Function to call when the HTTP request fails or returns a non-ok status +function c2.HTTPRequest:on_error(callback) end --- Sets the finally callback --- ---@param callback fun(): nil Function to call when the HTTP request finishes -function HTTPRequest:finally(callback) end +function c2.HTTPRequest:finally(callback) end --- Sets the timeout --- ---@param timeout integer How long in milliseconds until the times out -function HTTPRequest:set_timeout(timeout) end +function c2.HTTPRequest:set_timeout(timeout) end --- Sets the request payload --- ---@param data string -function HTTPRequest:set_payload(data) end +function c2.HTTPRequest:set_payload(data) end --- Sets a header in the request --- ---@param name string ---@param value string -function HTTPRequest:set_header(name, value) end +function c2.HTTPRequest:set_header(name, value) end --- Executes the HTTP request --- -function HTTPRequest:execute() end +function c2.HTTPRequest:execute() end ---@return string -function HTTPRequest:__tostring() end +function c2.HTTPRequest:__tostring() end --- Creates a new HTTPRequest --- ----@param method HTTPMethod Method to use +---@param method c2.HTTPMethod Method to use ---@param url string Where to send the request to ----@return HTTPRequest -function HTTPRequest.create(method, url) end +---@return c2.HTTPRequest +function c2.HTTPRequest.create(method, url) end -- End src/controllers/plugins/api/HTTPRequest.hpp -- Begin src/common/network/NetworkCommon.hpp ----@alias HTTPMethod.Get "HTTPMethod.Get" ----@alias HTTPMethod.Post "HTTPMethod.Post" ----@alias HTTPMethod.Put "HTTPMethod.Put" ----@alias HTTPMethod.Delete "HTTPMethod.Delete" ----@alias HTTPMethod.Patch "HTTPMethod.Patch" ----@alias HTTPMethod HTTPMethod.Get|HTTPMethod.Post|HTTPMethod.Put|HTTPMethod.Delete|HTTPMethod.Patch ----@type { Get: HTTPMethod.Get, Post: HTTPMethod.Post, Put: HTTPMethod.Put, Delete: HTTPMethod.Delete, Patch: HTTPMethod.Patch } -HTTPMethod = {} +---@enum c2.HTTPMethod +c2.HTTPMethod = { + Get = {}, ---@type c2.HTTPMethod.Get + Post = {}, ---@type c2.HTTPMethod.Post + Put = {}, ---@type c2.HTTPMethod.Put + Delete = {}, ---@type c2.HTTPMethod.Delete + Patch = {}, ---@type c2.HTTPMethod.Patch +} -- End src/common/network/NetworkCommon.hpp diff --git a/scripts/make_luals_meta.py b/scripts/make_luals_meta.py old mode 100644 new mode 100755 index b1420e780..e1dafe496 --- a/scripts/make_luals_meta.py +++ b/scripts/make_luals_meta.py @@ -1,3 +1,4 @@ +#!/usr/bin/env python3 """ This script generates docs/plugin-meta.lua. It accepts no arguments @@ -242,25 +243,19 @@ def read_file(path: Path, out: TextIOWrapper): ) name = header[0].split(" ", 1)[1] printmsg(path, reader.line_no(), f"enum {name}") - variants = reader.read_enum_variants() - - vtypes = [] - for variant in variants: - vtype = f'{name}.{variant}' - vtypes.append(vtype) - out.write(f'---@alias {vtype} "{vtype}"\n') - - out.write(f"---@alias {name} {'|'.join(vtypes)}\n") if header_comment: out.write(f"--- {header_comment}\n") - out.write("---@type { ") + out.write(f"---@enum {name}\n") + out.write(f"{name} = {{\n") out.write( - ", ".join( - [f"{variant}: {typ}" for variant, typ in zip(variants,vtypes)] + "\n".join( + [ + f" {variant} = {{}}, ---@type {name}.{variant}" + for variant in reader.read_enum_variants() + ] ) ) - out.write(" }\n") - out.write(f"{name} = {{}}\n\n") + out.write("\n}\n\n") continue # class diff --git a/src/common/Channel.cpp b/src/common/Channel.cpp index 18f55c02b..6396ef00c 100644 --- a/src/common/Channel.cpp +++ b/src/common/Channel.cpp @@ -255,21 +255,33 @@ void Channel::fillInMissingMessages(const std::vector &messages) } } -void Channel::replaceMessage(MessagePtr message, MessagePtr replacement) +void Channel::replaceMessage(const MessagePtr &message, + const MessagePtr &replacement) { int index = this->messages_.replaceItem(message, replacement); if (index >= 0) { - this->messageReplaced.invoke((size_t)index, replacement); + this->messageReplaced.invoke((size_t)index, message, replacement); } } -void Channel::replaceMessage(size_t index, MessagePtr replacement) +void Channel::replaceMessage(size_t index, const MessagePtr &replacement) { - if (this->messages_.replaceItem(index, replacement)) + MessagePtr prev; + if (this->messages_.replaceItem(index, replacement, &prev)) { - this->messageReplaced.invoke(index, replacement); + this->messageReplaced.invoke(index, prev, replacement); + } +} + +void Channel::replaceMessage(size_t hint, const MessagePtr &message, + const MessagePtr &replacement) +{ + auto index = this->messages_.replaceItem(hint, message, replacement); + if (index >= 0) + { + this->messageReplaced.invoke(hint, message, replacement); } } diff --git a/src/common/Channel.hpp b/src/common/Channel.hpp index ff75521a8..c7d006f1b 100644 --- a/src/common/Channel.hpp +++ b/src/common/Channel.hpp @@ -60,7 +60,9 @@ public: pajlada::Signals::Signal> messageAppended; pajlada::Signals::Signal &> messagesAddedAtStart; - pajlada::Signals::Signal messageReplaced; + /// (index, prev-message, replacement) + pajlada::Signals::Signal + messageReplaced; /// Invoked when some number of messages were filled in using time received pajlada::Signals::Signal &> filledInMessages; pajlada::Signals::NoArgSignal destroyed; @@ -91,8 +93,11 @@ public: void addOrReplaceTimeout(MessagePtr message, QTime now) final; void disableAllMessages() final; - void replaceMessage(MessagePtr message, MessagePtr replacement); - void replaceMessage(size_t index, MessagePtr replacement); + void replaceMessage(const MessagePtr &message, + const MessagePtr &replacement); + void replaceMessage(size_t index, const MessagePtr &replacement); + void replaceMessage(size_t hint, const MessagePtr &message, + const MessagePtr &replacement); void deleteMessage(QString messageID); /// Removes all messages from this channel and invokes #messagesCleared diff --git a/src/common/network/NetworkCommon.hpp b/src/common/network/NetworkCommon.hpp index 215b828a7..8efd66620 100644 --- a/src/common/network/NetworkCommon.hpp +++ b/src/common/network/NetworkCommon.hpp @@ -16,7 +16,7 @@ using NetworkErrorCallback = std::function; using NetworkFinallyCallback = std::function; /** - * @exposeenum HTTPMethod + * @exposeenum c2.HTTPMethod */ enum class NetworkRequestType { Get, diff --git a/src/controllers/plugins/api/HTTPRequest.hpp b/src/controllers/plugins/api/HTTPRequest.hpp index 6fe3b97be..ebf82967f 100644 --- a/src/controllers/plugins/api/HTTPRequest.hpp +++ b/src/controllers/plugins/api/HTTPRequest.hpp @@ -16,11 +16,11 @@ namespace chatterino::lua::api { // NOLINTBEGIN(readability-identifier-naming) /** - * @lua@alias HTTPCallback fun(result: HTTPResponse): nil + * @lua@alias c2.HTTPCallback fun(result: c2.HTTPResponse): nil */ /** - * @lua@class HTTPRequest + * @lua@class c2.HTTPRequest */ class HTTPRequest : public std::enable_shared_from_this { @@ -61,16 +61,16 @@ public: /** * Sets the success callback * - * @lua@param callback HTTPCallback Function to call when the HTTP request succeeds - * @exposed HTTPRequest:on_success + * @lua@param callback c2.HTTPCallback Function to call when the HTTP request succeeds + * @exposed c2.HTTPRequest:on_success */ void on_success(sol::protected_function func); /** * Sets the failure callback * - * @lua@param callback HTTPCallback Function to call when the HTTP request fails or returns a non-ok status - * @exposed HTTPRequest:on_error + * @lua@param callback c2.HTTPCallback Function to call when the HTTP request fails or returns a non-ok status + * @exposed c2.HTTPRequest:on_error */ void on_error(sol::protected_function func); @@ -78,7 +78,7 @@ public: * Sets the finally callback * * @lua@param callback fun(): nil Function to call when the HTTP request finishes - * @exposed HTTPRequest:finally + * @exposed c2.HTTPRequest:finally */ void finally(sol::protected_function func); @@ -86,7 +86,7 @@ public: * Sets the timeout * * @lua@param timeout integer How long in milliseconds until the times out - * @exposed HTTPRequest:set_timeout + * @exposed c2.HTTPRequest:set_timeout */ void set_timeout(int timeout); @@ -94,7 +94,7 @@ public: * Sets the request payload * * @lua@param data string - * @exposed HTTPRequest:set_payload + * @exposed c2.HTTPRequest:set_payload */ void set_payload(QByteArray payload); @@ -103,19 +103,19 @@ public: * * @lua@param name string * @lua@param value string - * @exposed HTTPRequest:set_header + * @exposed c2.HTTPRequest:set_header */ void set_header(QByteArray name, QByteArray value); /** * Executes the HTTP request * - * @exposed HTTPRequest:execute + * @exposed c2.HTTPRequest:execute */ void execute(sol::this_state L); /** * @lua@return string - * @exposed HTTPRequest:__tostring + * @exposed c2.HTTPRequest:__tostring */ QString to_string(); @@ -126,11 +126,11 @@ public: /** * Creates a new HTTPRequest * - * @lua@param method HTTPMethod Method to use + * @lua@param method c2.HTTPMethod Method to use * @lua@param url string Where to send the request to * - * @lua@return HTTPRequest - * @exposed HTTPRequest.create + * @lua@return c2.HTTPRequest + * @exposed c2.HTTPRequest.create */ static std::shared_ptr create(sol::this_state L, NetworkRequestType method, diff --git a/src/controllers/plugins/api/HTTPResponse.hpp b/src/controllers/plugins/api/HTTPResponse.hpp index 80eb49bd3..9997c858d 100644 --- a/src/controllers/plugins/api/HTTPResponse.hpp +++ b/src/controllers/plugins/api/HTTPResponse.hpp @@ -15,7 +15,7 @@ namespace chatterino::lua::api { // NOLINTBEGIN(readability-identifier-naming) /** - * @lua@class HTTPResponse + * @lua@class c2.HTTPResponse */ class HTTPResponse { @@ -38,26 +38,34 @@ public: * Returns the data. This is not guaranteed to be encoded using any * particular encoding scheme. It's just the bytes the server returned. * - * @exposed HTTPResponse:data + * @lua@return string + * @lua@nodiscard + * @exposed c2.HTTPResponse:data */ QByteArray data(); /** * Returns the status code. * - * @exposed HTTPResponse:status + * @lua@return number|nil + * @lua@nodiscard + * @exposed c2.HTTPResponse:status */ std::optional status(); /** * A somewhat human readable description of an error if such happened - * @exposed HTTPResponse:error + * + * @lua@return string + * @lua@nodiscard + * @exposed c2.HTTPResponse:error */ QString error(); /** * @lua@return string - * @exposed HTTPResponse:__tostring + * @lua@nodiscard + * @exposed c2.HTTPResponse:__tostring */ QString to_string(); }; diff --git a/src/messages/LimitedQueue.hpp b/src/messages/LimitedQueue.hpp index a204c84a9..4d223ab15 100644 --- a/src/messages/LimitedQueue.hpp +++ b/src/messages/LimitedQueue.hpp @@ -8,6 +8,7 @@ #include #include #include +#include #include namespace chatterino { @@ -212,9 +213,10 @@ public: * * @param[in] index the index of the item to replace * @param[in] replacement the item to put in place of the item at index + * @param[out] prev (optional) the item located at @a index before replacing * @return true if a replacement took place */ - bool replaceItem(size_t index, const T &replacement) + bool replaceItem(size_t index, const T &replacement, T *prev = nullptr) { std::unique_lock lock(this->mutex_); @@ -223,10 +225,46 @@ public: return false; } - this->buffer_[index] = replacement; + if (prev) + { + *prev = std::exchange(this->buffer_[index], replacement); + } + else + { + this->buffer_[index] = replacement; + } return true; } + /** + * @brief Replace the needle with the given item + * + * @param hint A hint on where the needle _might_ be + * @param[in] needle the item to search for + * @param[in] replacement the item to replace needle with + * @return the index of the replaced item, or -1 if no replacement took place + */ + int replaceItem(size_t hint, const T &needle, const T &replacement) + { + std::unique_lock lock(this->mutex_); + + if (hint < this->buffer_.size() && this->buffer_[hint] == needle) + { + this->buffer_[hint] = replacement; + return static_cast(hint); + } + + for (size_t i = 0; i < this->buffer_.size(); ++i) + { + if (this->buffer_[i] == needle) + { + this->buffer_[i] = replacement; + return static_cast(i); + } + } + return -1; + } + /** * @brief Inserts the given item before another item * @@ -315,6 +353,32 @@ public: return std::nullopt; } + /** + * @brief Find an item with a hint + * + * @param hint A hint on where the needle _might_ be + * @param predicate that will used to find the item + * @return the item and its index or none if it's not found + */ + std::optional> find(size_t hint, auto &&predicate) + { + std::unique_lock lock(this->mutex_); + + if (hint < this->buffer_.size() && predicate(this->buffer_[hint])) + { + return std::pair{hint, this->buffer_[hint]}; + }; + + for (size_t i = 0; i < this->buffer_.size(); i++) + { + if (predicate(this->buffer_[i])) + { + return std::pair{i, this->buffer_[i]}; + } + } + return std::nullopt; + } + /** * @brief Returns the first item matching a predicate, checking in reverse * diff --git a/src/widgets/helper/ChannelView.cpp b/src/widgets/helper/ChannelView.cpp index dc0a98cbd..a5fa62582 100644 --- a/src/widgets/helper/ChannelView.cpp +++ b/src/widgets/helper/ChannelView.cpp @@ -967,10 +967,10 @@ void ChannelView::setChannel(const ChannelPtr &underlyingChannel) this->channelConnections_.managedConnect( underlyingChannel->messageReplaced, - [this](auto index, const auto &replacement) { + [this](auto index, const auto &prev, const auto &replacement) { if (this->shouldIncludeMessage(replacement)) { - this->channel_->replaceMessage(index, replacement); + this->channel_->replaceMessage(index, prev, replacement); } }); @@ -1051,8 +1051,9 @@ void ChannelView::setChannel(const ChannelPtr &underlyingChannel) // on message replaced this->channelConnections_.managedConnect( this->channel_->messageReplaced, - [this](size_t index, MessagePtr replacement) { - this->messageReplaced(index, replacement); + [this](size_t index, const MessagePtr &prev, + const MessagePtr &replacement) { + this->messageReplaced(index, prev, replacement); }); // on messages filled in @@ -1258,19 +1259,21 @@ void ChannelView::messageAddedAtStart(std::vector &messages) this->queueLayout(); } -void ChannelView::messageReplaced(size_t index, MessagePtr &replacement) +void ChannelView::messageReplaced(size_t hint, const MessagePtr &prev, + const MessagePtr &replacement) { - auto oMessage = this->messages_.get(index); - if (!oMessage) + auto optItem = this->messages_.find(hint, [&](const auto &it) { + return it->getMessagePtr() == prev; + }); + if (!optItem) { return; } - - auto message = *oMessage; + const auto &[index, oldItem] = *optItem; auto newItem = std::make_shared(replacement); - if (message->flags.has(MessageLayoutFlag::AlternateBackground)) + if (oldItem->flags.has(MessageLayoutFlag::AlternateBackground)) { newItem->flags.set(MessageLayoutFlag::AlternateBackground); } @@ -1278,7 +1281,7 @@ void ChannelView::messageReplaced(size_t index, MessagePtr &replacement) this->scrollBar_->replaceHighlight(index, replacement->getScrollBarHighlight()); - this->messages_.replaceItem(message, newItem); + this->messages_.replaceItem(index, newItem); this->queueLayout(); } diff --git a/src/widgets/helper/ChannelView.hpp b/src/widgets/helper/ChannelView.hpp index 704210712..40b40444e 100644 --- a/src/widgets/helper/ChannelView.hpp +++ b/src/widgets/helper/ChannelView.hpp @@ -268,7 +268,8 @@ private: std::optional overridingFlags); void messageAddedAtStart(std::vector &messages); void messageRemoveFromStart(MessagePtr &message); - void messageReplaced(size_t index, MessagePtr &replacement); + void messageReplaced(size_t hint, const MessagePtr &prev, + const MessagePtr &replacement); void messagesUpdated(); void performLayout(bool causedByScrollbar = false, diff --git a/tests/src/LimitedQueue.cpp b/tests/src/LimitedQueue.cpp index 0a94ea928..803969789 100644 --- a/tests/src/LimitedQueue.cpp +++ b/tests/src/LimitedQueue.cpp @@ -114,20 +114,153 @@ TEST(LimitedQueue, PushFront) TEST(LimitedQueue, ReplaceItem) { - LimitedQueue queue(5); + LimitedQueue queue(10); queue.pushBack(1); queue.pushBack(2); queue.pushBack(3); + queue.pushBack(4); + queue.pushBack(5); + queue.pushBack(6); int idex = queue.replaceItem(2, 10); EXPECT_EQ(idex, 1); - idex = queue.replaceItem(5, 11); + idex = queue.replaceItem(7, 11); EXPECT_EQ(idex, -1); - bool res = queue.replaceItem(std::size_t(0), 9); + int prev = -1; + bool res = queue.replaceItem(std::size_t(0), 9, &prev); EXPECT_TRUE(res); - res = queue.replaceItem(std::size_t(5), 4); + EXPECT_EQ(prev, 1); + res = queue.replaceItem(std::size_t(6), 4); EXPECT_FALSE(res); - SNAPSHOT_EQUALS(queue.getSnapshot(), {9, 10, 3}, "first snapshot"); + // correct hint + EXPECT_EQ(queue.replaceItem(3, 4, 11), 3); + // incorrect hints + EXPECT_EQ(queue.replaceItem(5, 11, 12), 3); + EXPECT_EQ(queue.replaceItem(0, 12, 13), 3); + // oob hint + EXPECT_EQ(queue.replaceItem(42, 13, 14), 3); + // bad needle + EXPECT_EQ(queue.replaceItem(0, 15, 16), -1); + + SNAPSHOT_EQUALS(queue.getSnapshot(), {9, 10, 3, 14, 5, 6}, + "first snapshot"); +} + +TEST(LimitedQueue, Find) +{ + LimitedQueue queue(10); + queue.pushBack(1); + queue.pushBack(2); + queue.pushBack(3); + queue.pushBack(4); + queue.pushBack(5); + queue.pushBack(6); + + // without hint + EXPECT_FALSE(queue + .find([](int i) { + return i == 0; + }) + .has_value()); + EXPECT_EQ(queue + .find([](int i) { + return i == 1; + }) + .value(), + 1); + EXPECT_EQ(queue + .find([](int i) { + return i == 2; + }) + .value(), + 2); + EXPECT_EQ(queue + .find([](int i) { + return i == 6; + }) + .value(), + 6); + EXPECT_FALSE(queue + .find([](int i) { + return i == 7; + }) + .has_value()); + EXPECT_FALSE(queue + .find([](int i) { + return i > 6; + }) + .has_value()); + EXPECT_FALSE(queue + .find([](int i) { + return i <= 0; + }) + .has_value()); + + using Pair = std::pair; + // with hint + EXPECT_FALSE(queue + .find(0, + [](int i) { + return i == 0; + }) + .has_value()); + // correct hint + EXPECT_EQ(queue + .find(0, + [](int i) { + return i == 1; + }) + .value(), + (Pair{0, 1})); + EXPECT_EQ(queue + .find(1, + [](int i) { + return i == 2; + }) + .value(), + (Pair{1, 2})); + // incorrect hint + EXPECT_EQ(queue + .find(1, + [](int i) { + return i == 1; + }) + .value(), + (Pair{0, 1})); + EXPECT_EQ(queue + .find(5, + [](int i) { + return i == 6; + }) + .value(), + (Pair{5, 6})); + // oob hint + EXPECT_EQ(queue + .find(6, + [](int i) { + return i == 3; + }) + .value(), + (Pair{2, 3})); + // non-existent items + EXPECT_FALSE(queue + .find(42, + [](int i) { + return i == 7; + }) + .has_value()); + EXPECT_FALSE(queue + .find(0, + [](int i) { + return i > 6; + }) + .has_value()); + EXPECT_FALSE(queue + .find(0, + [](int i) { + return i <= 0; + }) + .has_value()); }