# HG changeset patch # User thomas_-_s # Date 1213610168 0 # Node ID 028b8e0c61393a87559855f1ee24710138e03407 # Parent 1a61b405be21ddf3dd0641aa76ff7c58f51eb381 Enabled formatting of selected text only. git-svn-id: svn://svn.code.sf.net/p/universalindent/code/trunk@734 59b1889a-e5ac-428c-b0c7-476e01d41282 diff -r 1a61b405be21 -r 028b8e0c6139 src/UniversalIndentGUI_NPP/UniversalIndentGUI_NPP.cpp --- 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)¤tEdit); - 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;