- 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: 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;

View File

@ -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<SOURCEPATH>%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: <DATETIME>");
// 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<SynEditingArea>();
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;i<sl.count();i++) {
int lastPos = -1;
int lastPos = 0;
QString s = sl[i];
if (i>0)
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<TabStop>();
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<TabStop>();
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();
}

View File

@ -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

View File

@ -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<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)
{
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)

View File

@ -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;

View File

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