From 5674786acd9574a52b9149966043c246baddc7c0 Mon Sep 17 00:00:00 2001 From: Axel Kohlmeyer Date: Mon, 30 Oct 2023 15:25:06 -0400 Subject: [PATCH] add feature to comment out and uncomment lines and selections from context menu --- tools/lammps-gui/CMakeLists.txt | 2 +- tools/lammps-gui/codeeditor.cpp | 96 +++++++++++++++++++++++++++++++-- tools/lammps-gui/codeeditor.h | 4 ++ 3 files changed, 98 insertions(+), 4 deletions(-) diff --git a/tools/lammps-gui/CMakeLists.txt b/tools/lammps-gui/CMakeLists.txt index e83db05fdd..caae722865 100644 --- a/tools/lammps-gui/CMakeLists.txt +++ b/tools/lammps-gui/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required(VERSION 3.16) -project(lammps-gui VERSION 1.5.9 LANGUAGES CXX) +project(lammps-gui VERSION 1.5.10 LANGUAGES CXX) set(CMAKE_AUTOUIC ON) set(CMAKE_AUTOMOC ON) diff --git a/tools/lammps-gui/codeeditor.cpp b/tools/lammps-gui/codeeditor.cpp index 672c009c35..ab8007805a 100644 --- a/tools/lammps-gui/codeeditor.cpp +++ b/tools/lammps-gui/codeeditor.cpp @@ -34,6 +34,7 @@ #include #include #include +#include #include #include @@ -424,7 +425,7 @@ void CodeEditor::setVarNameList() LammpsWrapper *lammps = &qobject_cast(parent())->lammps; int nvar = lammps->id_count("variable"); - constexpr int BUFLEN = 256; + constexpr int BUFLEN = 256; char buffer[BUFLEN]; for (int i = 0; i < nvar; ++i) { memset(buffer, 0, BUFLEN); @@ -683,17 +684,34 @@ void CodeEditor::contextMenuEvent(QContextMenuEvent *event) QString page, help; find_help(page, help); - // print augmented context menu if an entry was found auto *menu = createStandardContextMenu(); menu->addSeparator(); + if (textCursor().hasSelection()) { + auto action1 = menu->addAction("Comment out selection"); + action1->setIcon(QIcon(":/icons/expand-text.png")); + connect(action1, &QAction::triggered, this, &CodeEditor::comment_selection); + auto action2 = menu->addAction("Uncomment selection"); + action2->setIcon(QIcon(":/icons/expand-text.png")); + connect(action2, &QAction::triggered, this, &CodeEditor::uncomment_selection); + } else { + auto action1 = menu->addAction("Comment out line"); + action1->setIcon(QIcon(":/icons/expand-text.png")); + connect(action1, &QAction::triggered, this, &CodeEditor::comment_line); + auto action2 = menu->addAction("Uncomment line"); + action2->setIcon(QIcon(":/icons/expand-text.png")); + connect(action2, &QAction::triggered, this, &CodeEditor::uncomment_line); + } + menu->addSeparator(); + + // print augmented context menu if an entry was found if (!help.isEmpty()) { auto action = menu->addAction(QString("Display available completions for '%1'").arg(help)); action->setIcon(QIcon(":/icons/expand-text.png")); connect(action, &QAction::triggered, this, &CodeEditor::runCompletion); + menu->addSeparator(); } if (!page.isEmpty()) { - menu->addSeparator(); auto action = menu->addAction(QString("Reformat '%1' command").arg(help)); action->setIcon(QIcon(":/icons/format-indent-less-3.png")); connect(action, &QAction::triggered, this, &CodeEditor::reformatCurrentLine); @@ -741,6 +759,78 @@ void CodeEditor::reformatCurrentLine() } } +void CodeEditor::comment_line() +{ + auto cursor = textCursor(); + cursor.movePosition(QTextCursor::StartOfLine); + cursor.insertText("#"); +} + +void CodeEditor::comment_selection() +{ + auto cursor = textCursor(); + auto text = cursor.selection().toPlainText(); + auto lines = text.split('\n'); + QString newtext; + for (auto line : lines) { + newtext.append('#'); + newtext.append(line); + newtext.append('\n'); + } + if (newtext.isEmpty()) newtext = "#\n"; + cursor.insertText(newtext); + setTextCursor(cursor); +} + +void CodeEditor::uncomment_selection() +{ + auto cursor = textCursor(); + auto text = cursor.selection().toPlainText(); + auto lines = text.split('\n'); + QString newtext; + for (auto line : lines) { + QString newline; + bool start = true; + for (auto letter : line) { + if (start && (letter == '#')) { + start = false; + continue; + } + if (start && !letter.isSpace()) start = false; + newline.append(letter); + } + newtext.append(newline); + newtext.append('\n'); + } + cursor.insertText(newtext); + setTextCursor(cursor); +} + +void CodeEditor::uncomment_line() +{ + auto cursor = textCursor(); + auto text = cursor.block().text(); + QString newtext; + bool start = true; + for (auto letter : text) { + if (start && (letter == '#')) { + start = false; + continue; + } + if (start && !letter.isSpace()) start = false; + newtext.append(letter); + } + + // perform edit but only if text has changed + if (QString::compare(text, newtext)) { + cursor.beginEditBlock(); + cursor.movePosition(QTextCursor::StartOfLine); + cursor.movePosition(QTextCursor::EndOfLine, QTextCursor::KeepAnchor, 1); + cursor.insertText(newtext); + cursor.endEditBlock(); + } +} + void CodeEditor::runCompletion() { QAbstractItemView *popup = nullptr; diff --git a/tools/lammps-gui/codeeditor.h b/tools/lammps-gui/codeeditor.h index 703b2ffd3c..744953af06 100644 --- a/tools/lammps-gui/codeeditor.h +++ b/tools/lammps-gui/codeeditor.h @@ -81,6 +81,10 @@ private slots: void reformatCurrentLine(); void runCompletion(); void insertCompletedCommand(const QString &completion); + void comment_selection(); + void uncomment_selection(); + void comment_line(); + void uncomment_line(); private: QWidget *lineNumberArea;