Add emojis to autocomplete
Improve autocomplete: - Add space after autocompleted item - Emotes starting with `:` can now be autocompleted properly (i.e. emojis)
This commit is contained in:
@@ -29,11 +29,41 @@ int ResizingTextEdit::heightForWidth(int) const
|
||||
return margins.top() + document()->size().height() + margins.bottom() + 5;
|
||||
}
|
||||
|
||||
QString ResizingTextEdit::textUnderCursor() const
|
||||
QString ResizingTextEdit::textUnderCursor(bool *hadSpace) const
|
||||
{
|
||||
QTextCursor tc = textCursor();
|
||||
tc.select(QTextCursor::WordUnderCursor);
|
||||
return tc.selectedText();
|
||||
auto currentText = this->toPlainText();
|
||||
|
||||
QTextCursor tc = this->textCursor();
|
||||
|
||||
auto textUpToCursor = currentText.left(tc.selectionStart());
|
||||
|
||||
auto words = textUpToCursor.splitRef(' ');
|
||||
if (words.size() == 0) {
|
||||
return QString();
|
||||
}
|
||||
|
||||
bool first = true;
|
||||
QString lastWord;
|
||||
for (auto it = words.crbegin(); it != words.crend(); ++it) {
|
||||
auto word = *it;
|
||||
|
||||
if (first && word.isEmpty()) {
|
||||
first = false;
|
||||
if (hadSpace != nullptr) {
|
||||
*hadSpace = true;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
lastWord = word.toString();
|
||||
break;
|
||||
}
|
||||
|
||||
if (lastWord.isEmpty()) {
|
||||
return QString();
|
||||
}
|
||||
|
||||
return lastWord;
|
||||
}
|
||||
|
||||
void ResizingTextEdit::keyPressEvent(QKeyEvent *event)
|
||||
@@ -105,8 +135,17 @@ void ResizingTextEdit::insertCompletion(const QString &completion)
|
||||
return;
|
||||
}
|
||||
|
||||
bool hadSpace = false;
|
||||
auto prefix = this->textUnderCursor(&hadSpace);
|
||||
|
||||
int prefixSize = prefix.size();
|
||||
|
||||
if (hadSpace) {
|
||||
++prefixSize;
|
||||
}
|
||||
|
||||
QTextCursor tc = this->textCursor();
|
||||
tc.select(QTextCursor::SelectionType::WordUnderCursor);
|
||||
tc.movePosition(QTextCursor::PreviousCharacter, QTextCursor::KeepAnchor, prefixSize);
|
||||
tc.insertText(completion);
|
||||
this->setTextCursor(tc);
|
||||
}
|
||||
|
||||
@@ -25,7 +25,10 @@ protected:
|
||||
|
||||
private:
|
||||
QCompleter *completer = nullptr;
|
||||
QString textUnderCursor() const;
|
||||
|
||||
// hadSpace is set to true in case the "textUnderCursor" word was after a space
|
||||
QString textUnderCursor(bool *hadSpace = nullptr) const;
|
||||
|
||||
bool nextCompletion = false;
|
||||
|
||||
private slots:
|
||||
|
||||
Reference in New Issue
Block a user