add completion for compute and fix ID references

This commit is contained in:
Axel Kohlmeyer
2023-09-08 10:43:31 -04:00
parent 778263673c
commit f8e65b19a2
3 changed files with 100 additions and 7 deletions

View File

@ -294,8 +294,10 @@ QString CodeEditor::reformatLine(const QString &line)
int namesize = settings.value("name", "8").toInt(); int namesize = settings.value("name", "8").toInt();
settings.endGroup(); settings.endGroup();
bool rebuildGroupComp = false; bool rebuildGroupComp = false;
bool rebuildVarNameComp = false; bool rebuildVarNameComp = false;
bool rebuildComputeIDComp = false;
bool rebuildFixIDComp = false;
if (words.size()) { if (words.size()) {
// commented line. do nothing // commented line. do nothing
@ -310,6 +312,10 @@ QString CodeEditor::reformatLine(const QString &line)
if (words[0] == "group") rebuildGroupComp = true; if (words[0] == "group") rebuildGroupComp = true;
// new/updated variable command -> update completer // new/updated variable command -> update completer
if (words[0] == "variable") rebuildVarNameComp = true; if (words[0] == "variable") rebuildVarNameComp = true;
// new/updated compute command -> update completer
if (words[0] == "compute") rebuildComputeIDComp = true;
// new/updated fix command -> update completer
if (words[0] == "fix") rebuildFixIDComp = true;
} }
// append remaining words with just a single blank added. // append remaining words with just a single blank added.
@ -348,6 +354,8 @@ QString CodeEditor::reformatLine(const QString &line)
} }
if (rebuildGroupComp) setGroupList(); if (rebuildGroupComp) setGroupList();
if (rebuildVarNameComp) setVarNameList(); if (rebuildVarNameComp) setVarNameList();
if (rebuildComputeIDComp) setComputeIDList();
if (rebuildFixIDComp) setFixIDList();
return newtext; return newtext;
} }
@ -434,6 +442,54 @@ void CodeEditor::setVarNameList()
varname_comp->setModel(new QStringListModel(vars, varname_comp)); varname_comp->setModel(new QStringListModel(vars, varname_comp));
} }
void CodeEditor::setComputeIDList()
{
QStringList compid;
QRegularExpression compcmd(QStringLiteral("^\\s*compute\\s+(\\S+)\\s+"));
auto saved = textCursor();
// reposition cursor to beginning of text and search for group commands
auto cursor = textCursor();
cursor.movePosition(QTextCursor::Start);
setTextCursor(cursor);
while (find(compcmd)) {
auto words = textCursor().block().text().replace('\t', ' ').split(' ', Qt::SkipEmptyParts);
if ((words.size() > 1)) {
QString w = QString("c_%1").arg(words[1]);
if (!compid.contains(w)) compid << w;
w = QString("C_%1").arg(words[1]);
if (!compid.contains(w)) compid << w;
}
}
compid.sort();
setTextCursor(saved);
compid_comp->setModel(new QStringListModel(compid, compid_comp));
}
void CodeEditor::setFixIDList()
{
QStringList fixid;
QRegularExpression fixcmd(QStringLiteral("^\\s*fix\\s+(\\S+)\\s+"));
auto saved = textCursor();
// reposition cursor to beginning of text and search for group commands
auto cursor = textCursor();
cursor.movePosition(QTextCursor::Start);
setTextCursor(cursor);
while (find(fixcmd)) {
auto words = textCursor().block().text().replace('\t', ' ').split(' ', Qt::SkipEmptyParts);
if ((words.size() > 1)) {
QString w = QString("f_%1").arg(words[1]);
if (!fixid.contains(w)) fixid << w;
w = QString("F_%1").arg(words[1]);
if (!fixid.contains(w)) fixid << w;
}
}
fixid.sort();
setTextCursor(saved);
fixid_comp->setModel(new QStringListModel(fixid, fixid_comp));
}
void CodeEditor::keyPressEvent(QKeyEvent *event) void CodeEditor::keyPressEvent(QKeyEvent *event)
{ {
const auto key = event->key(); const auto key = event->key();
@ -485,7 +541,7 @@ void CodeEditor::keyPressEvent(QKeyEvent *event)
if (line[begin].isSpace()) break; if (line[begin].isSpace()) break;
--begin; --begin;
} }
if (((cursor.positionInBlock() - begin) > 2) || (line[begin+1] == '$')) runCompletion(); if (((cursor.positionInBlock() - begin) > 2) || (line[begin + 1] == '$')) runCompletion();
} }
} }
@ -731,6 +787,14 @@ void CodeEditor::runCompletion()
current_comp = group_comp; current_comp = group_comp;
else if (selected.startsWith("v_")) else if (selected.startsWith("v_"))
current_comp = varname_comp; current_comp = varname_comp;
else if (selected.startsWith("c_"))
current_comp = compid_comp;
else if (selected.startsWith("C_"))
current_comp = compid_comp;
else if (selected.startsWith("f_"))
current_comp = fixid_comp;
else if (selected.startsWith("F_"))
current_comp = fixid_comp;
if (current_comp) { if (current_comp) {
current_comp->setCompletionPrefix(words[1].c_str()); current_comp->setCompletionPrefix(words[1].c_str());
@ -764,6 +828,14 @@ void CodeEditor::runCompletion()
current_comp = group_comp; current_comp = group_comp;
else if (selected.startsWith("v_")) else if (selected.startsWith("v_"))
current_comp = varname_comp; current_comp = varname_comp;
else if (selected.startsWith("c_"))
current_comp = compid_comp;
else if (selected.startsWith("C_"))
current_comp = compid_comp;
else if (selected.startsWith("f_"))
current_comp = fixid_comp;
else if (selected.startsWith("F_"))
current_comp = fixid_comp;
if (current_comp) { if (current_comp) {
current_comp->setCompletionPrefix(words[2].c_str()); current_comp->setCompletionPrefix(words[2].c_str());
@ -793,6 +865,14 @@ void CodeEditor::runCompletion()
current_comp = dump_comp; current_comp = dump_comp;
else if (selected.startsWith("v_")) else if (selected.startsWith("v_"))
current_comp = varname_comp; current_comp = varname_comp;
else if (selected.startsWith("c_"))
current_comp = compid_comp;
else if (selected.startsWith("C_"))
current_comp = compid_comp;
else if (selected.startsWith("f_"))
current_comp = fixid_comp;
else if (selected.startsWith("F_"))
current_comp = fixid_comp;
if (current_comp) { if (current_comp) {
current_comp->setCompletionPrefix(words[3].c_str()); current_comp->setCompletionPrefix(words[3].c_str());
@ -809,15 +889,24 @@ void CodeEditor::runCompletion()
current_comp->complete(cr); current_comp->complete(cr);
} }
// reference located anywhere further right in the line // reference located anywhere further right in the line
} else if (words.size() > 3) { } else if (words.size() > 4) {
current_comp = nullptr; current_comp = nullptr;
if (selected.startsWith("v_")) current_comp = varname_comp; if (selected.startsWith("v_"))
current_comp = varname_comp;
else if (selected.startsWith("c_"))
current_comp = compid_comp;
else if (selected.startsWith("C_"))
current_comp = compid_comp;
else if (selected.startsWith("f_"))
current_comp = fixid_comp;
else if (selected.startsWith("F_"))
current_comp = fixid_comp;
if (current_comp) { if (current_comp) {
current_comp->setCompletionPrefix(words[3].c_str()); current_comp->setCompletionPrefix(selected);
auto popup = current_comp->popup(); auto popup = current_comp->popup();
// if the command is already a complete command, remove existing popup // if the command is already a complete command, remove existing popup
if (words[3] == current_comp->currentCompletion().toStdString()) { if (selected == current_comp->currentCompletion()) {
if (popup->isVisible()) popup->hide(); if (popup->isVisible()) popup->hide();
return; return;
} }

View File

@ -58,6 +58,8 @@ public:
void setUnitsList(const QStringList &words); void setUnitsList(const QStringList &words);
void setGroupList(); void setGroupList();
void setVarNameList(); void setVarNameList();
void setComputeIDList();
void setFixIDList();
static constexpr int NO_HIGHLIGHT = 1 << 30; static constexpr int NO_HIGHLIGHT = 1 << 30;

View File

@ -629,6 +629,8 @@ void LammpsGui::open_file(const QString &fileName)
ui->textEdit->document()->setModified(false); ui->textEdit->document()->setModified(false);
ui->textEdit->setGroupList(); ui->textEdit->setGroupList();
ui->textEdit->setVarNameList(); ui->textEdit->setVarNameList();
ui->textEdit->setComputeIDList();
ui->textEdit->setFixIDList();
file.close(); file.close();
dirstatus->setText(QString(" Directory: ") + current_dir); dirstatus->setText(QString(" Directory: ") + current_dir);
status->setText("Ready."); status->setText("Ready.");