diff --git a/crates/ide-sdk/src/apply.rs b/crates/ide-sdk/src/apply.rs index a21e7eca8..422179a86 100644 --- a/crates/ide-sdk/src/apply.rs +++ b/crates/ide-sdk/src/apply.rs @@ -745,7 +745,8 @@ pub fn native_tsdk(root: &Path, s: &Settings) -> Option { } } -const PYTHON_SERVERS: [&str; 7] = ["basedpyright", "pyright", "ty", "pyrefly", "pylsp", "jedi-language-server", "ruff"]; +const PYTHON_SERVERS: [&str; 8] = + ["basedpyright", "pyright", "ty", "pyrefly", "pylsp", "jedi-language-server", "ruff", "djlsp"]; pub(crate) fn merge(base: &mut Value, layer: Value) { match (base, layer) { @@ -979,6 +980,16 @@ pub fn resolve_run_command(config: &mut RunConfig, root: &Path, s: &Settings) { } } +/// The environment folder holding `python` (`/bin/python`, `/Scripts/python.exe`): +/// a virtualenv, or a conda or pixi environment. +fn environment_of(python: &Path) -> Option { + venv_of(python).or_else(|| { + let bin = python.parent()?; + let env = bin.parent()?; + matches!(bin.file_name()?.to_str()?, "bin" | "Scripts").then(|| env.to_path_buf()) + }) +} + pub fn configure_server(cfg: &mut LanguageServerConfig, root: &Path, server_root: &Path, s: &Settings) { let mut path_dirs: Vec = Vec::new(); if let Some(custom) = tool_command(root, s, &cfg.command) { @@ -1002,6 +1013,13 @@ pub fn configure_server(cfg: &mut LanguageServerConfig, root: &Path, server_root "basedpyright" | "pyright" => merge_init_options(cfg, json!({ "python": { "pythonPath": p } })), "pylsp" => merge_init_options(cfg, json!({ "pylsp": { "plugins": { "jedi": { "environment": p } } } })), "jedi-language-server" => merge_init_options(cfg, json!({ "workspace": { "environmentPath": p } })), + // Django's template tags, filters and template folders are read by running a script + // with the interpreter in one of these environment folders. + "djlsp" => { + if let Some(env) = environment_of(&python) { + merge_init_options(cfg, json!({ "env_directories": [env.to_string_lossy()] })); + } + } _ => {} } if let Some(venv) = venv_of(&python) { diff --git a/crates/ide-sdk/src/tests.rs b/crates/ide-sdk/src/tests.rs index bcb9289bb..9a92fb0b9 100644 --- a/crates/ide-sdk/src/tests.rs +++ b/crates/ide-sdk/src/tests.rs @@ -190,6 +190,18 @@ fn pyright_gets_python_path_and_venv_env() { assert!(cfg.env["PATH"].starts_with(&*tmp.path().join(".venv").join(bin_dir()).to_string_lossy())); } +#[test] +fn djlsp_reads_django_from_the_project_environment() { + let tmp = tempfile::tempdir().unwrap(); + let python = fake_venv(tmp.path(), "venvs/web"); + let mut s = Settings::default(); + s.languages.python.interpreter = python.to_string_lossy().into_owned(); + let mut cfg = server("djlsp"); + configure_server(&mut cfg, tmp.path(), tmp.path(), &s); + let opts = serde_json::to_value(cfg.initialization_options.unwrap()).unwrap(); + assert_eq!(opts["env_directories"], serde_json::json!([tmp.path().join("venvs/web").to_string_lossy()])); +} + #[test] fn a_run_target_interpreter_hides_local_pythons() { let tmp = tempfile::tempdir().unwrap(); diff --git a/crates/ide-tools/src/registry.rs b/crates/ide-tools/src/registry.rs index be65a2670..2a3829043 100644 --- a/crates/ide-tools/src/registry.rs +++ b/crates/ide-tools/src/registry.rs @@ -322,6 +322,17 @@ pub static TOOLS: &[ToolSpec] = &[ homepage: "https://docs.astral.sh/ruff/", needs: &[], }, + ToolSpec { + name: "djlsp", + display: "Django Template LSP", + kind: LanguageServer, + commands: &["djlsp"], + method: Method::PipVenv { package: "django-template-lsp" }, + fallback: None, + version: ver("djlsp"), + homepage: "https://github.com/fourdigits/django-template-lsp", + needs: &[], + }, ToolSpec { name: "bash-language-server", display: "Bash Language Server", diff --git a/docs/en/languages/index.mdx b/docs/en/languages/index.mdx index eae31b9c0..d514db656 100644 --- a/docs/en/languages/index.mdx +++ b/docs/en/languages/index.mdx @@ -107,7 +107,7 @@ The table lists every built-in language definition. Server and debugger names in | D | `d` | `.d`, `.di` | Yes | — | serve-d | — | | Dart | `dart` | `.dart` | Yes | Yes | dart | dart | | Diff | `diff` | `.diff`, `.patch`, `.rej` | Yes | — | — | — | -| Django Template | `django-html` | `.djhtml`, `templates/*.html`, `*.txt` | Yes | Yes | **vscode-html-language-server** | — | +| Django Template | `django-html` | `.djhtml`, `templates/*.html`, `*.txt` | Yes | Yes | **vscode-html-language-server**, **djlsp** | — | | Dockerfile | `dockerfile` | `.dockerfile`, `.containerfile` | Yes | — | **dockerfile-language-server** | — | | Elixir | `elixir` | `.ex`, `.exs` | Yes | Yes | **elixir-ls** | **elixir-ls-debugger** | | ERB | `erb` | `.erb`, `.html.erb`, `.rhtml`, `.js.erb`, `.text.erb`, … | Yes | — | — | — | diff --git a/docs/en/languages/python.mdx b/docs/en/languages/python.mdx index 4620b1a36..07e950438 100644 --- a/docs/en/languages/python.mdx +++ b/docs/en/languages/python.mdx @@ -140,6 +140,8 @@ Django is the only framework with dedicated support: the `manage.py runserver` a The HTML and text files in a `templates` folder of a Django project (one with a `manage.py` above it) open as **Django Template** files: the template tags, variables, filters and comments are highlighted on top of the HTML, and block tags (`{% block %}`, `{% if %}`, `{% for %}`, `{% with %}` and the others of Django and its contrib libraries) fold, indent their content and highlight their end tag. Typing `{%`, `{{` or `{#` closes the pair, finishing a block tag adds its end tag, and `{% end` completes the end tags of the open blocks. **Comment with Line Comment** puts each line in its own `{# … #}`, and **Comment with Block Comment** over several lines uses `{% comment %}`. The HTML language server and Emmet work in the markup between the tags. +With **Django Template LSP** (`djlsp`, installed from **Settings › Languages & Frameworks › Language Servers**), the template tags, filters, template names (`{% extends %}`, `{% include %}`), static files and URL names of the project complete, show their documentation on hover, and **Go to Declaration** opens the template or the tag's Python source. It reads them by running Django with the project interpreter, so Django has to be installed there. + An HTML file elsewhere with Django tags shows a banner that switches it to Django Template or keeps it as HTML; either choice is the file's **Override File Type**. ## Limitations diff --git a/docs/zh/languages/index.mdx b/docs/zh/languages/index.mdx index b819dd563..2ad1cc9f3 100644 --- a/docs/zh/languages/index.mdx +++ b/docs/zh/languages/index.mdx @@ -102,7 +102,7 @@ IDE 安装工具从不需要 `sudo`。工具装在本地数据目录下 IDE 自 | D | `d` | `.d`, `.di` | 是 | — | serve-d | — | | Dart | `dart` | `.dart` | 是 | 是 | dart | dart | | Diff | `diff` | `.diff`, `.patch`, `.rej` | 是 | — | — | — | -| Django Template | `django-html` | `.djhtml`, `templates/*.html`, `*.txt` | 是 | 是 | **vscode-html-language-server** | — | +| Django Template | `django-html` | `.djhtml`, `templates/*.html`, `*.txt` | 是 | 是 | **vscode-html-language-server**, **djlsp** | — | | Dockerfile | `dockerfile` | `.dockerfile`, `.containerfile` | 是 | — | **dockerfile-language-server** | — | | Elixir | `elixir` | `.ex`, `.exs` | 是 | 是 | **elixir-ls** | **elixir-ls-debugger** | | ERB | `erb` | `.erb`, `.html.erb`, `.rhtml`, `.js.erb`, `.text.erb`, … | 是 | — | — | — | diff --git a/docs/zh/languages/python.mdx b/docs/zh/languages/python.mdx index 7fefb65cb..958adbb1a 100644 --- a/docs/zh/languages/python.mdx +++ b/docs/zh/languages/python.mdx @@ -140,6 +140,8 @@ Python 调试使用 debugpy。开始调试时如果缺少 debugpy,OpenIDEA 会 Django 项目(上层目录有 `manage.py`)中 `templates` 目录里的 HTML 和文本文件以 **Django Template** 打开:模板标签、变量、过滤器和注释在 HTML 之上高亮,块标签(`{% block %}`、`{% if %}`、`{% for %}`、`{% with %}` 以及 Django 和其 contrib 库的其他块标签)可以折叠、缩进内容并高亮对应的结束标签。输入 `{%`、`{{` 或 `{#` 会自动补全成对符号,写完块标签会自动添加结束标签,输入 `{% end` 会补全当前打开块的结束标签。**使用行注释** 会把每一行放进各自的 `{# … #}`,多行的 **使用块注释** 则用 `{% comment %}`。HTML 语言服务器和 Emmet 在标签之间的标记中照常工作。 +安装 **Django Template LSP**(`djlsp`,在 **设置 › 语言和框架 › 语言服务器** 中安装)后,项目的模板标签、过滤器、模板名(`{% extends %}`、`{% include %}`)、静态文件和 URL 名可以补全、悬停查看文档,**转到声明** 会打开对应模板或标签的 Python 源码。它通过项目解释器运行 Django 读取这些信息,因此解释器中需要装有 Django。 + 其他位置含有 Django 标签的 HTML 文件会显示一个横幅,可以切换为 Django Template 或保留为 HTML;两种选择都会成为该文件的 **覆盖文件类型**。 ## 限制 diff --git a/languages/django-html.toml b/languages/django-html.toml index aca0a2ac8..da817744e 100644 --- a/languages/django-html.toml +++ b/languages/django-html.toml @@ -21,3 +21,12 @@ command = "vscode-html-language-server" args = ["--stdio"] initialization-options = { embeddedLanguages = { css = true, javascript = true } } install-hint = "npm i -g vscode-langservers-extracted" + +# Template tags, filters, template names and URL names of the project: completion, +# hover and go to definition. It reads them by running Django with the project +# interpreter. +[[language-servers]] +name = "djlsp" +command = "djlsp" +root-markers = ["manage.py"] +install-hint = "uv tool install django-template-lsp (or: pip install django-template-lsp)"