- enhancement: add "auto reformat when saving codes" in "Options" / "Editor" / "Misc" (off by default)

- enhancement: use "todo" and "fixme" as the keyword for TODO comments
This commit is contained in:
Roy Qu 2022-10-25 10:13:51 +08:00
parent b958cdc00c
commit cab0c8ca24
11 changed files with 443 additions and 395 deletions

View File

@ -22,6 +22,8 @@ Red Panda C++ Version 2.0
- fix: compiler settings not correctly handled when create makefile
- enhancement: auto locate current open file in the project view panel
- enhancement: when closing project, prevent all editors that belongs to the project check syntax and parse todos.
- enhancement: add "auto reformat when saving codes" in "Options" / "Editor" / "Misc" (off by default)
- enhancement: use "todo" and "fixme" as the keyword for TODO comments
Red Panda C++ Version 1.5

View File

@ -251,13 +251,9 @@ bool Editor::save(bool force, bool doReparse) {
//is this file writable;
pMainWindow->fileSystemWatcher()->removePath(mFilename);
try {
// QFileInfo info(mFilename);
// if (!force && !info.isWritable()) {
// QMessageBox::critical(pMainWindow,tr("Error"),
// tr("File %1 is not writable!").arg(mFilename));
// return false;
// }
// qDebug()<<"saving "<<mFilename;
if (pSettings->editor().autoFormatWhenSaved()) {
reformat(false);
}
saveFile(mFilename);
pMainWindow->fileSystemWatcher()->addPath(mFilename);
setModified(false);
@ -342,6 +338,10 @@ bool Editor::saveAs(const QString &name, bool fromProject){
if (pSettings->codeCompletion().enabled() && mParser) {
mParser->invalidateFile(mFilename);
}
if (pSettings->editor().autoFormatWhenSaved()) {
reformat(false);
}
try {
mFilename = newName;
saveFile(mFilename);
@ -387,7 +387,6 @@ bool Editor::saveAs(const QString &name, bool fromProject){
pMainWindow->checkSyntaxInBack(this);
reparseTodo();
if (!shouldOpenInReadonly()) {
setReadOnly(false);
updateCaption();
@ -4303,7 +4302,7 @@ QString Editor::getPreviousWordAtPositionForSuggestion(const QSynedit::BufferCoo
return result;
}
void Editor::reformat()
void Editor::reformat(bool doReparse)
{
if (readOnly())
return;
@ -4349,10 +4348,14 @@ void Editor::reformat()
setTopLine(oldTopLine);
setOptions(oldOptions);
endUndoBlock();
if (doReparse && !pMainWindow->isQuitting() && !pMainWindow->isClosingAll()
&& !(inProject() && pMainWindow->closingProject())) {
reparse(true);
checkSyntaxInBack();
reparseTodo();
pMainWindow->updateEditorActions();
}
}
void Editor::checkSyntaxInBack()

View File

@ -195,7 +195,7 @@ public:
void modifyBreakpointProperty(int line);
void setActiveBreakpointFocus(int Line, bool setFocus=true);
QString getPreviousWordAtPositionForSuggestion(const QSynedit::BufferCoord& p);
void reformat();
void reformat(bool doReparse=true);
void checkSyntaxInBack();
void gotoDeclaration(const QSynedit::BufferCoord& pos);
void gotoDefinition(const QSynedit::BufferCoord& pos);

View File

@ -655,6 +655,16 @@ void Settings::Editor::setUndoMemoryUsage(int newUndoMemoryUsage)
mUndoMemoryUsage = newUndoMemoryUsage;
}
bool Settings::Editor::autoFormatWhenSaved() const
{
return mAutoFormatWhenSaved;
}
void Settings::Editor::setAutoFormatWhenSaved(bool newAutoFormatWhenSaved)
{
mAutoFormatWhenSaved = newAutoFormatWhenSaved;
}
bool Settings::Editor::highlightCurrentWord() const
{
return mHighlightCurrentWord;
@ -1235,6 +1245,7 @@ void Settings::Editor::doSave()
saveValue("auto_detect_file_encoding",mAutoDetectFileEncoding);
saveValue("undo_limit",mUndoLimit);
saveValue("undo_memory_usage", mUndoMemoryUsage);
saveValue("auto_format_when_saved", mAutoFormatWhenSaved);
//tooltips
saveValue("enable_tooltips",mEnableTooltips);
@ -1377,6 +1388,7 @@ void Settings::Editor::doLoad()
mAutoDetectFileEncoding = boolValue("auto_detect_file_encoding",true);
mUndoLimit = intValue("undo_limit",0);
mUndoMemoryUsage = intValue("undo_memory_usage", 10);
mAutoFormatWhenSaved = boolValue("auto_format_when_saved", false);
//tooltips
mEnableTooltips = boolValue("enable_tooltips",true);

View File

@ -370,6 +370,9 @@ public:
int undoMemoryUsage() const;
void setUndoMemoryUsage(int newUndoMemoryUsage);
bool autoFormatWhenSaved() const;
void setAutoFormatWhenSaved(bool newAutoFormatWhenSaved);
private:
//General
// indents
@ -478,6 +481,7 @@ public:
bool mDefaultFileCpp;
int mUndoLimit;
int mUndoMemoryUsage;
bool mAutoFormatWhenSaved;
//hints tooltip

View File

@ -66,6 +66,7 @@ void EditorMiscWidget::doLoad()
}
ui->spinMaxUndo->setValue(pSettings->editor().undoLimit());
ui->spinMaxUndoMemory->setValue(pSettings->editor().undoMemoryUsage());
ui->chkAutoReformat->setChecked(pSettings->editor().autoFormatWhenSaved());
}
void EditorMiscWidget::doSave()
@ -82,6 +83,8 @@ void EditorMiscWidget::doSave()
}
pSettings->editor().setUndoLimit(ui->spinMaxUndo->value());
pSettings->editor().setUndoMemoryUsage(ui->spinMaxUndoMemory->value());
pSettings->editor().setAutoFormatWhenSaved(ui->chkAutoReformat->isChecked());
pSettings->editor().save();
pMainWindow->updateEditorSettings();
}

View File

@ -7,7 +7,7 @@
<x>0</x>
<y>0</y>
<width>515</width>
<height>315</height>
<height>408</height>
</rect>
</property>
<property name="windowTitle">
@ -35,6 +35,13 @@
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="chkAutoReformat">
<property name="text">
<string>Auto reformat code before saving files</string>
</property>
</widget>
</item>
<item>
<widget class="QWidget" name="widget" native="true">
<layout class="QHBoxLayout" name="horizontalLayout_2">

View File

@ -19,6 +19,10 @@
#include "editor.h"
#include "editorlist.h"
#include <QRegularExpression>
static QRegularExpression todoReg("\\b(todo|fixme)\\b", QRegularExpression::CaseInsensitiveOption);
TodoParser::TodoParser(QObject *parent) : QObject(parent),
mMutex(QMutex::Recursive)
{
@ -131,7 +135,7 @@ void TodoThread::doParseFile(const QString &filename, QSynedit::PHighlighter hig
attr = highlighter->getTokenAttribute();
if (attr == commentAttr) {
QString token = highlighter->getToken();
int pos = token.indexOf("TODO:",0,Qt::CaseInsensitive);
int pos = token.indexOf(todoReg);
if (pos>=0) {
emit todoFound(
filename,

View File

@ -1431,6 +1431,10 @@
<source>MB</source>
<translation type="unfinished">MB</translation>
</message>
<message>
<source>Auto reformat code before saving files</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>EditorSnippetWidget</name>

File diff suppressed because it is too large Load Diff

View File

@ -1328,6 +1328,10 @@
<source>MB</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Auto reformat code before saving files</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>EditorSnippetWidget</name>