From 537cbaaf4043f3d60dc08eb45a56134b8da7df30 Mon Sep 17 00:00:00 2001 From: Joachim Meyer Date: Wed, 10 Jun 2015 19:03:46 +0200 Subject: [PATCH 1/3] *Using more common hotkeys for jumping over words (ctrl + arrow key), indenting (multiple) lines ((shift +) tab). *Adding hotkey for comment / uncomment a section (ctrl + (shift +) k). *Adding "Auto-indentation" - it just adds as many tabs to the new line as the line before had. *Adding erase to String. *Adding count to String (you can count a char). --- Core/Contents/Include/PolyString.h | 12 + Core/Contents/Source/PolyString.cpp | 13 ++ Modules/Contents/UI/Include/PolyUITextInput.h | 2 + .../Contents/UI/Source/PolyUITextInput.cpp | 207 +++++++++++------- 4 files changed, 160 insertions(+), 74 deletions(-) diff --git a/Core/Contents/Include/PolyString.h b/Core/Contents/Include/PolyString.h index 11d8ba96b..5f1cc5952 100644 --- a/Core/Contents/Include/PolyString.h +++ b/Core/Contents/Include/PolyString.h @@ -133,7 +133,19 @@ namespace Polycode { size_t find_first_of(const String &str, size_t pos = 0) { return contents.find_first_of(str.contents, pos); } + + /** + * Erase an amount (len) of characters from pos. + * @param pos First character position to be removed. The default value indicates that the entire string is deleted from the beginning. + * @param len Amount of characters to be removed from pos. The default value indicates that the entire string beginning with pos is deleted. + * @return The cleaned String. + */ + String erase(size_t pos = 0, size_t len = std::wstring::npos){ + return contents.erase(pos, len); + } + size_t count(const char str, size_t first = 0, size_t last = 0); + inline String operator + (const char *str) const { return String(contents + String(str).contents); } inline String operator + (const String &str) const { return String(contents + str.contents); } String operator += (const String &str) { contents = contents + str.contents; return *this; } diff --git a/Core/Contents/Source/PolyString.cpp b/Core/Contents/Source/PolyString.cpp index e6bdd9567..4941df0c8 100644 --- a/Core/Contents/Source/PolyString.cpp +++ b/Core/Contents/Source/PolyString.cpp @@ -23,6 +23,7 @@ #include "PolyString.h" #include #include +#include using namespace Polycode; using namespace std; @@ -286,3 +287,15 @@ void wstrToUtf8(Str& dest, const WStr& src){ dest.push_back('?'); } } + +size_t String::count(const char str, size_t first, size_t last){ + size_t tmp = MAX(first, last); + first = MIN(first, last); + last = tmp; + + if (last <= 0 || (contents.begin() + last) > contents.end()){ + return std::count(contents.begin() + first, contents.end(), str); + } else { + return std::count(contents.begin() + first, contents.begin() + last, str); + } +} \ No newline at end of file diff --git a/Modules/Contents/UI/Include/PolyUITextInput.h b/Modules/Contents/UI/Include/PolyUITextInput.h index e072e5517..c807002af 100755 --- a/Modules/Contents/UI/Include/PolyUITextInput.h +++ b/Modules/Contents/UI/Include/PolyUITextInput.h @@ -362,6 +362,8 @@ namespace Polycode { void convertIndentToTabs(); void convertIndentToSpaces(); + void commentText(bool uncomment = false); + void doMultilineResize(); static void setMenuSingleton(UIGlobalMenu *_globalMenu); diff --git a/Modules/Contents/UI/Source/PolyUITextInput.cpp b/Modules/Contents/UI/Source/PolyUITextInput.cpp index ab368adff..2ecf059fc 100755 --- a/Modules/Contents/UI/Source/PolyUITextInput.cpp +++ b/Modules/Contents/UI/Source/PolyUITextInput.cpp @@ -735,73 +735,78 @@ void UITextInput::Resize(Number width, Number height) { int UITextInput::insertLine(String lineText) { numLines++; - - String newText = lineText; - if(lines.size() > 0 && !settingText) { - String ctext = lines[actualLineOffset].text; - String text2 = ctext.substr(actualCaretPosition, ctext.length()-actualCaretPosition); - ctext = ctext.substr(0,actualCaretPosition); - lines[actualLineOffset].text = ctext; - newText = newText+text2; - actualCaretPosition=0; - caretPosition = 0; - } - - vector::iterator it; - - lineOffset = lineOffset + 1; - actualLineOffset = actualLineOffset + 1; + + String newText = lineText; - if(actualLineOffset >= lines.size()) { - it = lines.end(); - } else { - it = lines.begin() + actualLineOffset; + if(lines.size() > 0 && !settingText) { + String ctext = lines[actualLineOffset].text; + int indent = ctext.count('\t'); + String text2 = ctext.substr(actualCaretPosition, ctext.length()-actualCaretPosition); + ctext = ctext.substr(0,actualCaretPosition); + lines[actualLineOffset].text = ctext; + for (int i = 0; i < indent; i++){ + newText = "\t" + newText; } - - SyntaxHighlightToken _overrideToken; - - if(actualLineOffset > 0) { - if(lines[actualLineOffset-1].blockOverrideToken.overrideType != SyntaxHighlightToken::TOKEN_TYPE_OVERRIDE_START && lines[actualLineOffset-1].blockOverrideToken.overrideType != SyntaxHighlightToken::TOKEN_TYPE_OVERRIDE_END) { - _overrideToken = lines[actualLineOffset-1].blockOverrideToken; - } else if(lines[actualLineOffset-1].blockOverrideToken.overrideType == SyntaxHighlightToken::TOKEN_TYPE_OVERRIDE_START) { - _overrideToken = lines[actualLineOffset-1].blockOverrideToken; - _overrideToken.overrideType = SyntaxHighlightToken::TOKEN_TYPE_OVERRIDE_LINE; - } + newText = newText+text2; + actualCaretPosition=indent; + caretPosition = indent; + } + + vector::iterator it; + + lineOffset = lineOffset + 1; + actualLineOffset = actualLineOffset + 1; + + if(actualLineOffset >= lines.size()) { + it = lines.end(); + } else { + it = lines.begin() + actualLineOffset; + } + + SyntaxHighlightToken _overrideToken; + + if(actualLineOffset > 0) { + if(lines[actualLineOffset-1].blockOverrideToken.overrideType != SyntaxHighlightToken::TOKEN_TYPE_OVERRIDE_START && lines[actualLineOffset-1].blockOverrideToken.overrideType != SyntaxHighlightToken::TOKEN_TYPE_OVERRIDE_END) { + _overrideToken = lines[actualLineOffset-1].blockOverrideToken; + } else if(lines[actualLineOffset-1].blockOverrideToken.overrideType == SyntaxHighlightToken::TOKEN_TYPE_OVERRIDE_START) { + _overrideToken = lines[actualLineOffset-1].blockOverrideToken; + _overrideToken.overrideType = SyntaxHighlightToken::TOKEN_TYPE_OVERRIDE_LINE; } - - LineInfo info; - info.text = newText; - info.blockOverrideToken = _overrideToken; - lines.insert(it,info); - - WordWrapLine line; - line.text = info.text; - line.isWordWrap = false; - line.blockOverrideToken = _overrideToken; - line.actualLineNumber = actualLineOffset+1; - lines[actualLineOffset].wordWrapLineIndex = lineOffset; - line.colorInfo = lines[actualLineOffset].colorInfo; - - vector::iterator wit; - wit = wordWrapLines.begin() + lineOffset; - wordWrapLines.insert(wit,line); + } + + LineInfo info; + info.text = newText; + info.blockOverrideToken = _overrideToken; + lines.insert(it,info); + + WordWrapLine line; + line.text = info.text; + line.isWordWrap = false; + line.blockOverrideToken = _overrideToken; + line.actualLineNumber = actualLineOffset+1; + lines[actualLineOffset].wordWrapLineIndex = lineOffset; + line.colorInfo = lines[actualLineOffset].colorInfo; + + vector::iterator wit; + wit = wordWrapLines.begin() + lineOffset; + wordWrapLines.insert(wit,line); - for(int i=actualLineOffset+1; i < lines.size(); i++) { - lines[i].wordWrapLineIndex += 1; - } - - for(int i=lineOffset+1; i < wordWrapLines.size(); i++) { - wordWrapLines[i].actualLineNumber += 1; - wordWrapLines[i].dirty = true; - } - - if(!settingText) { - + for(int i=actualLineOffset+1; i < lines.size(); i++) { + lines[i].wordWrapLineIndex += 1; + } + + for(int i=lineOffset+1; i < wordWrapLines.size(); i++) { + wordWrapLines[i].actualLineNumber += 1; + wordWrapLines[i].dirty = true; + } + + if(!settingText) { restructLines(); changedText(actualLineOffset-1, actualLineOffset); - } - return 1; + } + //setCaretPosition(indent); + return 1; } void UITextInput::enableLineNumbers(bool val) { @@ -1729,7 +1734,7 @@ void UITextInput::onKeyDown(PolyKEY key, wchar_t charCode) { setActualToCaret(); updateCaretPosition(); } - } else if (input->getKeyState(KEY_LALT) || input->getKeyState(KEY_RALT)) { + } else if (input->getKeyState(KEY_LCTRL) || input->getKeyState(KEY_RCTRL)) { if(input->getKeyState(KEY_LSHIFT) || input->getKeyState(KEY_RSHIFT)) { if(hasSelection) { setSelection(actualLineOffset, selectionLine, actualCaretPosition, caretSkipWordBack(selectionLine, selectionCaretPosition)); @@ -1798,7 +1803,7 @@ void UITextInput::onKeyDown(PolyKEY key, wchar_t charCode) { updateCaretPosition(); } } - } else if (input->getKeyState(KEY_LALT) || input->getKeyState(KEY_RALT)) { + } else if (input->getKeyState(KEY_LCTRL) || input->getKeyState(KEY_RCTRL)) { if(actualCaretPosition < lines[actualLineOffset].text.length()) { if(input->getKeyState(KEY_LSHIFT) || input->getKeyState(KEY_RSHIFT)) { if(hasSelection) { @@ -2023,11 +2028,13 @@ void UITextInput::onKeyDown(PolyKEY key, wchar_t charCode) { } // indent/shift text - if (multiLine && (key == KEY_LEFTBRACKET || key == KEY_RIGHTBRACKET) && - (input->getKeyState(KEY_LSUPER) || input->getKeyState(KEY_RSUPER) || - input->getKeyState(KEY_LCTRL) || input->getKeyState(KEY_RCTRL))) { - shiftText( (key == KEY_RIGHTBRACKET) ? false : true ); - return; + if (multiLine && key == KEY_TAB) { + shiftText(input->getKeyState(KEY_LSHIFT)); + return; + } + + if (multiLine && key == KEY_k && (input->getKeyState(KEY_LCTRL) || input->getKeyState(KEY_RCTRL))){ + commentText(input->getKeyState(KEY_LSHIFT) || input->getKeyState(KEY_RSHIFT)); } // at this point, return if certain modifier keys are held down so as not to potentially add any unwanted text @@ -2617,7 +2624,7 @@ void UITextInput::shiftText(bool left) { if (hasSelection) { for (int i = selectionTop; i <= selectionBottom; i++) { - if (i == selectionBottom && selectionCaretPosition <= 0) + if (i == selectionBottom && selectionR < 0) // at least one character of bottom line needs to be selected before indenting, so... break; if (indentType == INDENT_TAB) { @@ -2638,8 +2645,10 @@ void UITextInput::shiftText(bool left) { } } } - } - else { + changedText(selectionTop, selectionBottom); + updateCaretPosition(); + setSelection(selectionTop, selectionBottom, 0, lines[selectionBottom].text.length()); + } else { if (indentType == INDENT_TAB) { if (left) { if (lines[lineOffset].text.substr(0,1) == t) { @@ -2657,10 +2666,9 @@ void UITextInput::shiftText(bool left) { // TODO } } + changedText(lineOffset, lineOffset); + updateCaretPosition(); } - - changedText(selectionTop, selectionBottom); - updateCaretPosition(); } } @@ -2678,4 +2686,55 @@ void UITextInput::convertIndentToTabs() { //TODO } -} \ No newline at end of file +} + +void UITextInput::commentText(bool uncomment){ + saveUndoState(); + + int selL = selectionL, selR = selectionR, selB = selectionBottom, selT = selectionTop; + + if (!uncomment){ + if ((selL == 0 && selR == lines[selB].text.length()) || !hasSelection){ + for (int i = selT; i <= selB; i++){ + lines[i].text = "--" + lines[i].text; + } + + selR = lines[selB].text.length(); + } else { + lines[selT].text = lines[selT].text.substr(0, selL) + "--[[" + lines[selT].text.substr(selL); + lines[selB].text = lines[selB].text.substr(0, selR + strlen("--[[")) + "]]--" + lines[selB].text.substr(selR + strlen("--[[")); + + selL += strlen("--[["); + selR += strlen("--[["); + } + } else { + if ((selL == 0 && selR == lines[selB].text.length()) || !hasSelection){ + for (int i = selT; i <= selB; i++){ + if (lines[i].text.substr(0, 2) == "--"){ + lines[i].text = lines[i].text.substr(2); + } + selR = lines[selB].text.length(); + } + } else { + int l = lines[selT].text.find("--[[", MAX(0, selL - strlen("--[["))); + if (l > 0){ + lines[selT].text = lines[selT].text.erase(l, strlen("--[[")); + selL -= strlen("--[["); + } + + int r = lines[selB].text.find("]]--", MAX(0, selR - strlen("]]--"))); + if (r > 0){ + lines[selB].text = lines[selB].text.erase(r, strlen("]]--")); + selR -= strlen("]]--"); + } + } + } + actualCaretPosition = selL; + updateCaretPosition(); + + setActualLineOffset(); + + changedText(selT, selB); + + setSelection(selT, selB, selL, selR); +} From c9994f4fb6490443a37835408432ec3cc270567b Mon Sep 17 00:00:00 2001 From: Joachim Meyer Date: Wed, 10 Jun 2015 21:18:58 +0200 Subject: [PATCH 2/3] *Fixing to make a tab inline directly where the caret is when nothing is selected (Multiline still buggy.. not updating the show correctly..) --- .../Contents/UI/Source/PolyUITextInput.cpp | 26 ++++++++----------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/Modules/Contents/UI/Source/PolyUITextInput.cpp b/Modules/Contents/UI/Source/PolyUITextInput.cpp index 2ecf059fc..1ecdb2e38 100755 --- a/Modules/Contents/UI/Source/PolyUITextInput.cpp +++ b/Modules/Contents/UI/Source/PolyUITextInput.cpp @@ -2027,12 +2027,6 @@ void UITextInput::onKeyDown(PolyKEY key, wchar_t charCode) { return; } - // indent/shift text - if (multiLine && key == KEY_TAB) { - shiftText(input->getKeyState(KEY_LSHIFT)); - return; - } - if (multiLine && key == KEY_k && (input->getKeyState(KEY_LCTRL) || input->getKeyState(KEY_RCTRL))){ commentText(input->getKeyState(KEY_LSHIFT) || input->getKeyState(KEY_RSHIFT)); } @@ -2070,14 +2064,16 @@ void UITextInput::onKeyDown(PolyKEY key, wchar_t charCode) { if(key == KEY_TAB && multiLine) { saveUndoState(); - if(hasSelection) - deleteSelection(); - ctext = lines[actualLineOffset].text; - String text2 = ctext.substr(actualCaretPosition, ctext.length()-actualCaretPosition); - ctext = ctext.substr(0,actualCaretPosition); - ctext += (wchar_t)'\t' + text2; - actualCaretPosition++; - _changedText = true; + if (hasSelection){ + shiftText(input->getKeyState(KEY_LSHIFT) || input->getKeyState(KEY_RSHIFT)); + } else { + ctext = lines[actualLineOffset].text; + String text2 = ctext.substr(actualCaretPosition, ctext.length() - actualCaretPosition); + ctext = ctext.substr(0, actualCaretPosition); + ctext += (wchar_t)'\t' + text2; + actualCaretPosition++; + _changedText = true; + } } if(key == KEY_DELETE) { @@ -2630,7 +2626,7 @@ void UITextInput::shiftText(bool left) { if (indentType == INDENT_TAB) { if (left) { if (lines[i].text.substr(0,1) == t) { - lines[i].text = lines[i].text.substr(1, lines[i].text.length()-1); + lines[i].text = lines[i].text.substr(1); caretPosition--; } } else { From 657ca86127d44574e349e952c987114c5359feaf Mon Sep 17 00:00:00 2001 From: Joachim Meyer Date: Thu, 18 Jun 2015 22:18:05 +0200 Subject: [PATCH 3/3] Using old hotkeys on Mac, on other OSs use the more common for their texteditors. Moving commenting to the IDE's TextEditor and making it more general - can be used for several file types (atm. Lua and GLSL). Adding some extra functions to UITextInput. --- IDE/Contents/Include/PolycodeTextEditor.h | 3 + IDE/Contents/Source/PolycodeTextEditor.cpp | 107 ++++++++++++++++++ Modules/Contents/UI/Include/PolyUITextInput.h | 18 ++- .../Contents/UI/Source/PolyUITextInput.cpp | 104 ++++++++--------- 4 files changed, 175 insertions(+), 57 deletions(-) diff --git a/IDE/Contents/Include/PolycodeTextEditor.h b/IDE/Contents/Include/PolycodeTextEditor.h index c4323da5f..2ede683cc 100644 --- a/IDE/Contents/Include/PolycodeTextEditor.h +++ b/IDE/Contents/Include/PolycodeTextEditor.h @@ -75,6 +75,7 @@ class PolycodeSyntaxHighlighter : public UITextInputSyntaxHighlighter { static const int MODE_LUA = 0; static const int MODE_GLSL = 1; + int getMode(); protected: int mode; @@ -102,6 +103,8 @@ class PolycodeTextEditor : public PolycodeEditor { void highlightLine(unsigned int lineNumber); + void commentText(bool uncomment = false); + protected: FindBar *findBar; diff --git a/IDE/Contents/Source/PolycodeTextEditor.cpp b/IDE/Contents/Source/PolycodeTextEditor.cpp index 7b329d49f..54acbfd3f 100644 --- a/IDE/Contents/Source/PolycodeTextEditor.cpp +++ b/IDE/Contents/Source/PolycodeTextEditor.cpp @@ -448,6 +448,10 @@ std::vector PolycodeSyntaxHighlighter::parseLua(String tex return tokens; } +int PolycodeSyntaxHighlighter::getMode(){ + return mode; +} + PolycodeTextEditor::PolycodeTextEditor() : PolycodeEditor(true){ firstTimeResize = true; editorType = "PolycodeTextEditor"; @@ -490,6 +494,8 @@ bool PolycodeTextEditor::openFile(OSFileEntry filePath) { findBar->closeButton->addEventListener(this, UIEvent::CLICK_EVENT); findBar->replaceAllButton->addEventListener(this, UIEvent::CLICK_EVENT); findBar->functionList->addEventListener(this, UIEvent::CHANGE_EVENT); + + Services()->getInput()->addEventListener(this, InputEvent::EVENT_KEYDOWN); syntaxHighligher = NULL; @@ -527,6 +533,100 @@ void PolycodeTextEditor::applyEditorConfig(ObjectEntry *configEntry) { } } + +void PolycodeTextEditor::commentText(bool uncomment){ + textInput->saveUndoState(); + + String lineComment = ""; + String multiLineCommentL = ""; + String multiLineCommentR = ""; + if (syntaxHighligher->getMode() == PolycodeSyntaxHighlighter::MODE_GLSL){ + lineComment = "//"; + multiLineCommentL = "/*"; + multiLineCommentR = "*/"; + } else if (syntaxHighligher->getMode() == PolycodeSyntaxHighlighter::MODE_LUA){ + lineComment = "--"; + multiLineCommentL = "--[["; + multiLineCommentR = "]]--"; + } + + int selT = 0, selB = 0, selR = 0, selL = 0; + textInput->readSelection(selT, selB, selL, selR); + String selText = textInput->getSelectionText(); + vector lines = selText.split("\n"); + String topSelLine = selText.substr(0, selText.find("\n")); + String topLine = textInput->getLineText(selT); + + if (!uncomment){ + if (selText == "" || (textInput->getLineText(selT) == selText.substr(0, selText.find("\n")) && textInput->getLineText(selB) == selText.substr(selText.find_last_of('\n')+1))){ + String newLines; + if (lines.size() == 0){ + textInput->replaceLines(selT, lineComment + topLine); + } else { + newLines = lineComment + lines[0]; + for (int i = 1; i < lines.size(); i++){ + newLines = newLines + "\n" + lineComment + lines[i]; + } + selR = lines[lines.size() - 1].length() + lineComment.length(); + textInput->replaceLines(selT, newLines); + } + } else { + lines[0] = textInput->getLineText(selT); + lines[lines.size() - 1] = textInput->getLineText(selB); + + lines[0] = lines[0].substr(0, selL) + multiLineCommentL + lines[0].substr(selL); + lines[lines.size() - 1] = lines[lines.size() - 1].substr(0, selR + multiLineCommentR.length()) + multiLineCommentR + lines[lines.size() - 1].substr(selR + multiLineCommentR.length()); + + textInput->replaceLines(selT, lines[0]); + textInput->replaceLines(selB, lines[lines.size() - 1]); + + selL += multiLineCommentL.length(); + selR += multiLineCommentR.length(); + } + } else { + if (selText == "" || (textInput->getLineText(selT) == selText.substr(0, selText.find("\n")) && textInput->getLineText(selB) == selText.substr(selText.find_last_of('\n') + 1))){ + if (lines.size() == 0){ + lines.push_back(topLine); + } + + String newLines; + if (lines[0].substr(0, lineComment.length()) == lineComment){ + newLines = lines[0].substr(lineComment.length()); + } + for (int i = 1; i < lines.size(); i++){ + if (lines[i].substr(0, lineComment.length()) == lineComment){ + newLines = newLines + "\n" + lines[i].substr(lineComment.length()); + } + } + + if (lines.size() > 1){ + selR = lines[lines.size() - 1].length() - lineComment.length(); + } + + textInput->replaceLines(selT, newLines); + + } else { + lines[0] = textInput->getLineText(selT); + lines[lines.size() - 1] = textInput->getLineText(selB); + + int l = lines[0].find(multiLineCommentL, MAX(0, selL - multiLineCommentL.length())); + if (l > 0){ + textInput->replaceLines(selT, lines[0].erase(l, multiLineCommentL.length())); + selL -= multiLineCommentL.length(); + } + + int r = lines[lines.size() - 1].find(multiLineCommentR, MAX(0, selR - multiLineCommentR.length())); + if (r > 0){ + textInput->replaceLines(selB, lines[lines.size() - 1].erase(r, multiLineCommentR.length())); + selR -= multiLineCommentR.length(); + } + } + } + textInput->setCaretPosition(selL); + + textInput->setSelection(selT, selB, selL, selR); +} + void PolycodeTextEditor::handleEvent(Event *event) { if(event->getDispatcher() == textInput && event->getEventType() == "UIEvent") { @@ -536,6 +636,13 @@ void PolycodeTextEditor::handleEvent(Event *event) { } } + if (event->getDispatcher() == Services()->getInput() && event->getEventCode() == InputEvent::EVENT_KEYDOWN){ + CoreInput *input = Services()->getInput(); + InputEvent* iEvent = (InputEvent*)event; + if (iEvent->getKey() == KEY_k && (input->getKeyState(KEY_LCTRL) || input->getKeyState(KEY_RCTRL))){ + commentText(input->getKeyState(KEY_LSHIFT) || input->getKeyState(KEY_RSHIFT)); + } + } if(event->getDispatcher() == findBar->functionList) { if(event->getEventType() == "UIEvent" && event->getEventCode() == UIEvent::CHANGE_EVENT) { FindMatch *match = (FindMatch*)findBar->functionList->getSelectedItem()->data; diff --git a/Modules/Contents/UI/Include/PolyUITextInput.h b/Modules/Contents/UI/Include/PolyUITextInput.h index c807002af..78bec1254 100755 --- a/Modules/Contents/UI/Include/PolyUITextInput.h +++ b/Modules/Contents/UI/Include/PolyUITextInput.h @@ -158,7 +158,7 @@ namespace Polycode { * If the input is single-line, insert the complete text into * the line, without taking linebreaks into account. * - * If the input is multi-line, each line is inserted separately + * If the input is multi-line, each line is inserted separately * into the text field * * @param text The new text contents. @@ -282,6 +282,14 @@ namespace Polycode { * @param withWhat The string to replace each occurrence with. */ void replaceAll(String what, String withWhat); + + /** + * Replace lines from index start with some new lines. + * + * @param start The index of the first line to be replaced. + * @param newLines The string containing lines split by '\n' to replace the current once. If too many lines it is automatically adding them as new lines. + */ + void replaceLines(int start, String newLines); /** * Find and optionally replace a string. @@ -345,6 +353,11 @@ namespace Polycode { */ String getSelectionText(); + /** + * Reads the bounds of selection to the given integers. + */ + void readSelection(int& top, int& bottom, int& left, int& right); + /** * Replace the current selection with the given text. * @@ -362,7 +375,7 @@ namespace Polycode { void convertIndentToTabs(); void convertIndentToSpaces(); - void commentText(bool uncomment = false); + void saveUndoState(); void doMultilineResize(); @@ -390,7 +403,6 @@ namespace Polycode { Color lineNumberColor; void setUndoState(UITextInputUndoState state); - void saveUndoState(); void setTextDiff(String text); diff --git a/Modules/Contents/UI/Source/PolyUITextInput.cpp b/Modules/Contents/UI/Source/PolyUITextInput.cpp index 1ecdb2e38..93a49be64 100755 --- a/Modules/Contents/UI/Source/PolyUITextInput.cpp +++ b/Modules/Contents/UI/Source/PolyUITextInput.cpp @@ -1145,6 +1145,21 @@ void UITextInput::replaceAll(String what, String withWhat) { } } +void UITextInput::replaceLines(int start, String newLines) { + vector newLinesV = newLines.split("\n"); + int i; + for (i = 0; i < newLinesV.size(); i++){ + if (lines.size() > start + i){ + lines[i + start].text = newLinesV[i]; + } else { + actualLineOffset = lines.size() - 1; + actualCaretPosition = lines[lines.size() - 1].text.length(); + insertLine(newLinesV[i]); + } + } + changedText(start, start + i); +} + std::vector UITextInput::getFindMatches(String stringToFind) { std::vector findMatches; @@ -1407,6 +1422,20 @@ String UITextInput::getSelectionText() { return totalText; } +void UITextInput::readSelection(int& top, int& bottom, int& left, int& right){ + if (hasSelection){ + top = selectionTop; + bottom = selectionBottom; + left = selectionL; + right = selectionR; + } else { + top = lineOffset; + bottom = lineOffset; + left = caretPosition; + right = caretPosition; + } +} + void UITextInput::setSelectionColor(Color color) { selectionColor = color; _setSelectionColor(color); @@ -1734,7 +1763,11 @@ void UITextInput::onKeyDown(PolyKEY key, wchar_t charCode) { setActualToCaret(); updateCaretPosition(); } +#if defined(__APPLE__) && defined(__MACH__) + } else if (input->getKeyState(KEY_LALT) || input->getKeyState(KEY_RALT)) { +#else } else if (input->getKeyState(KEY_LCTRL) || input->getKeyState(KEY_RCTRL)) { +#endif if(input->getKeyState(KEY_LSHIFT) || input->getKeyState(KEY_RSHIFT)) { if(hasSelection) { setSelection(actualLineOffset, selectionLine, actualCaretPosition, caretSkipWordBack(selectionLine, selectionCaretPosition)); @@ -1803,7 +1836,11 @@ void UITextInput::onKeyDown(PolyKEY key, wchar_t charCode) { updateCaretPosition(); } } +#if defined(__APPLE__) && defined(__MACH__) + } else if (input->getKeyState(KEY_LALT) || input->getKeyState(KEY_RALT)) { +#else } else if (input->getKeyState(KEY_LCTRL) || input->getKeyState(KEY_RCTRL)) { +#endif if(actualCaretPosition < lines[actualLineOffset].text.length()) { if(input->getKeyState(KEY_LSHIFT) || input->getKeyState(KEY_RSHIFT)) { if(hasSelection) { @@ -2027,9 +2064,19 @@ void UITextInput::onKeyDown(PolyKEY key, wchar_t charCode) { return; } - if (multiLine && key == KEY_k && (input->getKeyState(KEY_LCTRL) || input->getKeyState(KEY_RCTRL))){ - commentText(input->getKeyState(KEY_LSHIFT) || input->getKeyState(KEY_RSHIFT)); + // indent/shift text +#if defined(__APPLE__) && defined(__MACH__) + if (multiLine && (key == KEY_LEFTBRACKET || key == KEY_RIGHTBRACKET) && + (input->getKeyState(KEY_LSUPER) || input->getKeyState(KEY_RSUPER) || + input->getKeyState(KEY_LCTRL) || input->getKeyState(KEY_RCTRL))) { + shiftText((key == KEY_RIGHTBRACKET) ? false : true); + return; } +#endif + + //if (multiLine && key == KEY_k && (input->getKeyState(KEY_LCTRL) || input->getKeyState(KEY_RCTRL))){ + // commentText(input->getKeyState(KEY_LSHIFT) || input->getKeyState(KEY_RSHIFT)); + //} // at this point, return if certain modifier keys are held down so as not to potentially add any unwanted text if (input->getKeyState(KEY_LSUPER) || input->getKeyState(KEY_RSUPER) || input->getKeyState(KEY_LCTRL) || @@ -2062,7 +2109,7 @@ void UITextInput::onKeyDown(PolyKEY key, wchar_t charCode) { } } - if(key == KEY_TAB && multiLine) { + if (key == KEY_TAB && multiLine) { saveUndoState(); if (hasSelection){ shiftText(input->getKeyState(KEY_LSHIFT) || input->getKeyState(KEY_RSHIFT)); @@ -2683,54 +2730,3 @@ void UITextInput::convertIndentToTabs() { //TODO } } - -void UITextInput::commentText(bool uncomment){ - saveUndoState(); - - int selL = selectionL, selR = selectionR, selB = selectionBottom, selT = selectionTop; - - if (!uncomment){ - if ((selL == 0 && selR == lines[selB].text.length()) || !hasSelection){ - for (int i = selT; i <= selB; i++){ - lines[i].text = "--" + lines[i].text; - } - - selR = lines[selB].text.length(); - } else { - lines[selT].text = lines[selT].text.substr(0, selL) + "--[[" + lines[selT].text.substr(selL); - lines[selB].text = lines[selB].text.substr(0, selR + strlen("--[[")) + "]]--" + lines[selB].text.substr(selR + strlen("--[[")); - - selL += strlen("--[["); - selR += strlen("--[["); - } - } else { - if ((selL == 0 && selR == lines[selB].text.length()) || !hasSelection){ - for (int i = selT; i <= selB; i++){ - if (lines[i].text.substr(0, 2) == "--"){ - lines[i].text = lines[i].text.substr(2); - } - selR = lines[selB].text.length(); - } - } else { - int l = lines[selT].text.find("--[[", MAX(0, selL - strlen("--[["))); - if (l > 0){ - lines[selT].text = lines[selT].text.erase(l, strlen("--[[")); - selL -= strlen("--[["); - } - - int r = lines[selB].text.find("]]--", MAX(0, selR - strlen("]]--"))); - if (r > 0){ - lines[selB].text = lines[selB].text.erase(r, strlen("]]--")); - selR -= strlen("]]--"); - } - } - } - actualCaretPosition = selL; - updateCaretPosition(); - - setActualLineOffset(); - - changedText(selT, selB); - - setSelection(selT, selB, selL, selR); -}