diff --git a/include/eepp/ui/doc/textposition.hpp b/include/eepp/ui/doc/textposition.hpp index 94bccf785..bb93e4efa 100644 --- a/include/eepp/ui/doc/textposition.hpp +++ b/include/eepp/ui/doc/textposition.hpp @@ -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 }; diff --git a/include/eepp/ui/doc/textrange.hpp b/include/eepp/ui/doc/textrange.hpp index 4a03fbccd..eb6b2cd29 100644 --- a/include/eepp/ui/doc/textrange.hpp +++ b/include/eepp/ui/doc/textrange.hpp @@ -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; diff --git a/src/eepp/ui/doc/textrange.cpp b/src/eepp/ui/doc/textrange.cpp index 71565c199..486c678e7 100644 --- a/src/eepp/ui/doc/textrange.cpp +++ b/src/eepp/ui/doc/textrange.cpp @@ -1,5 +1,6 @@ #include #include +#include namespace EE { namespace UI { namespace Doc { @@ -163,6 +164,25 @@ TextRange TextRange::convertToLineColumn( const std::string_view& text, Int64 st return convertToLineColumn( text, startOffset, endOffset ); } +Int64 TextRange::minimumDistance( const TextRange& other ) const { + if ( intersects( other ) ) + return 0; + + auto minDist = std::numeric_limits::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& ranges ) : std::vector( ranges ) {}