Skip to content

Commit

Permalink
libshell|AbstractLineEditor: Adjusted behavior of autocompletion
Browse files Browse the repository at this point in the history
The first press of Tab only sends the autocompletion notification if
there is no unambiguous completion available. Further presses send
the notification and then on subsequent presses start cycling through
the possible completions.

The objective here is to avoid showing the autocompletion popup
unnecessarily/too early.
  • Loading branch information
skyjake committed Aug 5, 2013
1 parent 0e1c565 commit a8d7e11
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 22 deletions.
2 changes: 1 addition & 1 deletion doomsday/client/include/ui/widgets/consolecommandwidget.h
Expand Up @@ -43,7 +43,7 @@ class ConsoleCommandWidget : public LineEditWidget
bool handleEvent(de::Event const &event);

protected:
void autoCompletionBegan();
void autoCompletionBegan(de::String const &prefix);
void autoCompletionEnded(bool accepted);

signals:
Expand Down
2 changes: 1 addition & 1 deletion doomsday/client/src/ui/widgets/consolecommandwidget.cpp
Expand Up @@ -162,7 +162,7 @@ bool ConsoleCommandWidget::handleEvent(Event const &event)
return false;
}

void ConsoleCommandWidget::autoCompletionBegan()
void ConsoleCommandWidget::autoCompletionBegan(String const &)
{
// Prepare a list of annotated completions to show in the popup.
QStringList const compls = suggestedCompletions();
Expand Down
2 changes: 1 addition & 1 deletion doomsday/libshell/include/de/shell/abstractlineeditor.h
Expand Up @@ -127,7 +127,7 @@ class LIBSHELL_PUBLIC AbstractLineEditor : public ITextEditor
virtual void numberOfLinesChanged(int lineCount) = 0;
virtual void cursorMoved() = 0;
virtual void contentChanged() = 0;
virtual void autoCompletionBegan();
virtual void autoCompletionBegan(String const &wordBase);
virtual void autoCompletionEnded(bool accepted);

enum LineWrapUpdateBehavior {
Expand Down
66 changes: 47 additions & 19 deletions doomsday/libshell/src/abstractlineeditor.cpp
Expand Up @@ -50,12 +50,16 @@ DENG2_PIMPL(AbstractLineEditor)
};
Completion completion;
QStringList suggestions;
bool suggesting;
bool completionNotified;

Instance(Public *i, ILineWrapping *lineWraps)
: Base(i),
cursor(0),
echoMode(NormalEchoMode),
wraps(lineWraps)
wraps(lineWraps),
suggesting(false),
completionNotified(false)
{
// Initialize line wrapping.
completion.reset();
Expand Down Expand Up @@ -158,11 +162,8 @@ DENG2_PIMPL(AbstractLineEditor)

void doBackspace()
{
if(suggestingCompletion())
{
rejectCompletion();
if(rejectCompletion())
return;
}

if(!text.isEmpty() && cursor > 0)
{
Expand Down Expand Up @@ -264,17 +265,23 @@ DENG2_PIMPL(AbstractLineEditor)

bool suggestingCompletion() const
{
return completion.size > 0;
return suggesting;
//return completion.size > 0;
}

String wordBehindCursor() const
String wordBehindPos(int pos) const
{
String word;
int i = cursor - 1;
int i = pos - 1;
while(i >= 0 && lexicon.isWordChar(text[i])) word.prepend(text[i--]);
return word;
}

String wordBehindCursor() const
{
return wordBehindPos(cursor);
}

QStringList completionsForBase(String base, String &commonPrefix) const
{
bool first = true;
Expand Down Expand Up @@ -306,6 +313,7 @@ DENG2_PIMPL(AbstractLineEditor)
{
if(!suggestingCompletion())
{
completionNotified = false;
String const base = wordBehindCursor();
if(!base.isEmpty())
{
Expand All @@ -322,26 +330,37 @@ DENG2_PIMPL(AbstractLineEditor)
text.insert(cursor, commonPrefix);
cursor += completion.size;
rewrapNow();
self.autoCompletionBegan();
suggesting = true;
return true;
}
if(!suggestions.isEmpty())
{
completion.ordinal = (forwardCycle? 0 : suggestions.size() - 1);
String comp = suggestions[completion.ordinal];
comp.remove(0, base.size());
completion.ordinal = -1; //(forwardCycle? 0 : suggestions.size() - 1);
/*String comp = suggestions[completion.ordinal];
comp.remove(0, base.size());*/
completion.pos = cursor;
completion.size = comp.size();
text.insert(cursor, comp);
cursor += completion.size;
rewrapNow();
self.autoCompletionBegan();
completion.size = 0; //comp.size();
//text.insert(cursor, comp);
//cursor += completion.size;
//rewrapNow();
suggesting = true;
// Notify immediately.
self.autoCompletionBegan(base);
completionNotified = true;
return true;
}
}
}
else
{
if(!completionNotified)
{
// Time to notify now.
self.autoCompletionBegan(wordBehindPos(completion.pos));
completionNotified = true;
return true;
}

// Replace the current completion with another suggestion.
cursor = completion.pos;
String const base = wordBehindCursor();
Expand Down Expand Up @@ -371,6 +390,7 @@ DENG2_PIMPL(AbstractLineEditor)
completion.size = comp.size();
cursor = completion.pos + completion.size;
rewrapNow();

return true;
}
return false;
Expand All @@ -386,6 +406,8 @@ DENG2_PIMPL(AbstractLineEditor)
{
completion.reset();
suggestions.clear();
suggesting = false;
completionNotified = false;
}

void acceptCompletion()
Expand All @@ -397,14 +419,20 @@ DENG2_PIMPL(AbstractLineEditor)
self.autoCompletionEnded(true);
}

void rejectCompletion()
bool rejectCompletion()
{
if(!suggestingCompletion()) return false;

int oldCursor = cursor;

text.remove(completion.pos, completion.size);
cursor = completion.pos;
resetCompletion();
rewrapNow();

self.autoCompletionEnded(false);

return cursor != oldCursor; // cursor was moved as part of the rejection
}
};

Expand Down Expand Up @@ -609,7 +637,7 @@ ILineWrapping &AbstractLineEditor::lineWraps()
return *d->wraps;
}

void AbstractLineEditor::autoCompletionBegan()
void AbstractLineEditor::autoCompletionBegan(String const &)
{}

void AbstractLineEditor::autoCompletionEnded(bool /*accepted*/)
Expand Down

0 comments on commit a8d7e11

Please sign in to comment.