- fix: errors in code snippet processing

- change: auto open a new editor at start
This commit is contained in:
royqh1979@gmail.com 2021-10-03 09:57:19 +08:00
parent caaf07a6cc
commit d58e2ac54e
6 changed files with 109 additions and 43 deletions

View File

@ -2,6 +2,8 @@ Version 0.6.0
- fix: old data not displayed when editing code snippets - fix: old data not displayed when editing code snippets
- fix: shift-tab for unindent not work - fix: shift-tab for unindent not work
- fix: can't save code snippets modifications - 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 Version 0.5.0
- enhancement: support C++ using type alias; - enhancement: support C++ using type alias;

View File

@ -522,6 +522,7 @@ void Editor::keyPressEvent(QKeyEvent *event)
params.append(child->command); params.append(child->command);
} }
insertString.append(QString(" * @brief ")+USER_CODE_IN_INSERT_POS); insertString.append(QString(" * @brief ")+USER_CODE_IN_INSERT_POS);
if (!params.isEmpty())
insertString.append(" * "); insertString.append(" * ");
foreach (const QString& param, params) { foreach (const QString& param, params) {
insertString.append(QString(" * @param %1 %2") insertString.append(QString(" * @param %1 %2")
@ -532,18 +533,17 @@ void Editor::keyPressEvent(QKeyEvent *event)
insertString.append(QString(" * @return ")+USER_CODE_IN_INSERT_POS); insertString.append(QString(" * @return ")+USER_CODE_IN_INSERT_POS);
} }
insertString.append(" **/"); insertString.append(" **/");
} else if (caretY()==1) { /* file header */ // } else if (caretY()==1) { /* file header */
insertString.append(QString(" * @file %1%2%3") // insertString.append(QString(" * @file %1<SOURCEPATH>%2")
.arg(USER_CODE_IN_REPL_POS_BEGIN) // .arg(USER_CODE_IN_REPL_POS_BEGIN)
.arg(mFilename) // .arg(USER_CODE_IN_REPL_POS_END));
.arg(USER_CODE_IN_REPL_POS_END)); // insertString.append(QString(" * @brief: ")+ USER_CODE_IN_INSERT_POS);
insertString.append(QString(" * @brief: ")+ USER_CODE_IN_INSERT_POS); // insertString.append(QString(" * @version: ")+ 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(" * @copyright: ")+ USER_CODE_IN_INSERT_POS); // insertString.append(QString(" * @author: ")+ USER_CODE_IN_INSERT_POS);
insertString.append(QString(" * @author: ")+ USER_CODE_IN_INSERT_POS); // insertString.append(" * @date: <DATETIME>");
insertString.append(QString(" * @date: ") + QDateTime::currentDateTime().toString("yyyy-MM-dd hh::mm")); // insertString.append(" * ");
insertString.append(" * "); // insertString.append(" **/");
insertString.append(" **/");
} else { } else {
insertString.append(QString(" * ")+USER_CODE_IN_INSERT_POS); insertString.append(QString(" * ")+USER_CODE_IN_INSERT_POS);
insertString.append(" **/"); insertString.append(" **/");
@ -758,10 +758,10 @@ void Editor::onGetEditingAreas(int Line, SynEditingAreaList &areaList)
if (mTabStopBegin>=0 && mTabStopY == Line) { if (mTabStopBegin>=0 && mTabStopY == Line) {
PSynEditingArea p = make_shared<SynEditingArea>(); PSynEditingArea p = make_shared<SynEditingArea>();
p->type = SynEditingAreaType::eatRectangleBorder; p->type = SynEditingAreaType::eatRectangleBorder;
int spaceCount = leftSpaces(mLineBeforeTabStop); // int spaceCount = leftSpaces(mLineBeforeTabStop);
int spaceBefore = mLineBeforeTabStop.length()-TrimLeft(mLineBeforeTabStop).length(); // int spaceBefore = mLineBeforeTabStop.length()-TrimLeft(mLineBeforeTabStop).length();
p->beginX = mTabStopBegin + spaceCount - spaceBefore ; p->beginX = charToColumn(Line,mTabStopBegin);
p->endX = mTabStopEnd + spaceCount - spaceBefore ; p->endX = charToColumn(Line,mTabStopEnd) ;
p->color = highlighter()->stringAttribute()->foreground(); p->color = highlighter()->stringAttribute()->foreground();
areaList.append(p); areaList.append(p);
} }
@ -1296,9 +1296,9 @@ void Editor::onStatusChanged(SynStatusChanges changes)
} else { } else {
if (lineText().startsWith(mLineBeforeTabStop) if (lineText().startsWith(mLineBeforeTabStop)
&& lineText().endsWith(mLineAfterTabStop)) && lineText().endsWith(mLineAfterTabStop))
mTabStopBegin = mLineBeforeTabStop.length(); mTabStopBegin = mLineBeforeTabStop.length()+1;
mTabStopEnd = lineText().length() mTabStopEnd = lineText().length()
- mLineAfterTabStop.length(); - mLineAfterTabStop.length()+1;
} }
mXOffsetSince = mTabStopEnd - caretX(); mXOffsetSince = mTabStopEnd - caretX();
if (caretX() < mTabStopBegin || if (caretX() < mTabStopBegin ||
@ -1933,34 +1933,34 @@ void Editor::insertCodeSnippet(const QString &code)
leftSpaces(lineText()),true).length(); leftSpaces(lineText()),true).length();
QStringList newSl; QStringList newSl;
for (int i=0;i<sl.count();i++) { for (int i=0;i<sl.count();i++) {
int lastPos = -1; int lastPos = 0;
QString s = sl[i]; QString s = sl[i];
if (i>0) if (i>0)
lastPos = -spaceCount-1; lastPos = -spaceCount;
while (true) { while (true) {
int insertPos = s.indexOf(USER_CODE_IN_INSERT_POS); int insertPos = s.indexOf(USER_CODE_IN_INSERT_POS);
if (insertPos < 0) // no %INSERT% macro in this line now if (insertPos < 0) // no %INSERT% macro in this line now
break; break;
PTabStop p = std::make_shared<TabStop>(); PTabStop p = std::make_shared<TabStop>();
s.remove(insertPos, QString(USER_CODE_IN_INSERT_POS).length()); s.remove(insertPos, QString(USER_CODE_IN_INSERT_POS).length());
insertPos--; //insertPos--;
p->x = insertPos - lastPos; p->x = insertPos - lastPos;
p->endX = p->x; p->endX = p->x ;
p->y = i - lastI; p->y = i - lastI;
lastPos = insertPos; lastPos = insertPos;
lastI = i; lastI = i;
mUserCodeInTabStops.append(p); mUserCodeInTabStops.append(p);
} }
lastPos = -1; lastPos = 0;
if (i>0) if (i>0)
lastPos = -spaceCount-1; lastPos = -spaceCount;
while (true) { while (true) {
int insertPos = s.indexOf(USER_CODE_IN_REPL_POS_BEGIN); int insertPos = s.indexOf(USER_CODE_IN_REPL_POS_BEGIN);
if (insertPos < 0) // no %INSERT% macro in this line now if (insertPos < 0) // no %INSERT% macro in this line now
break; break;
PTabStop p = std::make_shared<TabStop>(); PTabStop p = std::make_shared<TabStop>();
s.remove(insertPos, QString(USER_CODE_IN_REPL_POS_BEGIN).length()); s.remove(insertPos, QString(USER_CODE_IN_REPL_POS_BEGIN).length());
insertPos--; //insertPos--;
p->x = insertPos - lastPos; p->x = insertPos - lastPos;
int insertEndPos = insertPos + int insertEndPos = insertPos +
@ -1969,7 +1969,7 @@ void Editor::insertCodeSnippet(const QString &code)
p->endX = s.length(); p->endX = s.length();
} else { } else {
s.remove(insertEndPos, QString(USER_CODE_IN_REPL_POS_END).length()); s.remove(insertEndPos, QString(USER_CODE_IN_REPL_POS_END).length());
insertEndPos--; //insertEndPos--;
p->endX = insertEndPos - lastPos; p->endX = insertEndPos - lastPos;
} }
p->y=i-lastI; p->y=i-lastI;
@ -2766,28 +2766,29 @@ void Editor::popUserCodeInTabStops()
} }
BufferCoord newCursorPos; BufferCoord newCursorPos;
int tabStopEnd; int tabStopEnd;
int tabStopBegin;
if (mUserCodeInTabStops.count() > 0) { if (mUserCodeInTabStops.count() > 0) {
PTabStop p = mUserCodeInTabStops.front(); PTabStop p = mUserCodeInTabStops.front();
// Update the cursor // Update the cursor
if (p->y ==0) { if (p->y ==0) {
newCursorPos.Char = mTabStopEnd + p->x; tabStopBegin = mTabStopEnd + p->x;
tabStopEnd = mTabStopEnd + p->endX; tabStopEnd = mTabStopEnd + p->endX;
} else { } else {
newCursorPos.Char = p->x+1; tabStopBegin = p->x+1;
tabStopEnd = p->endX+1; tabStopEnd = p->endX+1;
} }
mTabStopY = caretY() + p->y; mTabStopY = caretY() + p->y;
newCursorPos.Line = mTabStopY; newCursorPos.Line = mTabStopY;
newCursorPos.Char = tabStopBegin;
setCaretXY(newCursorPos); setCaretXY(newCursorPos);
setBlockBegin(newCursorPos); setBlockBegin(newCursorPos);
newCursorPos.Char = tabStopEnd; newCursorPos.Char = tabStopEnd;
setBlockEnd(newCursorPos); setBlockEnd(newCursorPos);
mTabStopBegin= caretX();
mTabStopBegin = tabStopBegin;
mTabStopEnd = tabStopEnd; mTabStopEnd = tabStopEnd;
mLineBeforeTabStop = lineText().mid(0, mTabStopBegin) ; mLineBeforeTabStop = lineText().mid(0, mTabStopBegin-1) ;
mLineAfterTabStop = lineText().mid(mTabStopEnd) ; mLineAfterTabStop = lineText().mid(mTabStopEnd-1) ;
mXOffsetSince=0; mXOffsetSince=0;
mUserCodeInTabStops.pop_front(); mUserCodeInTabStops.pop_front();
} }

View File

@ -15,6 +15,7 @@
#include "autolinkmanager.h" #include "autolinkmanager.h"
#include "platform.h" #include "platform.h"
#include "parser/parserutils.h" #include "parser/parserutils.h"
#include "editorlist.h"
QString getSettingFilename(const QString& filepath = QString()) { QString getSettingFilename(const QString& filepath = QString()) {
QString filename; QString filename;
@ -99,6 +100,9 @@ int main(int argc, char *argv[])
pMainWindow = &mainWindow; pMainWindow = &mainWindow;
if (pSettings->editor().autoLoadLastFiles()) if (pSettings->editor().autoLoadLastFiles())
pMainWindow->loadLastOpens(); pMainWindow->loadLastOpens();
if (pMainWindow->editorList()->pageCount()==0) {
pMainWindow->newEditor();
}
mainWindow.show(); mainWindow.show();
int retCode = app.exec(); int retCode = app.exec();
// save settings // save settings

View File

@ -108,6 +108,13 @@ MainWindow::MainWindow(QWidget *parent)
ui->menuFile->insertSeparator(ui->actionExit); ui->menuFile->insertSeparator(ui->actionExit);
rebuildOpenedFileHisotryMenu(); 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; mCPUDialog = nullptr;
updateProjectView(); updateProjectView();
@ -162,6 +169,7 @@ MainWindow::MainWindow(QWidget *parent)
connect(ui->menuProject, &QMenu::aboutToShow, connect(ui->menuProject, &QMenu::aboutToShow,
this, &MainWindow::updateProjectActions); this, &MainWindow::updateProjectActions);
buildContextMenus(); buildContextMenus();
updateEditorColorSchemes(); updateEditorColorSchemes();
@ -1534,6 +1542,18 @@ void MainWindow::loadLastOpens()
focusedEditor->activate(); 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() void MainWindow::buildContextMenus()
{ {
@ -2122,6 +2142,46 @@ void MainWindow::onDebugConsoleContextMenu(const QPoint &pos)
menu.exec(ui->debugConsole->mapToGlobal(pos)); menu.exec(ui->debugConsole->mapToGlobal(pos));
} }
void MainWindow::onShowInsertCodeSnippetMenu()
{
mMenuInsertCodeSnippet->clear();
QList<PCodeSnippet> 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->section<s2->section;
});
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) void MainWindow::onEditorContextMenu(const QPoint &pos)
{ {
Editor * editor = mEditorList->getEditor(); Editor * editor = mEditorList->getEditor();
@ -2402,13 +2462,7 @@ CPUDialog *MainWindow::cpuDialog() const
void MainWindow::on_actionNew_triggered() void MainWindow::on_actionNew_triggered()
{ {
try { newEditor();
Editor * editor=mEditorList->newEditor("",ENCODING_AUTO_DETECT,false,true);
editor->activate();
updateForEncodingInfo();
} catch (FileError e) {
QMessageBox::critical(this,tr("Error"),e.reason());
}
} }
void MainWindow::on_EditorTabsLeft_tabCloseRequested(int index) void MainWindow::on_EditorTabsLeft_tabCloseRequested(int index)

View File

@ -90,6 +90,8 @@ public:
void saveLastOpens(); void saveLastOpens();
void loadLastOpens(); void loadLastOpens();
void newEditor();
QPlainTextEdit* txtLocals(); QPlainTextEdit* txtLocals();
CPUDialog *cpuDialog() const; CPUDialog *cpuDialog() const;
@ -177,6 +179,8 @@ private slots:
void onClassBrowserContextMenu(const QPoint& pos); void onClassBrowserContextMenu(const QPoint& pos);
void onDebugConsoleContextMenu(const QPoint& pos); void onDebugConsoleContextMenu(const QPoint& pos);
void onShowInsertCodeSnippetMenu();
void on_actionNew_triggered(); void on_actionNew_triggered();
void on_EditorTabsLeft_tabCloseRequested(int index); void on_EditorTabsLeft_tabCloseRequested(int index);
@ -364,6 +368,7 @@ private:
QMenu *mMenuRecentFiles; QMenu *mMenuRecentFiles;
QMenu *mMenuRecentProjects; QMenu *mMenuRecentProjects;
QMenu *mMenuNew; QMenu *mMenuNew;
QMenu *mMenuInsertCodeSnippet;
QComboBox *mCompilerSet; QComboBox *mCompilerSet;
CompilerManager *mCompilerManager; CompilerManager *mCompilerManager;
Debugger *mDebugger; Debugger *mDebugger;

View File

@ -755,8 +755,8 @@ QString parseMacros(const QString &s)
QDate today = QDate::currentDate(); QDate today = QDate::currentDate();
QDateTime now = QDateTime::currentDateTime(); QDateTime now = QDateTime::currentDateTime();
result.replace("<DATE>", "yyyy-MM-dd"); result.replace("<DATE>", today.toString("yyyy-MM-dd"));
result.replace("<DATETIME>", "hh::mm::ss"); result.replace("<DATETIME>", now.toString("yyyy-MM-dd hh:mm:ss"));
Settings::PCompilerSet compilerSet = pSettings->compilerSets().defaultSet(); Settings::PCompilerSet compilerSet = pSettings->compilerSets().defaultSet();
if (compilerSet) { if (compilerSet) {