diff --git a/apps/code/console_controller.cpp b/apps/code/console_controller.cpp index 36456a896..da5603e04 100644 --- a/apps/code/console_controller.cpp +++ b/apps/code/console_controller.cpp @@ -30,7 +30,7 @@ ConsoleController::ConsoleController(Responder * parentResponder, App * pythonDe m_pythonDelegate(pythonDelegate), m_importScriptsWhenViewAppears(false), m_selectableTableView(this, this, this, this), - m_editCell(this, pythonDelegate, this), + m_editCell(this, this, this), m_scriptStore(scriptStore), m_sandboxController(this), m_inputRunLoopActive(false) @@ -378,6 +378,12 @@ bool ConsoleController::textFieldDidAbortEditing(TextField * textField) { return true; } +VariableBoxController * ConsoleController::variableBoxForInputEventHandler(InputEventHandler * textInput) { + VariableBoxController * varBox = App::app()->variableBoxController(); + varBox->loadVariablesImportedFromScripts(); + return varBox; +} + void ConsoleController::resetSandbox() { if (stackViewController()->topViewController() != sandbox()) { return; diff --git a/apps/code/console_controller.h b/apps/code/console_controller.h index f198255a6..cd3cae701 100644 --- a/apps/code/console_controller.h +++ b/apps/code/console_controller.h @@ -9,12 +9,14 @@ #include "console_store.h" #include "sandbox_controller.h" #include "script_store.h" +#include "variable_box_controller.h" +#include "../shared/input_event_handler_delegate.h" namespace Code { class App; -class ConsoleController : public ViewController, public ListViewDataSource, public SelectableTableViewDataSource, public SelectableTableViewDelegate, public TextFieldDelegate, public MicroPython::ExecutionEnvironment { +class ConsoleController : public ViewController, public ListViewDataSource, public SelectableTableViewDataSource, public SelectableTableViewDelegate, public TextFieldDelegate, public Shared::InputEventHandlerDelegate, public MicroPython::ExecutionEnvironment { public: ConsoleController(Responder * parentResponder, App * pythonDelegate, ScriptStore * scriptStore #if EPSILON_GETOPT @@ -59,6 +61,9 @@ public: bool textFieldDidFinishEditing(TextField * textField, const char * text, Ion::Events::Event event) override; bool textFieldDidAbortEditing(TextField * textField) override; + // InputEventHandlerDelegate + VariableBoxController * variableBoxForInputEventHandler(InputEventHandler * textInput) override; + // MicroPython::ExecutionEnvironment ViewController * sandbox() override { return &m_sandboxController; } void resetSandbox() override; diff --git a/apps/code/variable_box_controller.cpp b/apps/code/variable_box_controller.cpp index 87240f40f..68ef37d0b 100644 --- a/apps/code/variable_box_controller.cpp +++ b/apps/code/variable_box_controller.cpp @@ -159,9 +159,7 @@ int VariableBoxController::typeAtLocation(int i, int j) { void VariableBoxController::loadFunctionsAndVariables(int scriptIndex, const char * textToAutocomplete, int textToAutocompleteLength) { // Reset the node counts - m_currentScriptNodesCount = 0; - m_builtinNodesCount = 0; - m_importedNodesCount = 0; + empty(); if (scriptIndex < 0) { /* If not script index is given, the variable box is loaded from console. We @@ -241,6 +239,16 @@ const char * VariableBoxController::autocompletionAlternativeAtIndex(int textToA return currentName + textToAutocompleteLength; } +void VariableBoxController::loadVariablesImportedFromScripts() { + const int scriptsCount = m_scriptStore->numberOfScripts(); + for (int i = 0; i < scriptsCount; i++) { + Script script = m_scriptStore->scriptAtIndex(i); + if (script.contentFetchedFromConsole()) { + loadGlobalAndImportedVariablesInScriptAsImported(script.fullName(), script.scriptContent(), nullptr, -1, false); // TODO optimize number of script fetches + } + } +} + void VariableBoxController::empty() { m_builtinNodesCount = 0; m_currentScriptNodesCount = 0; @@ -382,16 +390,6 @@ void VariableBoxController::insertTextInCaller(const char * text, int textLength sender()->handleEventWithText(commandBuffer); } -void VariableBoxController::loadVariablesImportedFromScripts() { - const int scriptsCount = m_scriptStore->numberOfScripts(); - for (int i = 0; i < scriptsCount; i++) { - Script script = m_scriptStore->scriptAtIndex(i); - if (script.contentFetchedFromConsole()) { - loadGlobalAndImportedVariablesInScriptAsImported(script.fullName(), script.scriptContent(), nullptr, -1, false); // TODO optimize number of script fetches - } - } -} - void VariableBoxController::loadBuiltinNodes(const char * textToAutocomplete, int textToAutocompleteLength) { //TODO LEA could be great to use strings defined in STATIC const char *const tok_kw[] from python/lexer.c //TODO LEA Prune these (check all are usable in our Python, but just comment those which aren't -> there might become usable later) diff --git a/apps/code/variable_box_controller.h b/apps/code/variable_box_controller.h index 0a0b9323c..54a341f25 100644 --- a/apps/code/variable_box_controller.h +++ b/apps/code/variable_box_controller.h @@ -38,6 +38,7 @@ public: void loadFunctionsAndVariables(int scriptIndex, const char * textToAutocomplete, int textToAutocompleteLength); const char * autocompletionForText(int scriptIndex, const char * textToAutocomplete, int textToAutocompleteLength, int * textToInsertLength, bool * addParentheses); const char * autocompletionAlternativeAtIndex(int textToAutocompleteLength, int * textToInsertLength, bool * addParentheses, int index, int * indexToUpdate = nullptr); + void loadVariablesImportedFromScripts(); void empty(); private: @@ -86,7 +87,6 @@ private: void insertTextInCaller(const char * text, int textLength = -1); // Loading - void loadVariablesImportedFromScripts(); void loadBuiltinNodes(const char * textToAutocomplete, int textToAutocompleteLength); void loadImportedVariablesInScript(const char * scriptContent, const char * textToAutocomplete, int textToAutocompleteLength); void loadCurrentVariablesInScript(const char * scriptContent, const char * textToAutocomplete, int textToAutocompleteLength);