Skip to content

Commit

Permalink
Add distance functions to TextPosition and TextRange.
Browse files Browse the repository at this point in the history
  • Loading branch information
SpartanJ committed Feb 9, 2025
1 parent 0da3ffc commit 4e7c9f7
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
4 changes: 4 additions & 0 deletions include/eepp/ui/doc/textposition.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
Expand Down
2 changes: 2 additions & 0 deletions include/eepp/ui/doc/textrange.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
20 changes: 20 additions & 0 deletions src/eepp/ui/doc/textrange.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <eepp/core/debug.hpp>
#include <eepp/ui/doc/textrange.hpp>
#include <limits>

namespace EE { namespace UI { namespace Doc {

Expand Down Expand Up @@ -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 ) {}
Expand Down

0 comments on commit 4e7c9f7

Please sign in to comment.