Skip to content
This repository has been archived by the owner on Oct 17, 2019. It is now read-only.

Commit

Permalink
Add tab-completion for usernames
Browse files Browse the repository at this point in the history
fixes #394
  • Loading branch information
mujx committed Jul 29, 2018
1 parent edf9f52 commit 6ffb747
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 11 deletions.
45 changes: 34 additions & 11 deletions src/TextInputWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ FilteredTextEdit::showResults(const QVector<SearchResult> &results)
{
QPoint pos;

if (atTriggerPosition_ != -1) {
if (isAnchorValid()) {
auto cursor = textCursor();
cursor.setPosition(atTriggerPosition_);
pos = viewport()->mapToGlobal(cursorRect(cursor).topLeft());
Expand All @@ -134,7 +134,7 @@ FilteredTextEdit::keyPressEvent(QKeyEvent *event)
}

// calculate the new query
if (textCursor().position() < atTriggerPosition_ || atTriggerPosition_ == -1) {
if (textCursor().position() < atTriggerPosition_ || !isAnchorValid()) {
resetAnchor();
closeSuggestions();
}
Expand Down Expand Up @@ -165,9 +165,31 @@ FilteredTextEdit::keyPressEvent(QKeyEvent *event)
switch (event->key()) {
case Qt::Key_At:
atTriggerPosition_ = textCursor().position();
anchorType_ = AnchorType::Sigil;

QTextEdit::keyPressEvent(event);
break;
case Qt::Key_Tab: {
auto cursor = textCursor();
const int initialPos = cursor.position();

cursor.movePosition(QTextCursor::StartOfWord, QTextCursor::KeepAnchor);
auto word = cursor.selectedText();

const int startOfWord = cursor.position();

// There is a word to complete.
if (initialPos != startOfWord) {
atTriggerPosition_ = startOfWord;
anchorType_ = AnchorType::Tab;

emit showSuggestions(word);
} else {
QTextEdit::keyPressEvent(event);
}

break;
}
case Qt::Key_Return:
case Qt::Key_Enter:
if (!(event->modifiers() & Qt::ShiftModifier)) {
Expand Down Expand Up @@ -213,26 +235,27 @@ FilteredTextEdit::keyPressEvent(QKeyEvent *event)
default:
QTextEdit::keyPressEvent(event);

// Check if the current word should be autocompleted.
auto cursor = textCursor();
cursor.movePosition(QTextCursor::StartOfWord, QTextCursor::KeepAnchor);
auto word = cursor.selectedText();
if (isModifier)
return;

if (cursor.position() == 0) {
if (textCursor().position() == 0) {
resetAnchor();
closeSuggestions();
return;
}

if (cursor.position() == atTriggerPosition_ + 1) {
const auto q = query();
// Check if the current word should be autocompleted.
auto cursor = textCursor();
cursor.movePosition(QTextCursor::StartOfWord, QTextCursor::KeepAnchor);
auto word = cursor.selectedText();

if (q.isEmpty()) {
if (hasAnchor(cursor.position(), anchorType_) && isAnchorValid()) {
if (word.isEmpty()) {
closeSuggestions();
return;
}

emit showSuggestions(query());
emit showSuggestions(word);
} else {
resetAnchor();
closeSuggestions();
Expand Down
15 changes: 15 additions & 0 deletions src/TextInputWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,23 @@ public slots:

SuggestionsPopup popup_;

enum class AnchorType
{
Tab = 0,
Sigil = 1,
};

AnchorType anchorType_ = AnchorType::Sigil;

int anchorWidth(AnchorType anchor) { return static_cast<int>(anchor); }

void closeSuggestions() { popup_.hide(); }
void resetAnchor() { atTriggerPosition_ = -1; }
bool isAnchorValid() { return atTriggerPosition_ != -1; }
bool hasAnchor(int pos, AnchorType anchor)
{
return pos == atTriggerPosition_ + anchorWidth(anchor);
}

QString query()
{
Expand Down

0 comments on commit 6ffb747

Please sign in to comment.