Skip to content

Commit 418e706

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

File tree

6 files changed

+51
-2
lines changed

6 files changed

+51
-2
lines changed

debug_toolbar/panels/templates/panel.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from debug_toolbar.panels import Panel
1717
from debug_toolbar.panels.sql.tracking import SQLQueryTriggered, allow_sql
1818
from debug_toolbar.panels.templates import views
19+
from debug_toolbar.utils import get_editor_url
1920

2021
if find_spec("jinja2"):
2122
from debug_toolbar.panels.templates.jinja2 import patch_jinja_render
@@ -195,13 +196,15 @@ def generate_stats(self, request, response):
195196
if hasattr(template, "origin") and template.origin and template.origin.name:
196197
template.origin_name = template.origin.name
197198
template.origin_hash = signing.dumps(template.origin.name)
199+
template.editor_url = get_editor_url(template.origin.name)
198200
else:
199201
template.origin_name = _("No origin")
200202
template.origin_hash = ""
201203
info["template"] = {
202204
"name": template.name,
203205
"origin_name": template.origin_name,
204206
"origin_hash": template.origin_hash,
207+
"editor_url": getattr(template, "editor_url", None),
205208
}
206209
# Clean up context for better readability
207210
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": "vscode",
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>

debug_toolbar/utils.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,3 +401,28 @@ def is_processable_html_response(response):
401401
and content_encoding == ""
402402
and content_type in _HTML_TYPES
403403
)
404+
405+
406+
def get_editor_url(file: str, line: int = 1) -> str | None:
407+
formats = {
408+
"cursor": "cursor://file/{file}:{line}",
409+
"emacs": "emacs://open?url=file://{file}&line={line}",
410+
"espresso": "x-espresso://open?filepath={file}&lines={line}",
411+
"idea": "idea://open?file={file}&line={line}",
412+
"idea-remote": "javascript:(()=>{let r=new XMLHttpRequest; r.open('get','http://localhost:63342/api/file/?file={file}&line={line}');r.send();})()",
413+
"macvim": "mvim://open/?url=file://{file}&line={line}",
414+
"nova": "nova://open?path={file}&line={line}",
415+
"pycharm": "pycharm://open?file={file}&line={line}",
416+
"pycharm-remote": "javascript:(()=>{let r=new XMLHttpRequest; r.open('get','http://localhost:63342/api/file/{file}:{line}');r.send();})()",
417+
"sublime": "subl://open?url=file://{file}&line={line}",
418+
"vscode": "vscode://file/{file}:{line}",
419+
"vscode-insiders": "vscode-insiders://file/{file}:{line}",
420+
"vscode-remote": "vscode://vscode-remote/{file}:{line}",
421+
"vscode-insiders-remote": "vscode-insiders://vscode-remote/{file}:{line}",
422+
"vscodium": "vscodium://file/{file}:{line}",
423+
"windsurf": "windsurf://file/{file}:{line}",
424+
}
425+
template = formats.get(dt_settings.get_config()["EDITOR"])
426+
if template is None:
427+
return None
428+
return template.format(file=file, line=line)

docs/configuration.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,16 @@ 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``
71+
72+
Default: ``'vscode'``
73+
74+
The editor to use to open file paths from the toolbar.
75+
76+
Available editors: ``'vscode'``, ``'cursor'``, ``'emacs'``, ``'idea'``,
77+
``'pycharm'``, ``'sublime'``, ``'vscode-insiders'``, ``'vscode-remote'``,
78+
``'vscodium'``, ``'windsurf'``
79+
7080
* ``INSERT_BEFORE``
7181

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

example/settings.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,4 +118,6 @@
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+
}

0 commit comments

Comments
 (0)