mirror of
https://github.com/SpartanJ/eepp.git
synced 2026-08-06 06:49:31 +03:00
Add distance functions to TextPosition and TextRange.
This commit is contained in:
@@ -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 };
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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 ) {}
|
||||
|
||||
Reference in New Issue
Block a user