From: Rico Tzschichholz Date: Wed, 8 Mar 2017 15:25:23 +0000 (+0100) Subject: vala: Allow specifying the nick of enum values X-Git-Tag: 0.41.90~125 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=ba1258de7cc9e1dcd5c8068f04da245c13c0e6b2;p=thirdparty%2Fvala.git vala: Allow specifying the nick of enum values https://bugzilla.gnome.org/show_bug.cgi?id=625209 --- diff --git a/codegen/valaccodebasemodule.vala b/codegen/valaccodebasemodule.vala index 0752a0209..4c1e60685 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 8894117fc..9b7c46a2f 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 3556bbb6a..b30489598 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 6d2f35bb1..1352e6b03 100644 --- a/vala/valaenumvalue.vala +++ b/vala/valaenumvalue.vala @@ -26,6 +26,23 @@ using GLib; * Represents an enum member in the source code. */ 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. *