add completion for variables and variable references

This commit is contained in:
Axel Kohlmeyer
2023-09-08 08:53:06 -04:00
parent f293c5dd87
commit 778263673c
3 changed files with 64 additions and 6 deletions

View File

@ -129,7 +129,9 @@ CodeEditor::CodeEditor(QWidget *parent) :
improper_comp(new QCompleter(this)), kspace_comp(new QCompleter(this)),
region_comp(new QCompleter(this)), integrate_comp(new QCompleter(this)),
minimize_comp(new QCompleter(this)), variable_comp(new QCompleter(this)),
units_comp(new QCompleter(this)), group_comp(new QCompleter(this)), highlight(NO_HIGHLIGHT)
units_comp(new QCompleter(this)), group_comp(new QCompleter(this)),
varname_comp(new QCompleter(this)), fixid_comp(new QCompleter(this)),
compid_comp(new QCompleter(this)), highlight(NO_HIGHLIGHT)
{
help_action = new QShortcut(QKeySequence::fromString("Ctrl+?"), parent);
connect(help_action, &QShortcut::activated, this, &CodeEditor::get_help);
@ -161,6 +163,9 @@ CodeEditor::CodeEditor(QWidget *parent) :
COMPLETER_SETUP(variable_comp);
COMPLETER_SETUP(units_comp);
COMPLETER_SETUP(group_comp);
COMPLETER_SETUP(varname_comp);
COMPLETER_SETUP(fixid_comp);
COMPLETER_SETUP(compid_comp);
#undef COMPLETER_SETUP
// initialize help system
@ -225,6 +230,9 @@ CodeEditor::~CodeEditor()
delete variable_comp;
delete units_comp;
delete group_comp;
delete varname_comp;
delete fixid_comp;
delete compid_comp;
}
int CodeEditor::lineNumberAreaWidth()
@ -286,7 +294,8 @@ QString CodeEditor::reformatLine(const QString &line)
int namesize = settings.value("name", "8").toInt();
settings.endGroup();
bool rebuildGroupComp = false;
bool rebuildGroupComp = false;
bool rebuildVarNameComp = false;
if (words.size()) {
// commented line. do nothing
@ -299,6 +308,8 @@ QString CodeEditor::reformatLine(const QString &line)
newtext += ' ';
// new/updated group command -> update completer
if (words[0] == "group") rebuildGroupComp = true;
// new/updated variable command -> update completer
if (words[0] == "variable") rebuildVarNameComp = true;
}
// append remaining words with just a single blank added.
@ -336,6 +347,7 @@ QString CodeEditor::reformatLine(const QString &line)
}
}
if (rebuildGroupComp) setGroupList();
if (rebuildVarNameComp) setVarNameList();
return newtext;
}
@ -473,7 +485,7 @@ void CodeEditor::keyPressEvent(QKeyEvent *event)
if (line[begin].isSpace()) break;
--begin;
}
if ((cursor.positionInBlock() - begin) > 2) runCompletion();
if (((cursor.positionInBlock() - begin) > 2) || (line[begin+1] == '$')) runCompletion();
}
}
@ -649,8 +661,26 @@ void CodeEditor::runCompletion()
}
const auto selected = line.mid(begin, end - begin);
// if on first word, try to complete command
if ((words.size() > 0) && (words[0] == selected.toStdString())) {
// variable expansion may be anywhere
if (selected.startsWith("$")) {
current_comp = varname_comp;
current_comp->setCompletionPrefix(selected);
auto popup = current_comp->popup();
// if the command is already a complete command, remove existing popup
if (selected == current_comp->currentCompletion()) {
if (popup->isVisible()) {
popup->hide();
current_comp = nullptr;
}
return;
}
QRect cr = cursorRect();
cr.setWidth(popup->sizeHintForColumn(0) + popup->verticalScrollBar()->sizeHint().width());
popup->setAlternatingRowColors(true);
current_comp->complete(cr);
// if on first word, try to complete command
} else if ((words.size() > 0) && (words[0] == selected.toStdString())) {
// no completion on comment lines
if (words[0][0] == '#') return;
@ -699,6 +729,8 @@ void CodeEditor::runCompletion()
else if ((words[0] == "change_box") || (words[0] == "displace_atoms") ||
(words[0] == "velocity") || (words[0] == "write_dump"))
current_comp = group_comp;
else if (selected.startsWith("v_"))
current_comp = varname_comp;
if (current_comp) {
current_comp->setCompletionPrefix(words[1].c_str());
@ -730,6 +762,8 @@ void CodeEditor::runCompletion()
current_comp = group_comp;
else if (words[0] == "dump")
current_comp = group_comp;
else if (selected.startsWith("v_"))
current_comp = varname_comp;
if (current_comp) {
current_comp->setCompletionPrefix(words[2].c_str());
@ -757,6 +791,27 @@ void CodeEditor::runCompletion()
current_comp = compute_comp;
else if (words[0] == "dump")
current_comp = dump_comp;
else if (selected.startsWith("v_"))
current_comp = varname_comp;
if (current_comp) {
current_comp->setCompletionPrefix(words[3].c_str());
auto popup = current_comp->popup();
// if the command is already a complete command, remove existing popup
if (words[3] == current_comp->currentCompletion().toStdString()) {
if (popup->isVisible()) popup->hide();
return;
}
QRect cr = cursorRect();
cr.setWidth(popup->sizeHintForColumn(0) +
popup->verticalScrollBar()->sizeHint().width());
popup->setAlternatingRowColors(true);
current_comp->complete(cr);
}
// reference located anywhere further right in the line
} else if (words.size() > 3) {
current_comp = nullptr;
if (selected.startsWith("v_")) current_comp = varname_comp;
if (current_comp) {
current_comp->setCompletionPrefix(words[3].c_str());