Skip to content

Commit

Permalink
refactor: Move relevant files to a utils library
Browse files Browse the repository at this point in the history
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 <build@kdab.com>
Reviewed-by: Nicolas Arnaud-Cormos <nicolas@kdab.com>
  • Loading branch information
mgiroday committed May 14, 2024
1 parent 65b82d3 commit 4700264
Show file tree
Hide file tree
Showing 27 changed files with 112 additions and 127 deletions.
3 changes: 1 addition & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -127,7 +127,6 @@ add_subdirectory(src)
add_subdirectory(scripts)
add_subdirectory(tools)
add_subdirectory(tests)
add_subdirectory(utils)

file(
GLOB
Expand Down
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ add_subdirectory(treesitter)
add_subdirectory(rccore)
add_subdirectory(rcui)
add_subdirectory(gui)
add_subdirectory(utils)
6 changes: 2 additions & 4 deletions src/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ set(PROJECT_SOURCES
file.cpp
fileinfo.h
fileinfo.cpp
json_utils.h
imagedocument.h
imagedocument.cpp
knutcore.h
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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}/..)
2 changes: 1 addition & 1 deletion src/core/cppdocument_p.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

#include "json_utils.h"
#include "utils/json.h"

#include <map>
#include <optional>
Expand Down
2 changes: 1 addition & 1 deletion src/core/document.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

#include "json_utils.h"
#include "../utils/json.h"

#include <QDateTime>
#include <QFileSystemWatcher>
Expand Down
77 changes: 0 additions & 77 deletions src/core/json_utils.h

This file was deleted.

2 changes: 0 additions & 2 deletions src/core/logger.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#pragma once

#include "string_utils.h"

#include <QAbstractItemModel>
#include <QCoreApplication>
#include <QMetaEnum>
Expand Down
8 changes: 4 additions & 4 deletions src/core/lspdocument.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <QFile>
#include <QJSEngine>
Expand Down Expand Up @@ -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()));
}
Expand Down Expand Up @@ -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*
Expand Down
2 changes: 1 addition & 1 deletion src/core/project_p.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

#include "document.h"
#include "json_utils.h"
#include "utils/json.h"

namespace Core {

Expand Down
6 changes: 3 additions & 3 deletions src/core/textdocument.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <QFile>
#include <QKeyEvent>
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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());
Expand Down
2 changes: 1 addition & 1 deletion src/core/textdocument_p.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

#include "json_utils.h"
#include "utils/json.h"

class QPlainTextEdit;

Expand Down
2 changes: 1 addition & 1 deletion src/core/utils.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

#include "string_utils.h"
#include "utils/strings.h"

#include <QHash>
#include <QObject>
Expand Down
3 changes: 1 addition & 2 deletions src/lsp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ set(PROJECT_SOURCES
client.cpp
clientbackend.h
clientbackend.cpp
json.h
lsp_utils.h
lsp_utils.cpp
notificationmessage.h
Expand All @@ -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}/..)
2 changes: 1 addition & 1 deletion src/lsp/notificationmessage_json.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

#include "json.h"
#include "notificationmessage.h"
#include "utils/json.h"

#include <type_traits>

Expand Down
2 changes: 1 addition & 1 deletion src/lsp/requestmessage_json.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

#include "json.h"
#include "requestmessage.h"
#include "utils/json.h"

#include <type_traits>

Expand Down
2 changes: 1 addition & 1 deletion src/lsp/types_json.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

#pragma once

#include "json.h"
#include "types.h"
#include "utils/json.h"

namespace Lsp {

Expand Down
15 changes: 15 additions & 0 deletions src/utils/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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}/..)
69 changes: 68 additions & 1 deletion src/lsp/json.h → src/utils/json.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once

#include <QFlags>
#include <QMetaEnum>
#include <QString>
#include <QStringList>

Expand All @@ -8,8 +10,73 @@
#include <optional>
#include <variant>

///////////////////////////////////////////////////////////////////////////////
// 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<std::string>());
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<QString> 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<std::vector<QString>>();
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 <typename T>
struct adl_serializer<QFlags<T>>
{
static void to_json(nlohmann::json &j, const QFlags<T> &data)
{
std::vector<T> flags;
auto metaEnum = QMetaEnum::fromType<T>();
for (int i = 0; i < metaEnum.keyCount(); ++i) {
if (data & metaEnum.value(i))
flags.push_back(static_cast<T>(metaEnum.value(i)));
}
j = flags;
}

static void from_json(const nlohmann::json &j, QFlags<T> &data)
{
if (j.is_array()) {
auto list = j.get<std::vector<T>>();
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
///////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -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); \
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit 4700264

Please sign in to comment.