From 118aa55048d7754009a2deadc10e81e64232c156 Mon Sep 17 00:00:00 2001 From: Jeroen Date: Mon, 14 Jul 2025 22:34:52 +0200 Subject: [PATCH 1/2] implement logic Counts elements in at the current path. Distinguishes between objects in an array and attributes in an object. --- StatusBarJsonPath.py | 38 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/StatusBarJsonPath.py b/StatusBarJsonPath.py index c4b3ba2..fa97a9b 100644 --- a/StatusBarJsonPath.py +++ b/StatusBarJsonPath.py @@ -3,6 +3,8 @@ import sublime import sublime_plugin +import json + class CopyJsonPathCommand(sublime_plugin.TextCommand): def run(self, edit): json_paths = get_json_path(self.view) @@ -22,13 +24,45 @@ class StatusBarJsonPath(sublime_plugin.EventListener): def update_json_path(self, view): json_paths = get_json_path(view) - if len(json_paths): - view.set_status(self.KEY_SIZE, "JSONPath: " + ", ".join(json_paths)) + try: + text = view.substr(sublime.Region(0, view.size())) + cursor = view.sel()[0].end() + obj = json.loads(text) + node = self._walk_path(obj, "".join(json_paths)) + except Exception: + print("Error in JSONPath") + node = None + + if isinstance(node, list): + view.set_status(self.KEY_SIZE, "JSONPath: " + ", ".join(json_paths) + " OBJECTS: " + format(len(node))) + elif isinstance(node, dict): + view.set_status(self.KEY_SIZE, "JSONPath: " + ", ".join(json_paths) + " ATTRIBUTES: " + format(len(node))) + else: + view.set_status(self.KEY_SIZE, "JSONPath: " + ", ".join(json_paths)) else: view.erase_status(self.KEY_SIZE) on_selection_modified_async = update_json_path + def _walk_path(self, obj, path_str): + """ + Given a Python obj (dict/list) and a json-path like ".foo[2].bar", + step through and return the leaf node. + """ + #print("walk Path") + cur = obj + # strip leading dot and split on dots + parts = path_str.lstrip(".").split(".") + for part in parts: + if "[" in part and part.endswith("]"): + # e.g. "foo[2]" → key="foo", idx="2" + key, idx = part[:-1].split("[") + cur = cur[key][int(idx)] + else: + cur = cur[part] + + return cur + def get_json_path(view): json_paths = [] tag = view.change_count() From 7ba7fe0e050f555dda0d6b90bd69c4e219df0447 Mon Sep 17 00:00:00 2001 From: Jeroen Date: Mon, 14 Jul 2025 22:38:10 +0200 Subject: [PATCH 2/2] fix mistake --- StatusBarJsonPath.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/StatusBarJsonPath.py b/StatusBarJsonPath.py index fa97a9b..1202c63 100644 --- a/StatusBarJsonPath.py +++ b/StatusBarJsonPath.py @@ -24,6 +24,8 @@ class StatusBarJsonPath(sublime_plugin.EventListener): def update_json_path(self, view): json_paths = get_json_path(view) + + if len(json_paths): try: text = view.substr(sublime.Region(0, view.size())) cursor = view.sel()[0].end()