fix [#30589] RNA function descriptions not showing in Python console on autocomplete

show rna function description and arguments now.
This commit is contained in:
Campbell Barton 2012-03-20 07:41:47 +00:00
parent 65a71a1bbf
commit 3b9b53d876
4 changed files with 149 additions and 47 deletions

@ -951,6 +951,13 @@ int RNA_property_is_idprop(PropertyRNA *prop);
/* python compatible string representation of this property, (must be freed!) */ /* python compatible string representation of this property, (must be freed!) */
char *RNA_property_as_string(struct bContext *C, PointerRNA *ptr, PropertyRNA *prop); char *RNA_property_as_string(struct bContext *C, PointerRNA *ptr, PropertyRNA *prop);
char *RNA_pointer_as_string(struct bContext *C, PointerRNA *ptr); char *RNA_pointer_as_string(struct bContext *C, PointerRNA *ptr);
char *RNA_pointer_as_string_keywords_ex(struct bContext *C, PointerRNA *ptr, PointerRNA *ptr_default,
const short skip_optional_value, const short all_args,
PropertyRNA *iterprop);
char *RNA_pointer_as_string_keywords(struct bContext *C, PointerRNA *ptr, PointerRNA *ptr_default,
const short skip_optional_value, const short all_args);
char *RNA_function_as_string_keywords(struct bContext *C, FunctionRNA *func, PointerRNA *ptr_default,
const short as_function, const short all_args);
/* Function */ /* Function */

@ -4507,6 +4507,118 @@ char *RNA_pointer_as_string(bContext *C, PointerRNA *ptr)
return cstring; return cstring;
} }
/* context and ptr_default can be NULL */
char *RNA_pointer_as_string_keywords_ex(bContext *C, PointerRNA *ptr, PointerRNA *ptr_default,
const short as_function, const short all_args,
PropertyRNA *iterprop)
{
const char *arg_name = NULL;
PropertyRNA *prop;
DynStr *dynstr= BLI_dynstr_new();
char *cstring, *buf;
int first_iter = TRUE, ok = TRUE;
int flag;
/* only to get the orginal props for comparisons */
PropertyRNA *prop_default;
char *buf_default;
RNA_PROP_BEGIN(ptr, propptr, iterprop) {
prop = propptr.data;
flag = RNA_property_flag(prop);
if (as_function && (flag & PROP_OUTPUT)) {
continue;
}
arg_name = RNA_property_identifier(prop);
if (strcmp(arg_name, "rna_type") == 0) {
continue;
}
if (as_function && (flag & PROP_REQUIRED)) {
/* required args don't have useful defaults */
BLI_dynstr_appendf(dynstr, first_iter ? "%s":", %s", arg_name);
first_iter = FALSE;
}
else {
if (as_function && RNA_property_type(prop) == PROP_POINTER) {
/* don't expand pointers for functions */
if (flag & PROP_NEVER_NULL) {
/* we cant really do the right thing here. arg=arg?, hrmf! */
buf = BLI_strdup(arg_name);
}
else {
buf = BLI_strdup("None");
}
}
else {
buf = RNA_property_as_string(C, ptr, prop);
}
ok = TRUE;
if (all_args == FALSE && ptr_default) {
/* not verbose, so only add in attributes that use non-default values
* slow but good for tooltips */
prop_default= RNA_struct_find_property(ptr_default, arg_name);
if (prop_default) {
buf_default= RNA_property_as_string(C, ptr_default, prop_default);
if (strcmp(buf, buf_default) == 0)
ok = FALSE; /* values match, don't bother printing */
MEM_freeN(buf_default);
}
}
if (ok) {
BLI_dynstr_appendf(dynstr, first_iter ? "%s=%s":", %s=%s", arg_name, buf);
first_iter = FALSE;
}
MEM_freeN(buf);
}
}
RNA_PROP_END;
cstring = BLI_dynstr_get_cstring(dynstr);
BLI_dynstr_free(dynstr);
return cstring;
}
char *RNA_pointer_as_string_keywords(bContext *C, PointerRNA *ptr, PointerRNA *ptr_default,
const short as_function, const short all_args)
{
PropertyRNA *iterprop;
iterprop = RNA_struct_iterator_property(ptr->type);
return RNA_pointer_as_string_keywords_ex(C, ptr, ptr_default, as_function, all_args,
iterprop);
}
char *RNA_function_as_string_keywords(bContext *C, FunctionRNA *func, PointerRNA *ptr_default,
const short as_function, const short all_args)
{
PointerRNA funcptr;
PropertyRNA *iterprop;
RNA_pointer_create(NULL, &RNA_Function, func, &funcptr);
iterprop = RNA_struct_find_property(&funcptr, "parameters");
RNA_struct_iterator_property(funcptr.type);
return RNA_pointer_as_string_keywords_ex(C, &funcptr, ptr_default, as_function, all_args,
iterprop);
}
char *RNA_property_as_string(bContext *C, PointerRNA *ptr, PropertyRNA *prop) char *RNA_property_as_string(bContext *C, PointerRNA *ptr, PropertyRNA *prop)
{ {
int type = RNA_property_type(prop); int type = RNA_property_type(prop);

@ -3938,6 +3938,12 @@ static PyGetSetDef pyrna_struct_getseters[] = {
{NULL, NULL, NULL, NULL, NULL} /* Sentinel */ {NULL, NULL, NULL, NULL, NULL} /* Sentinel */
}; };
static PyObject *pyrna_func_doc_get(BPy_FunctionRNA *self, void *closure);
static PyGetSetDef pyrna_func_getseters[] = {
{(char *)"__doc__", (getter)pyrna_func_doc_get, (setter)NULL, NULL, NULL},
{NULL, NULL, NULL, NULL, NULL} /* Sentinel */
};
PyDoc_STRVAR(pyrna_prop_collection_keys_doc, PyDoc_STRVAR(pyrna_prop_collection_keys_doc,
".. method:: keys()\n" ".. method:: keys()\n"
@ -5150,6 +5156,22 @@ static PyObject *pyrna_func_call(BPy_FunctionRNA *self, PyObject *args, PyObject
Py_RETURN_NONE; Py_RETURN_NONE;
} }
static PyObject *pyrna_func_doc_get(BPy_FunctionRNA *self, void *UNUSED(closure))
{
PyObject *ret;
char *args;
args = RNA_function_as_string_keywords(NULL, self->func, NULL, TRUE, TRUE);
ret = PyUnicode_FromFormat("%.200s.%.200s(%.200s)\n%s",
RNA_struct_identifier(self->ptr.type),
RNA_function_identifier(self->func),
args, RNA_function_ui_description(self->func));
MEM_freeN(args);
return ret;
}
/* subclasses of pyrna_struct_Type which support idprop definitions use this as a metaclass */ /* subclasses of pyrna_struct_Type which support idprop definitions use this as a metaclass */
/* note: tp_base member is set to &PyType_Type on init */ /* note: tp_base member is set to &PyType_Type on init */
@ -5726,7 +5748,7 @@ PyTypeObject pyrna_func_Type = {
/*** Attribute descriptor and subclassing stuff ***/ /*** Attribute descriptor and subclassing stuff ***/
NULL, /* struct PyMethodDef *tp_methods; */ NULL, /* struct PyMethodDef *tp_methods; */
NULL, /* struct PyMemberDef *tp_members; */ NULL, /* struct PyMemberDef *tp_members; */
NULL, /* struct PyGetSetDef *tp_getset; */ pyrna_func_getseters, /* struct PyGetSetDef *tp_getset; */
NULL, /* struct _typeobject *tp_base; */ NULL, /* struct _typeobject *tp_base; */
NULL, /* PyObject *tp_dict; */ NULL, /* PyObject *tp_dict; */
NULL, /* descrgetfunc tp_descr_get; */ NULL, /* descrgetfunc tp_descr_get; */

@ -512,68 +512,29 @@ void WM_operator_bl_idname(char *to, const char *from)
*/ */
char *WM_operator_pystring(bContext *C, wmOperatorType *ot, PointerRNA *opptr, int all_args) char *WM_operator_pystring(bContext *C, wmOperatorType *ot, PointerRNA *opptr, int all_args)
{ {
const char *arg_name= NULL;
char idname_py[OP_MAX_TYPENAME]; char idname_py[OP_MAX_TYPENAME];
PropertyRNA *prop, *iterprop;
/* for building the string */ /* for building the string */
DynStr *dynstr= BLI_dynstr_new(); DynStr *dynstr= BLI_dynstr_new();
char *cstring, *buf; char *cstring;
int first_iter=1, ok= 1; char *cstring_args;
/* only to get the orginal props for comparisons */ /* only to get the orginal props for comparisons */
PointerRNA opptr_default; PointerRNA opptr_default;
PropertyRNA *prop_default;
char *buf_default; if (all_args==0 || opptr==NULL) {
if(all_args==0 || opptr==NULL) {
WM_operator_properties_create_ptr(&opptr_default, ot); WM_operator_properties_create_ptr(&opptr_default, ot);
if(opptr==NULL) if(opptr==NULL)
opptr = &opptr_default; opptr = &opptr_default;
} }
WM_operator_py_idname(idname_py, ot->idname); WM_operator_py_idname(idname_py, ot->idname);
BLI_dynstr_appendf(dynstr, "bpy.ops.%s(", idname_py); BLI_dynstr_appendf(dynstr, "bpy.ops.%s(", idname_py);
iterprop= RNA_struct_iterator_property(opptr->type); cstring_args = RNA_pointer_as_string_keywords(C, opptr, &opptr_default, FALSE, all_args);
BLI_dynstr_append(dynstr, cstring_args);
RNA_PROP_BEGIN(opptr, propptr, iterprop) { MEM_freeN(cstring_args);
prop= propptr.data;
arg_name= RNA_property_identifier(prop);
if (strcmp(arg_name, "rna_type")==0) continue;
buf= RNA_property_as_string(C, opptr, prop);
ok= 1;
if(!all_args) {
/* not verbose, so only add in attributes that use non-default values
* slow but good for tooltips */
prop_default= RNA_struct_find_property(&opptr_default, arg_name);
if(prop_default) {
buf_default= RNA_property_as_string(C, &opptr_default, prop_default);
if(strcmp(buf, buf_default)==0)
ok= 0; /* values match, don't bother printing */
MEM_freeN(buf_default);
}
}
if(ok) {
BLI_dynstr_appendf(dynstr, first_iter?"%s=%s":", %s=%s", arg_name, buf);
first_iter = 0;
}
MEM_freeN(buf);
}
RNA_PROP_END;
if(all_args==0 || opptr==&opptr_default ) if(all_args==0 || opptr==&opptr_default )
WM_operator_properties_free(&opptr_default); WM_operator_properties_free(&opptr_default);