From 47002644a0a36b2ca3de9a7af87ebcade6232de6 Mon Sep 17 00:00:00 2001 From: Michel Boyer de la Giroday Date: Thu, 18 Apr 2024 11:01:25 +0200 Subject: [PATCH] refactor: Move relevant files to a utils library Merge json_utils.h to json.h Rename string_utils.[cpp h] to strings.[cpp h] Move json.h strings[cpp h] qt_fmt_helpers.h qt_std_format.h qt_fmt_format.h to the utils library. Task-Id: KNUT-130 Change-Id: I08e34c1f7d38b6fe51d0b3f562cdaeff062589da Reviewed-on: https://codereview.kdab.com/c/knut/+/140830 Tested-by: Continuous Integration Reviewed-by: Nicolas Arnaud-Cormos --- CMakeLists.txt | 3 +- src/CMakeLists.txt | 1 + src/core/CMakeLists.txt | 6 +- src/core/cppdocument_p.h | 2 +- src/core/document.h | 2 +- src/core/json_utils.h | 77 ------------------- src/core/logger.h | 2 - src/core/lspdocument.cpp | 8 +- src/core/project_p.h | 2 +- src/core/textdocument.cpp | 6 +- src/core/textdocument_p.h | 2 +- src/core/utils.h | 2 +- src/lsp/CMakeLists.txt | 3 +- src/lsp/notificationmessage_json.h | 2 +- src/lsp/requestmessage_json.h | 2 +- src/lsp/types_json.h | 2 +- src/utils/CMakeLists.txt | 15 ++++ src/{lsp => utils}/json.h | 69 ++++++++++++++++- {utils => src/utils}/qt_fmt_format.h | 0 {utils => src/utils}/qt_fmt_helpers.h | 0 {utils => src/utils}/qt_std_format.h | 0 .../string_utils.cpp => utils/strings.cpp} | 14 ++-- src/{core/string_utils.h => utils/strings.h} | 2 +- tests/tst_jsonify.cpp | 3 +- tests/tst_qt_std_format.cpp | 2 +- tests/tst_stringutils.cpp | 2 +- utils/CMakeLists.txt | 10 --- 27 files changed, 112 insertions(+), 127 deletions(-) delete mode 100644 src/core/json_utils.h create mode 100644 src/utils/CMakeLists.txt rename src/{lsp => utils}/json.h (69%) rename {utils => src/utils}/qt_fmt_format.h (100%) rename {utils => src/utils}/qt_fmt_helpers.h (100%) rename {utils => src/utils}/qt_std_format.h (100%) rename src/{core/string_utils.cpp => utils/strings.cpp} (94%) rename src/{core/string_utils.h => utils/strings.h} (95%) delete mode 100644 utils/CMakeLists.txt diff --git a/CMakeLists.txt b/CMakeLists.txt index adeab0ab..4a480a47 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -82,7 +82,7 @@ add_subdirectory(test_data) # compile_commands.json # ############################################################################## -# Ensure we have the appropriate compile_commands.json files availabe. +# Ensure we have the appropriate compile_commands.json files available. set(CMAKE_EXPORT_COMPILE_COMMANDS ON) @@ -127,7 +127,6 @@ add_subdirectory(src) add_subdirectory(scripts) add_subdirectory(tools) add_subdirectory(tests) -add_subdirectory(utils) file( GLOB diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 58eb8ffa..c57471af 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -18,3 +18,4 @@ add_subdirectory(treesitter) add_subdirectory(rccore) add_subdirectory(rcui) add_subdirectory(gui) +add_subdirectory(utils) diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index 4b4427e5..826b2ad5 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -23,7 +23,6 @@ set(PROJECT_SOURCES file.cpp fileinfo.h fileinfo.cpp - json_utils.h imagedocument.h imagedocument.cpp knutcore.h @@ -71,8 +70,6 @@ set(PROJECT_SOURCES settings.cpp slintdocument.h slintdocument.cpp - string_utils.h - string_utils.cpp symbol.h symbol.cpp testutil.h @@ -113,6 +110,7 @@ target_link_libraries( Qt${QT_VERSION_MAJOR}::UiTools knut-lsp knut-treesitter - knut-rccore) + knut-rccore + knut-utils) target_include_directories(${PROJECT_NAME} INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/..) diff --git a/src/core/cppdocument_p.h b/src/core/cppdocument_p.h index 324f9580..8ed93c70 100644 --- a/src/core/cppdocument_p.h +++ b/src/core/cppdocument_p.h @@ -1,6 +1,6 @@ #pragma once -#include "json_utils.h" +#include "utils/json.h" #include #include diff --git a/src/core/document.h b/src/core/document.h index 1eb65734..b18500c1 100644 --- a/src/core/document.h +++ b/src/core/document.h @@ -1,6 +1,6 @@ #pragma once -#include "json_utils.h" +#include "../utils/json.h" #include #include diff --git a/src/core/json_utils.h b/src/core/json_utils.h deleted file mode 100644 index 44f79fee..00000000 --- a/src/core/json_utils.h +++ /dev/null @@ -1,77 +0,0 @@ -#pragma once - -#include -#include -#include -#include - -#include - -/////////////////////////////////////////////////////////////////////////////// -// QString -/////////////////////////////////////////////////////////////////////////////// -inline void to_json(nlohmann::json &j, const QString &str) -{ - j = nlohmann::json(str.toStdString()); -} - -inline void from_json(const nlohmann::json &j, QString &str) -{ - if (j.is_string()) - str = QString::fromStdString(j.get()); - else { - throw nlohmann::detail::type_error::create(302, "type must be a string, but is not", &j); - } -} - -/////////////////////////////////////////////////////////////////////////////// -// QStringList -/////////////////////////////////////////////////////////////////////////////// -inline void to_json(nlohmann::json &j, const QStringList &strList) -{ - std::vector list(strList.cbegin(), strList.cend()); - j = list; -} - -inline void from_json(const nlohmann::json &j, QStringList &strList) -{ - if (j.is_array()) { - auto list = j.get>(); - strList = QStringList(list.cbegin(), list.cend()); - } else { - throw nlohmann::detail::type_error::create(302, "type must be an array, but is not", &j); - } -} - -/////////////////////////////////////////////////////////////////////////////// -// QFlags -/////////////////////////////////////////////////////////////////////////////// -namespace nlohmann { - -template -struct adl_serializer> -{ - static void to_json(nlohmann::json &j, const QFlags &data) - { - std::vector flags; - auto metaEnum = QMetaEnum::fromType(); - for (int i = 0; i < metaEnum.keyCount(); ++i) { - if (data & metaEnum.value(i)) - flags.push_back(static_cast(metaEnum.value(i))); - } - j = flags; - } - - static void from_json(const nlohmann::json &j, QFlags &data) - { - if (j.is_array()) { - auto list = j.get>(); - for (auto v : list) - data.setFlag(v); - } else { - throw nlohmann::detail::type_error::create(302, "type must be an array, but is not", &j); - } - } -}; - -} diff --git a/src/core/logger.h b/src/core/logger.h index 8e1c50c8..8b0341db 100644 --- a/src/core/logger.h +++ b/src/core/logger.h @@ -1,7 +1,5 @@ #pragma once -#include "string_utils.h" - #include #include #include diff --git a/src/core/lspdocument.cpp b/src/core/lspdocument.cpp index df3b937f..03c03178 100644 --- a/src/core/lspdocument.cpp +++ b/src/core/lspdocument.cpp @@ -2,15 +2,15 @@ #include "lspdocument_p.h" #include "astnode.h" -#include "json_utils.h" #include "logger.h" #include "project.h" #include "querymatch.h" #include "rangemark.h" #include "scriptmanager.h" -#include "string_utils.h" #include "symbol.h" #include "textlocation.h" +#include "utils/json.h" +#include "utils/strings.h" #include #include @@ -307,7 +307,7 @@ Document *LspDocument::followSymbol() // Set the cursor position to the beginning of any selected text. // That way, calling followSymbol twice in a row causes Clangd - // to switch between delcaration and definition. + // to switch between declaration and definition. auto cursor = textEdit()->textCursor(); LOG_RETURN("document", followSymbol(cursor.selectionStart())); } @@ -686,7 +686,7 @@ void LspDocument::changeContentLsp(int position, int charsRemoved, int charsAdde if (client()->canSendDocumentChanges(Lsp::TextDocumentSyncKind::Full) || client()->canSendDocumentChanges(Lsp::TextDocumentSyncKind::Incremental)) { // TODO: We currently always send the entire document to the Language server, even - // if it suppports incremental changes. + // if it supports incremental changes. // Change this, so we also send incremental updates. // // This currently isn't implemented, as changeContent only gets called *after* diff --git a/src/core/project_p.h b/src/core/project_p.h index 401f2f70..286acfad 100644 --- a/src/core/project_p.h +++ b/src/core/project_p.h @@ -1,7 +1,7 @@ #pragma once #include "document.h" -#include "json_utils.h" +#include "utils/json.h" namespace Core { diff --git a/src/core/textdocument.cpp b/src/core/textdocument.cpp index 4eed29b1..311ee185 100644 --- a/src/core/textdocument.cpp +++ b/src/core/textdocument.cpp @@ -5,8 +5,8 @@ #include "mark.h" #include "rangemark.h" #include "settings.h" -#include "string_utils.h" #include "texteditor.h" +#include "utils/strings.h" #include #include @@ -1353,7 +1353,7 @@ bool TextDocument::replaceOne(const QString &before, const QString &after, int o const bool usesRegExp = options & FindRegexp; const bool preserveCase = options & PreserveCase; - auto regexp = createRegularExpression(before, options); + auto regexp = createRegularExpression(before, options, usesRegExp); if (find(before, options)) { cursor.beginEditBlock(); auto found = m_document->textCursor(); @@ -1444,7 +1444,7 @@ int TextDocument::replaceAll(const QString &before, const QString &after, int op m_document->setTextCursor(cursor); cursor.beginEditBlock(); - auto regexp = createRegularExpression(before, options); + auto regexp = createRegularExpression(before, options, usesRegExp); while (find(before, options)) { auto found = m_document->textCursor(); cursor.setPosition(found.selectionStart()); diff --git a/src/core/textdocument_p.h b/src/core/textdocument_p.h index af7084b3..b2a6f348 100644 --- a/src/core/textdocument_p.h +++ b/src/core/textdocument_p.h @@ -1,6 +1,6 @@ #pragma once -#include "json_utils.h" +#include "utils/json.h" class QPlainTextEdit; diff --git a/src/core/utils.h b/src/core/utils.h index 566da5e4..e0be5a41 100644 --- a/src/core/utils.h +++ b/src/core/utils.h @@ -1,6 +1,6 @@ #pragma once -#include "string_utils.h" +#include "utils/strings.h" #include #include diff --git a/src/lsp/CMakeLists.txt b/src/lsp/CMakeLists.txt index 43bfc278..31aab0ee 100644 --- a/src/lsp/CMakeLists.txt +++ b/src/lsp/CMakeLists.txt @@ -5,7 +5,6 @@ set(PROJECT_SOURCES client.cpp clientbackend.h clientbackend.cpp - json.h lsp_utils.h lsp_utils.cpp notificationmessage.h @@ -23,7 +22,7 @@ if(MSVC) endif() add_library(${PROJECT_NAME} STATIC ${PROJECT_SOURCES}) -target_link_libraries(${PROJECT_NAME} nlohmann_json::nlohmann_json +target_link_libraries(${PROJECT_NAME} knut-utils nlohmann_json::nlohmann_json spdlog::spdlog Qt${QT_VERSION_MAJOR}::Core) target_include_directories(${PROJECT_NAME} INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/..) diff --git a/src/lsp/notificationmessage_json.h b/src/lsp/notificationmessage_json.h index f3630aea..c1461937 100644 --- a/src/lsp/notificationmessage_json.h +++ b/src/lsp/notificationmessage_json.h @@ -1,7 +1,7 @@ #pragma once -#include "json.h" #include "notificationmessage.h" +#include "utils/json.h" #include diff --git a/src/lsp/requestmessage_json.h b/src/lsp/requestmessage_json.h index 56e114f8..acc87296 100644 --- a/src/lsp/requestmessage_json.h +++ b/src/lsp/requestmessage_json.h @@ -1,7 +1,7 @@ #pragma once -#include "json.h" #include "requestmessage.h" +#include "utils/json.h" #include diff --git a/src/lsp/types_json.h b/src/lsp/types_json.h index 8ba0b3ed..9acb7d73 100644 --- a/src/lsp/types_json.h +++ b/src/lsp/types_json.h @@ -3,8 +3,8 @@ #pragma once -#include "json.h" #include "types.h" +#include "utils/json.h" namespace Lsp { diff --git a/src/utils/CMakeLists.txt b/src/utils/CMakeLists.txt new file mode 100644 index 00000000..31cf3f87 --- /dev/null +++ b/src/utils/CMakeLists.txt @@ -0,0 +1,15 @@ +project(knut-utils LANGUAGES CXX) +find_package(Qt6 REQUIRED COMPONENTS Core Gui) +set(PROJECT_SOURCES json.h qt_fmt_helpers.h strings.h strings.cpp) +add_library(${PROJECT_NAME} STATIC ${PROJECT_SOURCES}) +if(KNUT_USE_STD_FORMAT) + target_sources(${PROJECT_NAME} PUBLIC qt_std_format.h) +else() + target_sources(${PROJECT_NAME} PUBLIC qt_fmt_format.h) +endif() + +target_link_libraries(${PROJECT_NAME} PUBLIC nlohmann_json::nlohmann_json + Qt6::Core Qt6::Gui) + +target_include_directories(${PROJECT_NAME} + INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/..) diff --git a/src/lsp/json.h b/src/utils/json.h similarity index 69% rename from src/lsp/json.h rename to src/utils/json.h index d23d95a7..8a5ec285 100644 --- a/src/lsp/json.h +++ b/src/utils/json.h @@ -1,5 +1,7 @@ #pragma once +#include +#include #include #include @@ -8,8 +10,73 @@ #include #include +/////////////////////////////////////////////////////////////////////////////// +// QString +/////////////////////////////////////////////////////////////////////////////// +inline void to_json(nlohmann::json &j, const QString &str) +{ + j = nlohmann::json(str.toStdString()); +} + +inline void from_json(const nlohmann::json &j, QString &str) +{ + if (j.is_string()) + str = QString::fromStdString(j.get()); + else { + throw nlohmann::detail::type_error::create(302, "type must be a string, but is not", &j); + } +} + +/////////////////////////////////////////////////////////////////////////////// +// QStringList +/////////////////////////////////////////////////////////////////////////////// +inline void to_json(nlohmann::json &j, const QStringList &strList) +{ + std::vector list(strList.cbegin(), strList.cend()); + j = list; +} + +inline void from_json(const nlohmann::json &j, QStringList &strList) +{ + if (j.is_array()) { + auto list = j.get>(); + strList = QStringList(list.cbegin(), list.cend()); + } else { + throw nlohmann::detail::type_error::create(302, "type must be an array, but is not", &j); + } +} + +/////////////////////////////////////////////////////////////////////////////// +// QFlags +/////////////////////////////////////////////////////////////////////////////// namespace nlohmann { +template +struct adl_serializer> +{ + static void to_json(nlohmann::json &j, const QFlags &data) + { + std::vector flags; + auto metaEnum = QMetaEnum::fromType(); + for (int i = 0; i < metaEnum.keyCount(); ++i) { + if (data & metaEnum.value(i)) + flags.push_back(static_cast(metaEnum.value(i))); + } + j = flags; + } + + static void from_json(const nlohmann::json &j, QFlags &data) + { + if (j.is_array()) { + auto list = j.get>(); + for (auto v : list) + data.setFlag(v); + } else { + throw nlohmann::detail::type_error::create(302, "type must be an array, but is not", &j); + } + } +}; + /////////////////////////////////////////////////////////////////////////////// // std::variant /////////////////////////////////////////////////////////////////////////////// @@ -131,7 +198,7 @@ void knut_from_json(const char *key, const nlohmann::json &j, T &value) #define JSONIFY_ENUM NLOHMANN_JSON_SERIALIZE_ENUM /** - * \brief Macro used to foward declare serialization/deserialization methods + * \brief Macro used to forward declare serialization/deserialization methods */ #define JSONIFY_FWD(Type) \ void to_json(nlohmann::json &nlohmann_json_j, const Type &nlohmann_json_t); \ diff --git a/utils/qt_fmt_format.h b/src/utils/qt_fmt_format.h similarity index 100% rename from utils/qt_fmt_format.h rename to src/utils/qt_fmt_format.h diff --git a/utils/qt_fmt_helpers.h b/src/utils/qt_fmt_helpers.h similarity index 100% rename from utils/qt_fmt_helpers.h rename to src/utils/qt_fmt_helpers.h diff --git a/utils/qt_std_format.h b/src/utils/qt_std_format.h similarity index 100% rename from utils/qt_std_format.h rename to src/utils/qt_std_format.h diff --git a/src/core/string_utils.cpp b/src/utils/strings.cpp similarity index 94% rename from src/core/string_utils.cpp rename to src/utils/strings.cpp index 8c77fdaa..22121bd5 100644 --- a/src/core/string_utils.cpp +++ b/src/utils/strings.cpp @@ -1,8 +1,7 @@ -#include "string_utils.h" - -#include "textdocument.h" +#include "strings.h" #include +#include namespace Core { @@ -204,12 +203,9 @@ QString expandRegExpReplacement(const QString &replaceText, const QStringList &c return result; } -QRegularExpression createRegularExpression(const QString &txt, int flags) +QRegularExpression createRegularExpression(const QString &txt, int flags, bool isRegExp) { - // Also add PreserveCase when FindCaseSensitively is used - flags |= (flags & TextDocument::FindCaseSensitively) ? TextDocument::PreserveCase : TextDocument::NoFindFlags; - - QRegularExpression::PatternOptions options = flags & TextDocument::PreserveCase + QRegularExpression::PatternOptions options = (flags & QTextDocument::FindCaseSensitively) ? QRegularExpression::NoPatternOption : QRegularExpression::CaseInsensitiveOption; @@ -217,7 +213,7 @@ QRegularExpression createRegularExpression(const QString &txt, int flags) if (txt.contains('\n')) options |= QRegularExpression::MultilineOption; - return QRegularExpression((flags & TextDocument::FindRegexp) ? txt : QRegularExpression::escape(txt), options); + return QRegularExpression(isRegExp ? txt : QRegularExpression::escape(txt), options); } } // namespace Migration diff --git a/src/core/string_utils.h b/src/utils/strings.h similarity index 95% rename from src/core/string_utils.h rename to src/utils/strings.h index 6a50cb06..5ab0245b 100644 --- a/src/core/string_utils.h +++ b/src/utils/strings.h @@ -22,6 +22,6 @@ QString expandRegExpReplacement(const QString &replaceText, const QStringList &c * @brief createRegularExpression * Create a regular expression based on options from TextDocument::FindFlags */ -QRegularExpression createRegularExpression(const QString &txt, int flags); +QRegularExpression createRegularExpression(const QString &txt, int flags, bool isRegExp = true); } // namespace Core diff --git a/tests/tst_jsonify.cpp b/tests/tst_jsonify.cpp index 6555bb62..2a15a0f0 100644 --- a/tests/tst_jsonify.cpp +++ b/tests/tst_jsonify.cpp @@ -1,5 +1,4 @@ -#include "core/json_utils.h" -#include "lsp/json.h" +#include "utils/json.h" #include #include diff --git a/tests/tst_qt_std_format.cpp b/tests/tst_qt_std_format.cpp index 315d0776..997d0297 100644 --- a/tests/tst_qt_std_format.cpp +++ b/tests/tst_qt_std_format.cpp @@ -6,7 +6,7 @@ SPDX-License-Identifier: MIT */ -#include "../utils/qt_std_format.h" +#include "utils/qt_std_format.h" #include #include diff --git a/tests/tst_stringutils.cpp b/tests/tst_stringutils.cpp index fded1665..b7104b8b 100644 --- a/tests/tst_stringutils.cpp +++ b/tests/tst_stringutils.cpp @@ -1,6 +1,6 @@ #include -#include "core/string_utils.h" +#include "utils/strings.h" using namespace Core; diff --git a/utils/CMakeLists.txt b/utils/CMakeLists.txt deleted file mode 100644 index ea949555..00000000 --- a/utils/CMakeLists.txt +++ /dev/null @@ -1,10 +0,0 @@ -project(Utils) -find_package(Qt6 REQUIRED COMPONENTS Core) -add_library(${PROJECT_NAME} STATIC qt_fmt_helpers.h) -if(KNUT_USE_STD_FORMAT) - target_sources(${PROJECT_NAME} PUBLIC qt_std_format.h) -else() - target_sources(${PROJECT_NAME} PUBLIC qt_fmt_format.h) -endif() - -target_link_libraries(${PROJECT_NAME} PRIVATE Qt6::Core)