[vala/staging] vala: Allow specifying the nick of enum values



commit ba1258de7cc9e1dcd5c8068f04da245c13c0e6b2
Author: Rico Tzschichholz <ricotz ubuntu com>
Date:   Wed Mar 8 16:25:23 2017 +0100

    vala: Allow specifying the nick of enum values
    
    https://bugzilla.gnome.org/show_bug.cgi?id=625209

 codegen/valaccodebasemodule.vala      |    4 ----
 codegen/valatyperegisterfunction.vala |    4 ++--
 tests/annotations/description.vala    |    7 +++++++
 vala/valaenumvalue.vala               |   17 +++++++++++++++++
 4 files changed, 26 insertions(+), 6 deletions(-)
---
diff --git a/codegen/valaccodebasemodule.vala b/codegen/valaccodebasemodule.vala
index 0752a02..4c1e606 100644
--- a/codegen/valaccodebasemodule.vala
+++ b/codegen/valaccodebasemodule.vala
@@ -6311,10 +6311,6 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
                return new CCodeConstant ("\"%s%s\"".printf (get_ccode_name (sig), (detail != null ? 
"::%s".printf (detail) : "")));
        }
 
-       public static CCodeConstant get_enum_value_canonical_cconstant (EnumValue ev) {
-               return new CCodeConstant ("\"%s\"".printf (ev.name.down ().replace ("_", "-")));
-       }
-
        public bool get_signal_has_emitter (Signal sig) {
                return sig.get_attribute ("HasEmitter") != null;
        }
diff --git a/codegen/valatyperegisterfunction.vala b/codegen/valatyperegisterfunction.vala
index 8894117..9b7c46a 100644
--- a/codegen/valatyperegisterfunction.vala
+++ b/codegen/valatyperegisterfunction.vala
@@ -163,8 +163,8 @@ public abstract class Vala.TypeRegisterFunction {
                        foreach (EnumValue ev in en.get_values ()) {
                                clist_ev = new CCodeInitializerList ();
                                clist_ev.append (new CCodeConstant (get_ccode_name (ev)));
-                               clist_ev.append (new CCodeIdentifier ("\"%s\"".printf (get_ccode_name (ev))));
-                               clist_ev.append (CCodeBaseModule.get_enum_value_canonical_cconstant (ev));
+                               clist_ev.append (new CCodeConstant ("\"%s\"".printf (get_ccode_name (ev))));
+                               clist_ev.append (new CCodeConstant ("\"%s\"".printf (ev.nick)));
                                clist.append (clist_ev);
                        }
 
diff --git a/tests/annotations/description.vala b/tests/annotations/description.vala
index 3556bbb..b304895 100644
--- a/tests/annotations/description.vala
+++ b/tests/annotations/description.vala
@@ -3,6 +3,11 @@ class Foo : Object {
        public int foo { get; set; }
 }
 
+enum Bar {
+       [Description (nick = "foo's nick")]
+       FOO
+}
+
 void main () {
        var foo = new Foo ();
        (unowned ParamSpec)[] properties = foo.get_class ().list_properties ();
@@ -11,4 +16,6 @@ void main () {
                assert (p.get_nick () == "foo's nick");
                assert (p.get_blurb () == "foo's blurb");
        }
+
+       assert (((EnumClass) typeof (Bar).class_ref ()).get_value_by_name (Bar.FOO.to_string ()).value_nick 
== "foo's nick");
 }
diff --git a/vala/valaenumvalue.vala b/vala/valaenumvalue.vala
index 6d2f35b..1352e6b 100644
--- a/vala/valaenumvalue.vala
+++ b/vala/valaenumvalue.vala
@@ -27,6 +27,23 @@ using GLib;
  */
 public class Vala.EnumValue : Constant {
        /**
+        * The nick of this enum-value
+        */
+       public string nick {
+               get {
+                       if (_nick == null) {
+                               _nick = get_attribute_string ("Description", "nick");
+                               if (_nick == null) {
+                                       _nick = name.down ().replace ("_", "-");
+                               }
+                       }
+                       return _nick;
+               }
+       }
+
+       private string? _nick = null;
+
+       /**
         * Creates a new enum value with the specified numerical representation.
         *
         * @param name  enum value name


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]