- Enhancement: New file templates for C / C++ / GAS files

This commit is contained in:
Roy Qu 2023-02-22 22:18:21 +08:00
parent 4522403744
commit e5c6ea1a43
14 changed files with 643 additions and 678 deletions

View File

@ -8,6 +8,7 @@ Red Panda C++ Version 2.14
- change: Remove nasm support
- change: Don't stop debug when breakpoint can't be set
- fix: "Generate assembly" menu item is wrongly enabled for new GNU assembly files
- Enhancement: New file templates for C / C++ / GAS files
Red Panda C++ Version 2.13

View File

@ -32,13 +32,17 @@ CodeSnippetsManager::CodeSnippetsManager(QObject *parent) : QObject(parent)
void CodeSnippetsManager::load()
{
loadSnippets();
loadNewFileTemplate();
mNewCppFileTemplate = loadNewFileTemplate(DEV_NEWFILETEMPLATES_FILE);
mNewCFileTemplate = loadNewFileTemplate(DEV_NEWCFILETEMPLATES_FILE);
mNewGASFileTemplate = loadNewFileTemplate(DEV_NEWGASFILETEMPLATES_FILE);
}
void CodeSnippetsManager::save()
{
saveSnippets();
saveNewFileTemplate();
saveNewFileTemplate(DEV_NEWFILETEMPLATES_FILE, mNewCppFileTemplate);
saveNewFileTemplate(DEV_NEWCFILETEMPLATES_FILE, mNewCFileTemplate);
saveNewFileTemplate(DEV_NEWGASFILETEMPLATES_FILE, mNewGASFileTemplate);
}
void CodeSnippetsManager::loadSnippets()
@ -135,27 +139,26 @@ void CodeSnippetsManager::saveSnippets()
}
}
void CodeSnippetsManager::loadNewFileTemplate()
QString CodeSnippetsManager::loadNewFileTemplate(const QString &fn)
{
QString filename = includeTrailingPathDelimiter(pSettings->dirs().config()) + DEV_NEWFILETEMPLATES_FILE;
QString filename = includeTrailingPathDelimiter(pSettings->dirs().config()) + fn;
QFile file(filename);
if (!file.exists()) {
mNewFileTemplate = "";
return;
return "";
}
if (!file.open(QFile::ReadOnly)) {
QMessageBox::critical(nullptr,
tr("Load new file template failed"),
tr("Can't open new file template file '%1' for read.")
.arg(filename));
return;
return "";
}
mNewFileTemplate=QString::fromUtf8(file.readAll());
return QString::fromUtf8(file.readAll());
}
void CodeSnippetsManager::saveNewFileTemplate()
void CodeSnippetsManager::saveNewFileTemplate(const QString &fn, const QString &templateContent)
{
QString filename = includeTrailingPathDelimiter(pSettings->dirs().config()) + DEV_NEWFILETEMPLATES_FILE;
QString filename = includeTrailingPathDelimiter(pSettings->dirs().config()) + fn;
QFile file(filename);
if (!file.open(QFile::WriteOnly | QFile::Truncate)) {
QMessageBox::critical(nullptr,
@ -164,7 +167,27 @@ void CodeSnippetsManager::saveNewFileTemplate()
.arg(filename));
return;
}
file.write(mNewFileTemplate.toUtf8());
file.write(templateContent.toUtf8());
}
QString CodeSnippetsManager::newGASFileTemplate() const
{
return mNewGASFileTemplate;
}
void CodeSnippetsManager::setNewGASFileTemplate(const QString &newContent)
{
mNewGASFileTemplate = newContent;
}
QString CodeSnippetsManager::newCFileTemplate() const
{
return mNewCFileTemplate;
}
void CodeSnippetsManager::setNewCFileTemplate(const QString &newContent)
{
mNewCFileTemplate = newContent;
}
const QList<PCodeSnippet> &CodeSnippetsManager::snippets() const
@ -177,14 +200,14 @@ void CodeSnippetsManager::setSnippets(const QList<PCodeSnippet> &newSnippets)
mSnippets = newSnippets;
}
const QString &CodeSnippetsManager::newFileTemplate() const
const QString &CodeSnippetsManager::newCppFileTemplate() const
{
return mNewFileTemplate;
return mNewCppFileTemplate;
}
void CodeSnippetsManager::setNewFileTemplate(const QString &newNewFileTemplate)
void CodeSnippetsManager::setNewCppFileTemplate(const QString &content)
{
mNewFileTemplate = newNewFileTemplate;
mNewCppFileTemplate = content;
}
void CodeSnippetsModel::addSnippet(const QString &caption, const QString &prefix, const QString &code, const QString &description, int menuSection)

View File

@ -62,18 +62,27 @@ public:
void setSnippets(const QList<PCodeSnippet> &newSnippets);
const QString &newFileTemplate() const;
void setNewFileTemplate(const QString &newNewFileTemplate);
const QString &newCppFileTemplate() const;
void setNewCppFileTemplate(const QString &newContent);
QString newCFileTemplate() const;
void setNewCFileTemplate(const QString &newContent);
QString newGASFileTemplate() const;
void setNewGASFileTemplate(const QString &newContent);
private:
void loadSnippets();
void saveSnippets();
void loadNewFileTemplate();
void saveNewFileTemplate();
QString loadNewFileTemplate(const QString &filename);
void saveNewFileTemplate(const QString &filename, const QString &templateContent);
private:
QList<PCodeSnippet> mSnippets;
QString mNewFileTemplate;
QString mNewCppFileTemplate; //C++ file template
QString mNewCFileTemplate;
QString mNewGASFileTemplate;
};
using PCodeSnippetManager = std::shared_ptr<CodeSnippetsManager>;

View File

@ -164,7 +164,21 @@ Editor::Editor(QWidget *parent, const QString& filename,
mCanAutoSave = false;
if (isNew && parentPageControl!=nullptr ) {
QString fileTemplate = pMainWindow->codeSnippetManager()->newFileTemplate();
FileType fileType = getFileType(filename);
QString fileTemplate;
switch (fileType) {
case FileType::CSource:
fileTemplate = pMainWindow->codeSnippetManager()->newCFileTemplate();
break;
case FileType::CppSource:
fileTemplate = pMainWindow->codeSnippetManager()->newCppFileTemplate();
break;
case FileType::GAS:
fileTemplate = pMainWindow->codeSnippetManager()->newGASFileTemplate();
break;
default:
break;
}
if (!fileTemplate.isEmpty()) {
insertCodeSnippet(fileTemplate);
setCaretPosition(1,1);

View File

@ -19,6 +19,7 @@
#include "../mainwindow.h"
#include "../codesnippetsmanager.h"
#include "../iconsmanager.h"
#include "../syntaxermanager.h"
#include <QItemSelectionModel>
@ -57,8 +58,13 @@ EditorSnippetWidget::EditorSnippetWidget(const QString& name, const QString& gro
mUpdatingCode = false;
}
});
connect(ui->editFileTemplate,&Editor::changed,
connect(ui->editCppFileTemplate,&Editor::changed,
this, &SettingsWidget::setSettingsChanged);
connect(ui->editCFileTemplate,&Editor::changed,
this, &SettingsWidget::setSettingsChanged);
connect(ui->editGASFileTemplate,&Editor::changed,
this, &SettingsWidget::setSettingsChanged);
ui->editGASFileTemplate->setSyntaxer(syntaxerManager.getSyntaxer(QSynedit::ProgrammingLanguage::ATTAssembly));
}
EditorSnippetWidget::~EditorSnippetWidget()
@ -69,13 +75,17 @@ EditorSnippetWidget::~EditorSnippetWidget()
void EditorSnippetWidget::doLoad()
{
mModel.updateSnippets(pMainWindow->codeSnippetManager()->snippets());
ui->editFileTemplate->document()->setText(pMainWindow->codeSnippetManager()->newFileTemplate());
ui->editCppFileTemplate->document()->setText(pMainWindow->codeSnippetManager()->newCppFileTemplate());
ui->editCFileTemplate->document()->setText(pMainWindow->codeSnippetManager()->newCFileTemplate());
ui->editGASFileTemplate->document()->setText(pMainWindow->codeSnippetManager()->newGASFileTemplate());
}
void EditorSnippetWidget::doSave()
{
pMainWindow->codeSnippetManager()->setSnippets(mModel.snippets());
pMainWindow->codeSnippetManager()->setNewFileTemplate(ui->editFileTemplate->text());
pMainWindow->codeSnippetManager()->setNewCppFileTemplate(ui->editCppFileTemplate->text());
pMainWindow->codeSnippetManager()->setNewCFileTemplate(ui->editCFileTemplate->text());
pMainWindow->codeSnippetManager()->setNewGASFileTemplate(ui->editGASFileTemplate->text());
pMainWindow->codeSnippetManager()->save();
}

View File

@ -129,13 +129,47 @@
</item>
</layout>
</widget>
<widget class="QWidget" name="tab">
<attribute name="title">
<string>New C File Template</string>
</attribute>
<layout class="QHBoxLayout" name="horizontalLayout_4">
<item>
<widget class="Editor" name="editCFileTemplate">
<property name="frameShape">
<enum>QFrame::StyledPanel</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Raised</enum>
</property>
</widget>
</item>
</layout>
</widget>
<widget class="QWidget" name="tabFileTemplate">
<attribute name="title">
<string>New File Template</string>
<string>New C++ File Template</string>
</attribute>
<layout class="QHBoxLayout" name="horizontalLayout_5">
<item>
<widget class="Editor" name="editFileTemplate">
<widget class="Editor" name="editCppFileTemplate">
<property name="frameShape">
<enum>QFrame::StyledPanel</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Raised</enum>
</property>
</widget>
</item>
</layout>
</widget>
<widget class="QWidget" name="tab_2">
<attribute name="title">
<string>New GAS File Template</string>
</attribute>
<layout class="QHBoxLayout" name="horizontalLayout_6">
<item>
<widget class="Editor" name="editGASFileTemplate">
<property name="frameShape">
<enum>QFrame::StyledPanel</enum>
</property>

View File

@ -93,6 +93,8 @@
#define DEV_SYMBOLUSAGE_FILE "symbolusage.json"
#define DEV_CODESNIPPET_FILE "codesnippets.json"
#define DEV_NEWFILETEMPLATES_FILE "newfiletemplate.txt"
#define DEV_NEWCFILETEMPLATES_FILE "newcfiletemplate.txt"
#define DEV_NEWGASFILETEMPLATES_FILE "newgasfiletemplate.txt"
#define DEV_AUTOLINK_FILE "autolink.json"
#define DEV_SHORTCUT_FILE "shortcuts.json"
#define DEV_TOOLS_FILE "tools.json"

View File

@ -753,11 +753,7 @@
</message>
<message>
<source>Assembler</source>
<translation type="unfinished">Assembler</translation>
</message>
<message>
<source>Locate nasm</source>
<translation type="unfinished"></translation>
<translation type="obsolete">Assembler</translation>
</message>
</context>
<context>
@ -1580,7 +1576,19 @@
</message>
<message>
<source>New File Template</source>
<translation>Novo arquivo de modelo</translation>
<translation type="vanished">Novo arquivo de modelo</translation>
</message>
<message>
<source>New C File Template</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>New C++ File Template</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>New GAS File Template</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
@ -5032,22 +5040,6 @@
<source>Please turn off your compiler set&apos;s &quot;Strip executable (-s)&quot; option, recompile and retry debug.</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>The executable doesn&apos;t have enough debug info to set breakpoint.</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Then recompile and retry debug.</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Or you can remove all breakpoints, open cpu info dialog, and try debug machine codes.</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Please choose a Debug compiler set in the toolbar, or turn on your compiler set&apos;s &quot;Generate debug info (-g3)&quot; option in the options dialog.</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>NewClassDialog</name>
@ -5503,7 +5495,7 @@
</message>
<message>
<source>Assembler</source>
<translation type="unfinished">Assembler</translation>
<translation type="obsolete">Assembler</translation>
</message>
</context>
<context>
@ -6579,10 +6571,6 @@
<source>GAS files</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>ASM files</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Lua files</source>
<translation type="unfinished"></translation>
@ -7530,49 +7518,29 @@
</context>
<context>
<name>XMakeCompiler</name>
<message>
<source>Building xmake.lua file...</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>- Filename: %1</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Can&apos;t open &apos;%1&apos; for write!</source>
<translation type="unfinished">Impossível abrir &apos;%1&apos; para gravar!</translation>
<translation type="obsolete">Impossível abrir &apos;%1&apos; para gravar!</translation>
</message>
<message>
<source>Compiling project changes...</source>
<translation type="unfinished">Compilando alterações em projeto ...</translation>
<translation type="obsolete">Compilando alterações em projeto ...</translation>
</message>
<message>
<source>- Project Filename: %1</source>
<translation type="unfinished">- Nome de arquivo de projeto: %1</translation>
</message>
<message>
<source>- Compiler Set Name: %1</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Make program &apos;%1&apos; doesn&apos;t exists!</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Please check the &quot;program&quot; page of compiler settings.</source>
<translation type="unfinished"></translation>
<translation type="obsolete">- Nome de arquivo de projeto: %1</translation>
</message>
<message>
<source>Processing makefile:</source>
<translation type="unfinished">Processando makefile:</translation>
<translation type="obsolete">Processando makefile:</translation>
</message>
<message>
<source>- makefile processer: %1</source>
<translation type="unfinished">- Processador do makefile: %1</translation>
<translation type="obsolete">- Processador do makefile: %1</translation>
</message>
<message>
<source>- Command: %1 %2</source>
<translation type="unfinished">- Comando: %1 %2</translation>
<translation type="obsolete">- Comando: %1 %2</translation>
</message>
</context>
<context>

File diff suppressed because it is too large Load Diff

View File

@ -652,14 +652,6 @@
<source>Locate windres</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Assembler</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Locate nasm</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>CppRefacter</name>
@ -1448,7 +1440,15 @@
<translation type="unfinished"></translation>
</message>
<message>
<source>New File Template</source>
<source>New C File Template</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>New C++ File Template</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>New GAS File Template</source>
<translation type="unfinished"></translation>
</message>
</context>
@ -4793,22 +4793,6 @@
<source>Please turn off your compiler set&apos;s &quot;Strip executable (-s)&quot; option, recompile and retry debug.</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>The executable doesn&apos;t have enough debug info to set breakpoint.</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Then recompile and retry debug.</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Or you can remove all breakpoints, open cpu info dialog, and try debug machine codes.</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Please choose a Debug compiler set in the toolbar, or turn on your compiler set&apos;s &quot;Generate debug info (-g3)&quot; option in the options dialog.</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>NewClassDialog</name>
@ -5226,10 +5210,6 @@
<source>Resource</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Assembler</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>ProjectCompiler</name>
@ -6172,10 +6152,6 @@
<source>GAS files</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>ASM files</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Lua files</source>
<translation type="unfinished"></translation>
@ -6983,53 +6959,6 @@
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>XMakeCompiler</name>
<message>
<source>Building xmake.lua file...</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>- Filename: %1</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Can&apos;t open &apos;%1&apos; for write!</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Compiling project changes...</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>- Project Filename: %1</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>- Compiler Set Name: %1</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Make program &apos;%1&apos; doesn&apos;t exists!</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Please check the &quot;program&quot; page of compiler settings.</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Processing makefile:</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>- makefile processer: %1</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>- Command: %1 %2</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>editorcustomctypekeywords</name>
<message>

View File

@ -86,6 +86,9 @@ FileType getFileType(const QString &filename)
if (filename.endsWith(".s",PATH_SENSITIVITY)) {
return FileType::GAS;
}
if (filename.endsWith(".S",PATH_SENSITIVITY)) {
return FileType::GAS;
}
if (filename.endsWith(".dev",PATH_SENSITIVITY)) {
return FileType::Project;
}

View File

@ -36,7 +36,6 @@ SOURCES += qsynedit/codefolding.cpp \
qsynedit/searcher/regexsearcher.cpp \
qsynedit/syntaxer/asm.cpp \
qsynedit/syntaxer/cpp.cpp \
qsynedit/syntaxer/customhighlighterv1.cpp \
qsynedit/syntaxer/glsl.cpp \
qsynedit/syntaxer/lua.cpp \
qsynedit/types.cpp \
@ -62,7 +61,6 @@ HEADERS += \
qsynedit/searcher/regexsearcher.h \
qsynedit/syntaxer/asm.h \
qsynedit/syntaxer/cpp.h \
qsynedit/syntaxer/customhighlighterv1.h \
qsynedit/syntaxer/glsl.h \
qsynedit/syntaxer/lua.h \
qsynedit/syntaxer/makefile.h \

View File

@ -83,9 +83,6 @@ const QSet<QString> GLSLSyntaxer::Keywords {
GLSLSyntaxer::GLSLSyntaxer(): Syntaxer()
{
mAsmAttribute = std::make_shared<TokenAttribute>(SYNS_AttrAssembler,
TokenType::Embeded);
addAttribute(mAsmAttribute);
mCharAttribute = std::make_shared<TokenAttribute>(SYNS_AttrCharacter,
TokenType::Character);
addAttribute(mCharAttribute);
@ -131,11 +128,6 @@ GLSLSyntaxer::GLSLSyntaxer(): Syntaxer()
resetState();
}
const PTokenAttribute &GLSLSyntaxer::asmAttribute() const
{
return mAsmAttribute;
}
const PTokenAttribute &GLSLSyntaxer::preprocessorAttribute() const
{
return mPreprocessorAttribute;
@ -1279,8 +1271,6 @@ QString GLSLSyntaxer::getToken() const
const PTokenAttribute &GLSLSyntaxer::getTokenAttribute() const
{
switch (mTokenId) {
case TokenId::Asm:
return mAsmAttribute;
case TokenId::Comment:
return mCommentAttribute;
case TokenId::Directive:

View File

@ -24,7 +24,6 @@ namespace QSynedit {
class GLSLSyntaxer: public Syntaxer
{
enum class TokenId {
Asm,
Comment,
Directive,
Identifier,
@ -57,8 +56,6 @@ public:
GLSLSyntaxer(const GLSLSyntaxer&)=delete;
GLSLSyntaxer& operator=(const GLSLSyntaxer&)=delete;
const PTokenAttribute &asmAttribute() const;
const PTokenAttribute &preprocessorAttribute() const;
const PTokenAttribute &invalidAttribute() const;
@ -148,7 +145,6 @@ private:
int mLeftBraces;
int mRightBraces;
PTokenAttribute mAsmAttribute;
PTokenAttribute mPreprocessorAttribute;
PTokenAttribute mInvalidAttribute;
PTokenAttribute mNumberAttribute;