From c309cb2d29d1dd791632d37cf5d5952728440703 Mon Sep 17 00:00:00 2001 From: Faris Ansari Date: Tue, 20 Apr 2021 16:23:20 +0530 Subject: [PATCH] fix: Pass score to ace to let it handle sorting --- .../doctype/server_script/server_script.py | 19 +++++++++---------- frappe/public/js/frappe/form/controls/code.js | 16 ++++++++++------ 2 files changed, 19 insertions(+), 16 deletions(-) diff --git a/frappe/core/doctype/server_script/server_script.py b/frappe/core/doctype/server_script/server_script.py index b791997a8b..f80a067cf1 100644 --- a/frappe/core/doctype/server_script/server_script.py +++ b/frappe/core/doctype/server_script/server_script.py @@ -140,32 +140,31 @@ class ServerScript(Document): value = obj[key] if isinstance(value, (NamespaceDict, dict)) and value: if key == 'form_dict': - out.append(['form_dict', 3]) + out.append(['form_dict', 7]) continue for subkey, score in get_keys(value): fullkey = f'{key}.{subkey}' out.append([fullkey, score]) else: - if isinstance(value, ModuleType): + if isinstance(value, type) and issubclass(value, Exception): score = 0 + elif isinstance(value, ModuleType): + score = 10 elif isinstance(value, (FunctionType, MethodType)): - score = 1 - elif isinstance(value, type) and issubclass(value, Exception): score = 9 elif isinstance(value, type): - score = 2 + score = 8 elif isinstance(value, dict): - score = 3 + score = 7 else: - score = 4 + score = 6 out.append([key, score]) return out items = frappe.cache().get_value('server_script_autocompletion_items') if not items: - unsorted_items = get_keys(get_safe_globals()) - sorted_items = sorted(unsorted_items, key=lambda k: k[1]) - items = [d[0] for d in sorted_items] + items = get_keys(get_safe_globals()) + items = [{'value': d[0], 'score': d[1]} for d in items] frappe.cache().set_value('server_script_autocompletion_items', items) return items diff --git a/frappe/public/js/frappe/form/controls/code.js b/frappe/public/js/frappe/form/controls/code.js index 635146563e..33579b3b88 100644 --- a/frappe/public/js/frappe/form/controls/code.js +++ b/frappe/public/js/frappe/form/controls/code.js @@ -66,12 +66,16 @@ frappe.ui.form.ControlCode = frappe.ui.form.ControlText.extend({ if (autocompletions.length) { callback( null, - autocompletions.map(a => ({ - name: 'frappe', - value: a, - score: 100, - meta: 'Frappe API' - })) + autocompletions.map(a => { + if (typeof a === 'string') { + a = { value: a }; + } + return { + name: 'frappe', + value: a.value, + score: a.score + } + }) ); } }