Repository: microsoft/terminal (language: cpp)

Issue:
`repositionCursorWithMouse` doesn't work once the viewport starts scrolling
version 1.20.whatever. Probably in 1.19 too. 

`"experimental.repositionCursorWithMouse": true` in the json. 

enable shell integration. 

do a `dir` or two, then try to move the cursor with the mouse. 


Discussion / hints:

Fix patch:
diff --git a/src/cascadia/TerminalControl/ControlCore.cpp b/src/cascadia/TerminalControl/ControlCore.cpp
index 98c70398559..95c80e4e9ad 100644
--- a/src/cascadia/TerminalControl/ControlCore.cpp
+++ b/src/cascadia/TerminalControl/ControlCore.cpp
@@ -1891,7 +1891,9 @@ namespace winrt::Microsoft::Terminal::Control::implementation
             {
                 const auto& last{ marks.back() };
                 const auto [start, end] = last.GetExtent();
-                const auto lastNonSpace = _terminal->GetTextBuffer().GetLastNonSpaceCharacter();
+                const auto bufferSize = _terminal->GetTextBuffer().GetSize();
+                auto lastNonSpace = _terminal->GetTextBuffer().GetLastNonSpaceCharacter();
+                bufferSize.IncrementInBounds(lastNonSpace, true);
 
                 // If the user clicked off to the right side of the prompt, we
                 // want to send keystrokes to the last character in the prompt +1.
@@ -1904,17 +1906,21 @@ namespace winrt::Microsoft::Terminal::Control::implementation
                 // By only sending keypresses to the end of the command + 1, we
                 // should leave the cursor at the very end of the prompt,
                 // without adding any characters from a previous command.
-                auto clampedClick = terminalPosition;
-                if (terminalPosition > lastNonSpace)
+
+                // terminalPosition is viewport-relative.
+                const auto bufferPos = _terminal->GetViewport().Origin() + terminalPosition;
+                if (bufferPos.y > lastNonSpace.y)
                 {
-                    clampedClick = lastNonSpace + til::point{ 1, 0 };
-                    _terminal->GetTextBuffer().GetSize().Clamp(clampedClick);
+                    // Clicked under the prompt. Bail.
+                    return;
                 }
 
+                // Limit the click to 1 past the last character on the last line.
+                const auto clampedClick = std::min(bufferPos, lastNonSpace);
+
                 if (clampedClick >= end)
                 {
                     // Get the distance between the cursor and the click, in cells.
-                    const auto bufferSize = _terminal->GetTextBuffer().GetSize();
 
                     // First, make sure to iterate from the first point to the
                     // second. The user may have clicked _earlier_ in the
@@ -1944,7 +1950,12 @@ namespace winrt::Microsoft::Terminal::Control::implementation
                         append(_terminal->SendKeyEvent(key, 0, {}, false));
                     }
 
-                    _sendInputToConnection(buffer);
+                    {
+                        // Sending input requires that we're unlocked, because
+                        // writing the input pipe may block indefinitely.
+                        const auto suspension = _terminal->SuspendLock();
+                        _sendInputToConnection(buffer);
+                    }
                 }
             }
         }


Test patch: