From 8c6dc5fa17874a9b6fefee07a8b69e4430975101 Mon Sep 17 00:00:00 2001 From: Emmanuele Bassi Date: Sun, 8 Jan 2023 14:31:41 +0000 Subject: Add `default-value` annotation An escape hatch to specify a freeform string for the default value of a property. Fixes: #4 --- docs/website/annotations/giannotations.rst | 4 ++++ giscanner/annotationparser.py | 16 ++++++++++++++++ giscanner/maintransformer.py | 4 ++++ tests/scanner/Regress-1.0-expected.gir | 10 ++++++++-- tests/scanner/regress.c | 10 +++++----- 5 files changed, 37 insertions(+), 7 deletions(-) diff --git a/docs/website/annotations/giannotations.rst b/docs/website/annotations/giannotations.rst index 3e4bfa92..8099bd5b 100644 --- a/docs/website/annotations/giannotations.rst +++ b/docs/website/annotations/giannotations.rst @@ -129,6 +129,10 @@ Support for GObject objects - identifier (only applies to methods) - This signal is emitted by the given method - + * - ``(default-value VALUE)`` + - identifier (only applies to properties) + - The default value of a GObject property, as a freeform string + - :issue:`4` Support for GObject closures diff --git a/giscanner/annotationparser.py b/giscanner/annotationparser.py index bce99804..3952b24b 100644 --- a/giscanner/annotationparser.py +++ b/giscanner/annotationparser.py @@ -193,6 +193,7 @@ ANN_ARRAY = 'array' ANN_ATTRIBUTES = 'attributes' ANN_CLOSURE = 'closure' ANN_CONSTRUCTOR = 'constructor' +ANN_DEFAULT_VALUE = 'default-value' ANN_DESTROY = 'destroy' ANN_ELEMENT_TYPE = 'element-type' ANN_EMITTER = 'emitter' @@ -228,6 +229,7 @@ GI_ANNS = [ANN_ALLOW_NONE, ANN_ATTRIBUTES, ANN_CLOSURE, ANN_CONSTRUCTOR, + ANN_DEFAULT_VALUE, ANN_DESTROY, ANN_ELEMENT_TYPE, ANN_EMITTER, @@ -788,6 +790,19 @@ class GtkDocAnnotatable(object): self._validate_annotation(position, ann_name, options, exact_n_options=0) + def _do_validate_default_value(self, position, ann_name, options): + ''' + Validate the ``(default-value)`` annotation. + + :param position: :class:`giscanner.message.Position` of the line in the source file + containing the annotation to be validated + :param ann_name: name of the annotation holding the options to validate + :param options: annotation options to validate + ''' + + # The 'default-value' annotation allows free form annotations. + pass + def _do_validate_destroy(self, position, ann_name, options): ''' Validate the ``(destroy)`` annotation. @@ -1167,6 +1182,7 @@ class GtkDocCommentBlock(GtkDocAnnotatable): valid_annotations = ( ANN_ATTRIBUTES, ANN_CONSTRUCTOR, + ANN_DEFAULT_VALUE, ANN_EMITTER, ANN_FOREIGN, ANN_GET_PROPERTY, diff --git a/giscanner/maintransformer.py b/giscanner/maintransformer.py index 2004d254..285712c7 100644 --- a/giscanner/maintransformer.py +++ b/giscanner/maintransformer.py @@ -28,6 +28,7 @@ from .annotationparser import ( ANN_ATTRIBUTES, ANN_CLOSURE, ANN_CONSTRUCTOR, + ANN_DEFAULT_VALUE, ANN_DESTROY, ANN_ELEMENT_TYPE, ANN_EMITTER, @@ -963,6 +964,9 @@ class MainTransformer(object): getter = block.annotations.get(ANN_GETTER) if getter: prop.getter = getter[0] + default_value = block.annotations.get(ANN_DEFAULT_VALUE) + if default_value: + prop.default_value = default_value[0] def _apply_annotations_signal(self, parent, signal): names = [] diff --git a/tests/scanner/Regress-1.0-expected.gir b/tests/scanner/Regress-1.0-expected.gir index 608f120c..d6ca9dc6 100644 --- a/tests/scanner/Regress-1.0-expected.gir +++ b/tests/scanner/Regress-1.0-expected.gir @@ -4870,7 +4870,10 @@ raise an error. default-value="1.000000"> - + @@ -4893,7 +4896,10 @@ raise an error. default-value="0"> - + diff --git a/tests/scanner/regress.c b/tests/scanner/regress.c index e21997de..15430bd1 100644 --- a/tests/scanner/regress.c +++ b/tests/scanner/regress.c @@ -2643,7 +2643,7 @@ regress_test_obj_class_init (RegressTestObjClass *klass) pspec); /** - * RegressTestObj:list: (type GLib.List(utf8)) (transfer none) + * RegressTestObj:list: (type GLib.List(utf8)) (transfer none) (default-value NULL) */ pspec = g_param_spec_pointer ("list", "GList property", @@ -2745,7 +2745,7 @@ regress_test_obj_class_init (RegressTestObjClass *klass) /** - * TestObj:gtype: + * RegressTestObj:gtype: (default-value G_TYPE_INVALID) */ pspec = g_param_spec_gtype ("gtype", "GType property", @@ -2757,7 +2757,7 @@ regress_test_obj_class_init (RegressTestObjClass *klass) pspec); /** - * TestObj:name-conflict: + * RegressTestObj:name-conflict: */ pspec = g_param_spec_int ("name-conflict", "name-conflict property", @@ -2771,7 +2771,7 @@ regress_test_obj_class_init (RegressTestObjClass *klass) pspec); /** - * TestObj:byte-array: + * RegressTestObj:byte-array: */ pspec = g_param_spec_boxed ("byte-array", "GByteArray property", @@ -2783,7 +2783,7 @@ regress_test_obj_class_init (RegressTestObjClass *klass) pspec); /** - * TestObj:write-only: + * RegressTestObj:write-only: */ pspec = g_param_spec_boolean ("write-only", "Write-only property", "A write-only bool property that resets the value of TestObj:int to 0 when true", -- cgit v1.2.1