diff --git a/apps/code/python_text_area.cpp b/apps/code/python_text_area.cpp index bcc839b28..08f751efd 100644 --- a/apps/code/python_text_area.cpp +++ b/apps/code/python_text_area.cpp @@ -196,4 +196,38 @@ KDRect PythonTextArea::ContentView::dirtyRectFromPosition(const char * position, ); } +bool PythonTextArea::handleEventWithText(const char * text, bool indentation, bool forceCursorRightOfText) { + if (*text == 0) { + return false; + } + if (m_contentView.isAutocompleting()) { + removeAutocompletion(); + } + bool result = TextArea::handleEventWithText(text, indentation, forceCursorRightOfText); + addAutocompletion(); + return result; +} + +void PythonTextArea::removeAutocompletion() { + assert(m_contentView.isAutocompleting()); + const char * autocompleteStart = m_contentView.cursorLocation(); + const char * autocompleteEnd = UTF8Helper::EndOfWord(autocompleteStart); + assert(autocompleteEnd > autocompleteStart); + m_contentView.removeText(autocompleteStart, autocompleteEnd); + m_contentView.setAutocompleting(false); +} + +void PythonTextArea::addAutocompletion() { + assert(!m_contentView.isAutocompleting()); + + // Compute the text to insert + // TODO LEA + const char * textToInsert = "test"; + + // Try to insert the text (this might fail if the buffer is full) + if (textToInsert && m_contentView.insertTextAtLocation(textToInsert, const_cast(cursorLocation()))) { + m_contentView.setAutocompleting(true); + } +} + } diff --git a/apps/code/python_text_area.h b/apps/code/python_text_area.h index cfc6e23ef..c05d24709 100644 --- a/apps/code/python_text_area.h +++ b/apps/code/python_text_area.h @@ -16,6 +16,7 @@ public: } void loadSyntaxHighlighter() { m_contentView.loadSyntaxHighlighter(); } void unloadSyntaxHighlighter() { m_contentView.unloadSyntaxHighlighter(); } + bool handleEventWithText(const char * text, bool indentation = false, bool forceCursorRightOfText = false) override; protected: class ContentView : public TextArea::ContentView { public: @@ -25,6 +26,8 @@ protected: m_autocomplete(false) { } + void setAutocompleting(bool autocomplete) { m_autocomplete = autocomplete; } + bool isAutocompleting() const { return m_autocomplete; } void loadSyntaxHighlighter(); void unloadSyntaxHighlighter(); void clearRect(KDContext * ctx, KDRect rect) const override; @@ -35,6 +38,8 @@ protected: bool m_autocomplete; }; private: + void removeAutocompletion(); + void addAutocompletion(); const ContentView * nonEditableContentView() const override { return &m_contentView; } ContentView m_contentView; }; diff --git a/escher/include/escher/text_area.h b/escher/include/escher/text_area.h index 640511dc1..7d7e58716 100644 --- a/escher/include/escher/text_area.h +++ b/escher/include/escher/text_area.h @@ -122,6 +122,7 @@ protected: bool removePreviousGlyph() override; bool removeEndOfLine() override; bool removeStartOfLine(); + size_t removeText(const char * start, const char * end); size_t deleteSelection() override; protected: KDRect glyphFrameAtPosition(const char * text, const char * position) const override; diff --git a/escher/src/text_area.cpp b/escher/src/text_area.cpp index 5a28a2a18..11dfc4045 100644 --- a/escher/src/text_area.cpp +++ b/escher/src/text_area.cpp @@ -521,9 +521,13 @@ bool TextArea::ContentView::removeStartOfLine() { return false; } +size_t TextArea::ContentView::removeText(const char * start, const char * end) { + return m_text.removeText(start, end); +} + size_t TextArea::ContentView::deleteSelection() { assert(!selectionIsEmpty()); - size_t removedLength = m_text.removeText(m_selectionStart, m_selectionEnd); + size_t removedLength = removeText(m_selectionStart, m_selectionEnd); /* We cannot call resetSelection() because m_selectionStart and m_selectionEnd * are invalid */ m_selectionStart = nullptr;