Show suggestions containing the input in the command buffer

Previously, only the suggestions starting with the current input were
shown.

Contains the following minor improvements:
- Add space to indicate when only one suggestion is found

- Select first item in suggestion-list in order to make selecting with
  arrow keys more intuitive

- Fix selection with Shift+Up/Down in text-field

Fix for bug #12572.
This commit is contained in:
Daniel Ramoeller 2022-08-06 07:13:51 +02:00 committed by Jean-Marc Lasgouttes
parent fad170be1a
commit 89394bcd0f
2 changed files with 31 additions and 18 deletions

View File

@ -184,9 +184,12 @@ void GuiCommandBuffer::complete()
string new_input; string new_input;
vector<string> const & comp = completions(input, new_input); vector<string> const & comp = completions(input, new_input);
if (comp.empty()) { if (comp.empty() || comp.size() == 1) {
if (new_input != input) if (new_input != input)
edit_->setText(toqstr(new_input)); edit_->setText(toqstr(new_input));
// If there is only one match, indicate this by adding a space
if (comp.size() == 1)
edit_->setText(edit_->text() + " ");
return; return;
} }
@ -208,6 +211,8 @@ void GuiCommandBuffer::showList(vector<string> const & list,
else else
listBox->addItem(toqstr(item)); listBox->addItem(toqstr(item));
} }
// Select the first item
listBox->setCurrentItem(listBox->item(0));
listBox->resize(listBox->sizeHint()); listBox->resize(listBox->sizeHint());
@ -296,6 +301,11 @@ GuiCommandBuffer::completions(string const & prefix, string & new_prefix)
if (prefixIs(act.first, prefix)) if (prefixIs(act.first, prefix))
comp.push_back(act.first); comp.push_back(act.first);
} }
// now add all the other items that contain the prefix
for (auto const & act : lyxaction) {
if (!prefixIs(act.first, prefix) && contains(act.first, prefix))
comp.push_back(act.first);
}
if (comp.empty()) { if (comp.empty()) {
new_prefix = prefix; new_prefix = prefix;
@ -304,7 +314,7 @@ GuiCommandBuffer::completions(string const & prefix, string & new_prefix)
if (comp.size() == 1) { if (comp.size() == 1) {
new_prefix = comp[0]; new_prefix = comp[0];
return vector<string>(); return comp;
} }
// find maximal available prefix // find maximal available prefix

View File

@ -29,27 +29,30 @@ GuiCommandEdit::GuiCommandEdit(QWidget * parent)
void GuiCommandEdit::keyPressEvent(QKeyEvent * e) void GuiCommandEdit::keyPressEvent(QKeyEvent * e)
{ {
if (e->modifiers() == Qt::NoModifier) {
switch (e->key()) { switch (e->key()) {
case Qt::Key_Escape: case Qt::Key_Escape:
// emit signal // emit signal
escapePressed(); escapePressed();
break; return;
case Qt::Key_Up: case Qt::Key_Up:
// emit signal // emit signal
upPressed(); upPressed();
break; return;
case Qt::Key_Down: case Qt::Key_Down:
// emit signal // emit signal
downPressed(); downPressed();
break; return;
default: default:
QLineEdit::keyPressEvent(e); // do nothing
break; break;
} }
} }
QLineEdit::keyPressEvent(e);
}
bool GuiCommandEdit::event(QEvent * e) bool GuiCommandEdit::event(QEvent * e)