mirror of
https://github.com/UpsilonNumworks/Upsilon.git
synced 2026-01-19 00:37:25 +01:00
[code] Auto indentation in the script editor.
Change-Id: I7066c41fa53068e6f18a5ad226b5e72f591f02e2
This commit is contained in:
@@ -5,13 +5,14 @@ namespace Code {
|
||||
|
||||
EditorController::EditorController(ScriptParameterController * scriptParameterController) :
|
||||
ViewController(nullptr),
|
||||
m_view(this),
|
||||
m_textArea(this),
|
||||
m_scriptParameterController(scriptParameterController)
|
||||
{
|
||||
m_textArea.setDelegate(this);
|
||||
}
|
||||
|
||||
void EditorController::setScript(Script script){
|
||||
m_view.setText(script.editableContent(), script.contentBufferSize());
|
||||
m_textArea.setText(script.editableContent(), script.contentBufferSize());
|
||||
}
|
||||
|
||||
bool EditorController::handleEvent(Ion::Events::Event event) {
|
||||
@@ -23,11 +24,78 @@ bool EditorController::handleEvent(Ion::Events::Event event) {
|
||||
}
|
||||
|
||||
void EditorController::didBecomeFirstResponder() {
|
||||
app()->setFirstResponder(&m_view);
|
||||
app()->setFirstResponder(&m_textArea);
|
||||
}
|
||||
|
||||
void EditorController::viewDidDisappear() {
|
||||
m_scriptParameterController->scriptContentEditionDidFinish();
|
||||
}
|
||||
|
||||
bool EditorController::textAreaShouldFinishEditing(TextArea * textArea, Ion::Events::Event event) {
|
||||
return false;
|
||||
}
|
||||
|
||||
bool EditorController::textAreaDidReceiveEvent(TextArea * textArea, Ion::Events::Event event) {
|
||||
if (event == Ion::Events::EXE) {
|
||||
// Auto-Indent
|
||||
char * text = const_cast<char *>(textArea->text());
|
||||
int charBeforeCursorIndex = textArea->cursorLocation()-1;
|
||||
int indentationSize = 0;
|
||||
// Indent more if the previous line ends with ':'.
|
||||
if (charBeforeCursorIndex >= 0 && text[charBeforeCursorIndex] == ':') {
|
||||
indentationSize += k_indentationSpacesNumber;
|
||||
}
|
||||
// Compute the indentation of the current line.
|
||||
int indentationIndex = charBeforeCursorIndex;
|
||||
while (indentationIndex >= 0 && text[indentationIndex] != '\n') {
|
||||
indentationIndex--;
|
||||
}
|
||||
if (indentationIndex >= 0) {
|
||||
indentationIndex++;
|
||||
while (text[indentationIndex] == ' ') {
|
||||
indentationSize++;
|
||||
indentationIndex++;
|
||||
}
|
||||
}
|
||||
textArea->insertText("\n");
|
||||
for (int i = 0; i < indentationSize; i++) {
|
||||
textArea->insertText(" ");
|
||||
}
|
||||
return true;
|
||||
} else if (event == Ion::Events::Backspace) {
|
||||
// If the cursor is on the left of the text of a line,
|
||||
// backspace one intentation space at a time.
|
||||
char * text = const_cast<char *>(textArea->text());
|
||||
int charBeforeCursorIndex = textArea->cursorLocation()-1;
|
||||
int indentationSize = 0;
|
||||
while (charBeforeCursorIndex >= 0 && text[charBeforeCursorIndex] == ' ') {
|
||||
charBeforeCursorIndex--;
|
||||
indentationSize++;
|
||||
}
|
||||
if (charBeforeCursorIndex >= 0 && text[charBeforeCursorIndex] == '\n'
|
||||
&& indentationSize >= k_indentationSpacesNumber)
|
||||
{
|
||||
for (int i = 0; i < k_indentationSpacesNumber; i++) {
|
||||
textArea->removeChar();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
} else if (event == Ion::Events::Space) {
|
||||
// If the cursor is on the left of the text of a line,
|
||||
// a space triggers an indentation.
|
||||
char * text = const_cast<char *>(textArea->text());
|
||||
int charBeforeCursorIndex = textArea->cursorLocation()-1;
|
||||
while (charBeforeCursorIndex >= 0 && text[charBeforeCursorIndex] == ' ') {
|
||||
charBeforeCursorIndex--;
|
||||
}
|
||||
if (charBeforeCursorIndex >= 0 && text[charBeforeCursorIndex] == '\n') {
|
||||
for (int i = 0; i < k_indentationSpacesNumber; i++) {
|
||||
textArea->insertText(" ");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user