From 2ed0c85ebfee8e7769962051698b5370e4ebba7b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9a=20Saviot?= Date: Fri, 24 Apr 2020 11:19:41 +0200 Subject: [PATCH] [apps/code] Autocompletion end is end of token, not next ' ' --- apps/code/python_text_area.cpp | 21 ++++++++++++--------- apps/code/python_text_area.h | 6 +++++- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/apps/code/python_text_area.cpp b/apps/code/python_text_area.cpp index 103f66f9e..37fa5d790 100644 --- a/apps/code/python_text_area.cpp +++ b/apps/code/python_text_area.cpp @@ -235,13 +235,13 @@ void PythonTextArea::ContentView::drawLine(KDContext * ctx, int line, const char // Redraw the autocompleted word in the right color if (m_autocomplete && autocompleteStart >= text && autocompleteStart < text + byteLength) { - const char * autocompleteEnd = UTF8Helper::EndOfWord(autocompleteStart); + assert(m_autocompletionEnd != nullptr && m_autocompletionEnd > autocompleteStart); drawStringAt( ctx, line, UTF8Helper::GlyphOffsetAtCodePoint(text, autocompleteStart), autocompleteStart, - std::min(text + byteLength, autocompleteEnd) - autocompleteStart, + std::min(text + byteLength, m_autocompletionEnd) - autocompleteStart, AutocompleteColor, BackgroundColor, nullptr, @@ -319,12 +319,13 @@ bool PythonTextArea::handleEventWithText(const char * text, bool indentation, bo void PythonTextArea::removeAutocompletion() { assert(m_contentView.isAutocompleting()); + assert(m_contentView.autocompletionEnd() != nullptr); const char * autocompleteStart = m_contentView.cursorLocation(); - const char * autocompleteEnd = UTF8Helper::EndOfWord(autocompleteStart); - assert(autocompleteEnd >= autocompleteStart); - if (autocompleteEnd > autocompleteStart) { - m_contentView.removeText(autocompleteStart, autocompleteEnd); - } + const char * autocompleteEnd = m_contentView.autocompletionEnd(); + assert(autocompleteEnd != nullptr && autocompleteEnd > autocompleteStart); + //TODO LEA if (autocompleteEnd > autocompleteStart) { + m_contentView.removeText(autocompleteStart, autocompleteEnd); + //TODO LEA } m_contentView.setAutocompleting(false); } @@ -348,15 +349,17 @@ void PythonTextArea::addAutocompletion() { // Try to insert the text (this might fail if the buffer is full) if (textToInsert && textToInsertLength > 0 && m_contentView.insertTextAtLocation(textToInsert, const_cast(autocompletionLocation), textToInsertLength)) { m_contentView.setAutocompleting(true); + m_contentView.setAutocompletionEnd(autocompletionLocation + textToInsertLength); } } void PythonTextArea::acceptAutocompletion(bool moveCursorToEndOfAutocompletion) { assert(m_contentView.isAutocompleting()); + const char * autocompEnd = m_contentView.autocompletionEnd(); m_contentView.setAutocompleting(false); + m_contentView.setAutocompletionEnd(nullptr); if (moveCursorToEndOfAutocompletion) { - const char * autocompleteEnd = UTF8Helper::EndOfWord(m_contentView.cursorLocation()); - setCursorLocation(autocompleteEnd); + setCursorLocation(autocompEnd); addAutocompletion(); } } diff --git a/apps/code/python_text_area.h b/apps/code/python_text_area.h index 28a69a5e0..037069446 100644 --- a/apps/code/python_text_area.h +++ b/apps/code/python_text_area.h @@ -38,12 +38,15 @@ protected: ContentView(App * pythonDelegate, const KDFont * font) : TextArea::ContentView(font), m_pythonDelegate(pythonDelegate), - m_autocomplete(false) + m_autocomplete(false), + m_autocompletionEnd(nullptr) { } App * pythonDelegate() { return m_pythonDelegate; } void setAutocompleting(bool autocomplete) { m_autocomplete = autocomplete; } bool isAutocompleting() const { return m_autocomplete; } + const char * autocompletionEnd() const { assert(m_autocomplete); return m_autocompletionEnd; } + void setAutocompletionEnd(const char * end) { m_autocompletionEnd = end; } const char * textToAutocomplete() const; void loadSyntaxHighlighter(); void unloadSyntaxHighlighter(); @@ -53,6 +56,7 @@ protected: private: App * m_pythonDelegate; bool m_autocomplete; + const char * m_autocompletionEnd; }; private: void removeAutocompletion();