Skip to content

Commit 67f1d91

Browse files
committed
Improve newline handling with triple-click
1 parent 66a6ef8 commit 67f1d91

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

textselect.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,10 @@ TextSelect::Selection TextSelect::getSelection() const {
115115
void TextSelect::handleMouseDown(const ImVec2& cursorPosStart) {
116116
const float textHeight = ImGui::GetTextLineHeightWithSpacing();
117117
ImVec2 mousePos = ImGui::GetMousePos() - cursorPosStart;
118+
std::size_t numLines = getNumLines();
118119

119120
// Get Y position of mouse cursor, in terms of line number (capped to the index of the last line)
120-
std::size_t y = std::min(static_cast<std::size_t>(std::floor(mousePos.y / textHeight)), getNumLines() - 1);
121+
std::size_t y = std::min(static_cast<std::size_t>(std::floor(mousePos.y / textHeight)), numLines - 1);
121122
if (y < 0) return;
122123

123124
std::string_view currentLine = getLineAtIdx(y);
@@ -127,8 +128,9 @@ void TextSelect::handleMouseDown(const ImVec2& cursorPosStart) {
127128
if (int mouseClicks = ImGui::GetMouseClickedCount(ImGuiMouseButton_Left); mouseClicks > 0) {
128129
if (mouseClicks % 3 == 0) {
129130
// Triple click - select line
131+
bool atLastLine = y == (numLines - 1);
130132
selectStart = { 0, y };
131-
selectEnd = { utf8Length(currentLine), y };
133+
selectEnd = { atLastLine ? utf8Length(currentLine) : 0, atLastLine ? y : y + 1 };
132134
} else if (mouseClicks % 2 == 0) {
133135
// Double click - select word
134136
// Initialize start and end iterators to current cursor position

0 commit comments

Comments
 (0)