diff --git a/plugins/astyle/astyle_formatter.cpp b/plugins/astyle/astyle_formatter.cpp index 2e04947fbf..a75c1aea8d 100644 --- a/plugins/astyle/astyle_formatter.cpp +++ b/plugins/astyle/astyle_formatter.cpp @@ -1,524 +1,540 @@ /* This file is part of KDevelop * Copyright (C) 2008 Cédric Pasteur Copyright (C) 2001 Matthias Hölzer-Klüpfel This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; see the file COPYING. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "astyle_formatter.h" #include #include #include #include "astyle_stringiterator.h" #include "debug.h" using namespace KDevelop; AStyleFormatter::AStyleFormatter() : ASFormatter() { } QString AStyleFormatter::formatSource(const QString &text, const QString& leftContext, const QString& rightContext) { QString useText = leftContext + text + rightContext; AStyleStringIterator is(useText); QString output; QTextStream os(&output, QIODevice::WriteOnly); init(&is); while(hasMoreLines()) os << QString::fromUtf8(nextLine().c_str()) << endl; init(nullptr); return extractFormattedTextFromContext(output, text, leftContext, rightContext, m_options[QStringLiteral("FillCount")].toInt()); } void AStyleFormatter::setOption(const QString &key, const QVariant &value) { m_options[key] = value; } void AStyleFormatter::updateFormatter() { qCDebug(KDEV_ASTYLE) << "Updating option with: " << ISourceFormatter::optionMapToString(m_options) << endl; // fill int wsCount = m_options[QStringLiteral("FillCount")].toInt(); if(m_options[QStringLiteral("Fill")].toString() == QLatin1String("Tabs")) { ///TODO: rename FillForce somehow... bool force = m_options[QStringLiteral("FillForce")].toBool(); AStyleFormatter::setTabSpaceConversionMode(false); AStyleFormatter::setTabIndentation(wsCount, force ); m_indentString = QStringLiteral("\t"); } else { AStyleFormatter::setSpaceIndentation(wsCount); m_indentString.fill(QLatin1Char(' '), wsCount); AStyleFormatter::setTabSpaceConversionMode(m_options[QStringLiteral("FillForce")].toBool()); } AStyleFormatter::setEmptyLineFill(m_options[QStringLiteral("Fill_EmptyLines")].toBool()); // indent AStyleFormatter::setSwitchIndent(m_options[QStringLiteral("IndentSwitches")].toBool()); AStyleFormatter::setClassIndent(m_options[QStringLiteral("IndentClasses")].toBool()); AStyleFormatter::setCaseIndent(m_options[QStringLiteral("IndentCases")].toBool()); AStyleFormatter::setBracketIndent(m_options[QStringLiteral("IndentBrackets")].toBool()); AStyleFormatter::setNamespaceIndent(m_options[QStringLiteral("IndentNamespaces")].toBool()); AStyleFormatter::setLabelIndent(m_options[QStringLiteral("IndentLabels")].toBool()); AStyleFormatter::setBlockIndent(m_options[QStringLiteral("IndentBlocks")].toBool()); AStyleFormatter::setPreprocessorIndent(m_options[QStringLiteral("IndentPreprocessors")].toBool()); + AStyleFormatter::setAfterParens(m_options[QStringLiteral("AfterParens")].toBool()); + AStyleFormatter::setContinuation(m_options[QStringLiteral("Continuation")].toInt()); // continuation AStyleFormatter::setMaxInStatementIndentLength(m_options[QStringLiteral("MaxStatement")].toInt()); if(m_options[QStringLiteral("MinConditional")].toInt() != -1) AStyleFormatter::setMinConditionalIndentLength(m_options[QStringLiteral("MinConditional")].toInt()); // brackets QString s = m_options[QStringLiteral("Brackets")].toString(); if(s == QLatin1String("Break")) AStyleFormatter::setBracketFormatMode(astyle::BREAK_MODE); else if(s == QLatin1String("Attach")) AStyleFormatter::setBracketFormatMode(astyle::ATTACH_MODE); else if(s == QLatin1String("Linux")) AStyleFormatter::setBracketFormatMode(astyle::LINUX_MODE); else if(s == QLatin1String("Stroustrup")) // In astyle 2.06 BracketMode STROUSTRUP_MODE was removed and LINUX_MODE is the replacement AStyleFormatter::setBracketFormatMode(astyle::LINUX_MODE); else if(s == QLatin1String("Horstmann") || s == QLatin1String("RunInMode")) AStyleFormatter::setBracketFormatMode(astyle::RUN_IN_MODE); else AStyleFormatter::setBracketFormatMode(astyle::NONE_MODE); AStyleFormatter::setBreakClosingHeaderBracketsMode(m_options[QStringLiteral("BracketsCloseHeaders")].toBool()); // blocks AStyleFormatter::setBreakBlocksMode(m_options[QStringLiteral("BlockBreak")].toBool()); AStyleFormatter::setBreakClosingHeaderBlocksMode(m_options[QStringLiteral("BlockBreakAll")].toBool()); AStyleFormatter::setBreakElseIfsMode(m_options[QStringLiteral("BlockIfElse")].toBool()); // padding AStyleFormatter::setOperatorPaddingMode(m_options[QStringLiteral("PadOperators")].toBool()); AStyleFormatter::setParensInsidePaddingMode(m_options[QStringLiteral("PadParenthesesIn")].toBool()); AStyleFormatter::setParensOutsidePaddingMode(m_options[QStringLiteral("PadParenthesesOut")].toBool()); AStyleFormatter::setParensHeaderPaddingMode(m_options[QStringLiteral("PadParenthesesHeader")].toBool()); AStyleFormatter::setParensUnPaddingMode(m_options[QStringLiteral("PadParenthesesUn")].toBool()); // oneliner AStyleFormatter::setBreakOneLineBlocksMode(!m_options[QStringLiteral("KeepBlocks")].toBool()); AStyleFormatter::setBreakOneLineStatementsMode(!m_options[QStringLiteral("KeepStatements")].toBool()); // pointer s = m_options[QStringLiteral("PointerAlign")].toString(); if(s == QLatin1String("Name")) AStyleFormatter::setPointerAlignment(astyle::PTR_ALIGN_NAME); else if(s == QLatin1String("Middle")) AStyleFormatter::setPointerAlignment(astyle::PTR_ALIGN_MIDDLE); else if(s == QLatin1String("Type")) AStyleFormatter::setPointerAlignment(astyle::PTR_ALIGN_TYPE); else AStyleFormatter::setPointerAlignment(astyle::PTR_ALIGN_NONE); } void AStyleFormatter::resetStyle() { setSpaceIndentation(4); setBracketFormatMode(astyle::NONE_MODE); setBreakOneLineBlocksMode(true); setBreakOneLineStatementsMode(true); // blocks setBreakBlocksMode(false); setBreakClosingHeaderBlocksMode(false); setBreakElseIfsMode(false); setBreakClosingHeaderBracketsMode(false); //indent setTabIndentation(4, false); setEmptyLineFill(false); setMaxInStatementIndentLength(40); setMinConditionalIndentLength(-1); setSwitchIndent(true); setClassIndent(true); setCaseIndent(false); setBracketIndent(false); setNamespaceIndent(true); setLabelIndent(true); setBlockIndent(false); setPreprocessorIndent(false); + setAfterParens(false); + setContinuation(1); //padding setOperatorPaddingMode(false); setParensInsidePaddingMode(true); setParensOutsidePaddingMode(true); setParensHeaderPaddingMode(true); setParensUnPaddingMode(true); } bool AStyleFormatter::predefinedStyle( const QString & style ) { if(style == QLatin1String("ANSI")) { resetStyle(); setBracketIndent(false); setSpaceIndentation(4); setBracketFormatMode(astyle::BREAK_MODE); setClassIndent(false); setSwitchIndent(false); setNamespaceIndent(false); return true; } else if(style == QLatin1String("K&R")) { resetStyle(); setBracketIndent(false); setSpaceIndentation(4); setBracketFormatMode(astyle::ATTACH_MODE); setClassIndent(false); setSwitchIndent(false); setNamespaceIndent(false); return true; } else if(style == QLatin1String("Linux")) { resetStyle(); setBracketIndent(false); setSpaceIndentation(8); setBracketFormatMode(astyle::LINUX_MODE); setClassIndent(false); setSwitchIndent(false); setNamespaceIndent(false); return true; } else if(style == QLatin1String("GNU")) { resetStyle(); setBlockIndent(true); setSpaceIndentation(2); setBracketFormatMode(astyle::BREAK_MODE); setClassIndent(false); setSwitchIndent(false); setNamespaceIndent(false); return true; } else if(style == QLatin1String("Java")) { resetStyle(); setJavaStyle(); setBracketIndent(false); setSpaceIndentation(4); setBracketFormatMode(astyle::ATTACH_MODE); setSwitchIndent(false); return true; } else if (style == QLatin1String("Stroustrup")) { resetStyle(); setBracketFormatMode(astyle::LINUX_MODE); setBlockIndent(false); setBracketIndent(false); setSpaceIndentation(5); setClassIndent(false); setSwitchIndent(false); setNamespaceIndent(false); return true; } else if (style == QLatin1String("Horstmann")) { resetStyle(); setBracketFormatMode(astyle::RUN_IN_MODE); setBlockIndent(false); setBracketIndent(false); setSwitchIndent(true); setSpaceIndentation(3); setClassIndent(false); setNamespaceIndent(false); return true; } else if (style == QLatin1String("Whitesmith")) { resetStyle(); setSpaceIndentation(4); setBracketFormatMode(astyle::BREAK_MODE); setBlockIndent(false); setBracketIndent(true); setClassIndent(true); setSwitchIndent(true); setNamespaceIndent(false); return true; } else if (style == QLatin1String("Banner")) { resetStyle(); setSpaceIndentation(4); setBracketFormatMode(astyle::ATTACH_MODE); setBlockIndent(false); setBracketIndent(true); setClassIndent(true); setSwitchIndent(true); setNamespaceIndent(false); return true; } else if (style == QLatin1String("1TBS")) { resetStyle(); setSpaceIndentation(4); setBracketFormatMode(astyle::LINUX_MODE); setBlockIndent(false); setBracketIndent(false); setAddBracketsMode(true); setClassIndent(false); setSwitchIndent(false); setNamespaceIndent(false); return true; } else if (style == QLatin1String("KDELibs")) { // https://community.kde.org/Policies/Kdelibs_Coding_Style resetStyle(); setSpaceIndentation(4); setBracketFormatMode(astyle::LINUX_MODE); setPointerAlignment(astyle::PTR_ALIGN_TYPE); setLabelIndent(true); setOperatorPaddingMode(true); setParensInsidePaddingMode(false); setParensOutsidePaddingMode(false); setParensHeaderPaddingMode(true); setParensUnPaddingMode(true); setBreakOneLineStatementsMode(false); setTabSpaceConversionMode(true); setPreprocessorIndent(true); setSwitchIndent(false); setClassIndent(false); setNamespaceIndent(false); return true; } else if (style == QLatin1String("Qt")) { // https://wiki.qt.io/Qt_Coding_Style resetStyle(); setPointerAlignment(astyle::PTR_ALIGN_NAME); setOperatorPaddingMode(true); setBracketFormatMode(astyle::LINUX_MODE); setSwitchIndent(false); setParensInsidePaddingMode(false); setParensOutsidePaddingMode(false); setParensHeaderPaddingMode(true); setParensUnPaddingMode(true); setSpaceIndentation(4); setClassIndent(false); setNamespaceIndent(false); return true; } return false; } QVariant AStyleFormatter::option(const QString &key) { if(!m_options.contains(key)) qCDebug(KDEV_ASTYLE) << "Missing option name " << key << endl; return m_options[key]; } QString AStyleFormatter::indentString() { return QString::fromUtf8(getIndentString().c_str()); } void AStyleFormatter::loadStyle(const QString &content) { m_options = ISourceFormatter::stringToOptionMap(content); updateFormatter(); } QString AStyleFormatter::saveStyle() { return ISourceFormatter::optionMapToString(m_options); } void AStyleFormatter::setTabIndentation(int length, bool forceTabs) { ASFormatter::setTabIndentation(length, forceTabs); m_options[QStringLiteral("Fill")] = QStringLiteral("Tabs"); m_options[QStringLiteral("FillForce")] = forceTabs; m_options[QStringLiteral("FillCount")] = length; } void AStyleFormatter::setSpaceIndentation(int length) { ASFormatter::setSpaceIndentation(length); m_options[QStringLiteral("Fill")] = QStringLiteral("Spaces"); m_options[QStringLiteral("FillCount")] = length; } void AStyleFormatter::setTabSpaceConversionMode(bool mode) { m_options[QStringLiteral("FillForce")] = mode; ASFormatter::setTabSpaceConversionMode(mode); } void AStyleFormatter::setFillEmptyLines(bool on) { m_options[QStringLiteral("FillEmptyLines")] = on; ASFormatter::setEmptyLineFill(on); } void AStyleFormatter::setBlockIndent(bool on) { m_options[QStringLiteral("IndentBlocks")] = on; ASFormatter::setBlockIndent(on); } void AStyleFormatter::setBracketIndent(bool on) { m_options[QStringLiteral("IndentBrackets")] = on; ASFormatter::setBraceIndent(on); } void AStyleFormatter::setCaseIndent(bool on) { m_options[QStringLiteral("IndentCases")] = on; ASFormatter::setCaseIndent(on); } void AStyleFormatter::setClassIndent(bool on) { m_options[QStringLiteral("IndentClasses")] = on; ASFormatter::setClassIndent(on); } void AStyleFormatter::setLabelIndent(bool on) { m_options[QStringLiteral("IndentLabels")] = on; ASFormatter::setLabelIndent(on); } void AStyleFormatter::setNamespaceIndent(bool on) { m_options[QStringLiteral("IndentNamespaces")] = on; ASFormatter::setNamespaceIndent(on); } void AStyleFormatter::setPreprocessorIndent(bool on) { m_options[QStringLiteral("IndentPreprocessors")] = on; ASFormatter::setPreprocDefineIndent(on); } void AStyleFormatter::setSwitchIndent(bool on) { m_options[QStringLiteral("IndentSwitches")] = on; ASFormatter::setSwitchIndent(on); } void AStyleFormatter::setMaxInStatementIndentLength(int max) { m_options[QStringLiteral("MaxStatement")] = max; ASFormatter::setMaxInStatementIndentLength(max); } void AStyleFormatter::setMinConditionalIndentLength(int min) { m_options[QStringLiteral("MinConditional")] = min; ASFormatter::setMinConditionalIndentOption(min); ASFormatter::setMinConditionalIndentLength(); } +void AStyleFormatter::setAfterParens(bool on) +{ + m_options[QStringLiteral("AfterParens")] = on; + ASFormatter::setAfterParenIndent(on); +} + +void AStyleFormatter::setContinuation(int n) +{ + m_options[QStringLiteral("Continuation")] = n; + ASFormatter::setContinuationIndentation(n); +} + void AStyleFormatter::setBracketFormatMode(astyle::BraceMode mode) { switch (mode) { case astyle::NONE_MODE: m_options[QStringLiteral("Brackets")] = QString(); break; case astyle::ATTACH_MODE: m_options[QStringLiteral("Brackets")] = QStringLiteral("Attach"); break; case astyle::BREAK_MODE: m_options[QStringLiteral("Brackets")] = QStringLiteral("Break"); break; case astyle::LINUX_MODE: m_options[QStringLiteral("Brackets")] = QStringLiteral("Linux"); break; case astyle::RUN_IN_MODE: m_options[QStringLiteral("Brackets")] = QStringLiteral("RunInMode"); break; } ASFormatter::setBraceFormatMode(mode); } void AStyleFormatter::setBreakClosingHeaderBracketsMode(bool state) { m_options[QStringLiteral("BracketsCloseHeaders")] = state; ASFormatter::setBreakClosingHeaderBracketsMode(state); } void AStyleFormatter::setBreakBlocksMode(bool state) { m_options[QStringLiteral("BlockBreak")] = state; ASFormatter::setBreakBlocksMode(state); } void AStyleFormatter::setBreakElseIfsMode(bool state) { m_options[QStringLiteral("BlockIfElse")] = state; ASFormatter::setBreakElseIfsMode(state); } void AStyleFormatter::setBreakClosingHeaderBlocksMode(bool state) { m_options[QStringLiteral("BlockBreakAll")] = state; ASFormatter::setBreakClosingHeaderBlocksMode(state); } void AStyleFormatter::setOperatorPaddingMode(bool mode) { m_options[QStringLiteral("PadOperators")] = mode; ASFormatter::setOperatorPaddingMode(mode); } void AStyleFormatter::setParensOutsidePaddingMode(bool mode) { m_options[QStringLiteral("PadParenthesesOut")] = mode; ASFormatter::setParensOutsidePaddingMode(mode); } void AStyleFormatter::setParensInsidePaddingMode(bool mode) { m_options[QStringLiteral("PadParenthesesIn")] = mode; ASFormatter::setParensInsidePaddingMode(mode); } void AStyleFormatter::setParensHeaderPaddingMode(bool mode) { m_options[QStringLiteral("PadParenthesesHeader")] = mode; ASFormatter::setParensHeaderPaddingMode(mode); } void AStyleFormatter::setParensUnPaddingMode(bool state) { m_options[QStringLiteral("PadParenthesesUn")] = state; ASFormatter::setParensUnPaddingMode(state); } void AStyleFormatter::setBreakOneLineBlocksMode(bool state) { m_options[QStringLiteral("KeepBlocks")] = !state; ASFormatter::setBreakOneLineBlocksMode(state); } void AStyleFormatter::setBreakOneLineStatementsMode(bool state) { m_options[QStringLiteral("KeepStatements")] = !state; ASFormatter::setBreakOneLineStatementsMode(state); } void AStyleFormatter::setPointerAlignment(astyle::PointerAlign alignment) { switch (alignment) { case astyle::PTR_ALIGN_NONE: m_options[QStringLiteral("PointerAlign")] = QStringLiteral("None"); break; case astyle::PTR_ALIGN_NAME: m_options[QStringLiteral("PointerAlign")] = QStringLiteral("Name"); break; case astyle::PTR_ALIGN_MIDDLE: m_options[QStringLiteral("PointerAlign")] = QStringLiteral("Middle"); break; case astyle::PTR_ALIGN_TYPE: m_options[QStringLiteral("PointerAlign")] = QStringLiteral("Type"); break; } ASFormatter::setPointerAlignment(alignment); } diff --git a/plugins/astyle/astyle_formatter.h b/plugins/astyle/astyle_formatter.h index e125d3f85f..f23826755d 100644 --- a/plugins/astyle/astyle_formatter.h +++ b/plugins/astyle/astyle_formatter.h @@ -1,90 +1,92 @@ /* This file is part of KDevelop * Copyright (C) 2008 Cédric Pasteur Copyright (C) 2001 Matthias Hölzer-Klüpfel This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; see the file COPYING. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #ifndef ASTYLEFORMATTER_H #define ASTYLEFORMATTER_H #include #include #include "astyle.h" class AStyleFormatter : public astyle::ASFormatter { public: /** Creates an empty AStyleFormatter with C style by default. */ AStyleFormatter(); QString formatSource(const QString& text, const QString& leftContext = QString(), const QString& rightContext = QString()); QVariant option(const QString &name); void setOption(const QString &key, const QVariant &value); QString indentString(); bool predefinedStyle(const QString &name); void loadStyle(const QString &content); QString saveStyle(); // indent void setTabIndentation(int length, bool forceTabs); void setSpaceIndentation(int length); void setTabSpaceConversionMode(bool mode); void setFillEmptyLines(bool on); void setBlockIndent(bool on); void setBracketIndent(bool on); void setCaseIndent(bool on); void setClassIndent(bool on); void setLabelIndent(bool on); void setNamespaceIndent(bool on); void setPreprocessorIndent(bool on); void setSwitchIndent(bool on); void setMaxInStatementIndentLength(int max); void setMinConditionalIndentLength(int min); + void setAfterParens(bool on); + void setContinuation(int n); //brackets void setBracketFormatMode(astyle::BraceMode mode); void setBreakClosingHeaderBracketsMode(bool state); //blocks void setBreakBlocksMode(bool state); void setBreakElseIfsMode(bool state); void setBreakClosingHeaderBlocksMode(bool state); //padding void setOperatorPaddingMode(bool mode); void setParensOutsidePaddingMode(bool mode); void setParensInsidePaddingMode(bool mode); void setParensHeaderPaddingMode(bool mode); void setParensUnPaddingMode(bool state); //oneliners void setBreakOneLineBlocksMode(bool state); void setBreakOneLineStatementsMode(bool state); //pointer void setPointerAlignment(astyle::PointerAlign alignment); protected: void updateFormatter(); void resetStyle(); private: QString m_indentString; QVariantMap m_options; }; #endif // ASTYLEFORMATTER_H diff --git a/plugins/astyle/astyle_preferences.cpp b/plugins/astyle/astyle_preferences.cpp index ca3123ba32..156923b64c 100644 --- a/plugins/astyle/astyle_preferences.cpp +++ b/plugins/astyle/astyle_preferences.cpp @@ -1,433 +1,450 @@ /* This file is part of KDevelop * Copyright (C) 2008 Cédric Pasteur Copyright (C) 2001 Matthias Hölzer-Klüpfel This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; see the file COPYING. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "astyle_preferences.h" #include "astyle_formatter.h" #include "astyle_plugin.h" using namespace KDevelop; namespace { const int INDENT_BLOCK = 0; const int INDENT_BRACKET = 1; const int INDENT_CASE = 2; const int INDENT_CLASS = 3; const int INDENT_LABEL = 4; const int INDENT_NAMESPACE = 5; const int INDENT_PREPROCESSOR = 6; const int INDENT_SWITCH = 7; const int PADDING_NOCHANGE = 0; const int PADDING_NO = 1; const int PADDING_IN = 2; const int PADDING_OUT = 3; const int PADDING_INOUT = 4; const int INDENT_TABS = 0; const int INDENT_TABSFORCE = 1; const int INDENT_SPACES = 2; const int BRACKET_NOCHANGE = 0; const int BRACKET_ATTACH = 1; const int BRACKET_BREAK = 2; const int BRACKET_LINUX = 3; const int BRACKET_RUNINMODE = 4; const int POINTERALIGN_NOCHANGE = 0; const int POINTERALIGN_NAME = 1; const int POINTERALIGN_MIDDLE = 2; const int POINTERALIGN_TYPE = 3; } AStylePreferences::AStylePreferences(Language lang, QWidget *parent) : SettingsWidget(parent) , m_formatter(new AStyleFormatter) , m_currentLanguage(lang) { setupUi(this); switch(lang) { case AStylePreferences::CPP: case AStylePreferences::ObjC: m_formatter->setCStyle(); break; case AStylePreferences::Java: m_formatter->setJavaStyle(); break; case AStylePreferences::CSharp: m_formatter->setSharpStyle(); break; } m_enableWidgetSignals = true; init(); } AStylePreferences::~AStylePreferences( ) { } void AStylePreferences::init() { // setup list widget to have checked items for(int i = 0; i < listIdentObjects->count(); i++) { QListWidgetItem *item = listIdentObjects->item(i); item->setFlags(Qt::ItemIsUserCheckable|Qt::ItemIsEnabled); item->setCheckState(Qt::Checked); } connect(tabWidget, &QTabWidget::currentChanged, this, &AStylePreferences::currentTabChanged); connect(cbIndentType, QOverload::of(&KComboBox::currentIndexChanged), this, &AStylePreferences::indentChanged); connect(inpNuberSpaces, QOverload::of(&QSpinBox::valueChanged), this, &AStylePreferences::indentChanged); connect(chkConvertTabs, &QCheckBox::stateChanged, this, &AStylePreferences::indentChanged); connect(chkFillEmptyLines, &QCheckBox::stateChanged, this, &AStylePreferences::indentChanged); connect(listIdentObjects, &QListWidget::itemChanged, this, &AStylePreferences::indentObjectsChanged); connect(inpMaxStatement, QOverload::of(&QSpinBox::valueChanged), this, &AStylePreferences::minMaxValuesChanged); connect(inpMinConditional, QOverload::of(&QSpinBox::valueChanged), this, &AStylePreferences::minMaxValuesChanged); connect(cbBrackets, QOverload::of(&KComboBox::currentIndexChanged), this, &AStylePreferences::bracketsChanged); connect(chkBracketsCloseHeaders, &QCheckBox::stateChanged, this, &AStylePreferences::bracketsChanged); connect(chkBlockBreak, &QCheckBox::stateChanged, this, &AStylePreferences::blocksChanged); connect(chkBlockBreakAll, &QCheckBox::stateChanged, this, &AStylePreferences::blocksChanged); connect(chkBlockIfElse, &QCheckBox::stateChanged, this, &AStylePreferences::blocksChanged); connect(cbParenthesisPadding, QOverload::of(&KComboBox::currentIndexChanged), this, &AStylePreferences::paddingChanged); connect(chkPadParenthesisHeader, &QCheckBox::stateChanged, this, &AStylePreferences::paddingChanged); connect(chkPadOperators, &QCheckBox::stateChanged, this, &AStylePreferences::paddingChanged); connect(chkKeepStatements, &QCheckBox::stateChanged, this, &AStylePreferences::onelinersChanged); connect(chkKeepBlocks, &QCheckBox::stateChanged, this, &AStylePreferences::onelinersChanged); connect(cbPointerAlign, QOverload::of(&QComboBox::currentIndexChanged), this, &AStylePreferences::pointerAlignChanged); + + connect(chkAfterParens, &QCheckBox::stateChanged, this, &AStylePreferences::afterParensChanged); + connect(inpContinuation, QOverload::of(&QSpinBox::valueChanged), this, &AStylePreferences::afterParensChanged); } void AStylePreferences::load(const SourceFormatterStyle &style) { if(!style.content().isEmpty()) m_formatter->loadStyle(style.content()); else m_formatter->predefinedStyle(style.name()); updateWidgets(); updatePreviewText(); } QString AStylePreferences::save() { return m_formatter->saveStyle(); } void AStylePreferences::updateWidgets() { // block signals to avoid writing stuff to m_formatter m_enableWidgetSignals = false; //indent if(m_formatter->option(QStringLiteral("Fill")).toString() == QLatin1String("Tabs")) { chkConvertTabs->setEnabled(false); chkConvertTabs->setChecked(false); if(m_formatter->option(QStringLiteral("FillForce")).toBool()) { cbIndentType->setCurrentIndex(INDENT_TABSFORCE); } else { cbIndentType->setCurrentIndex(INDENT_TABS); } } else { cbIndentType->setCurrentIndex(INDENT_SPACES); chkConvertTabs->setEnabled(true); chkConvertTabs->setChecked(m_formatter->option(QStringLiteral("FillForce")).toBool()); } inpNuberSpaces->setValue(m_formatter->option(QStringLiteral("FillCount")).toInt()); chkFillEmptyLines->setChecked(m_formatter->option(QStringLiteral("FillEmptyLines")).toBool()); // indent objects setItemChecked(INDENT_BLOCK, m_formatter->option(QStringLiteral("IndentBlocks")).toBool()); setItemChecked(INDENT_BRACKET, m_formatter->option(QStringLiteral("IndentBrackets")).toBool()); setItemChecked(INDENT_CASE, m_formatter->option(QStringLiteral("IndentCases")).toBool()); setItemChecked(INDENT_CLASS, m_formatter->option(QStringLiteral("IndentClasses")).toBool()); setItemChecked(INDENT_LABEL, m_formatter->option(QStringLiteral("IndentLabels")).toBool()); setItemChecked(INDENT_NAMESPACE, m_formatter->option(QStringLiteral("IndentNamespaces")).toBool()); setItemChecked(INDENT_PREPROCESSOR, m_formatter->option(QStringLiteral("IndentPreprocessors")).toBool()); setItemChecked(INDENT_SWITCH, m_formatter->option(QStringLiteral("IndentSwitches")).toBool()); inpMaxStatement->setValue(m_formatter->option(QStringLiteral("MaxStatement")).toInt()); inpMinConditional->setValue(m_formatter->option(QStringLiteral("MinConditional")).toInt()); + chkAfterParens->setChecked(m_formatter->option(QStringLiteral("AfterParens")).toBool()); + inpContinuation->setValue(m_formatter->option(QStringLiteral("Continuation")).toInt()); + inpContinuation->setEnabled(chkAfterParens->isChecked()); + // brackets QString s = m_formatter->option(QStringLiteral("Brackets")).toString(); if(s == QLatin1String("Attach")) cbBrackets->setCurrentIndex(BRACKET_ATTACH); else if(s == QLatin1String("Break")) cbBrackets->setCurrentIndex(BRACKET_BREAK); else if(s == QLatin1String("Linux")) cbBrackets->setCurrentIndex(BRACKET_LINUX); else cbBrackets->setCurrentIndex(BRACKET_NOCHANGE); chkBracketsCloseHeaders->setChecked( m_formatter->option(QStringLiteral("BracketsCloseHeaders")).toBool()); // blocks chkBlockBreak->setChecked(m_formatter->option(QStringLiteral("BlockBreak")).toBool()); chkBlockBreakAll->setChecked(m_formatter->option(QStringLiteral("BlockBreakAll")).toBool()); chkBlockIfElse->setChecked(m_formatter->option(QStringLiteral("BlockIfElse")).toBool()); // enable or not chkBlockBreakAll chkBlockBreakAll->setEnabled(chkBlockBreak->isChecked()); // padding bool padin = m_formatter->option(QStringLiteral("PadParenthesesIn")).toBool(); bool padout = m_formatter->option(QStringLiteral("PadParenthesesOut")).toBool(); bool unpad = m_formatter->option(QStringLiteral("PadParenthesesUn")).toBool(); if(unpad) { if(padin) { if(padout) cbParenthesisPadding->setCurrentIndex(PADDING_INOUT); else cbParenthesisPadding->setCurrentIndex(PADDING_IN); } else if(padout) cbParenthesisPadding->setCurrentIndex(PADDING_OUT); else cbParenthesisPadding->setCurrentIndex(PADDING_NO); } else cbParenthesisPadding->setCurrentIndex(PADDING_NOCHANGE); // padding header has no influence with padding out if (padout) chkPadParenthesisHeader->setDisabled(true); chkPadParenthesisHeader->setChecked(m_formatter->option(QStringLiteral("PadParenthesesHeader")).toBool()); chkPadOperators->setChecked(m_formatter->option(QStringLiteral("PadOperators")).toBool()); // oneliner chkKeepStatements->setChecked(m_formatter->option(QStringLiteral("KeepStatements")).toBool()); chkKeepBlocks->setChecked(m_formatter->option(QStringLiteral("KeepBlocks")).toBool()); // pointer align s = m_formatter->option(QStringLiteral("PointerAlign")).toString(); if (s == QLatin1String("Name")) cbPointerAlign->setCurrentIndex(POINTERALIGN_NAME); else if (s == QLatin1String("Type")) cbPointerAlign->setCurrentIndex(POINTERALIGN_TYPE); else if (s == QLatin1String("Middle")) cbPointerAlign->setCurrentIndex(POINTERALIGN_MIDDLE); else cbPointerAlign->setCurrentIndex(POINTERALIGN_NOCHANGE); m_enableWidgetSignals = true; // re enable signals } void AStylePreferences::setItemChecked(int idx, bool checked) { QListWidgetItem *item = listIdentObjects->item(idx); if(!item) return; if(checked) item->setCheckState(Qt::Checked); else item->setCheckState(Qt::Unchecked); } void AStylePreferences::updatePreviewText(bool emitChangedSignal) { Q_UNUSED(emitChangedSignal); if(tabWidget->currentIndex() == 0) emit previewTextChanged(AStylePlugin::indentingSample(m_currentLanguage)); else emit previewTextChanged(AStylePlugin::formattingSample(m_currentLanguage)); } void AStylePreferences::currentTabChanged() { updatePreviewText(false); } void AStylePreferences::indentChanged() { if(!m_enableWidgetSignals) return; switch(cbIndentType->currentIndex()) { case INDENT_TABS: m_formatter->setTabSpaceConversionMode( false ); m_formatter->setTabIndentation(inpNuberSpaces->value(), false); chkConvertTabs->setEnabled(false); break; case INDENT_TABSFORCE: m_formatter->setTabSpaceConversionMode( false ); m_formatter->setTabIndentation(inpNuberSpaces->value(), true); chkConvertTabs->setEnabled(false); break; case INDENT_SPACES: m_formatter->setSpaceIndentation(inpNuberSpaces->value()); chkConvertTabs->setEnabled(true); m_formatter->setTabSpaceConversionMode( chkConvertTabs->isEnabled() & chkConvertTabs->isChecked() ); break; } m_formatter->setFillEmptyLines( chkFillEmptyLines->isChecked() ); updatePreviewText(); } void AStylePreferences::indentObjectsChanged(QListWidgetItem *item) { if(!m_enableWidgetSignals) return; if(!item) return; bool checked = (item->checkState() == Qt::Checked); switch(listIdentObjects->row(item)) { case INDENT_BLOCK: m_formatter->setBlockIndent(checked); break; case INDENT_BRACKET: m_formatter->setBracketIndent(checked); break; case INDENT_CASE: m_formatter->setCaseIndent(checked); break; case INDENT_CLASS: m_formatter->setClassIndent(checked); break; case INDENT_LABEL: m_formatter->setLabelIndent(checked); break; case INDENT_NAMESPACE: m_formatter->setNamespaceIndent(checked); break; case INDENT_PREPROCESSOR: m_formatter->setPreprocessorIndent(checked); break; case INDENT_SWITCH: m_formatter->setSwitchIndent(checked); break; } updatePreviewText(); } void AStylePreferences::minMaxValuesChanged() { if(!m_enableWidgetSignals) return; m_formatter->setMaxInStatementIndentLength(inpMaxStatement->value()); m_formatter->setMinConditionalIndentLength(inpMinConditional->value()); updatePreviewText(); } void AStylePreferences::bracketsChanged() { if(!m_enableWidgetSignals) return; switch(cbBrackets->currentIndex()) { case BRACKET_NOCHANGE: m_formatter->setBracketFormatMode(astyle::NONE_MODE); break; case BRACKET_ATTACH: m_formatter->setBracketFormatMode(astyle::ATTACH_MODE); break; case BRACKET_BREAK: m_formatter->setBracketFormatMode(astyle::BREAK_MODE); break; case BRACKET_LINUX: m_formatter->setBracketFormatMode(astyle::LINUX_MODE); break; case BRACKET_RUNINMODE: m_formatter->setBracketFormatMode(astyle::RUN_IN_MODE); break; } m_formatter->setBreakClosingHeaderBracketsMode(chkBracketsCloseHeaders->isChecked()); updatePreviewText(); } void AStylePreferences::blocksChanged() { if(!m_enableWidgetSignals) return; m_formatter->setBreakBlocksMode(chkBlockBreak->isChecked()); m_formatter->setBreakClosingHeaderBlocksMode(chkBlockBreakAll->isChecked()); m_formatter->setBreakElseIfsMode(chkBlockIfElse->isChecked()); chkBlockBreakAll->setEnabled(chkBlockBreak->isChecked()); updatePreviewText(); } void AStylePreferences::paddingChanged() { if(!m_enableWidgetSignals) return; switch(cbParenthesisPadding->currentIndex()) { case PADDING_NOCHANGE: m_formatter->setParensUnPaddingMode(false); m_formatter->setParensInsidePaddingMode(false); m_formatter->setParensOutsidePaddingMode(false); chkPadParenthesisHeader->setDisabled(false); break; case PADDING_NO: m_formatter->setParensUnPaddingMode(true); m_formatter->setParensInsidePaddingMode(false); m_formatter->setParensOutsidePaddingMode(false); chkPadParenthesisHeader->setDisabled(false); break; case PADDING_IN: m_formatter->setParensUnPaddingMode(true); m_formatter->setParensInsidePaddingMode(true); m_formatter->setParensOutsidePaddingMode(false); chkPadParenthesisHeader->setDisabled(false); break; case PADDING_OUT: m_formatter->setParensUnPaddingMode(true); m_formatter->setParensInsidePaddingMode(false); m_formatter->setParensOutsidePaddingMode(true); // padding header has no influence with padding out chkPadParenthesisHeader->setDisabled(true); break; case PADDING_INOUT: m_formatter->setParensUnPaddingMode(true); m_formatter->setParensInsidePaddingMode(true); m_formatter->setParensOutsidePaddingMode(true); // padding header has no influence with padding out chkPadParenthesisHeader->setDisabled(true); break; } m_formatter->setParensHeaderPaddingMode(chkPadParenthesisHeader->isChecked()); m_formatter->setOperatorPaddingMode(chkPadOperators->isChecked()); updatePreviewText(); } void AStylePreferences::onelinersChanged() { if(!m_enableWidgetSignals) return; m_formatter->setBreakOneLineStatementsMode(!chkKeepStatements->isChecked()); m_formatter->setBreakOneLineBlocksMode(!chkKeepBlocks->isChecked()); updatePreviewText(); } void AStylePreferences::pointerAlignChanged() { if(!m_enableWidgetSignals) return; switch (cbPointerAlign->currentIndex()) { case POINTERALIGN_NAME: m_formatter->setPointerAlignment(astyle::PTR_ALIGN_NAME); break; case POINTERALIGN_TYPE: m_formatter->setPointerAlignment(astyle::PTR_ALIGN_TYPE); break; case POINTERALIGN_MIDDLE: m_formatter->setPointerAlignment(astyle::PTR_ALIGN_MIDDLE); break; default: case POINTERALIGN_NOCHANGE: m_formatter->setPointerAlignment(astyle::PTR_ALIGN_NONE); break; } updatePreviewText(); } +void AStylePreferences::afterParensChanged() +{ + if(!m_enableWidgetSignals) + return; + m_formatter->setAfterParens(chkAfterParens->isChecked()); + inpContinuation->setEnabled(chkAfterParens->isChecked()); + m_formatter->setContinuation(inpContinuation->value()); + + updatePreviewText(); +} diff --git a/plugins/astyle/astyle_preferences.h b/plugins/astyle/astyle_preferences.h index cf2ff6553a..8cbabf92c7 100644 --- a/plugins/astyle/astyle_preferences.h +++ b/plugins/astyle/astyle_preferences.h @@ -1,65 +1,66 @@ /* This file is part of KDevelop * Copyright (C) 2008 Cédric Pasteur Copyright (C) 2001 Matthias Hölzer-Klüpfel This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; see the file COPYING. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #ifndef ASTYLEPREFERENCES_H #define ASTYLEPREFERENCES_H #include #include "ui_astyle_preferences.h" class AStyleFormatter; class AStylePreferences : public KDevelop::SettingsWidget, public Ui::AStylePreferences { Q_OBJECT public: enum Language { CPP, Java, CSharp, ObjC}; explicit AStylePreferences(Language lang=CPP, QWidget *parent=nullptr); ~AStylePreferences() override; void load(const KDevelop::SourceFormatterStyle &style) override; QString save() override; protected: void init(); void updatePreviewText(bool emitChangedSignal = true); void setItemChecked(int idx, bool checked); void updateWidgets(); private Q_SLOTS: void currentTabChanged(); void indentChanged(); void indentObjectsChanged(QListWidgetItem *item); void minMaxValuesChanged(); void bracketsChanged(); void blocksChanged(); void paddingChanged(); void onelinersChanged(); void pointerAlignChanged(); + void afterParensChanged(); private: QScopedPointer m_formatter; bool m_enableWidgetSignals; const Language m_currentLanguage; }; #endif // ASTYLEPREFERENCES_H diff --git a/plugins/astyle/astyle_preferences.ui b/plugins/astyle/astyle_preferences.ui index 97275a655b..f5abb22230 100644 --- a/plugins/astyle/astyle_preferences.ui +++ b/plugins/astyle/astyle_preferences.ui @@ -1,584 +1,607 @@ AStylePreferences 0 0 332 - 519 + 600 0 0 0 0 0 Indentation 0 0 Tabs Force tabs Spaces Qt::Horizontal QSizePolicy::Minimum 18 21 true Number of spaces that will be converted to a tab. The number of spaces per tab is controlled by the editor. 1 4 true Convert tabs to spaces. Con&vert tabs into spaces Fill empty lines with the white space of their previous lines. Fill empt&y lines Qt::Vertical 20 10 Indent: 0 0 Blocks Brackets Cases Class Labels Namespaces Preprocessor directives Switches Indent a maximal # spaces in a continuous statement, relative to the previous line. 40 Indent a minimal # spaces in a continuous conditional belonging to a conditional header. Minimum in conditional: false Indent a minimal # spaces in a continuous conditional belonging to a conditional header. Twice current -1 -1 Indent a maximal # spaces in a continuous statement, relative to the previous line. Maximum in statement: false + + + + Number of indents + + + 4 + + + 1 + + + + + + + Indent, instead of align, continuation lines following lines that contain an opening paren '(' or an assignment '='. + + + Continuation lines: + + + Qt::Vertical 20 144 Other Brackets 0 0 Type: true Break brackets before closing headers (e.g. 'else', 'catch', ...) from their immediately preceding closing brackets. Brea&k closing headers 0 0 No change Attach Break Linux Run-In Blocks Insert empty lines around unrelated blocks, labels, classes,... Known problems: 1. If a statement is NOT part of a block, the following statements are all double spaced. Statements enclosed in a block are formatted correctly. 2. Comments are broken from the block. &Break blocks Like --break-blocks, except also insert empty lines around closing headers (e.g. 'else', 'catch', ...). Known problems: 1. If a statement is NOT part of a block, the following statements are all double spaced. Statements enclosed in a block are formatted correctly. 2. Comments are broken from the block. Break all bl&ocks Break 'else if()' statements into two different lines. Break i&f-else Padding 0 0 No change Unpad Inside only Outside only Inside and outside Insert space padding around operators. Once padded, operators stay padded. There is no unpad operator option. &Add spaces around operators Pad parenthesis: Insert space padding after parenthesis headers (e.g. 'if', 'for', 'while', ...) Add spaces after parenthesis &headers One liners Do not break lines containing multiple statements into multiple single-statement lines. &Keep one-line statements Do not break blocks residing completely on one line. Keep o&ne-line blocks Other Pointer Alignment: No change Name Middle Type Qt::Vertical 20 70 KComboBox QComboBox
kcombobox.h