changeset 494:028b8e0c6139

Enabled formatting of selected text only. git-svn-id: svn://svn.code.sf.net/p/universalindent/code/trunk@734 59b1889a-e5ac-428c-b0c7-476e01d41282
author thomas_-_s <thomas_-_s@59b1889a-e5ac-428c-b0c7-476e01d41282>
date Mon, 16 Jun 2008 09:56:08 +0000
parents 1a61b405be21
children 2f6b8f923697
files src/UniversalIndentGUI_NPP/UniversalIndentGUI_NPP.cpp
diffstat 1 files changed, 38 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/src/UniversalIndentGUI_NPP/UniversalIndentGUI_NPP.cpp	Mon Jun 16 08:56:27 2008 +0000
+++ b/src/UniversalIndentGUI_NPP/UniversalIndentGUI_NPP.cpp	Mon Jun 16 09:56:08 2008 +0000
@@ -247,26 +247,54 @@
 
 void indentText() {
     int currentEdit = 0;
+    int textLength = 0;
     char *fullEditorText = NULL;
 
     // Get scintilla text edit window handle
     //::SendMessage(nppData._nppHandle, WM_GETCURRENTSCINTILLA, 0, (LPARAM)&currentEdit);
 
-    int textLength = ::SendMessage(getCurrentHScintilla(currentEdit), SCI_GETTEXTLENGTH, 0, 0);
-    // Because the returned length is the index of the last character, increment the length.
-    textLength++;
+    // Get the length of the selected text plus a 0 byte ending.
+    textLength = ::SendMessage(getCurrentHScintilla(currentEdit), SCI_GETSELTEXT, 0, 0);
+
+    // No text has been selected to format the whole text.
+    if ( textLength - 1 == 0 ) {
 
-    fullEditorText = new char[textLength];
+        textLength = ::SendMessage(getCurrentHScintilla(currentEdit), SCI_GETTEXTLENGTH, 0, 0);
+        // Because we need space for a trailing 0 byte increment the length.
+        textLength++;
+
+        fullEditorText = new char[textLength];
+
+        // Get whole text
+        ::SendMessage(getCurrentHScintilla(currentEdit), SCI_GETTEXT, textLength, (LPARAM)fullEditorText);
+
+        QString indentedText = indentHandler->callIndenter(fullEditorText, "cpp");
+
+        QByteArray indentedTextByteArray = indentedText.toAscii();
 
-    // Get whole text
-    ::SendMessage(getCurrentHScintilla(currentEdit), SCI_GETTEXT, textLength, (LPARAM)fullEditorText);
+        // Set whole text
+        ::SendMessage(getCurrentHScintilla(currentEdit), SCI_SETTEXT, 0, (LPARAM)indentedTextByteArray.constData());
+    }
+    // Format only the selected text.
+    else {
+        fullEditorText = new char[textLength];
 
-    QString indentedText = indentHandler->callIndenter(fullEditorText, "cpp");
+        // Get selection start.
+        int selectionStartPos = ::SendMessage(getCurrentHScintilla(currentEdit), SCI_GETSELECTIONSTART, 0, 0);
 
-    QByteArray indentedTextByteArray = indentedText.toAscii();
+        // Get the selected text.
+        ::SendMessage(getCurrentHScintilla(currentEdit), SCI_GETSELTEXT, 0, (LPARAM)fullEditorText);
+
+        QString indentedText = indentHandler->callIndenter(fullEditorText, "cpp");
+
+        QByteArray indentedTextByteArray = indentedText.toAscii();
 
-    // Set whole text
-    ::SendMessage(getCurrentHScintilla(currentEdit), SCI_SETTEXT, 0, (LPARAM)indentedTextByteArray.constData());
+        // Replace selected text.
+        ::SendMessage(getCurrentHScintilla(currentEdit), SCI_REPLACESEL, 0, (LPARAM)indentedTextByteArray.constData());
+
+        // Set selection again.
+        ::SendMessage(getCurrentHScintilla(currentEdit), SCI_SETSEL, selectionStartPos, selectionStartPos + indentedTextByteArray.length() );
+    }
 
     delete fullEditorText;
     fullEditorText = NULL;