From 1e2492c5f4e51f275df2d2720d89fa92aabd51f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9a=20Saviot?= Date: Tue, 17 Mar 2020 16:02:48 +0100 Subject: [PATCH] [apps/code] Autocomplete only at the end of the word For instance, if the cursor is: "he|llo" and the user adds the letter 'i', there is no autocompletion provided as the cursor is still in the middle of a word. --- apps/code/python_text_area.cpp | 6 ++++-- ion/include/ion/unicode/utf8_helper.h | 1 + ion/src/shared/unicode/utf8_helper.cpp | 8 ++++++-- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/apps/code/python_text_area.cpp b/apps/code/python_text_area.cpp index bf5e8c28c..54b7eeaf3 100644 --- a/apps/code/python_text_area.cpp +++ b/apps/code/python_text_area.cpp @@ -246,9 +246,11 @@ void PythonTextArea::addAutocompletion() { const char * autocompletionLocation = const_cast(cursorLocation()); const char * textToInsert = nullptr; CodePoint prevCodePoint = UTF8Helper::PreviousCodePoint(m_contentView.editedText(), autocompletionLocation); - if (!UTF8Helper::CodePointIsEndOfWord(prevCodePoint)) { + if (!UTF8Helper::CodePointIsEndOfWord(prevCodePoint) + && UTF8Helper::CodePointIsEndOfWord(UTF8Helper::CodePointAtLocation(autocompletionLocation))) + { /* The previous code point is neither the beginning of the text, nor a - * space, nor a \n. + * space, nor a \n, and the next code point is the end of the word. * Compute the text to insert */ // TODO LEA textToInsert = "test"; diff --git a/ion/include/ion/unicode/utf8_helper.h b/ion/include/ion/unicode/utf8_helper.h index 32ef6f042..8b20a926f 100644 --- a/ion/include/ion/unicode/utf8_helper.h +++ b/ion/include/ion/unicode/utf8_helper.h @@ -67,6 +67,7 @@ const char * PerformAtCodePoints( const char * stoppingPosition = nullptr); CodePoint PreviousCodePoint(const char * buffer, const char * location); // returns 0 if location == buffer +CodePoint CodePointAtLocation(const char * location); bool PreviousCodePointIs(const char * buffer, const char * location, CodePoint c); bool CodePointIs(const char * location, CodePoint c); bool CodePointIsEndOfWord(CodePoint c); diff --git a/ion/src/shared/unicode/utf8_helper.cpp b/ion/src/shared/unicode/utf8_helper.cpp index 3f0d44f36..7ddc93f7c 100644 --- a/ion/src/shared/unicode/utf8_helper.cpp +++ b/ion/src/shared/unicode/utf8_helper.cpp @@ -265,6 +265,11 @@ CodePoint PreviousCodePoint(const char * buffer, const char * location) { return decoder.previousCodePoint(); } +CodePoint CodePointAtLocation(const char * location) { + UTF8Decoder decoder(location); + return decoder.nextCodePoint(); +} + bool PreviousCodePointIs(const char * buffer, const char * location, CodePoint c) { assert(location > buffer); if (UTF8Decoder::CharSizeOfCodePoint(c) == 1) { @@ -277,8 +282,7 @@ bool CodePointIs(const char * location, CodePoint c) { if (UTF8Decoder::CharSizeOfCodePoint(c) == 1) { return *(location) == c; } - UTF8Decoder decoder(location); - return decoder.nextCodePoint() == c; + return CodePointAtLocation(location) == c; } bool CodePointIsEndOfWord(CodePoint c) {