Skip to content

*Using more common hotkeys for jumping over words (ctrl + arrow key),… #642

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions Core/Contents/Include/PolyString.h
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down
13 changes: 13 additions & 0 deletions Core/Contents/Source/PolyString.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "PolyString.h"
#include <iomanip>
#include <sstream>
#include <algorithm>

using namespace Polycode;
using namespace std;
Expand Down Expand Up @@ -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);
}
}
3 changes: 3 additions & 0 deletions IDE/Contents/Include/PolycodeTextEditor.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -102,6 +103,8 @@ class PolycodeTextEditor : public PolycodeEditor {

void highlightLine(unsigned int lineNumber);

void commentText(bool uncomment = false);

protected:

FindBar *findBar;
Expand Down
107 changes: 107 additions & 0 deletions IDE/Contents/Source/PolycodeTextEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,10 @@ std::vector<SyntaxHighlightToken> PolycodeSyntaxHighlighter::parseLua(String tex
return tokens;
}

int PolycodeSyntaxHighlighter::getMode(){
return mode;
}

PolycodeTextEditor::PolycodeTextEditor() : PolycodeEditor(true){
firstTimeResize = true;
editorType = "PolycodeTextEditor";
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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<String> 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") {
Expand All @@ -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;
Expand Down
18 changes: 16 additions & 2 deletions Modules/Contents/UI/Include/PolyUITextInput.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
*
Expand All @@ -362,6 +375,8 @@ namespace Polycode {
void convertIndentToTabs();
void convertIndentToSpaces();

void saveUndoState();

void doMultilineResize();

static void setMenuSingleton(UIGlobalMenu *_globalMenu);
Expand All @@ -388,7 +403,6 @@ namespace Polycode {
Color lineNumberColor;

void setUndoState(UITextInputUndoState state);
void saveUndoState();

void setTextDiff(String text);

Expand Down
Loading