add support for completion on file names

This commit is contained in:
Axel Kohlmeyer
2023-09-24 16:31:28 -04:00
parent 934269e456
commit 5201e6b2f1
3 changed files with 29 additions and 2 deletions

View File

@ -19,8 +19,10 @@
#include <QAction>
#include <QCompleter>
#include <QDesktopServices>
#include <QDir>
#include <QDragEnterEvent>
#include <QDropEvent>
#include <QFileInfo>
#include <QIcon>
#include <QKeySequence>
#include <QMenu>
@ -131,7 +133,7 @@ CodeEditor::CodeEditor(QWidget *parent) :
minimize_comp(new QCompleter(this)), variable_comp(new QCompleter(this)),
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)
compid_comp(new QCompleter(this)), file_comp(new QCompleter(this)), highlight(NO_HIGHLIGHT)
{
help_action = new QShortcut(QKeySequence::fromString("Ctrl+?"), parent);
connect(help_action, &QShortcut::activated, this, &CodeEditor::get_help);
@ -166,6 +168,7 @@ CodeEditor::CodeEditor(QWidget *parent) :
COMPLETER_SETUP(varname_comp);
COMPLETER_SETUP(fixid_comp);
COMPLETER_SETUP(compid_comp);
COMPLETER_SETUP(file_comp);
#undef COMPLETER_SETUP
// initialize help system
@ -233,6 +236,7 @@ CodeEditor::~CodeEditor()
delete varname_comp;
delete fixid_comp;
delete compid_comp;
delete file_comp;
}
int CodeEditor::lineNumberAreaWidth()
@ -506,6 +510,16 @@ void CodeEditor::setFixIDList()
fixid_comp->setModel(new QStringListModel(fixid, fixid_comp));
}
void CodeEditor::setFileList()
{
QStringList files;
QDir dir(".");
for (const auto &file : dir.entryInfoList(QDir::Files))
files << file.fileName();
files.sort();
file_comp->setModel(new QStringListModel(files, file_comp));
}
void CodeEditor::keyPressEvent(QKeyEvent *event)
{
const auto key = event->key();
@ -801,6 +815,10 @@ 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 ((words[0] == "fitpod") || (words[0] == "include") || (words[0] == "ndx2group") ||
(words[0] == "read_data") || (words[0] == "read_dump") ||
(words[0] == "read_restart") || (words[0] == "rerun"))
current_comp = file_comp;
else if (selected.startsWith("v_"))
current_comp = varname_comp;
else if (selected.startsWith("c_"))
@ -852,6 +870,8 @@ void CodeEditor::runCompletion()
current_comp = fixid_comp;
else if (selected.startsWith("F_"))
current_comp = fixid_comp;
else if ((words[0] == "fitpod") || (words[0] == "molecule"))
current_comp = file_comp;
if (current_comp) {
current_comp->setCompletionPrefix(words[2].c_str());
@ -879,6 +899,8 @@ void CodeEditor::runCompletion()
current_comp = compute_comp;
else if (words[0] == "dump")
current_comp = dump_comp;
else if ((words[0] == "pair_coeff") && (words[1] == "*") && (words[2] == "*"))
current_comp = file_comp;
else if (selected.startsWith("v_"))
current_comp = varname_comp;
else if (selected.startsWith("c_"))

View File

@ -60,6 +60,7 @@ public:
void setVarNameList();
void setComputeIDList();
void setFixIDList();
void setFileList();
static constexpr int NO_HIGHLIGHT = 1 << 30;
@ -87,7 +88,7 @@ private:
QCompleter *current_comp, *command_comp, *fix_comp, *compute_comp, *dump_comp, *atom_comp,
*pair_comp, *bond_comp, *angle_comp, *dihedral_comp, *improper_comp, *kspace_comp,
*region_comp, *integrate_comp, *minimize_comp, *variable_comp, *units_comp, *group_comp,
*varname_comp, *fixid_comp, *compid_comp;
*varname_comp, *fixid_comp, *compid_comp, *file_comp;
int highlight;
bool reformat_on_return;

View File

@ -305,6 +305,8 @@ LammpsGui::LammpsGui(QWidget *parent, const char *filename) :
style_list.sort();
ui->textEdit->setUnitsList(style_list);
ui->textEdit->setFileList();
#define ADD_STYLES(keyword, Type) \
style_list.clear(); \
if ((std::string(#keyword) == "pair") || (std::string(#keyword) == "bond") || \
@ -596,6 +598,7 @@ void LammpsGui::open_file(const QString &fileName)
ui->textEdit->setVarNameList();
ui->textEdit->setComputeIDList();
ui->textEdit->setFixIDList();
ui->textEdit->setFileList();
file.close();
dirstatus->setText(QString(" Directory: ") + current_dir);
status->setText("Ready.");
@ -938,6 +941,7 @@ void LammpsGui::run_done()
QString("Error running LAMMPS:\n\n") + errorbuf);
}
ui->textEdit->setCursor(nline);
ui->textEdit->setFileList();
progress->hide();
dirstatus->show();
}