Skip to content

Commit

Permalink
[SUTK] Return new (advanced) caret position after insertion
Browse files Browse the repository at this point in the history
  • Loading branch information
ravi688 committed Aug 19, 2024
1 parent 6b6cc52 commit 36ae75a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
2 changes: 1 addition & 1 deletion sutk/include/sutk/Text.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ namespace SUTK
void append(const std::string& str) noexcept { insert(CursorPosition<LineCountType>::EndOfText(), str); }
LineText* getLine(LineCountType line) noexcept;
const LineText* getLine(LineCountType line) const noexcept { return const_cast<Text*>(this)->getLine(line); }
void insert(CursorPosition<LineCountType> position, const std::string& str) noexcept;
CursorPosition<LineCountType> insert(CursorPosition<LineCountType> position, const std::string& str) noexcept;
void removeRange(CursorPosition<LineCountType> start, CursorPosition<LineCountType> end) noexcept;
void set(const std::string& str) noexcept;
void enableClipping(bool isEnable = true) noexcept;
Expand Down
25 changes: 23 additions & 2 deletions sutk/source/Text.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -405,11 +405,14 @@ namespace SUTK
return m_lines[line];
}

void Text::insert(CursorPosition<LineCountType> position, const std::string& str) noexcept
CursorPosition<LineCountType> Text::insert(CursorPosition<LineCountType> position, const std::string& str) noexcept
{
// if there is nothing to write then return
if(str.empty())
return;
{
if(position > end())
return end();
}

LineText* lineText = NULL;
if(position >= end())
Expand Down Expand Up @@ -474,11 +477,29 @@ namespace SUTK
index = end;
}

LineCountType column = 0;
if(newLineText == NULL)
{
if(position.getColumn() == END_OF_LINE)
column = lineText->getColumnCount();
else
{
if(index == std::string::npos)
column = position.getColumn() + str.size();
else
column = position.getColumn() + index + 2;
}
}
else
column = newLineText->getColumnCount();

if((newLineText != NULL) && (position.getColumn() != END_OF_LINE))
{
newLineText->append(lineText->substr(position.getColumn() + saveIndex, std::string::npos));
lineText->removeRange(position.getColumn() + saveIndex);
}

return { static_cast<LineCountType>(line), column };
}

void Text::removeRange(CursorPosition<LineCountType> start, CursorPosition<LineCountType> end) noexcept
Expand Down

0 comments on commit 36ae75a

Please sign in to comment.