[apps/code] Autocomplete only if char before cursor is not ' ' or '\n'

This commit is contained in:
Léa Saviot
2020-03-13 16:51:28 +01:00
committed by Émilie Feral
parent a2cc923a02
commit f82079a40a
3 changed files with 32 additions and 11 deletions

View File

@@ -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<char *>(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<char *>(cursorLocation()))) {
if (textToInsert && m_contentView.insertTextAtLocation(textToInsert, const_cast<char *>(autocompletionLocation))) {
m_contentView.setAutocompleting(true);
}
}

View File

@@ -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);

View File

@@ -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;
}