From 2778e9f4bd69be0ae441f3ec5e5572dfe51173e0 Mon Sep 17 00:00:00 2001 From: "royqh1979@gmail.com" Date: Wed, 1 Sep 2021 22:40:58 +0800 Subject: [PATCH] feature: astyle reformat option dialog --- RedPandaIDE/RedPandaIDE.pro | 1 + RedPandaIDE/RedPandaIDE_zh_CN.ts | 252 +++++++++--------- RedPandaIDE/codes.qrc | 5 + RedPandaIDE/settings.cpp | 193 ++++++++++++++ RedPandaIDE/settings.h | 1 + .../settingsdialog/formattergeneralwidget.cpp | 193 ++++++++------ .../settingsdialog/formattergeneralwidget.h | 5 + .../settingsdialog/formattergeneralwidget.ui | 24 +- RedPandaIDE/utils.cpp | 5 +- RedPandaIDE/utils.h | 6 +- 10 files changed, 462 insertions(+), 223 deletions(-) create mode 100644 RedPandaIDE/codes.qrc diff --git a/RedPandaIDE/RedPandaIDE.pro b/RedPandaIDE/RedPandaIDE.pro index d2c7931a..be2b4711 100644 --- a/RedPandaIDE/RedPandaIDE.pro +++ b/RedPandaIDE/RedPandaIDE.pro @@ -185,6 +185,7 @@ else: unix:!android: target.path = /opt/$${TARGET}/bin !isEmpty(target.path): INSTALLS += target RESOURCES += \ + codes.qrc \ colorschemes.qrc \ themes/dark/dark.qrc \ themes/light/light.qrc \ diff --git a/RedPandaIDE/RedPandaIDE_zh_CN.ts b/RedPandaIDE/RedPandaIDE_zh_CN.ts index e8e4ac5d..0904f181 100644 --- a/RedPandaIDE/RedPandaIDE_zh_CN.ts +++ b/RedPandaIDE/RedPandaIDE_zh_CN.ts @@ -573,20 +573,20 @@ Are you really want to continue? 要剪切的内容超过了字符数限制! - - - + + + Ctrl+click for more info Ctrl+单击以获取更多信息 - - + + Symbol '%1' not found! 未找到符号'%1'! - + Readonly 只读 @@ -1319,371 +1319,371 @@ Are you really want to continue? 预定义排版方案 - + Basic 基本 - + Brace modifications 花括号修改 - + Attach braces to namespace statements Attach spaces to namespace statements 添加花括号到命名空间(namespace)定义 - + Attach braces to classes Attach spaces to classes 添加花括号到类(class)定义 - + Attach braces to class inline function definitions Attach spaces to class inline function definitions 添加花括号到类内联函数(inline function)定义 - + Attach braces to extern "C" statements Attach spaces to extern "C" statements 添加花括号到extern "C"语句 - + Attach the closing while of do-while to the close brace 将do-while语句的while部分与结尾花括号合并 - + Convert tabs to the appropriate number of spaces 将制表符(Tab)转换为相应数量的空格 - + Indentation 1 缩进1 - + Indent with: 使用什么符号缩进: - + Indent using spaces 使用空格缩进 - + Spaces 空格 - + Indent using tabs 使用制表符缩进 - + Tabs 制表符 - + Tab Size: 缩进宽度 - + Indent for continuation lines: 连续行的缩进量: - + Minimal indent for a continuous conditional beloning to a conditional header: 属于同一个条件语句('if','while',...)的条件的最小缩进量 - + Maximal indent spaces for a continuation line 连续行的最大缩进空格数: - + Indentation 2 缩进2 - + Indent labels 缩进标签(label) - + Indent class blocks 缩进类定义(class) - + Indent namespaces 缩进命名空间定义(namespace) - + Indent class access modifiers 缩进类访问限制符('public','private'...) - + Indent after parenthesis '(' or assignment '=' 在括号'('或赋值号'='后缩进 - + Indent preprocessor conditional statements 缩进预处理条件指令('#if','#else',...) - + Indent multi-line preprocessor #define statements 缩进多行#define语句 - + Indent line comments that start in column one 缩进在第一列开始的行注释 - + Indent preprocessor blocks 缩进预处理代码段 - + Indent switch blocks 缩进switch语句 - + Indent cases 缩进case语句 - + Padding 1 填充1 - + Insert spaces around operators 在运算符周围插入空格 - + Insert spaces after commas 在逗号后插入空格 - + Insert spaces after parenthesis headers ('if','for',...) 在语句和括号间插入空格('if','for'...) - + Insert spaces around parenthesis on the outside only 在括号外侧加入空格 - + Insert spaces around parenthesis on the inside only 在括号内侧加入空格 - + Insert empty lines arround unrelated blocks 在代码段之间加入空行 - + Insert empty lines around all blocks Insert empy lines around all blocks 在所有代码段之间加入空格 - + Insert spaces around first parenthesis in a series on the out side only 在多层嵌套括号的最外侧加入空格 - + Insert spaces around parenthesis 在括号两侧加入空格 - + Padding 2 填充2 - + Remove all empty lines. It will NOT delete lines added by the padding options. 删除所有空行。不影响填充选项自动插入的空行。 - + Attach a pointer operator to its : 将指针运算符('*')放在: - + Remove all consecutive empty lines. It will NOT delete lines added by the padding options. 将连续的多个空行合并成一个。不影响填充选项自动插入的空行。 - - + + type(left) 类型侧(左侧) - + Fill empty lines with the white space of the previous lines. 使用前一行的缩进空格填充空行 - - + + middle 中间 - + <html><head/><body><p>Note for the predefined format style</p></body></html> <html><head/><body><p>预定义排版方案说明</p></body></html> - - + + name(right) 名称侧(右侧) - + Remove unnecessary space adding around parenthesis 删除括号周围的多余空格 - + Attach a reference operator to its : 将引用运算符('&')放在: - - + + none 不变 - + Other 1 其它1 - + Break one line headers ('if','while','else'...) from the statement on the same line 对单行条件('if','while','else')语句分行 - + Add one line braces to unbraced one line conditional statements 用花括号括起单行条件体 - + Break braces before close headers ('else','catch"...) 对结束语句('else','catch')和它之前的花括号断行 - + Remove braces from a braced one line conditional statements 删除括起单行条件体语句的花括号 - + Break 'else if' statements into two lines 将'else if'语句断成两行 - + Add braces to unbraced one line conditional statements 对条件和条件体语句在同一行上的,删除括起条件体语句的花括号 - + Other 2 其它2 - + Attach return type to the function name in its declaration 将函数声明中的函数名和返回值类型放在同一行 - + Break return type from the function name in its declaration 将函数声明中的函数名和返回值类型断成两行 - + Break return type from the function name in its definition 将函数定义中的函数名和返回值类型断成两行 - + Attach return type to the function name in its definition 将函数定义中的函数名和返回值类型放在同一行 - + Don't break blocks residing completely on one line 不对同一行上的多个代码段断行 - + Don't break multimple statements residing on one line 不对同一行上的多条语句断行 - + Other 3 其它3 - + Place the logical conditional to the last on the previous line, when break lines 在断行时,将逻辑条件放在前一行的末尾 - + Close ending angle brackets on template definitions 关闭模板定义末尾的尖括号 - + Remove the leading '*' prefix on multi-line comments and indent the comment text one line indent. 删除多行注释前的'*'并缩进注释内容 - + Break lines exceeds 对超过指定长度的行断行 - + characters 字符 @@ -1691,179 +1691,187 @@ Are you really want to continue? FormatterStyleModel - + Default 缺省 - + The opening braces will not be changed and closing braces will be broken from the preceding line. 左花括号不变,右花括号单独一行。 - + Allman Allman - - - + Broken braces. 花括号单独一行。 - + Java Java - + + Attached braces. + 花括号不断行。 + + + K&R - Kernighan & Ritchie + K&R - + + Linux braces. + Linux风格花括号 + + + Stroustrup - Kernighan & Ritchie + Stroustrup - + Linux braces, with broken closing headers. Linux风格花括号,对条件结束语句(如do-while的while)断行。 - + Whitesmith Whitesmith - + Broken, indented braces. 花括号独立成行,并且缩进。 - + Indented class blocks and switch blocks. 类代码段和switch代码段缩进。 - + VTK VTK - + Broken, indented braces except for the opening braces. 除左花括号外,其他花括号独立断行且缩进。 - + Ratliff Ratliff - + Attached, indented braces. 花括号不断行,但是缩进 - + GNU GNU - + Broken braces, indented blocks. 花括号短号,代码段缩进 - + Linux Linux - + Linux braces, minimum conditional indent is one-half indent. Linux风格花括号,最小条件缩进为1.5单位。 - + Horstmann Horstmann - + Run-in braces, indented switches. Run-in花括号,switch语句缩进 - + One True Brace One True Brace - + Linux braces, add braces to all conditionals. Linux封号花括号,所有条件语句条件花括号 - + Google Google - + Attached braces, indented class modifiers. 花括号不断行,类访问限定符缩进 - + Mozilla Mozilla - + Linux braces, with broken braces for structs and enums, and attached braces for namespaces. Linux风格花括号, 结构和枚举的花括号断行, 命名空间的花括号不断行。 - + Webkit Webkit - + Linux braces, with attached closing headers. Linux风格花括号, 条件结束语句(如do-while的while)不断行 - + Pico Pico - + Run-in opening braces and attached closing braces. Run-in左花括号,右花括号不断行 - + Uses keep one line blocks and keep one line statements. 启用保持单行代码段和保持多语句单行选项。 - + Lisp Lisp - + Attached opening braces and attached closing braces. 花括号不断行。 - + Uses keep one line statements. 启用保持多语句单行选项。 @@ -2912,7 +2920,7 @@ Are you really want to continue? 无标题 - + Index %1 out of range 下标"%1"越界 diff --git a/RedPandaIDE/codes.qrc b/RedPandaIDE/codes.qrc new file mode 100644 index 00000000..8c48bfa0 --- /dev/null +++ b/RedPandaIDE/codes.qrc @@ -0,0 +1,5 @@ + + + resources/formatdemo.cpp + + diff --git a/RedPandaIDE/settings.cpp b/RedPandaIDE/settings.cpp index 81157740..b4114415 100644 --- a/RedPandaIDE/settings.cpp +++ b/RedPandaIDE/settings.cpp @@ -2704,6 +2704,199 @@ Settings::CodeFormatter::CodeFormatter(Settings *settings): } +QStringList Settings::CodeFormatter::getArguments() +{ + QStringList result; + switch(mBraceStyle) { + case FormatterBraceStyle::fbsDefault: + break; + case FormatterBraceStyle::fbsAllman: + result.append("--style=allman"); + break; + case FormatterBraceStyle::fbsJava: + result.append("--style=java"); + break; + case FormatterBraceStyle::fbsKR: + result.append("--style=kr"); + break; + case FormatterBraceStyle::fbsStroustrup: + result.append("--style=stroustrup"); + break; + case FormatterBraceStyle::fbsWitesmith: + result.append("--style=whitesmith"); + break; + case FormatterBraceStyle::fbsVtk: + result.append("--style=vtk"); + break; + case FormatterBraceStyle::fbsRatliff: + result.append("--style=ratliff"); + break; + case FormatterBraceStyle::fbsGNU: + result.append("--style=gnu"); + break; + case FormatterBraceStyle::fbsLinux: + result.append("--style=linux"); + break; + case FormatterBraceStyle::fbsHorstmann: + result.append("--style=horstmann"); + break; + case FormatterBraceStyle::fbs1TBS: + result.append("--style=1tbs"); + break; + case FormatterBraceStyle::fbsGoogle: + result.append("--style=google"); + break; + case FormatterBraceStyle::fbsMozilla: + result.append("--style=mozilla"); + break; + case FormatterBraceStyle::fbsWebkit: + result.append("--style=webkit"); + break; + case FormatterBraceStyle::fbsPico: + result.append("--style=pico"); + break; + case FormatterBraceStyle::fbsLisp: + result.append("--style=lisp"); + break; + }; + switch(mIndentStyle) { + case FormatterIndentType::fitTab: + result.append(QString("--indent=tab=%1").arg(mTabWidth)); + break; + case FormatterIndentType::fitSpace: + result.append(QString("--indent=spaces=%1").arg(mTabWidth)); + break; + } + if (mAttachNamespaces) + result.append("--attach-namespaces"); + if (mAttachClasses) + result.append("--attach-classes"); + if (mAttachInlines) + result.append("--attach-inlines"); + if (mAttachExternC) + result.append("--attach-extern-c"); + if (mAttachClosingWhile) + result.append("--attach-closing-while"); + if (mIndentClasses) + result.append("--indent-classes"); + if (mIndentModifiers) + result.append("--indent-modifiers"); + if (mIndentSwitches) + result.append("--indent-switches"); + if (mIndentCases) + result.append("--indent-cases"); + if (mIndentNamespaces) + result.append("--indent-namespaces"); + if (mIndentAfterParens) + result.append("--indent-after-parens"); + if (mIndentContinuation!=1) + result.append(QString("--indent-continuation=%1").arg(mIndentContinuation)); + if (mIndentLabels) + result.append("--indent-labels"); + if (mIndentPreprocBlock) + result.append("--indent-preproc-block"); + if (mIndentPreprocCond) + result.append("--indent-preproc-cond"); + if (mIndentPreprocDefine) + result.append("--indent-preproc-define"); + if (mIndentCol1Comments) + result.append("--indent-col1-comments"); + if (mMinConditionalIndent!=2) + result.append(QString("--min-conditional-indent=%1").arg(mMinConditionalIndent)); + if (mMaxContinuationIndent!=40) + result.append(QString("--max-continuation-indent=%1").arg(mMaxContinuationIndent)); + if (mBreakBlocks) + result.append("--break-blocks"); + if (mBreakBlocksAll) + result.append("--break-blocks=all"); + if (mPadOper) + result.append("--pad-oper"); + if (mPadComma) + result.append("--pad-comma"); + if (mPadParen) + result.append("--pad-paren"); + if (mPadParenOut) + result.append("--pad-paren-out"); + if (mPadFirstParenOut) + result.append("--pad-first-paren-out"); + if (mPadParenIn) + result.append("--pad-paren-in"); + if (mPadHeader) + result.append("--pad-header"); + if (mUnpadParen) + result.append("--unpad-paren"); + if (mDeleteEmptyLines) + result.append("--delete-empty-lines"); + if (mDeleteMultipleEmptyLines) + result.append("--delete-multiple-empty-lines"); + if (mFillEmptyLines) + result.append("--fill-empty-lines"); + switch(mAlignPointerStyle) { + case FormatterOperatorAlign::foaNone: + break; + case FormatterOperatorAlign::foaType: + result.append("--align-pointer=type"); + break; + case FormatterOperatorAlign::foaMiddle: + result.append("--align-pointer=middle"); + break; + case FormatterOperatorAlign::foaName: + result.append("--align-pointer=name"); + break; + } + switch(mAlignReferenceStyle) { + case FormatterOperatorAlign::foaNone: + break; + case FormatterOperatorAlign::foaType: + result.append("--align-reference=type"); + break; + case FormatterOperatorAlign::foaMiddle: + result.append("--align-reference=middle"); + break; + case FormatterOperatorAlign::foaName: + result.append("--align-reference=name"); + break; + } + + if (mBreakClosingBraces) + result.append("--break-closing-braces"); + if (mBreakElseIf) + result.append("--break-elseifs"); + if (mBreakOneLineHeaders) + result.append("--break-one-line-headers"); + if (mAddBraces) + result.append("--add-braces"); + if (mAddOneLineBraces) + result.append("--add-one-line-braces"); + if (mRemoveBraces) + result.append("--remove-braces"); + if (mBreakReturnType) + result.append("--break-return-type"); + if (mBreakReturnTypeDecl) + result.append("--break-return-type-decl"); + if (mAttachReturnType) + result.append("--attach-return-type"); + if (mAttachReturnTypeDecl) + result.append("--attach-return-type-decl"); + if (mKeepOneLineBlocks) + result.append("--keep-one-line-blocks"); + if (mKeepOneLineStatements) + result.append("--keep-one-line-statements"); + if (mConvertTabs) + result.append("--convert-tabs"); + if (mCloseTemplates) + result.append("--close-templates"); + if (mRemoveCommentPrefix) + result.append("--remove-comment-prefix"); + if (mBreakMaxCodeLength) { + result.append(QString("--max-code-length=%1").arg(mMaxCodeLength)); + if (mBreakAfterLogical) + result.append("--break-after-logical"); + } + + return result; +} + int Settings::CodeFormatter::indentStyle() const { return mIndentStyle; diff --git a/RedPandaIDE/settings.h b/RedPandaIDE/settings.h index 0b377926..abd33bc7 100644 --- a/RedPandaIDE/settings.h +++ b/RedPandaIDE/settings.h @@ -468,6 +468,7 @@ public: class CodeFormatter: public _Base { public: explicit CodeFormatter(Settings* settings); + QStringList getArguments(); int braceStyle() const; void setBraceStyle(int newBraceStyle); diff --git a/RedPandaIDE/settingsdialog/formattergeneralwidget.cpp b/RedPandaIDE/settingsdialog/formattergeneralwidget.cpp index f3898ea4..51189aa2 100644 --- a/RedPandaIDE/settingsdialog/formattergeneralwidget.cpp +++ b/RedPandaIDE/settingsdialog/formattergeneralwidget.cpp @@ -11,7 +11,8 @@ FormatterGeneralWidget::FormatterGeneralWidget(const QString& name, const QStrin connect(ui->cbBraceStyle, QOverload::of(&QComboBox::currentIndexChanged), this, &FormatterGeneralWidget::onBraceStyleChanged); ui->editDemo->setReadOnly(true); - ui->editDemo->lines()->setText("int test;"); + connect(this, &SettingsWidget::settingsChanged, + this, &FormatterGeneralWidget::updateDemo); } FormatterGeneralWidget::~FormatterGeneralWidget() @@ -30,7 +31,13 @@ void FormatterGeneralWidget::onBraceStyleChanged() void FormatterGeneralWidget::doLoad() { Settings::CodeFormatter& format = pSettings->codeFormatter(); - ui->cbBraceStyle->setCurrentIndex(format.braceStyle()); + for (int i=0;istyle == format.braceStyle()) { + ui->cbBraceStyle->setCurrentIndex(i); + break; + } + } if (format.indentStyle() == FormatterIndentType::fitSpace) { ui->rbIndentSpaces->setChecked(true); } else { @@ -115,87 +122,13 @@ void FormatterGeneralWidget::doLoad() ui->chkBreakMaxCodeLength->setChecked(format.breakMaxCodeLength()); ui->spinMaxCodeLength->setValue(format.maxCodeLength()); ui->chkBreakAfterLogical->setChecked(format.breakAfterLogical()); + updateDemo(); } void FormatterGeneralWidget::doSave() { Settings::CodeFormatter& format = pSettings->codeFormatter(); - if (ui->cbBraceStyle->currentIndex()>0) - format.setBraceStyle(ui->cbBraceStyle->currentIndex()); - if (ui->rbIndentSpaces) { - format.setIndentStyle(FormatterIndentType::fitSpace); - } else { - format.setIndentStyle(FormatterIndentType::fitTab); - } - format.setTabWidth(ui->spinTabSize->value()); - format.setAttachNamespaces(ui->chkAttachNamespaces->isChecked()); - format.setAttachClasses(ui->chkAttachClasses->isChecked()); - format.setAttachInlines(ui->chkAttachInline->isChecked()); - format.setAttachExternC(ui->chkAttachExternC->isChecked()); - format.setAttachClosingWhile(ui->chkAttachClosingWhile->isChecked()); - format.setIndentClasses(ui->chkIndentClasses->isChecked()); - format.setIndentModifiers(ui->chkIndentModifiers->isChecked()); - format.setIndentSwitches(ui->chkIndentSwiches->isChecked()); - format.setIndentCases(ui->chkIndentCases->isChecked()); - format.setIndentNamespaces(ui->chkIndentNamespaces->isChecked()); - format.setIndentAfterParens(ui->chkIndentAfterParens->isChecked()); - format.setIndentContinuation(ui->spinIndentContinuation->value()); - format.setIndentLabels(ui->chkIndentLabels->isChecked()); - format.setIndentPreprocBlock(ui->chkIndentPreprocBlock->isChecked()); - format.setIndentPreprocCond(ui->chkIndentPreprocCond->isChecked()); - format.setIndentPreprocDefine(ui->chkIndentPreprocDefine->isChecked()); - format.setIndentCol1Comments(ui->chkIndentCol1Comments->isChecked()); - format.setMinConditionalIndent(ui->spinMinConditionalIndent->value()); - format.setMaxContinuationIndent(ui->spinMaxContinuationIndent->value()); - format.setBreakBlocks(ui->chkBreakBlocks->isChecked()); - format.setBreakBlocksAll(ui->chkBreakBlocksAll->isChecked()); - format.setPadOper(ui->chkPadOper->isChecked()); - format.setPadComma(ui->chkPadComma->isChecked()); - format.setPadParen(ui->chkPadParen->isChecked()); - format.setPadParenOut(ui->chkPadParenOut->isChecked()); - format.setPadFirstParenOut(ui->chkPadFirstParenOut->isChecked()); - format.setPadParenIn(ui->chkPadParenIn->isChecked()); - format.setPadHeader(ui->chkPadHeader->isChecked()); - format.setUnpadParen(ui->chkUnpadParen->isChecked()); - format.setDeleteEmptyLines(ui->chkDeleteEmptyLines->isChecked()); - format.setDeleteMultipleEmptyLines(ui->chkDeleteMultipleEmptyLines->isChecked()); - format.setFillEmptyLines(ui->chkFillEmptyLines->isChecked()); - if (ui->rbAlignPointNone->isChecked()) { - format.setAlignPointerStyle(FormatterOperatorAlign::foaNone); - } else if (ui->rbAlignPointType->isChecked()) { - format.setAlignPointerStyle(FormatterOperatorAlign::foaType); - } else if (ui->rbAlignPointerMiddle->isChecked()) { - format.setAlignPointerStyle(FormatterOperatorAlign::foaMiddle); - } else if (ui->rbAlignPointerName->isChecked()) { - format.setAlignPointerStyle(FormatterOperatorAlign::foaName); - } - if (ui->rbAlignReferenceNone->isChecked()) { - format.setAlignReferenceStyle(FormatterOperatorAlign::foaNone); - } else if (ui->rbAlignReferenceType->isChecked()) { - format.setAlignReferenceStyle(FormatterOperatorAlign::foaType); - } else if (ui->rbAlignReferenceMiddle->isChecked()) { - format.setAlignReferenceStyle(FormatterOperatorAlign::foaMiddle); - } else if (ui->rbAlignReferenceName->isChecked()) { - format.setAlignReferenceStyle(FormatterOperatorAlign::foaName); - } - format.setBreakClosingBraces(ui->chkBreakClosingBraces->isChecked()); - format.setBreakElseIf(ui->chkBreakElseIf->isChecked()); - format.setBreakOneLineHeaders(ui->chkBreakOneLineHeaders->isChecked()); - format.setAddBraces(ui->chkAddBraces->isChecked()); - format.setAddOneLineBraces(ui->chkAddOneLineBraces->isChecked()); - format.setRemoveBraces(ui->chkRemoveBraces->isChecked()); - format.setBreakReturnType(ui->chkBreakReturnType->isChecked()); - format.setBreakReturnTypeDecl(ui->chkBreakReturnTypeDecl->isChecked()); - format.setAttachReturnType(ui->chkAttachReturnType->isChecked()); - format.setAttachReturnTypeDecl(ui->chkAttachReturnTypeDecl->isChecked()); - format.setKeepOneLineBlocks(ui->chkKeepOneLineBlocks->isChecked()); - format.setKeepOneLineStatements(ui->chkKeepOneLineStatements->isChecked()); - format.setConvertTabs(ui->chkConvertTabs->isChecked()); - format.setCloseTemplates(ui->chkCloseTemplates->isChecked()); - format.setRemoveCommentPrefix(ui->chkRemoveCommentPrefix->isChecked()); - format.setBreakMaxCodeLength(ui->chkBreakMaxCodeLength->isChecked()); - format.setMaxCodeLength(ui->spinMaxCodeLength->value()); - format.setBreakAfterLogical(ui->chkBreakAfterLogical->isChecked()); + updateCodeFormatter(format); format.save(); } @@ -216,13 +149,13 @@ FormatterStyleModel::FormatterStyleModel(QObject *parent):QAbstractListModel(par mStyles.append( std::make_shared( tr("Java"), - tr("Broken braces."), + tr("Attached braces."), FormatterBraceStyle::fbsJava) ); mStyles.append( std::make_shared( tr("K&R"), - tr("Broken braces."), + tr("Linux braces."), FormatterBraceStyle::fbsKR) ); mStyles.append( @@ -267,7 +200,7 @@ FormatterStyleModel::FormatterStyleModel(QObject *parent):QAbstractListModel(par std::make_shared( tr("Horstmann"), tr("Run-in braces, indented switches."), - FormatterBraceStyle::fbsHorstman) + FormatterBraceStyle::fbsHorstmann) ); mStyles.append( std::make_shared( @@ -362,3 +295,101 @@ void FormatterGeneralWidget::on_chkBreakMaxCodeLength_stateChanged(int) ui->chkBreakAfterLogical->setEnabled(ui->chkBreakMaxCodeLength->isChecked()); } +void FormatterGeneralWidget::updateDemo() +{ + QFile file(":/codes/formatdemo.cpp"); + if (!file.open(QFile::ReadOnly)) + return; + QByteArray content = file.readAll(); + + Settings::CodeFormatter formatter(nullptr); + updateCodeFormatter(formatter); + + QByteArray newContent = runAndGetOutput("astyle.exe", + pSettings->dirs().app(), + formatter.getArguments(), + content); + ui->editDemo->lines()->setText(newContent); +} + +void FormatterGeneralWidget::updateCodeFormatter(Settings::CodeFormatter &format) +{ + PFormatterStyleItem item = mStylesModel.getStyle(ui->cbBraceStyle->currentIndex()); + if (item) + format.setBraceStyle(item->style); + if (ui->rbIndentSpaces) { + format.setIndentStyle(FormatterIndentType::fitSpace); + } else { + format.setIndentStyle(FormatterIndentType::fitTab); + } + format.setTabWidth(ui->spinTabSize->value()); + format.setAttachNamespaces(ui->chkAttachNamespaces->isChecked()); + format.setAttachClasses(ui->chkAttachClasses->isChecked()); + format.setAttachInlines(ui->chkAttachInline->isChecked()); + format.setAttachExternC(ui->chkAttachExternC->isChecked()); + format.setAttachClosingWhile(ui->chkAttachClosingWhile->isChecked()); + format.setIndentClasses(ui->chkIndentClasses->isChecked()); + format.setIndentModifiers(ui->chkIndentModifiers->isChecked()); + format.setIndentSwitches(ui->chkIndentSwiches->isChecked()); + format.setIndentCases(ui->chkIndentCases->isChecked()); + format.setIndentNamespaces(ui->chkIndentNamespaces->isChecked()); + format.setIndentAfterParens(ui->chkIndentAfterParens->isChecked()); + format.setIndentContinuation(ui->spinIndentContinuation->value()); + format.setIndentLabels(ui->chkIndentLabels->isChecked()); + format.setIndentPreprocBlock(ui->chkIndentPreprocBlock->isChecked()); + format.setIndentPreprocCond(ui->chkIndentPreprocCond->isChecked()); + format.setIndentPreprocDefine(ui->chkIndentPreprocDefine->isChecked()); + format.setIndentCol1Comments(ui->chkIndentCol1Comments->isChecked()); + format.setMinConditionalIndent(ui->spinMinConditionalIndent->value()); + format.setMaxContinuationIndent(ui->spinMaxContinuationIndent->value()); + format.setBreakBlocks(ui->chkBreakBlocks->isChecked()); + format.setBreakBlocksAll(ui->chkBreakBlocksAll->isChecked()); + format.setPadOper(ui->chkPadOper->isChecked()); + format.setPadComma(ui->chkPadComma->isChecked()); + format.setPadParen(ui->chkPadParen->isChecked()); + format.setPadParenOut(ui->chkPadParenOut->isChecked()); + format.setPadFirstParenOut(ui->chkPadFirstParenOut->isChecked()); + format.setPadParenIn(ui->chkPadParenIn->isChecked()); + format.setPadHeader(ui->chkPadHeader->isChecked()); + format.setUnpadParen(ui->chkUnpadParen->isChecked()); + format.setDeleteEmptyLines(ui->chkDeleteEmptyLines->isChecked()); + format.setDeleteMultipleEmptyLines(ui->chkDeleteMultipleEmptyLines->isChecked()); + format.setFillEmptyLines(ui->chkFillEmptyLines->isChecked()); + if (ui->rbAlignPointNone->isChecked()) { + format.setAlignPointerStyle(FormatterOperatorAlign::foaNone); + } else if (ui->rbAlignPointType->isChecked()) { + format.setAlignPointerStyle(FormatterOperatorAlign::foaType); + } else if (ui->rbAlignPointerMiddle->isChecked()) { + format.setAlignPointerStyle(FormatterOperatorAlign::foaMiddle); + } else if (ui->rbAlignPointerName->isChecked()) { + format.setAlignPointerStyle(FormatterOperatorAlign::foaName); + } + if (ui->rbAlignReferenceNone->isChecked()) { + format.setAlignReferenceStyle(FormatterOperatorAlign::foaNone); + } else if (ui->rbAlignReferenceType->isChecked()) { + format.setAlignReferenceStyle(FormatterOperatorAlign::foaType); + } else if (ui->rbAlignReferenceMiddle->isChecked()) { + format.setAlignReferenceStyle(FormatterOperatorAlign::foaMiddle); + } else if (ui->rbAlignReferenceName->isChecked()) { + format.setAlignReferenceStyle(FormatterOperatorAlign::foaName); + } + format.setBreakClosingBraces(ui->chkBreakClosingBraces->isChecked()); + format.setBreakElseIf(ui->chkBreakElseIf->isChecked()); + format.setBreakOneLineHeaders(ui->chkBreakOneLineHeaders->isChecked()); + format.setAddBraces(ui->chkAddBraces->isChecked()); + format.setAddOneLineBraces(ui->chkAddOneLineBraces->isChecked()); + format.setRemoveBraces(ui->chkRemoveBraces->isChecked()); + format.setBreakReturnType(ui->chkBreakReturnType->isChecked()); + format.setBreakReturnTypeDecl(ui->chkBreakReturnTypeDecl->isChecked()); + format.setAttachReturnType(ui->chkAttachReturnType->isChecked()); + format.setAttachReturnTypeDecl(ui->chkAttachReturnTypeDecl->isChecked()); + format.setKeepOneLineBlocks(ui->chkKeepOneLineBlocks->isChecked()); + format.setKeepOneLineStatements(ui->chkKeepOneLineStatements->isChecked()); + format.setConvertTabs(ui->chkConvertTabs->isChecked()); + format.setCloseTemplates(ui->chkCloseTemplates->isChecked()); + format.setRemoveCommentPrefix(ui->chkRemoveCommentPrefix->isChecked()); + format.setBreakMaxCodeLength(ui->chkBreakMaxCodeLength->isChecked()); + format.setMaxCodeLength(ui->spinMaxCodeLength->value()); + format.setBreakAfterLogical(ui->chkBreakAfterLogical->isChecked()); +} + diff --git a/RedPandaIDE/settingsdialog/formattergeneralwidget.h b/RedPandaIDE/settingsdialog/formattergeneralwidget.h index 2a080d0a..a263bca9 100644 --- a/RedPandaIDE/settingsdialog/formattergeneralwidget.h +++ b/RedPandaIDE/settingsdialog/formattergeneralwidget.h @@ -5,6 +5,7 @@ #include #include "settingswidget.h" #include "../utils.h" +#include "../settings.h" namespace Ui { class FormatterGeneralWidget; @@ -46,6 +47,10 @@ private slots: void on_chkBreakMaxCodeLength_stateChanged(int arg1); + void updateDemo(); +private: + void updateCodeFormatter(Settings::CodeFormatter& format); + private: Ui::FormatterGeneralWidget *ui; FormatterStyleModel mStylesModel; diff --git a/RedPandaIDE/settingsdialog/formattergeneralwidget.ui b/RedPandaIDE/settingsdialog/formattergeneralwidget.ui index 8306ba84..60ff7373 100644 --- a/RedPandaIDE/settingsdialog/formattergeneralwidget.ui +++ b/RedPandaIDE/settingsdialog/formattergeneralwidget.ui @@ -850,6 +850,12 @@ + + + 0 + 1 + + 0 @@ -864,22 +870,6 @@ - - - - Qt::Vertical - - - QSizePolicy::Expanding - - - - 20 - 40 - - - - @@ -893,7 +883,7 @@ - + diff --git a/RedPandaIDE/utils.cpp b/RedPandaIDE/utils.cpp index dfa2af3a..57f25710 100644 --- a/RedPandaIDE/utils.cpp +++ b/RedPandaIDE/utils.cpp @@ -92,7 +92,7 @@ bool isGreenEdition() return gIsGreenEdition; } -QByteArray runAndGetOutput(const QString &cmd, const QString& workingDir, const QStringList& arguments, bool inheritEnvironment) +QByteArray runAndGetOutput(const QString &cmd, const QString& workingDir, const QStringList& arguments, const QByteArray &inputContent, bool inheritEnvironment) { QProcess process; QByteArray result; @@ -111,6 +111,9 @@ QByteArray runAndGetOutput(const QString &cmd, const QString& workingDir, const result.append(process.readAllStandardOutput()); }); process.start(cmd,arguments); + if (!inputContent.isEmpty()) { + process.write(inputContent); + } process.closeWriteChannel(); process.waitForFinished(); return result; diff --git a/RedPandaIDE/utils.h b/RedPandaIDE/utils.h index 83a56469..2893f275 100644 --- a/RedPandaIDE/utils.h +++ b/RedPandaIDE/utils.h @@ -63,7 +63,7 @@ enum FormatterBraceStyle { fbsRatliff, fbsGNU, fbsLinux, - fbsHorstman, + fbsHorstmann, fbs1TBS, fbsGoogle, fbsMozilla, @@ -111,7 +111,9 @@ const QByteArray GuessTextEncoding(const QByteArray& text); bool isTextAllAscii(const QString& text); -QByteArray runAndGetOutput(const QString& cmd, const QString& workingDir, const QStringList& arguments, bool inheritEnvironment = false); +QByteArray runAndGetOutput(const QString& cmd, const QString& workingDir, const QStringList& arguments, + const QByteArray& inputContent = QByteArray(), + bool inheritEnvironment = false); bool isNonPrintableAsciiChar(char ch);