diff --git a/plugins/astyle/astyle_plugin.cpp b/plugins/astyle/astyle_plugin.cpp index 1b26bb6fce..a4c3caa9fa 100644 --- a/plugins/astyle/astyle_plugin.cpp +++ b/plugins/astyle/astyle_plugin.cpp @@ -1,271 +1,394 @@ /* 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_plugin.h" #include #include #include #include #include "astyle_formatter.h" #include "astyle_stringiterator.h" #include "astyle_preferences.h" #include #include +static const char formattingCxxSample[] = + "void func(){\n" + "\tif(isFoo(a,b))\n" + "\tbar(a,b);\n" + "if(isFoo)\n" + "\ta=bar((b-c)*a,*d--);\n" + "if( isFoo( a,b ) )\n" + "\tbar(a, b);\n" + "if (isFoo) {isFoo=false;cat << isFoo <::const_iterator it = list.begin();\n" + "}\n" + "namespace A {\n" + "namespace B {\n" + "class someClass {\n" + "void foo() {\n" + " if (true) {\n" + " func();\n" + " } else {\n" + " // bla\n" + " }\n" + "}\n" + "};\n" + "}\n" + "}\n"; + +static const char formattingObjCSample[] = + "void func(){\n" + "\tif(isFoo(a,b))\n" + "\tbar(a,b);\n" + "if(isFoo)\n" + "\ta=bar((b-c)*a,*d--);\n" + "if( isFoo( a,b ) )\n" + "\tbar(a, b);\n" + "if (isFoo) {isFoo=false;cat << isFoo <\n" + "\n" + "#define foobar(A)\\\n" + "\t{Foo();Bar();}\n" + "#define anotherFoo(B)\\\n" + "\treturn Bar()\n" + "\n" + "@interface Foo : Bar {\n" + "@private\n" + "\tid Baz;\n" + "}\n" + "- (void) init;\n" + "- (NSString*) description;\n" + "@property (retain) id Baz;\n" + "@end\n" + "\n" + "@interface Foo (Bar)\n" + "- (void)bar:(int) foo;\n" + "@end\n" + "\n" + "@implementation Foo (Bar)\n" + "\n" + "- (void) bar:(int) foo\n" + "{\n" + "\tswitch (foo) {\n" + "case 1:\n" + "a += 1;\n" + "break;\n" + "case 2: {\n" + "a += 2;\n" + "break;\n" + "}\n" + "}\n" + "if (isFoo) {\n" + "bar();\n" + "} else {\n" + "[anotherBar withFoo:self];\n" + "}\n" + "}\n" + "\n" + "@end\n" + "int foo()\n" + "while (isFoo)\n" + "{\n" + "\t// ...\n" + "\tgoto error;\n" + "\t/* .... */\n" + "error:\n" + "\t//...\n" + "}\n" + "\n" + "fooArray[] = { red,\n" + "\tgreen,\n" + "\tdarkblue};\n" + "fooFunction(barArg1,\n" + "\tbarArg2,\n" + "\tbarArg3);\n" + "struct foo { int bar() {} };\n"; + using namespace KDevelop; K_PLUGIN_FACTORY_WITH_JSON(AStyleFactory, "kdevastyle.json", registerPlugin();) AStylePlugin::AStylePlugin(QObject *parent, const QVariantList&) : IPlugin(QStringLiteral("kdevastyle"), parent) , m_formatter(new AStyleFormatter()) { currentStyle = predefinedStyles().at(0); } AStylePlugin::~AStylePlugin() { } QString AStylePlugin::name() const { // This needs to match the X-KDE-PluginInfo-Name entry from the .desktop file! return QStringLiteral("kdevastyle"); } QString AStylePlugin::caption() const { return QStringLiteral("Artistic Style"); } QString AStylePlugin::description() const { return i18n("Artistic Style is a source code indenter, formatter," " and beautifier for the C, C++, C# and Java programming languages.
" "Home Page: http://astyle.sourceforge.net"); } QString AStylePlugin::formatSourceWithStyle( SourceFormatterStyle s, const QString& text, const QUrl& /*url*/, const QMimeType& mime, const QString& leftContext, const QString& rightContext ) const { if(mime.inherits(QStringLiteral("text/x-java"))) m_formatter->setJavaStyle(); else if(mime.inherits(QStringLiteral("text/x-csharp"))) m_formatter->setSharpStyle(); else m_formatter->setCStyle(); if( s.content().isEmpty() ) { m_formatter->predefinedStyle( s.name() ); } else { m_formatter->loadStyle( s.content() ); } return m_formatter->formatSource(text, leftContext, rightContext); } QString AStylePlugin::formatSource(const QString& text, const QUrl &url, const QMimeType& mime, const QString& leftContext, const QString& rightContext) const { auto style = ICore::self()->sourceFormatterController()->styleForUrl(url, mime); return formatSourceWithStyle(style, text, url, mime, leftContext, rightContext); } static SourceFormatterStyle::MimeList supportedMimeTypes() { return { {QStringLiteral("text/x-c++src"), QStringLiteral("C++")}, + {QStringLiteral("text/x-csrc"), QStringLiteral("C")}, {QStringLiteral("text/x-chdr"), QStringLiteral("C")}, {QStringLiteral("text/x-c++hdr"), QStringLiteral("C++")}, - {QStringLiteral("text/x-csrc"), QStringLiteral("C")}, {QStringLiteral("text/x-java"), QStringLiteral("Java")}, {QStringLiteral("text/x-csharp"), QStringLiteral("C#")}, + {QStringLiteral("text/x-objcsrc"), QStringLiteral("Objective-C")}, + {QStringLiteral("text/x-objc++src"), QStringLiteral("Objective-C++")}, + {QStringLiteral("text/x-objchdr"), QStringLiteral("Objective-C")}, }; } SourceFormatterStyle predefinedStyle(const QString& name, const QString& caption = QString()) { SourceFormatterStyle st = SourceFormatterStyle( name ); st.setCaption( caption.isEmpty() ? name : caption ); AStyleFormatter fmt; fmt.predefinedStyle( name ); st.setContent( fmt.saveStyle() ); st.setMimeTypes(supportedMimeTypes()); st.setUsePreview(true); return st; } QVector AStylePlugin::predefinedStyles() const { return { predefinedStyle(QStringLiteral("ANSI")), predefinedStyle(QStringLiteral("GNU")), predefinedStyle(QStringLiteral("Java")), predefinedStyle(QStringLiteral("K&R"), QStringLiteral("Kernighan & Ritchie")), predefinedStyle(QStringLiteral("Linux")), predefinedStyle(QStringLiteral("Stroustrup")), predefinedStyle(QStringLiteral("Horstmann")), predefinedStyle(QStringLiteral("Whitesmith")), predefinedStyle(QStringLiteral("Banner")), predefinedStyle(QStringLiteral("1TBS")), predefinedStyle(QStringLiteral("KDELibs")), predefinedStyle(QStringLiteral("Qt")), }; } SettingsWidget* AStylePlugin::editStyleWidget(const QMimeType& mime) const { AStylePreferences::Language lang = AStylePreferences::CPP; - if(mime.inherits(QStringLiteral("text/x-java"))) + if (mime.inherits(QStringLiteral("text/x-java"))) lang = AStylePreferences::Java; - else if(mime.inherits(QStringLiteral("text/x-csharp"))) + else if (mime.inherits(QStringLiteral("text/x-csharp"))) lang = AStylePreferences::CSharp; + else if (mime.inherits(QStringLiteral("text/x-objcsrc")) || mime.inherits(QStringLiteral("text/x-objc++src"))) { + // x-objc++src *should* inherit x-objcsrc but that is not always the case in practice + lang = AStylePreferences::ObjC; + } return new AStylePreferences(lang); } -QString AStylePlugin::previewText(const SourceFormatterStyle& /*style*/, const QMimeType& /*mime*/) const +QString AStylePlugin::previewText(const SourceFormatterStyle& /*style*/, const QMimeType& mime) const { - return QLatin1String("// Indentation\n") + indentingSample() + QLatin1String("\t// Formatting\n") - + formattingSample(); + AStylePreferences::Language lang; + if (mime.inherits(QStringLiteral("text/x-objcsrc")) || mime.inherits(QStringLiteral("text/x-objc++src"))) { + lang = AStylePreferences::ObjC; + } else { + lang = AStylePreferences::CPP; + } + // TODO: add previews for the other supported languages + return + QLatin1String("// Indentation\n") + + indentingSample(lang) + + QLatin1String("\t// Formatting\n") + + formattingSample(lang); } AStylePlugin::Indentation AStylePlugin::indentation(const QUrl& url) const { // Call formatSource first, to initialize the m_formatter data structures according to the URL formatSource(QString(), url, QMimeDatabase().mimeTypeForUrl(url), QString(), QString()); Indentation ret; ret.indentWidth = m_formatter->option(QStringLiteral("FillCount")).toInt(); QString s = m_formatter->option(QStringLiteral("Fill")).toString(); if(s == QLatin1String("Tabs")) { // Do tabs-only indentation ret.indentationTabWidth = ret.indentWidth; }else{ // Don't use tabs at all ret.indentationTabWidth = -1; } return ret; } -QString AStylePlugin::formattingSample() +QString AStylePlugin::formattingSample(AStylePreferences::Language lang) { - return QStringLiteral( - "void func(){\n" - "\tif(isFoo(a,b))\n" - "\tbar(a,b);\n" - "if(isFoo)\n" - "\ta=bar((b-c)*a,*d--);\n" - "if( isFoo( a,b ) )\n" - "\tbar(a, b);\n" - "if (isFoo) {isFoo=false;cat << isFoo <::const_iterator it = list.begin();\n" - "}\n" - "namespace A {\n" - "namespace B {\n" - "class someClass {\n" - "void foo() {\n" - " if (true) {\n" - " func();\n" - " } else {\n" - " // bla\n" - " }\n" - "}\n" - "};\n" - "}\n" - "}\n" - ); + switch (lang) { + case AStylePreferences::ObjC: + return QLatin1String(formattingObjCSample); + default: + return QLatin1String(formattingCxxSample); + } + Q_UNREACHABLE(); } -QString AStylePlugin::indentingSample() +QString AStylePlugin::indentingSample(AStylePreferences::Language lang) { - return QStringLiteral( - "#define foobar(A)\\\n" - "{Foo();Bar();}\n" - "#define anotherFoo(B)\\\n" - "return Bar()\n" - "\n" - "namespace Bar\n" - "{\n" - "class Foo\n" - "{public:\n" - "Foo();\n" - "virtual ~Foo();\n" - "};\n" - "void bar(int foo)\n" - "{\n" - "switch (foo)\n" - "{\n" - "case 1:\n" - "a+=1;\n" - "break;\n" - "case 2:\n" - "{\n" - "a += 2;\n" - " break;\n" - "}\n" - "}\n" - "if (isFoo)\n" - "{\n" - "bar();\n" - "}\n" - "else\n" - "{\n" - "anotherBar();\n" - "}\n" - "}\n" - "int foo()\n" - "\twhile(isFoo)\n" - "\t\t{\n" - "\t\t\t// ...\n" - "\t\t\tgoto error;\n" - "\t\t/* .... */\n" - "\t\terror:\n" - "\t\t\t//...\n" - "\t\t}\n" - "\t}\n" - "fooArray[]={ red,\n" - "\tgreen,\n" - "\tdarkblue};\n" - "fooFunction(barArg1,\n" - "\tbarArg2,\n" - "\tbarArg3);\n" - "struct foo{ int bar() {} };\n" - ); + switch (lang) { + case AStylePreferences::ObjC: + return QLatin1String(indentingObjCSample); + default: + return QLatin1String(indentingCxxSample); + } + Q_UNREACHABLE(); } #include "astyle_plugin.moc" diff --git a/plugins/astyle/astyle_plugin.h b/plugins/astyle/astyle_plugin.h index cce4a31831..babda5fede 100644 --- a/plugins/astyle/astyle_plugin.h +++ b/plugins/astyle/astyle_plugin.h @@ -1,77 +1,79 @@ /* 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 ASTYLEPLUGIN_H #define ASTYLEPLUGIN_H +#include "astyle_preferences.h" + #include #include class AStyleFormatter; class AStylePlugin : public KDevelop::IPlugin, public KDevelop::ISourceFormatter { Q_OBJECT Q_INTERFACES(KDevelop::ISourceFormatter) public: explicit AStylePlugin(QObject *parent, const QVariantList & = QVariantList()); ~AStylePlugin() override; QString name() const override; QString caption() const override; QString description() const override; /** Formats using the current style. */ QString formatSource(const QString& text, const QUrl &url, const QMimeType& mime, const QString& leftContext, const QString& rightContext) const override; /** \return A map of predefined styles (a key and a caption for each type) */ QVector predefinedStyles() const override; /** \return The widget to edit a style. */ KDevelop::SettingsWidget* editStyleWidget(const QMimeType& mime) const override; QString formatSourceWithStyle(KDevelop::SourceFormatterStyle, const QString& text, const QUrl &url, const QMimeType& mime, const QString& leftContext = QString(), const QString& rightContext = QString()) const override; /** \return The text used in the config dialog to preview the current style. */ QString previewText(const KDevelop::SourceFormatterStyle& style, const QMimeType& mime) const override; /** \return The indentation type of the currently selected style. */ Indentation indentation(const QUrl &url) const override; - static QString formattingSample(); - static QString indentingSample(); + static QString formattingSample(AStylePreferences::Language lang); + static QString indentingSample(AStylePreferences::Language lang); private: QScopedPointer m_formatter; KDevelop::SourceFormatterStyle currentStyle; }; #endif // ASTYLEPLUGIN_H diff --git a/plugins/astyle/astyle_preferences.cpp b/plugins/astyle/astyle_preferences.cpp index c2dcc7d222..9a99025d62 100644 --- a/plugins/astyle/astyle_preferences.cpp +++ b/plugins/astyle/astyle_preferences.cpp @@ -1,431 +1,433 @@ /* 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, static_cast(&KComboBox::currentIndexChanged), this, &AStylePreferences::indentChanged); connect(inpNuberSpaces, static_cast(&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, static_cast(&QSpinBox::valueChanged), this, &AStylePreferences::minMaxValuesChanged); connect(inpMinConditional, static_cast(&QSpinBox::valueChanged), this, &AStylePreferences::minMaxValuesChanged); connect(cbBrackets, static_cast(&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, static_cast(&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, static_cast(&QComboBox::currentIndexChanged), this, &AStylePreferences::pointerAlignChanged); } 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()); // 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()); + emit previewTextChanged(AStylePlugin::indentingSample(m_currentLanguage)); else - emit previewTextChanged(AStylePlugin::formattingSample()); + 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(); } diff --git a/plugins/astyle/astyle_preferences.h b/plugins/astyle/astyle_preferences.h index ccc7ca3bcf..cf2ff6553a 100644 --- a/plugins/astyle/astyle_preferences.h +++ b/plugins/astyle/astyle_preferences.h @@ -1,64 +1,65 @@ /* 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}; + 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(); private: QScopedPointer m_formatter; bool m_enableWidgetSignals; + const Language m_currentLanguage; }; #endif // ASTYLEPREFERENCES_H