[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-commits
Subject:    [kdevelop/5.3] plugins/astyle: [Astyle] Add Objective C to list of languages with formatters
From:       R.J.V. Bertin <null () kde ! org>
Date:       2018-09-20 12:12:28
Message-ID: E1g2xoq-0006NK-Pc () code ! kde ! org
[Download RAW message or body]

Git commit a0992d3166317810c6d361fdfe6d3b40e9db39d5 by R.J.V. Bertin.
Committed on 20/09/2018 at 12:12.
Pushed by rjvbb into branch '5.3'.

[Astyle] Add Objective C to list of languages with formatters

Improves support for ObjC and ObjC++ in the Artistic Style plugin
and makes those languages show up in the style selector.

Differential Revision: https://phabricator.kde.org/D15532

M  +219  -96   plugins/astyle/astyle_plugin.cpp
M  +4    -2    plugins/astyle/astyle_plugin.h
M  +4    -2    plugins/astyle/astyle_preferences.cpp
M  +2    -1    plugins/astyle/astyle_preferences.h

https://commits.kde.org/kdevelop/a0992d3166317810c6d361fdfe6d3b40e9db39d5

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
@@ -33,6 +33,186 @@
 #include <KLocalizedString>
 #include <QUrl>
 
+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 <<endl;}\n"
+    "if(isFoo)DoBar();if (isFoo){\n"
+    "\tbar();\n"
+    "}\n"
+    "\telse if(isBar()){\n"
+    "\tannotherBar();\n"
+    "}\n"
+    "int var = 1;\n"
+    "int *ptr = &var;\n"
+    "int& ref = i;\n"
+    "\n"
+    "QList<int>::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 <<endl;}\n"
+    "if(isFoo)DoBar();if (isFoo){\n"
+    "\tbar();\n"
+    "}\n"
+    "\telse if(isBar()){\n"
+    "\tannotherBar();\n"
+    "}\n"
+    "int var = 1;\n"
+    "int *ptr = &var;\n"
+    "\n"
+    "}\n"
+    "@implementation someClass \n"
+    "+ (someClass*) someClassWithFoo:(int)foo\n"
+    "{\n"
+    "  someClass *this;\n"
+    "  if (foo) {\n"
+    "    this = [[someClass alloc] initWith:foo];\n"
+    "  } else {\n"
+    "    // bla\n"
+    "  }\n"
+    "  return self;\n"
+    "}\n"
+    "@end\n";
+
+static const char indentingCxxSample[] =
+    "#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";
+
+static const char indentingObjCSample[] =
+    "#import <objc/Object.h>\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>();) @@ -96,11 +276,14 @@ 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")},
     };
 }
 
@@ -137,17 +320,31 @@ QVector<SourceFormatterStyle> AStylePlugin::predefinedStyles() \
const  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
@@ -172,100 +369,26 @@ AStylePlugin::Indentation AStylePlugin::indentation(const \
QUrl& url) const  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 <<endl;}\n"
-    "if(isFoo)DoBar();if (isFoo){\n"
-    "\tbar();\n"
-    "}\n"
-    "\telse if(isBar()){\n"
-    "\tannotherBar();\n"
-    "}\n"
-    "int var = 1;\n"
-    "int *ptr = &var;\n"
-    "int& ref = i;\n"
-    "\n"
-    "QList<int>::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
@@ -22,6 +22,8 @@ Copyright (C) 2001 Matthias Hölzer-Klüpfel <mhk@caldera.de>
 #ifndef ASTYLEPLUGIN_H
 #define ASTYLEPLUGIN_H
 
+#include "astyle_preferences.h"
+
 #include <interfaces/iplugin.h>
 #include <interfaces/isourceformatter.h>
 
@@ -66,8 +68,8 @@ public:
     */
     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<AStyleFormatter> m_formatter;
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
@@ -62,10 +62,12 @@ 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:
@@ -250,9 +252,9 @@ 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()
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
@@ -31,7 +31,7 @@ class AStylePreferences : public KDevelop::SettingsWidget, public \
Ui::AStylePref  Q_OBJECT
 
 public:
-    enum Language { CPP, Java, CSharp};
+    enum Language { CPP, Java, CSharp, ObjC};
 
     explicit AStylePreferences(Language lang=CPP, QWidget *parent=nullptr);
     ~AStylePreferences() override;
@@ -59,6 +59,7 @@ private Q_SLOTS:
 private:
     QScopedPointer<AStyleFormatter> m_formatter;
     bool m_enableWidgetSignals;
+    const Language m_currentLanguage;
 };
 
 #endif // ASTYLEPREFERENCES_H


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic