forked from qt-creator/qt-creator
QmlDesigner: Add common path to SourcePathView
Change-Id: I685a35a70ad597ac5b0a57f39dcc5037a7489d1a Reviewed-by: Thomas Hartmann <thomas.hartmann@qt.io>
This commit is contained in:
@@ -6,6 +6,7 @@
|
||||
#include <utils/smallstringview.h>
|
||||
|
||||
#include <algorithm>
|
||||
#include <ranges>
|
||||
#include <vector>
|
||||
|
||||
namespace QmlDesigner {
|
||||
@@ -60,13 +61,28 @@ public:
|
||||
std::size_t(std::ptrdiff_t(size()) - m_slashIndex - std::ptrdiff_t(1)));
|
||||
}
|
||||
|
||||
static
|
||||
std::ptrdiff_t lastSlashIndex(Utils::SmallStringView filePath)
|
||||
static constexpr std::tuple<std::string_view, std::string_view, std::string_view> commonPath(
|
||||
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 distance = std::distance(filePath.begin(), found);
|
||||
auto distance = std::ranges::distance(filePath.begin(), found);
|
||||
|
||||
return distance - 1;
|
||||
}
|
||||
|
@@ -98,4 +98,62 @@ TEST(SourcePathView, file_name_for_file_name_only)
|
||||
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"));
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user