Add distance functions to TextPosition and TextRange.

This commit is contained in:
Martín Lucas Golini
2025-02-09 01:01:13 -03:00
parent 0da3ffca17
commit 4e7c9f74c1
3 changed files with 26 additions and 0 deletions

View File

@@ -81,6 +81,10 @@ class EE_API TextPosition {
return {};
}
Int64 distance( const TextPosition& other ) const {
return std::abs( mLine - other.mLine ) + std::abs( mColumn - other.mColumn );
}
private:
Int64 mLine{ 0xffffffff };
Int64 mColumn{ 0xffffffff };

View File

@@ -120,6 +120,8 @@ class EE_API TextRange {
static TextRange convertToLineColumn( const std::string_view& text, Int64 startOffset,
Int64 endOffset );
Int64 minimumDistance(const TextRange& other) const;
private:
TextPosition mStart;
TextPosition mEnd;

View File

@@ -1,5 +1,6 @@
#include <eepp/core/debug.hpp>
#include <eepp/ui/doc/textrange.hpp>
#include <limits>
namespace EE { namespace UI { namespace Doc {
@@ -163,6 +164,25 @@ TextRange TextRange::convertToLineColumn( const std::string_view& text, Int64 st
return convertToLineColumn<std::string_view>( text, startOffset, endOffset );
}
Int64 TextRange::minimumDistance( const TextRange& other ) const {
if ( intersects( other ) )
return 0;
auto minDist = std::numeric_limits<Int64>::max();
const TextPosition corners1[] = { mStart, mEnd };
const TextPosition corners2[] = { other.start(), other.end() };
for ( const auto& c1 : corners1 ) {
for ( const auto& c2 : corners2 ) {
auto dist = c1.distance( c2 );
minDist = std::min( minDist, dist );
}
}
return minDist;
}
TextRanges::TextRanges() {}
TextRanges::TextRanges( const std::vector<TextRange>& ranges ) : std::vector<TextRange>( ranges ) {}