From 02b5426b8e223baf6815de936bccc99d44d74e3c Mon Sep 17 00:00:00 2001
From: Viktor Kireev <vitech-job@yandex.ru>
Date: Tue, 3 Oct 2017 19:17:02 +0300
Subject: [PATCH] Beautifier: Fix text selection for the Uncrustify

Extension of the selection of the text to full lines
does not work correctly if the selection is performed
from the top to the bottom.

Change-Id: I13a1bf9d6e92ebbbfd4f8190fb626dd3aca74686
Reviewed-by: David Schulz <david.schulz@qt.io>
---
 src/plugins/beautifier/uncrustify/uncrustify.cpp | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/src/plugins/beautifier/uncrustify/uncrustify.cpp b/src/plugins/beautifier/uncrustify/uncrustify.cpp
index b47672bc5e0..0f00d5fc698 100644
--- a/src/plugins/beautifier/uncrustify/uncrustify.cpp
+++ b/src/plugins/beautifier/uncrustify/uncrustify.cpp
@@ -138,10 +138,13 @@ void Uncrustify::formatSelectedText()
     if (tc.hasSelection()) {
         // Extend selection to full lines
         const int posSelectionEnd = tc.selectionEnd();
+        tc.setPosition(tc.selectionStart());
         tc.movePosition(QTextCursor::StartOfLine);
         const int startPos = tc.position();
         tc.setPosition(posSelectionEnd);
-        tc.movePosition(QTextCursor::EndOfLine);
+        // Don't extend the selection if the cursor is at the start of the line
+        if (tc.positionInBlock() > 0)
+            tc.movePosition(QTextCursor::EndOfLine);
         const int endPos = tc.position();
         m_beautifierPlugin->formatCurrentFile(command(cfgFileName, true), startPos, endPos);
     } else if (m_settings->formatEntireFileFallback()) {
-- 
GitLab