QmlDesigner: Add common path to SourcePathView

Change-Id: I685a35a70ad597ac5b0a57f39dcc5037a7489d1a
Reviewed-by: Thomas Hartmann <thomas.hartmann@qt.io>
This commit is contained in:
Marco Bubke
2025-05-23 00:02:21 +02:00
parent 608b065378
commit ee011dc19c
2 changed files with 78 additions and 4 deletions

View File

@@ -6,6 +6,7 @@
#include <utils/smallstringview.h> #include <utils/smallstringview.h>
#include <algorithm> #include <algorithm>
#include <ranges>
#include <vector> #include <vector>
namespace QmlDesigner { namespace QmlDesigner {
@@ -60,13 +61,28 @@ public:
std::size_t(std::ptrdiff_t(size()) - m_slashIndex - std::ptrdiff_t(1))); std::size_t(std::ptrdiff_t(size()) - m_slashIndex - std::ptrdiff_t(1)));
} }
static static constexpr std::tuple<std::string_view, std::string_view, std::string_view> commonPath(
std::ptrdiff_t lastSlashIndex(Utils::SmallStringView filePath) std::string_view first, std::string_view second)
{ {
auto foundReverse = std::find(filePath.rbegin(), filePath.rend(), separator); auto [firstSplit, secondSplit] = std::ranges::mismatch(first, second);
firstSplit = std::ranges::find(std::make_reverse_iterator(firstSplit),
std::make_reverse_iterator(first.begin()),
separator)
.base();
secondSplit = std::ranges::next(second.begin(),
std::ranges::distance(first.begin(), firstSplit));
return {{first.begin(), firstSplit}, {firstSplit, first.end()}, {secondSplit, second.end()}};
}
static constexpr std::ptrdiff_t lastSlashIndex(Utils::SmallStringView filePath)
{
auto foundReverse = std::ranges::find(filePath.rbegin(), filePath.rend(), separator);
auto found = foundReverse.base(); auto found = foundReverse.base();
auto distance = std::distance(filePath.begin(), found); auto distance = std::ranges::distance(filePath.begin(), found);
return distance - 1; return distance - 1;
} }

View File

@@ -98,4 +98,62 @@ TEST(SourcePathView, file_name_for_file_name_only)
ASSERT_THAT(filePath.name(), "file.h"); ASSERT_THAT(filePath.name(), "file.h");
} }
TEST(SourcePathView, common_path_for_same_path)
{
std::string_view input{"/foo/"};
auto matched = QmlDesigner::SourcePathView::commonPath(input, input);
ASSERT_THAT(matched, FieldsAre("/foo/", IsEmpty(), IsEmpty()));
}
TEST(SourcePathView, common_path)
{
std::string_view input1{"/foo/bar"};
std::string_view input2{"/foo/hoo"};
auto matched = QmlDesigner::SourcePathView::commonPath(input1, input2);
ASSERT_THAT(matched, FieldsAre("/foo/", "bar", "hoo"));
}
TEST(SourcePathView, common_path_last_is_sub_path)
{
std::string_view input1{"/foo/bar"};
std::string_view input2{"/foo/"};
auto matched = QmlDesigner::SourcePathView::commonPath(input1, input2);
ASSERT_THAT(matched, FieldsAre("/foo/", "bar", IsEmpty()));
}
TEST(SourcePathView, common_path_first_is_sub_path)
{
std::string_view input1{"/foo/"};
std::string_view input2{"/foo/bar"};
auto matched = QmlDesigner::SourcePathView::commonPath(input1, input2);
ASSERT_THAT(matched, FieldsAre("/foo/", IsEmpty(), "bar"));
}
TEST(SourcePathView, common_path_last_is_file)
{
std::string_view input1{"/foo/bar"};
std::string_view input2{"/foo"};
auto matched = QmlDesigner::SourcePathView::commonPath(input1, input2);
ASSERT_THAT(matched, FieldsAre("/", "foo/bar", "foo"));
}
TEST(SourcePathView, common_path_first_is_file)
{
std::string_view input1{"/foo"};
std::string_view input2{"/foo/bar"};
auto matched = QmlDesigner::SourcePathView::commonPath(input1, input2);
ASSERT_THAT(matched, FieldsAre("/", "foo", "foo/bar"));
}
} }