diff --git a/.gitmodules b/.gitmodules index 676b82923..612a461c2 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,6 +1,6 @@ [submodule "third_party/QCodeEditor"] path = third_party/QCodeEditor - url = https://github.com/Megaxela/QCodeEditor + url = https://github.com/ouuan/QCodeEditor [submodule "third_party/singleapplication"] path = third_party/singleapplication url = https://github.com/itay-grudev/SingleApplication.git diff --git a/CMakeLists.txt b/CMakeLists.txt index 6ef5bf1d2..985e22d79 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -53,13 +53,9 @@ add_executable(CPEditor include/Extensions/CompanionServer.hpp include/Extensions/CFTools.hpp include/Extensions/EditorTheme.hpp - include/Extensions/ExtendedEditorFeatures.hpp src/Extensions/CompanionServer.cpp src/Extensions/CFTools.cpp src/Extensions/EditorTheme.cpp - src/Extensions/ExtendedEditorFeatures.cpp - - include/mainwindow.hpp include/preferencewindow.hpp diff --git a/include/Extensions/ExtendedEditorFeatures.hpp b/include/Extensions/ExtendedEditorFeatures.hpp deleted file mode 100644 index 4caa2bfd4..000000000 --- a/include/Extensions/ExtendedEditorFeatures.hpp +++ /dev/null @@ -1,33 +0,0 @@ -#include -#include - -namespace Core -{ -class ExtendedQCodeEditorFeatures -{ - public: - ExtendedQCodeEditorFeatures(QCodeEditor *editor, int tabSize); - - QString language(); - int tabSize(); - - void setLanguage(QString lang); - void setTabSize(int tab); - void setEditor(QCodeEditor *editor); - - void toggleSelectionComment(bool forceLineCommenter = false); - - bool selectionTab(); - void selectionUnTab(); - void moveSelectionUp(); - void moveSelectionDown(); - - - private: - QString language_; - int tabSize_; - QCodeEditor *editor_ = nullptr; - bool selectionPrepend_(QString content); - bool selectionRemoveFirstCharIf_(QStringList deleteList); -}; -} // namespace Core \ No newline at end of file diff --git a/include/appwindow.hpp b/include/appwindow.hpp index 3e4f28113..6ddfc3687 100644 --- a/include/appwindow.hpp +++ b/include/appwindow.hpp @@ -23,7 +23,6 @@ #include "Core/SettingsManager.hpp" #include "Telemetry/UpdateNotifier.hpp" #include "mainwindow.hpp" -#include "Extensions/ExtendedEditorFeatures.hpp" #include "preferencewindow.hpp" namespace Ui @@ -121,15 +120,19 @@ class AppWindow : public QMainWindow void on_actionSplit_Mode_triggered(); - void on_comment_toggle(); + void on_action_indent_triggered(); - void on_tab_indent(); + void on_action_unindent_triggered(); - void on_tab_unindent(); + void on_action_swap_line_up_triggered(); - void on_move_up(); + void on_action_swap_line_down_triggered(); - void on_move_down(); + void on_action_delete_line_triggered(); + + void on_action_toggle_comment_triggered(); + + void on_action_toggle_block_comment_triggered(); void on_confirmTriggered(MainWindow *widget); @@ -146,8 +149,6 @@ class AppWindow : public QMainWindow Telemetry::UpdateNotifier *updater = nullptr; PreferenceWindow *preferenceWindow = nullptr; Network::CompanionServer *server; - Core::ExtendedQCodeEditorFeatures *editorFeaturs = nullptr; - QVector editorFeaturesShortucts; void setConnections(); void allocate(); diff --git a/src/Extensions/ExtendedEditorFeatures.cpp b/src/Extensions/ExtendedEditorFeatures.cpp deleted file mode 100644 index 392107743..000000000 --- a/src/Extensions/ExtendedEditorFeatures.cpp +++ /dev/null @@ -1,302 +0,0 @@ -#include "Extensions/ExtendedEditorFeatures.hpp" -#include -#include - -namespace Core -{ - -ExtendedQCodeEditorFeatures::ExtendedQCodeEditorFeatures(QCodeEditor *editor, int tabSize) -{ - this->editor_ = editor; - this->tabSize_ = tabSize; -} - -QString ExtendedQCodeEditorFeatures::language() -{ - return this->language_; -} - -int ExtendedQCodeEditorFeatures::tabSize() -{ - return this->tabSize_; -} - -void ExtendedQCodeEditorFeatures::setLanguage(QString lang) -{ - this->language_ = lang; -} - -void ExtendedQCodeEditorFeatures::setTabSize(int tab) -{ - this->tabSize_ = tab; -} - -void ExtendedQCodeEditorFeatures::setEditor(QCodeEditor *editor) -{ - this->editor_ = editor; -} - -void ExtendedQCodeEditorFeatures::toggleSelectionComment(bool _) -{ - if (editor_->textCursor().hasSelection()) - { - // todo(coder3101): This just checks if first line is comment and then works its way out. - // make it toggle line by line instead - auto sps = editor_->textCursor(); - sps.setPosition(sps.selectionStart()); - sps.select(QTextCursor::SelectionType::LineUnderCursor); - auto text = sps.selectedText(); - if (text.startsWith("// ") || text.startsWith("# ")) - { - - if (language_ != "Python") - { - - selectionRemoveFirstCharIf_({"/", " "}); - selectionRemoveFirstCharIf_({"/", " "}); - selectionRemoveFirstCharIf_({"/", " "}); - } - else - { - selectionRemoveFirstCharIf_({"#", " "}); - selectionRemoveFirstCharIf_({"#", " "}); - } - } - else - { - if (language_ == "Python") - selectionPrepend_("# "); - else - selectionPrepend_("// "); - } - } - else - { - // This replacement can be done via regex too. - auto cursor = editor_->textCursor(); - cursor.select(QTextCursor::SelectionType::LineUnderCursor); - QString thisLine = cursor.selectedText(); - if (language_ == "Python") - { - if (thisLine.startsWith("# ")) - thisLine.remove("# "); - else - thisLine.prepend("# "); - cursor.removeSelectedText(); - cursor.insertText(thisLine); - } - else - { - if (thisLine.startsWith("// ")) - thisLine.remove("// "); - else - thisLine.prepend("// "); - cursor.removeSelectedText(); - cursor.insertText(thisLine); - } - } -} - -bool ExtendedQCodeEditorFeatures::selectionTab() -{ - return selectionPrepend_("\t"); -} - -void ExtendedQCodeEditorFeatures::selectionUnTab() -{ - selectionRemoveFirstCharIf_({"\t", " "}); -} - -void ExtendedQCodeEditorFeatures::moveSelectionUp() -{ - if (editor_->textCursor().hasSelection()) - { - // Not working. The selection cursor behaves in differently when selection is swapped with an empty line. - /* - auto cursor = editor_->textCursor(); - int start = cursor.selectionStart(); - int stop = cursor.selectionEnd(); - - if (!cursor.atStart()) - { - QTextCursor fullCursor = cursor; - fullCursor.clearSelection(); - fullCursor.setPosition(start); - fullCursor.movePosition(QTextCursor::MoveOperation::StartOfLine); - fullCursor.setPosition(stop, QTextCursor::KeepAnchor); - fullCursor.movePosition(QTextCursor::MoveOperation::EndOfLine, QTextCursor::KeepAnchor); - - QString selection = fullCursor.selectedText(); - fullCursor.removeSelectedText(); - - QTextCursor upCursor = cursor; - upCursor.clearSelection(); - upCursor.movePosition(QTextCursor::MoveOperation::Up); - - upCursor.select(QTextCursor::SelectionType::LineUnderCursor); - QString thatLine = upCursor.selectedText(); - - - fullCursor.insertText(thatLine); - upCursor.removeSelectedText(); - upCursor.insertText(selection); - - int last_pos = upCursor.selectionEnd(); - upCursor.clearSelection(); - fullCursor.clearSelection(); - - cursor.setPosition(start); - cursor.movePosition(QTextCursor::MoveOperation::Up); - cursor.movePosition(QTextCursor::StartOfLine); - cursor.setPosition(last_pos, QTextCursor::KeepAnchor); - - - editor_->setTextCursor(cursor); - } */ - } - else - { - auto cursor = editor_->textCursor(); - - if (!cursor.atStart()) - { - cursor.select(QTextCursor::SelectionType::LineUnderCursor); - QString thisLine = cursor.selectedText(); - cursor.removeSelectedText(); - - QTextCursor upCursor = cursor; - upCursor.movePosition(QTextCursor::MoveOperation::Up); - upCursor.select(QTextCursor::SelectionType::LineUnderCursor); - QString thatLine = upCursor.selectedText(); - - cursor.insertText(thatLine); - upCursor.removeSelectedText(); - upCursor.insertText(thisLine); - - editor_->setTextCursor(upCursor); - } - } -} -void ExtendedQCodeEditorFeatures::moveSelectionDown() -{ - if (editor_->textCursor().hasSelection()) - { - // Not implemented - } - else - { - auto cursor = editor_->textCursor(); - - if (!cursor.atEnd()) - { - cursor.select(QTextCursor::SelectionType::LineUnderCursor); - QString thisLine = cursor.selectedText(); - cursor.removeSelectedText(); - - QTextCursor downCursor = cursor; - downCursor.movePosition(QTextCursor::MoveOperation::Down); - downCursor.select(QTextCursor::SelectionType::LineUnderCursor); - QString thatLine = downCursor.selectedText(); - - cursor.insertText(thatLine); - downCursor.removeSelectedText(); - downCursor.insertText(thisLine); - - editor_->setTextCursor(downCursor); - } - } -} - -bool ExtendedQCodeEditorFeatures::selectionPrepend_(QString content) -{ - if (editor_->textCursor().hasSelection()) - { - auto cursor = editor_->textCursor(); - QTextCursor originalCursor = cursor; - originalCursor.clearSelection(); - originalCursor.setPosition(cursor.selectionStart()); - originalCursor.movePosition(QTextCursor::MoveOperation::StartOfLine); - originalCursor.setPosition(cursor.selectionEnd(), QTextCursor::KeepAnchor); - originalCursor.movePosition(QTextCursor::MoveOperation::EndOfLine, QTextCursor::KeepAnchor); - - int start = cursor.selectionStart(); - int stop = cursor.selectionEnd(); - - cursor.setPosition(start); - - cursor.movePosition(QTextCursor::MoveOperation::StartOfLine); - - editor_->setTextCursor(cursor); - editor_->insertPlainText(content); - - cursor.movePosition(QTextCursor::MoveOperation::Down); - int i = content.size(); - while (cursor.position() < stop + i) - { - i += content.size(); - editor_->setTextCursor(cursor); - editor_->insertPlainText(content); - cursor.movePosition(QTextCursor::MoveOperation::Down); - } - - editor_->setTextCursor(originalCursor); - - return true; - } - return false; -} - -bool ExtendedQCodeEditorFeatures::selectionRemoveFirstCharIf_(QStringList deleteText) -{ - if (editor_->textCursor().hasSelection()) - { - auto cursor = editor_->textCursor(); - QTextCursor originalCursor = cursor; - - int start = cursor.selectionStart(); - int stop = cursor.selectionEnd(); - cursor.clearSelection(); - - cursor.setPosition(start); - - cursor.movePosition(QTextCursor::MoveOperation::StartOfLine); - cursor.movePosition(QTextCursor::MoveOperation::Right); - - QTextCursor tmp; - int i = 0; - tmp = cursor; - tmp.movePosition(QTextCursor::MoveOperation::Left, QTextCursor::KeepAnchor); - if (deleteText.contains(tmp.selectedText())) - { - i++; - cursor.deletePreviousChar(); - } - - editor_->setTextCursor(cursor); - - cursor.movePosition(QTextCursor::MoveOperation::Down); - cursor.movePosition(QTextCursor::MoveOperation::Right); - - while (cursor.position() < stop - i) - { - tmp = cursor; - tmp.movePosition(QTextCursor::MoveOperation::Left, QTextCursor::KeepAnchor); - if (deleteText.contains(tmp.selectedText())) - { - cursor.deletePreviousChar(); - i++; - } - - editor_->setTextCursor(cursor); - - cursor.movePosition(QTextCursor::MoveOperation::Down); - cursor.movePosition(QTextCursor::MoveOperation::Right); - } - - editor_->setTextCursor(originalCursor); - return true; - } - return false; -} - -} // namespace Core \ No newline at end of file diff --git a/src/appwindow.cpp b/src/appwindow.cpp index 1131aaef2..fd06f4c4d 100644 --- a/src/appwindow.cpp +++ b/src/appwindow.cpp @@ -115,7 +115,6 @@ AppWindow::~AppWindow() delete timer; delete updater; delete server; - delete editorFeaturs; } /******************* PUBLIC METHODS ***********************/ @@ -172,8 +171,6 @@ void AppWindow::allocate() timer->setInterval(3000); timer->setSingleShot(false); - - editorFeaturs = new Core::ExtendedQCodeEditorFeatures(nullptr, 0); } void AppWindow::applySettings() @@ -216,21 +213,6 @@ void AppWindow::maybeSetHotkeys() delete e; hotkeyObjects.clear(); - if (editorFeaturesShortucts.isEmpty()) - { - QKeySequence commentToggle(Qt::ALT + Qt::Key_Slash); - QKeySequence tabIndents(Qt::CTRL + Qt::Key_Tab); - QKeySequence tabUnIndent(Qt::SHIFT + Qt::Key_Tab); - QKeySequence moveUp(Qt::ALT + Qt::Key_Up); - QKeySequence moveDown(Qt::ALT + Qt::Key_Down); - - editorFeaturesShortucts.push_back(new QShortcut(commentToggle, this, SLOT(on_comment_toggle()))); - editorFeaturesShortucts.push_back(new QShortcut(tabIndents, this, SLOT(on_tab_indent()))); - editorFeaturesShortucts.push_back(new QShortcut(tabUnIndent, this, SLOT(on_tab_unindent()))); - editorFeaturesShortucts.push_back(new QShortcut(moveUp, this, SLOT(on_move_up()))); - editorFeaturesShortucts.push_back(new QShortcut(moveDown, this, SLOT(on_move_down()))); - } - if (!settingManager->isHotkeyInUse()) return; @@ -711,10 +693,6 @@ void AppWindow::onEditorChanged() { if (currentWindow() != nullptr) { - editorFeaturs->setLanguage(currentWindow()->getLanguage()); - editorFeaturs->setTabSize(settingManager->getTabStop()); - editorFeaturs->setEditor(currentWindow()->getEditor()); - QMap> tabsByName; for (int t = 0; t < ui->tabWidget->count(); ++t) @@ -918,60 +896,53 @@ void AppWindow::on_actionSplit_Mode_triggered() currentWindow()->getSplitter()->restoreState(state); } -void AppWindow::on_comment_toggle() +void AppWindow::on_action_indent_triggered() { - if (currentWindow() != nullptr) - { - if (currentWindow()->getEditor()->hasFocus()) - { - editorFeaturs->toggleSelectionComment(); - } - } + auto tmp = currentWindow(); + if (tmp != nullptr) + tmp->getEditor()->indent(); } -void AppWindow::on_tab_indent() +void AppWindow::on_action_unindent_triggered() { - if (currentWindow() != nullptr) - { - if (currentWindow()->getEditor()->hasFocus()) - { - if (!editorFeaturs->selectionTab()) // if nothing was selected, pass tab press to editor - currentWindow()->getEditor()->insertPlainText("\t"); - } - } + auto tmp = currentWindow(); + if (tmp != nullptr) + tmp->getEditor()->unindent(); } -void AppWindow::on_tab_unindent() +void AppWindow::on_action_swap_line_up_triggered() { - if (currentWindow() != nullptr) - { - if (currentWindow()->getEditor()->hasFocus()) - { - editorFeaturs->selectionUnTab(); - } - } + auto tmp = currentWindow(); + if (tmp != nullptr) + tmp->getEditor()->swapLineUp(); } -void AppWindow::on_move_up() +void AppWindow::on_action_swap_line_down_triggered() { - if (currentWindow() != nullptr) - { - if (currentWindow()->getEditor()->hasFocus()) - { - editorFeaturs->moveSelectionUp(); - } - } + auto tmp = currentWindow(); + if (tmp != nullptr) + tmp->getEditor()->swapLineDown(); } -void AppWindow::on_move_down() +void AppWindow::on_action_delete_line_triggered() { - if (currentWindow() != nullptr) - { - if (currentWindow()->getEditor()->hasFocus()) - { - editorFeaturs->moveSelectionDown(); - } - } + auto tmp = currentWindow(); + if (tmp != nullptr) + tmp->getEditor()->deleteLine(); +} + +void AppWindow::on_action_toggle_comment_triggered() +{ + auto tmp = currentWindow(); + if (tmp != nullptr) + tmp->getEditor()->toggleComment(); +} + +void AppWindow::on_action_toggle_block_comment_triggered() +{ + auto tmp = currentWindow(); + if (tmp != nullptr) + tmp->getEditor()->toggleBlockComment(); } void AppWindow::on_confirmTriggered(MainWindow *widget) diff --git a/third_party/QCodeEditor b/third_party/QCodeEditor index 10bca37b7..7ca15611d 160000 --- a/third_party/QCodeEditor +++ b/third_party/QCodeEditor @@ -1 +1 @@ -Subproject commit 10bca37b70a23b00375878f5c3b2632048c38afe +Subproject commit 7ca15611da3be20c947fc471e727e56786726316 diff --git a/ui/appwindow.ui b/ui/appwindow.ui index 8f5c9343f..1fba37234 100644 --- a/ui/appwindow.ui +++ b/ui/appwindow.ui @@ -66,9 +66,23 @@ - + - Action + Edit + + + + + + + + + + + + + + Actions @@ -79,7 +93,15 @@ - + + + View + + + + + + Options @@ -87,7 +109,7 @@ - + Help @@ -95,19 +117,12 @@ - - - View - - - - - - - + - + + + @@ -227,6 +242,62 @@ Ctrl+Shift+O + + + Indent + + + Ctrl+] + + + + + Unindent + + + Ctrl+[ + + + + + Swap Line Up + + + Ctrl+Shift+Up + + + + + Swap Line Down + + + Ctrl+Shift+Down + + + + + Delete Line + + + Ctrl+Shift+K + + + + + Toggle Comment + + + Ctrl+/ + + + + + Toggle Block Comment + + + Ctrl+Shift+/ + + Compile