diff --git a/apps/code/python_text_area.cpp b/apps/code/python_text_area.cpp index 54e0fe390..ba7172ec3 100644 --- a/apps/code/python_text_area.cpp +++ b/apps/code/python_text_area.cpp @@ -165,7 +165,7 @@ void PythonTextArea::ContentView::drawLine(KDContext * ctx, int line, const char // Redraw the autocompleted word in the right color const char * autocompleteStart = m_cursorLocation; assert(!m_autocomplete || autocompleteStart != text); - if (m_autocomplete && autocompleteStart > text && autocompleteStart < text + byteLength) { + if (m_autocomplete && autocompleteStart > text && autocompleteStart < text + byteLength) { const char * autocompleteEnd = UTF8Helper::EndOfWord(autocompleteStart); drawStringAt( ctx, @@ -241,12 +241,19 @@ void PythonTextArea::removeAutocompletion() { void PythonTextArea::addAutocompletion() { assert(!m_contentView.isAutocompleting()); - // Compute the text to insert - // TODO LEA - const char * textToInsert = "test"; + const char * autocompletionLocation = const_cast(cursorLocation()); + const char * textToInsert = nullptr; + CodePoint prevCodePoint = UTF8Helper::PreviousCodePoint(m_contentView.editedText(), autocompletionLocation); + if (!UTF8Helper::CodePointIsEndOfWord(prevCodePoint)) { + /* The previous code point is neither the beginning of the text, nor a + * space, nor a \n. + * Compute the text to insert */ + // TODO LEA + textToInsert = "test"; + } // Try to insert the text (this might fail if the buffer is full) - if (textToInsert && m_contentView.insertTextAtLocation(textToInsert, const_cast(cursorLocation()))) { + if (textToInsert && m_contentView.insertTextAtLocation(textToInsert, const_cast(autocompletionLocation))) { m_contentView.setAutocompleting(true); } } diff --git a/ion/include/ion/unicode/utf8_helper.h b/ion/include/ion/unicode/utf8_helper.h index fc906c03c..32ef6f042 100644 --- a/ion/include/ion/unicode/utf8_helper.h +++ b/ion/include/ion/unicode/utf8_helper.h @@ -66,8 +66,10 @@ const char * PerformAtCodePoints( const char * initialPosition = nullptr, const char * stoppingPosition = nullptr); +CodePoint PreviousCodePoint(const char * buffer, const char * location); // returns 0 if location == buffer bool PreviousCodePointIs(const char * buffer, const char * location, CodePoint c); bool CodePointIs(const char * location, CodePoint c); +bool CodePointIsEndOfWord(CodePoint c); // Shift the buffer and return the number of bytes removed. int RemovePreviousGlyph(const char * text, char * location, CodePoint * c = nullptr); diff --git a/ion/src/shared/unicode/utf8_helper.cpp b/ion/src/shared/unicode/utf8_helper.cpp index f17841394..3f0d44f36 100644 --- a/ion/src/shared/unicode/utf8_helper.cpp +++ b/ion/src/shared/unicode/utf8_helper.cpp @@ -257,13 +257,20 @@ const char * PerformAtCodePoints(const char * s, CodePoint c, CodePointAction ac return codePointPointer; } +CodePoint PreviousCodePoint(const char * buffer, const char * location) { + if (location == buffer) { + return UCodePointNull; + } + UTF8Decoder decoder(buffer, location); + return decoder.previousCodePoint(); +} + bool PreviousCodePointIs(const char * buffer, const char * location, CodePoint c) { assert(location > buffer); if (UTF8Decoder::CharSizeOfCodePoint(c) == 1) { return *(location -1) == c; } - UTF8Decoder decoder(buffer, location); - return decoder.previousCodePoint() == c; + return PreviousCodePoint(buffer, location) == c; } bool CodePointIs(const char * location, CodePoint c) { @@ -274,6 +281,10 @@ bool CodePointIs(const char * location, CodePoint c) { return decoder.nextCodePoint() == c; } +bool CodePointIsEndOfWord(CodePoint c) { + return c == '\n' || c == ' ' || c == UCodePointNull; +} + int RemovePreviousGlyph(const char * text, char * location, CodePoint * c) { if (location <= text) { assert(location == text); @@ -364,11 +375,12 @@ size_t StringGlyphLength(const char * s, int maxSize) { } const char * EndOfWord(const char * word) { - assert(UTF8Decoder::CharSizeOfCodePoint(' ') == 1); - assert(UTF8Decoder::CharSizeOfCodePoint('\n') == 1); + UTF8Decoder decoder(word); + CodePoint codePoint = decoder.nextCodePoint(); const char * result = word; - while (*result != ' ' && *result != '\n' && *result != UCodePointNull) { - result++; + while (!CodePointIsEndOfWord(codePoint)) { + result = decoder.stringPosition(); + codePoint = decoder.nextCodePoint(); } return result; }