From d58e2ac54efa161c8187c1850c60f60134adc2d5 Mon Sep 17 00:00:00 2001 From: "royqh1979@gmail.com" Date: Sun, 3 Oct 2021 09:57:19 +0800 Subject: [PATCH] - fix: errors in code snippet processing - change: auto open a new editor at start --- NEWS.md | 2 ++ RedPandaIDE/editor.cpp | 69 +++++++++++++++++++------------------- RedPandaIDE/main.cpp | 4 +++ RedPandaIDE/mainwindow.cpp | 68 +++++++++++++++++++++++++++++++++---- RedPandaIDE/mainwindow.h | 5 +++ RedPandaIDE/utils.cpp | 4 +-- 6 files changed, 109 insertions(+), 43 deletions(-) diff --git a/NEWS.md b/NEWS.md index 07cceb89..1fc731cc 100644 --- a/NEWS.md +++ b/NEWS.md @@ -2,6 +2,8 @@ Version 0.6.0 - fix: old data not displayed when editing code snippets - fix: shift-tab for unindent not work - fix: can't save code snippets modifications + - fix: errors in code snippet processing + - change: auto open a new editor at start Version 0.5.0 - enhancement: support C++ using type alias; diff --git a/RedPandaIDE/editor.cpp b/RedPandaIDE/editor.cpp index 4a19ce6e..665423d5 100644 --- a/RedPandaIDE/editor.cpp +++ b/RedPandaIDE/editor.cpp @@ -522,7 +522,8 @@ void Editor::keyPressEvent(QKeyEvent *event) params.append(child->command); } insertString.append(QString(" * @brief ")+USER_CODE_IN_INSERT_POS); - insertString.append(" * "); + if (!params.isEmpty()) + insertString.append(" * "); foreach (const QString& param, params) { insertString.append(QString(" * @param %1 %2") .arg(param, USER_CODE_IN_INSERT_POS)); @@ -532,18 +533,17 @@ void Editor::keyPressEvent(QKeyEvent *event) insertString.append(QString(" * @return ")+USER_CODE_IN_INSERT_POS); } insertString.append(" **/"); - } else if (caretY()==1) { /* file header */ - insertString.append(QString(" * @file %1%2%3") - .arg(USER_CODE_IN_REPL_POS_BEGIN) - .arg(mFilename) - .arg(USER_CODE_IN_REPL_POS_END)); - insertString.append(QString(" * @brief: ")+ USER_CODE_IN_INSERT_POS); - insertString.append(QString(" * @version: ")+ USER_CODE_IN_INSERT_POS); - insertString.append(QString(" * @copyright: ")+ USER_CODE_IN_INSERT_POS); - insertString.append(QString(" * @author: ")+ USER_CODE_IN_INSERT_POS); - insertString.append(QString(" * @date: ") + QDateTime::currentDateTime().toString("yyyy-MM-dd hh::mm")); - insertString.append(" * "); - insertString.append(" **/"); +// } else if (caretY()==1) { /* file header */ +// insertString.append(QString(" * @file %1%2") +// .arg(USER_CODE_IN_REPL_POS_BEGIN) +// .arg(USER_CODE_IN_REPL_POS_END)); +// insertString.append(QString(" * @brief: ")+ USER_CODE_IN_INSERT_POS); +// insertString.append(QString(" * @version: ")+ USER_CODE_IN_INSERT_POS); +// insertString.append(QString(" * @copyright: ")+ USER_CODE_IN_INSERT_POS); +// insertString.append(QString(" * @author: ")+ USER_CODE_IN_INSERT_POS); +// insertString.append(" * @date: "); +// insertString.append(" * "); +// insertString.append(" **/"); } else { insertString.append(QString(" * ")+USER_CODE_IN_INSERT_POS); insertString.append(" **/"); @@ -758,10 +758,10 @@ void Editor::onGetEditingAreas(int Line, SynEditingAreaList &areaList) if (mTabStopBegin>=0 && mTabStopY == Line) { PSynEditingArea p = make_shared(); p->type = SynEditingAreaType::eatRectangleBorder; - int spaceCount = leftSpaces(mLineBeforeTabStop); - int spaceBefore = mLineBeforeTabStop.length()-TrimLeft(mLineBeforeTabStop).length(); - p->beginX = mTabStopBegin + spaceCount - spaceBefore ; - p->endX = mTabStopEnd + spaceCount - spaceBefore ; +// int spaceCount = leftSpaces(mLineBeforeTabStop); +// int spaceBefore = mLineBeforeTabStop.length()-TrimLeft(mLineBeforeTabStop).length(); + p->beginX = charToColumn(Line,mTabStopBegin); + p->endX = charToColumn(Line,mTabStopEnd) ; p->color = highlighter()->stringAttribute()->foreground(); areaList.append(p); } @@ -1296,9 +1296,9 @@ void Editor::onStatusChanged(SynStatusChanges changes) } else { if (lineText().startsWith(mLineBeforeTabStop) && lineText().endsWith(mLineAfterTabStop)) - mTabStopBegin = mLineBeforeTabStop.length(); + mTabStopBegin = mLineBeforeTabStop.length()+1; mTabStopEnd = lineText().length() - - mLineAfterTabStop.length(); + - mLineAfterTabStop.length()+1; } mXOffsetSince = mTabStopEnd - caretX(); if (caretX() < mTabStopBegin || @@ -1933,34 +1933,34 @@ void Editor::insertCodeSnippet(const QString &code) leftSpaces(lineText()),true).length(); QStringList newSl; for (int i=0;i0) - lastPos = -spaceCount-1; + lastPos = -spaceCount; while (true) { int insertPos = s.indexOf(USER_CODE_IN_INSERT_POS); if (insertPos < 0) // no %INSERT% macro in this line now break; PTabStop p = std::make_shared(); s.remove(insertPos, QString(USER_CODE_IN_INSERT_POS).length()); - insertPos--; + //insertPos--; p->x = insertPos - lastPos; - p->endX = p->x; + p->endX = p->x ; p->y = i - lastI; lastPos = insertPos; lastI = i; mUserCodeInTabStops.append(p); } - lastPos = -1; + lastPos = 0; if (i>0) - lastPos = -spaceCount-1; + lastPos = -spaceCount; while (true) { int insertPos = s.indexOf(USER_CODE_IN_REPL_POS_BEGIN); if (insertPos < 0) // no %INSERT% macro in this line now break; PTabStop p = std::make_shared(); s.remove(insertPos, QString(USER_CODE_IN_REPL_POS_BEGIN).length()); - insertPos--; + //insertPos--; p->x = insertPos - lastPos; int insertEndPos = insertPos + @@ -1969,7 +1969,7 @@ void Editor::insertCodeSnippet(const QString &code) p->endX = s.length(); } else { s.remove(insertEndPos, QString(USER_CODE_IN_REPL_POS_END).length()); - insertEndPos--; + //insertEndPos--; p->endX = insertEndPos - lastPos; } p->y=i-lastI; @@ -2766,28 +2766,29 @@ void Editor::popUserCodeInTabStops() } BufferCoord newCursorPos; int tabStopEnd; + int tabStopBegin; if (mUserCodeInTabStops.count() > 0) { PTabStop p = mUserCodeInTabStops.front(); // Update the cursor if (p->y ==0) { - newCursorPos.Char = mTabStopEnd + p->x; + tabStopBegin = mTabStopEnd + p->x; tabStopEnd = mTabStopEnd + p->endX; } else { - newCursorPos.Char = p->x+1; + tabStopBegin = p->x+1; tabStopEnd = p->endX+1; } mTabStopY = caretY() + p->y; newCursorPos.Line = mTabStopY; - + newCursorPos.Char = tabStopBegin; setCaretXY(newCursorPos); - setBlockBegin(newCursorPos); newCursorPos.Char = tabStopEnd; setBlockEnd(newCursorPos); - mTabStopBegin= caretX(); + + mTabStopBegin = tabStopBegin; mTabStopEnd = tabStopEnd; - mLineBeforeTabStop = lineText().mid(0, mTabStopBegin) ; - mLineAfterTabStop = lineText().mid(mTabStopEnd) ; + mLineBeforeTabStop = lineText().mid(0, mTabStopBegin-1) ; + mLineAfterTabStop = lineText().mid(mTabStopEnd-1) ; mXOffsetSince=0; mUserCodeInTabStops.pop_front(); } diff --git a/RedPandaIDE/main.cpp b/RedPandaIDE/main.cpp index 8c9e671c..a7ec9b31 100644 --- a/RedPandaIDE/main.cpp +++ b/RedPandaIDE/main.cpp @@ -15,6 +15,7 @@ #include "autolinkmanager.h" #include "platform.h" #include "parser/parserutils.h" +#include "editorlist.h" QString getSettingFilename(const QString& filepath = QString()) { QString filename; @@ -99,6 +100,9 @@ int main(int argc, char *argv[]) pMainWindow = &mainWindow; if (pSettings->editor().autoLoadLastFiles()) pMainWindow->loadLastOpens(); + if (pMainWindow->editorList()->pageCount()==0) { + pMainWindow->newEditor(); + } mainWindow.show(); int retCode = app.exec(); // save settings diff --git a/RedPandaIDE/mainwindow.cpp b/RedPandaIDE/mainwindow.cpp index 92a9f746..ca8ade2c 100644 --- a/RedPandaIDE/mainwindow.cpp +++ b/RedPandaIDE/mainwindow.cpp @@ -108,6 +108,13 @@ MainWindow::MainWindow(QWidget *parent) ui->menuFile->insertSeparator(ui->actionExit); rebuildOpenedFileHisotryMenu(); + mMenuInsertCodeSnippet = new QMenu(); + mMenuInsertCodeSnippet->setTitle("Insert Snippet"); + ui->menuCode->insertMenu(ui->actionReformat_Code,mMenuInsertCodeSnippet); + ui->menuCode->insertSeparator(ui->actionReformat_Code); + connect(mMenuInsertCodeSnippet,&QMenu::aboutToShow, + this, onShowInsertCodeSnippetMenu); + mCPUDialog = nullptr; updateProjectView(); @@ -162,6 +169,7 @@ MainWindow::MainWindow(QWidget *parent) connect(ui->menuProject, &QMenu::aboutToShow, this, &MainWindow::updateProjectActions); + buildContextMenus(); updateEditorColorSchemes(); @@ -1534,6 +1542,18 @@ void MainWindow::loadLastOpens() focusedEditor->activate(); } +void MainWindow::newEditor() +{ + try { + Editor * editor=mEditorList->newEditor("",ENCODING_AUTO_DETECT,false,true); + editor->activate(); + updateForEncodingInfo(); + } catch (FileError e) { + QMessageBox::critical(this,tr("Error"),e.reason()); + } + +} + void MainWindow::buildContextMenus() { @@ -2122,6 +2142,46 @@ void MainWindow::onDebugConsoleContextMenu(const QPoint &pos) menu.exec(ui->debugConsole->mapToGlobal(pos)); } +void MainWindow::onShowInsertCodeSnippetMenu() +{ + mMenuInsertCodeSnippet->clear(); + QList snippets; + foreach (const PCodeSnippet& snippet, mCodeSnippetManager->snippets()) { + if (snippet->section>=0 && !snippet->caption.isEmpty()) + snippets.append(snippet); + } + if (snippets.isEmpty()) + return; + std::sort(snippets.begin(),snippets.end(),[](const PCodeSnippet& s1, const PCodeSnippet& s2){ + return s1->sectionsection; + }); + int section = 0; + int sectionCount = 0; + int count = 0; + bool sectionNotEmpty = false; + foreach (const PCodeSnippet& snippet, snippets) { + if (snippet->section>section && sectionCount<6) { + section = snippet->section; + sectionCount++; + if (sectionNotEmpty) + mMenuInsertCodeSnippet->addSeparator(); + } + QAction * action = mMenuInsertCodeSnippet->addAction(snippet->caption); + connect(action, &QAction::triggered, + [snippet,this](){ + Editor * editor = mEditorList->getEditor(); + if (editor) { + editor->insertCodeSnippet(snippet->code); + } + }); + sectionNotEmpty = true; + count++; + if (count>15) + break; + } + +} + void MainWindow::onEditorContextMenu(const QPoint &pos) { Editor * editor = mEditorList->getEditor(); @@ -2402,13 +2462,7 @@ CPUDialog *MainWindow::cpuDialog() const void MainWindow::on_actionNew_triggered() { - try { - Editor * editor=mEditorList->newEditor("",ENCODING_AUTO_DETECT,false,true); - editor->activate(); - updateForEncodingInfo(); - } catch (FileError e) { - QMessageBox::critical(this,tr("Error"),e.reason()); - } + newEditor(); } void MainWindow::on_EditorTabsLeft_tabCloseRequested(int index) diff --git a/RedPandaIDE/mainwindow.h b/RedPandaIDE/mainwindow.h index aa91a176..8a8c9a34 100644 --- a/RedPandaIDE/mainwindow.h +++ b/RedPandaIDE/mainwindow.h @@ -90,6 +90,8 @@ public: void saveLastOpens(); void loadLastOpens(); + void newEditor(); + QPlainTextEdit* txtLocals(); CPUDialog *cpuDialog() const; @@ -177,6 +179,8 @@ private slots: void onClassBrowserContextMenu(const QPoint& pos); void onDebugConsoleContextMenu(const QPoint& pos); + void onShowInsertCodeSnippetMenu(); + void on_actionNew_triggered(); void on_EditorTabsLeft_tabCloseRequested(int index); @@ -364,6 +368,7 @@ private: QMenu *mMenuRecentFiles; QMenu *mMenuRecentProjects; QMenu *mMenuNew; + QMenu *mMenuInsertCodeSnippet; QComboBox *mCompilerSet; CompilerManager *mCompilerManager; Debugger *mDebugger; diff --git a/RedPandaIDE/utils.cpp b/RedPandaIDE/utils.cpp index af05f937..eed5cd7d 100644 --- a/RedPandaIDE/utils.cpp +++ b/RedPandaIDE/utils.cpp @@ -755,8 +755,8 @@ QString parseMacros(const QString &s) QDate today = QDate::currentDate(); QDateTime now = QDateTime::currentDateTime(); - result.replace("", "yyyy-MM-dd"); - result.replace("", "hh::mm::ss"); + result.replace("", today.toString("yyyy-MM-dd")); + result.replace("", now.toString("yyyy-MM-dd hh:mm:ss")); Settings::PCompilerSet compilerSet = pSettings->compilerSets().defaultSet(); if (compilerSet) {