diff options
author | Dmitrii Akshintsev <dmitrii.akshintsev@qt.io> | 2024-01-10 00:08:18 +0100 |
---|---|---|
committer | Dmitrii Akshintsev <dmitrii.akshintsev@qt.io> | 2024-01-24 09:34:47 +0100 |
commit | 0574af7bb626bfae00a8e56fbb35dd88232869eb (patch) | |
tree | 224545408607f2274bc6c2820a6d2d529b30cb3f | |
parent | 8ddc623dd2c815bff4c8b7253566134c408f0407 (diff) |
DOM refactoring. DomUniverse::parse. Change signature & return type
This commit does the following:
1. Extracts callback call to the callsite of the parse function, where
the "failed" path calls the callback too. It allows to get rid of the
callback argument in the parse function, but requires it to return a
pair of DomItems (arguments for the callback) ->
2. Changes return type of the parse function
This change is helpful because it makes it easier to implement "early
exits" in the parse function and get rid of the god boolean
"skipParse" in the following commits, which will result in better
readability and more straightforward code flow.
Task-number: QTBUG-119550
Change-Id: I0bb9db01561b6eebb95059910bba6fbba80047f4
Reviewed-by: Ulf Hermann <ulf.hermann@qt.io>
-rw-r--r-- | src/qmldom/qqmldomtop.cpp | 47 | ||||
-rw-r--r-- | src/qmldom/qqmldomtop_p.h | 12 |
2 files changed, 34 insertions, 25 deletions
diff --git a/src/qmldom/qqmldomtop.cpp b/src/qmldom/qqmldomtop.cpp index 600d17728f..7a669f1ca3 100644 --- a/src/qmldom/qqmldomtop.cpp +++ b/src/qmldom/qqmldomtop.cpp @@ -235,16 +235,33 @@ void DomUniverse::loadFile(const FileToLoad &file, Callback callback, LoadOption std::optional<DomType> fileType) { DomItem selfItem(shared_from_this()); - DomType fType = - (bool(fileType) ? (*fileType) : fileTypeForPath(selfItem, file.canonicalPath())); + const auto &canonicalPath = file.canonicalPath(); + DomType fType = (bool(fileType) ? (*fileType) : fileTypeForPath(selfItem, canonicalPath)); switch (fType) { case DomType::QmlFile: case DomType::QmltypesFile: case DomType::QmldirFile: case DomType::QmlDirectory: case DomType::JsFile: { - return parse(file, fType, callback); - break; + const auto &valueChange = parse(file, fType); + // execute callback + if (callback) { + Path p; + if (fType == DomType::QmlFile) + p = Paths::qmlFileInfoPath(canonicalPath); + else if (fType == DomType::QmltypesFile) + p = Paths::qmltypesFileInfoPath(canonicalPath); + else if (fType == DomType::QmldirFile) + p = Paths::qmldirFileInfoPath(canonicalPath); + else if (fType == DomType::QmlDirectory) + p = Paths::qmlDirectoryInfoPath(canonicalPath); + else if (fType == DomType::JsFile) + p = Paths::jsFileInfoPath(canonicalPath); + else + Q_ASSERT(false); + callback(p, valueChange.formerItem, valueChange.currentItem); + } + return; } default: selfItem.addError( @@ -301,7 +318,7 @@ updateEntry(const DomItem &univ, const std::shared_ptr<T> &newItem, return qMakePair(oldValue, newValue); } -void DomUniverse::parse(const FileToLoad &file, DomType fType, Callback callback) +DomUniverse::ValueChange DomUniverse::parse(const FileToLoad &file, DomType fType) { QString canonicalPath = file.canonicalPath(); QString code = file.content() ? file.content()->data : QString(); @@ -398,25 +415,7 @@ void DomUniverse::parse(const FileToLoad &file, DomType fType, Callback callback Q_ASSERT(false); } } - - // to do: tell observers? - // execute callback - if (callback) { - Path p; - if (fType == DomType::QmlFile) - p = Paths::qmlFileInfoPath(canonicalPath); - else if (fType == DomType::QmltypesFile) - p = Paths::qmltypesFileInfoPath(canonicalPath); - else if (fType == DomType::QmldirFile) - p = Paths::qmldirFileInfoPath(canonicalPath); - else if (fType == DomType::QmlDirectory) - p = Paths::qmlDirectoryInfoPath(canonicalPath); - else if (fType == DomType::JsFile) - p = Paths::jsFileInfoPath(canonicalPath); - else - Q_ASSERT(false); - callback(p, oldValue, newValue); - } + return { oldValue, newValue }; } void DomUniverse::removePath(const QString &path) diff --git a/src/qmldom/qqmldomtop_p.h b/src/qmldom/qqmldomtop_p.h index d988b56466..7a169a4b22 100644 --- a/src/qmldom/qqmldomtop_p.h +++ b/src/qmldom/qqmldomtop_p.h @@ -199,7 +199,6 @@ public: void loadFile(const FileToLoad &file, Callback callback, LoadOptions loadOptions, std::optional<DomType> fileType = std::optional<DomType>()); - void parse(const FileToLoad &file, DomType fType, Callback callback); void removePath(const QString &dir); @@ -322,6 +321,17 @@ private: using ReadResult = std::variant<ContentWithDate, ErrorMessage>; ReadResult readFileContent(const QString &canonicalPath) const; + // Helper structure reflecting the change in the map after parsing completed + // formerItem - DomItem representing value existing in the map before the parsing. + // Might be empty (if didn't exist / failure) or equal to currentItem + // currentItem - DomItem representing current map value + struct ValueChange + { + DomItem formerItem; + DomItem currentItem; + }; + ValueChange parse(const FileToLoad &file, DomType fType); + std::shared_ptr<QmlFile> parseQmlFile(const QString &code, const FileToLoad &file, const QDateTime &contentDate); std::shared_ptr<JsFile> parseJsFile(const QString &code, const FileToLoad &file, |