add feature to comment out and uncomment lines and selections from context menu
This commit is contained in:
@ -34,6 +34,7 @@
|
||||
#include <QShortcut>
|
||||
#include <QStringListModel>
|
||||
#include <QTextBlock>
|
||||
#include <QTextDocumentFragment>
|
||||
#include <QUrl>
|
||||
|
||||
#include <string>
|
||||
@ -424,7 +425,7 @@ void CodeEditor::setVarNameList()
|
||||
|
||||
LammpsWrapper *lammps = &qobject_cast<LammpsGui *>(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;
|
||||
|
||||
Reference in New Issue
Block a user