Skip to content

Commit 5102676

Browse files
committed
Add Open in Editor support for template paths
1 parent 5002cf8 commit 5102676

File tree

5 files changed

+26
-2
lines changed

5 files changed

+26
-2
lines changed

debug_toolbar/panels/templates/panel.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,13 +195,17 @@ def generate_stats(self, request, response):
195195
if hasattr(template, "origin") and template.origin and template.origin.name:
196196
template.origin_name = template.origin.name
197197
template.origin_hash = signing.dumps(template.origin.name)
198+
template.editor_url = self.toolbar.config["EDITOR_URL_CALLBACK"](
199+
template.origin.name
200+
)
198201
else:
199202
template.origin_name = _("No origin")
200203
template.origin_hash = ""
201204
info["template"] = {
202205
"name": template.name,
203206
"origin_name": template.origin_name,
204207
"origin_hash": template.origin_hash,
208+
"editor_url": getattr(template, "editor_url", None),
205209
}
206210
# Clean up context for better readability
207211
if self.toolbar.config["SHOW_TEMPLATE_CONTEXT"]:

debug_toolbar/settings.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ def _is_running_tests():
2222
"debug_toolbar.panels.profiling.ProfilingPanel",
2323
"debug_toolbar.panels.redirects.RedirectsPanel",
2424
},
25+
"EDITOR_URL_CALLBACK": lambda f: None,
2526
"INSERT_BEFORE": "</body>",
2627
"RENDER_PANELS": None,
2728
"RESULTS_CACHE_SIZE": 25,

debug_toolbar/templates/debug_toolbar/panels/templates.html

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,15 @@ <h4>{% blocktranslate count template_count=templates|length %}Template{% plural
1515
<dl>
1616
{% for template in templates %}
1717
<dt><strong><a class="remoteCall toggleTemplate" href="{% url 'djdt:template_source' %}?template={{ template.template.name }}&amp;template_origin={{ template.template.origin_hash }}">{{ template.template.name|addslashes }}</a></strong></dt>
18-
<dd><samp>{{ template.template.origin_name|addslashes }}</samp></dd>
18+
<dd>
19+
<samp>
20+
{% if template.template.editor_url %}
21+
<a href="{{ template.template.editor_url }}" title="{% translate "Open in editor" %}">{{ template.template.origin_name|addslashes }}</a>
22+
{% else %}
23+
{{ template.template.origin_name|addslashes }}
24+
{% endif %}
25+
</samp>
26+
</dd>
1927
{% if template.context %}
2028
<dd>
2129
<details>

docs/configuration.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,14 @@ Toolbar options
6767
This setting is a set of the full Python paths to each panel that you
6868
want disabled (but still displayed) by default.
6969

70+
* ``EDITOR_URL_CALLBACK``
71+
72+
Default: ``lambda f: None``
73+
74+
This function is passed a file path and should return a URL to open the file in the editor.
75+
76+
For example, to open the file in VS Code, you can use: ``lambda f: f"vscode://file/{f}"``
77+
7078
* ``INSERT_BEFORE``
7179

7280
Default: ``'</body>'``

example/settings.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,4 +118,7 @@
118118
"debug_toolbar.middleware.DebugToolbarMiddleware",
119119
]
120120
# Customize the config to support turbo and htmx boosting.
121-
DEBUG_TOOLBAR_CONFIG = {"ROOT_TAG_EXTRA_ATTRS": "data-turbo-permanent hx-preserve"}
121+
DEBUG_TOOLBAR_CONFIG = {
122+
"ROOT_TAG_EXTRA_ATTRS": "data-turbo-permanent hx-preserve",
123+
"EDITOR_URL_CALLBACK": lambda f: f"vscode://file/{f}",
124+
}

0 commit comments

Comments
 (0)