diff --git a/apps/code/app.h b/apps/code/app.h index 605beb8ea..57ee6181a 100644 --- a/apps/code/app.h +++ b/apps/code/app.h @@ -50,6 +50,7 @@ public: } StackViewController * stackViewController() { return &m_codeStackViewController; } ConsoleController * consoleController() { return &m_consoleController; } + MenuController * menuController() { return &m_menuController; } /* Responder */ bool handleEvent(Ion::Events::Event event) override; diff --git a/apps/code/console_controller.cpp b/apps/code/console_controller.cpp index cb0f06f23..6c66b51be 100644 --- a/apps/code/console_controller.cpp +++ b/apps/code/console_controller.cpp @@ -57,7 +57,7 @@ bool ConsoleController::loadPythonEnvironment() { /* We load functions and variables names in the variable box before running * any other python code to avoid failling to load functions and variables * due to memory exhaustion. */ - App::app()->variableBoxController()->loadFunctionsAndVariables(); + App::app()->variableBoxController()->loadFunctionsAndVariables(-1, nullptr); return true; } diff --git a/apps/code/editor_controller.cpp b/apps/code/editor_controller.cpp index 863a29191..a03f29406 100644 --- a/apps/code/editor_controller.cpp +++ b/apps/code/editor_controller.cpp @@ -13,13 +13,15 @@ EditorController::EditorController(MenuController * menuController, App * python ViewController(nullptr), m_editorView(this, pythonDelegate), m_script(Ion::Storage::Record()), + m_scriptIndex(-1), m_menuController(menuController) { m_editorView.setTextAreaDelegates(this, this); } -void EditorController::setScript(Script script) { +void EditorController::setScript(Script script, int scriptIndex) { m_script = script; + m_scriptIndex = scriptIndex; /* We edit the script direclty in the storage buffer. We thus put all the * storage available space at the end of the current edited script and we set @@ -122,7 +124,7 @@ bool EditorController::textAreaDidReceiveEvent(TextArea * textArea, Ion::Events: VariableBoxController * EditorController::variableBoxForInputEventHandler(InputEventHandler * textInput) { VariableBoxController * varBox = App::app()->variableBoxController(); - varBox->loadFunctionsAndVariables(); + varBox->loadFunctionsAndVariables(m_scriptIndex, m_editorView.textToAutocomplete()); return varBox; } diff --git a/apps/code/editor_controller.h b/apps/code/editor_controller.h index 7fb5d9560..4cd32c1ed 100644 --- a/apps/code/editor_controller.h +++ b/apps/code/editor_controller.h @@ -16,7 +16,8 @@ class App; class EditorController : public ViewController, public TextAreaDelegate, public Shared::InputEventHandlerDelegate { public: EditorController(MenuController * menuController, App * pythonDelegate); - void setScript(Script script); + void setScript(Script script, int scriptIndex); + int scriptIndex() const { return m_scriptIndex; } void willExitApp(); /* ViewController */ @@ -39,6 +40,7 @@ private: StackViewController * stackController(); EditorView m_editorView; Script m_script; + int m_scriptIndex; MenuController * m_menuController; }; diff --git a/apps/code/editor_view.cpp b/apps/code/editor_view.cpp index cfcbe8113..455e5761e 100644 --- a/apps/code/editor_view.cpp +++ b/apps/code/editor_view.cpp @@ -16,6 +16,10 @@ EditorView::EditorView(Responder * parentResponder, App * pythonDelegate) : m_textArea.setScrollViewDelegate(this); } +const char * EditorView::textToAutocomplete() const { + return m_textArea.textToAutocomplete(); +} + void EditorView::resetSelection() { m_textArea.resetSelection(); } diff --git a/apps/code/editor_view.h b/apps/code/editor_view.h index bbc608e88..58ec93eff 100644 --- a/apps/code/editor_view.h +++ b/apps/code/editor_view.h @@ -9,6 +9,7 @@ namespace Code { class EditorView : public Responder, public View, public ScrollViewDelegate { public: EditorView(Responder * parentResponder, App * pythonDelegate); + const char * textToAutocomplete() const; void resetSelection(); void setTextAreaDelegates(InputEventHandlerDelegate * inputEventHandlerDelegate, TextAreaDelegate * delegate) { m_textArea.setDelegates(inputEventHandlerDelegate, delegate); diff --git a/apps/code/menu_controller.cpp b/apps/code/menu_controller.cpp index e07632993..0857a0499 100644 --- a/apps/code/menu_controller.cpp +++ b/apps/code/menu_controller.cpp @@ -372,7 +372,7 @@ void MenuController::configureScript() { void MenuController::editScriptAtIndex(int scriptIndex) { assert(scriptIndex >=0 && scriptIndex < m_scriptStore->numberOfScripts()); Script script = m_scriptStore->scriptAtIndex(scriptIndex); - m_editorController.setScript(script); + m_editorController.setScript(script, scriptIndex); stackViewController()->push(&m_editorController); } diff --git a/apps/code/menu_controller.h b/apps/code/menu_controller.h index a5fc84638..6d87672ad 100644 --- a/apps/code/menu_controller.h +++ b/apps/code/menu_controller.h @@ -24,6 +24,7 @@ public: void openConsoleWithScript(Script script); void scriptContentEditionDidFinish(); void willExitApp(); + int editedScriptIndex() const { return m_editorController.scriptIndex(); } /* ViewController */ View * view() override { return &m_selectableTableView; } diff --git a/apps/code/python_text_area.cpp b/apps/code/python_text_area.cpp index 92163632d..5d45bf203 100644 --- a/apps/code/python_text_area.cpp +++ b/apps/code/python_text_area.cpp @@ -53,6 +53,10 @@ static inline size_t TokenLength(mp_lexer_t * lex, const char * tokenPosition) { return lex->column - lex->tok_column; } +const char * PythonTextArea::ContentView::textToAutocomplete() const { + return UTF8Helper::BeginningOfWord(editedText(), cursorLocation()); +} + void PythonTextArea::ContentView::loadSyntaxHighlighter() { m_pythonDelegate->initPythonWithUser(this); } @@ -231,6 +235,13 @@ bool PythonTextArea::handleEventWithText(const char * text, bool indentation, bo return result; } +const char * PythonTextArea::textToAutocomplete() const { + if (!m_contentView.isAutocompleting()) { + return nullptr; + } + return m_contentView.textToAutocomplete(); +} + void PythonTextArea::removeAutocompletion() { assert(m_contentView.isAutocompleting()); const char * autocompleteStart = m_contentView.cursorLocation(); @@ -255,8 +266,8 @@ void PythonTextArea::addAutocompletion() { * Look first in the current script variables and functions, then in the * builtins, then in the imported modules/scripts. */ VariableBoxController * varBox = m_contentView.pythonDelegate()->variableBoxController(); - const char * beginningOfWord = UTF8Helper::BeginningOfWord(m_contentView.editedText(), autocompletionLocation); - textToInsert = varBox->autocompletionForText(beginningOfWord); + const char * beginningOfWord = m_contentView.textToAutocomplete(); + textToInsert = varBox->autocompletionForText(m_contentView.pythonDelegate()->menuController()->editedScriptIndex(), beginningOfWord); } // Try to insert the text (this might fail if the buffer is full) diff --git a/apps/code/python_text_area.h b/apps/code/python_text_area.h index eb36fd99c..7b0031b9d 100644 --- a/apps/code/python_text_area.h +++ b/apps/code/python_text_area.h @@ -18,6 +18,7 @@ public: void unloadSyntaxHighlighter() { m_contentView.unloadSyntaxHighlighter(); } bool handleEvent(Ion::Events::Event event) override; bool handleEventWithText(const char * text, bool indentation = false, bool forceCursorRightOfText = false) override; + const char * textToAutocomplete() const; protected: class ContentView : public TextArea::ContentView { public: @@ -30,6 +31,7 @@ protected: App * pythonDelegate() { return m_pythonDelegate; } void setAutocompleting(bool autocomplete) { m_autocomplete = autocomplete; } bool isAutocompleting() const { return m_autocomplete; } + const char * textToAutocomplete() const; void loadSyntaxHighlighter(); void unloadSyntaxHighlighter(); void clearRect(KDContext * ctx, KDRect rect) const override; diff --git a/apps/code/variable_box_controller.cpp b/apps/code/variable_box_controller.cpp index 8e98ce155..4b21a418a 100644 --- a/apps/code/variable_box_controller.cpp +++ b/apps/code/variable_box_controller.cpp @@ -60,7 +60,7 @@ void VariableBoxController::willDisplayCellForIndex(HighlightCell * cell, int in myCell->setScriptNode(scriptNodeAtIndex(index)); } -void VariableBoxController::loadFunctionsAndVariables() { +void VariableBoxController::loadFunctionsAndVariables(int scriptIndex, const char * textToAutocomplete) { //TODO LEA Prune these + add modules //TODO LEA load buitins only once int index = 0; @@ -187,9 +187,9 @@ void VariableBoxController::loadFunctionsAndVariables() { #endif } -const char * VariableBoxController::autocompletionForText(const char * text) { +const char * VariableBoxController::autocompletionForText(int scriptIndex, const char * text) { // TODO LEA Accelerate - loadFunctionsAndVariables(); + loadFunctionsAndVariables(scriptIndex, text); const char * endOfText = UTF8Helper::EndOfWord(text); const int textLength = endOfText - text; assert(textLength >= 1); diff --git a/apps/code/variable_box_controller.h b/apps/code/variable_box_controller.h index a898db686..f0e69448c 100644 --- a/apps/code/variable_box_controller.h +++ b/apps/code/variable_box_controller.h @@ -26,8 +26,8 @@ public: int typeAtLocation(int i, int j) override { return 0; } /* VariableBoxController */ - void loadFunctionsAndVariables(); - const char * autocompletionForText(const char * text); + void loadFunctionsAndVariables(int scriptIndex, const char * textToAutocomplete); + const char * autocompletionForText(int scriptIndex, const char * text); private: constexpr static int k_maxScriptObjectNameSize = 100; constexpr static int k_maxNumberOfDisplayedRows = 6; // 240/40