|
2 | 2 | title: Neovim |
3 | 3 | --- |
4 | 4 |
|
5 | | -{% include-markdown "../../clients/nvim/README.md" %} |
| 5 | +# Neovim |
| 6 | + |
| 7 | +## Requirements |
| 8 | + |
| 9 | +- Neovim 0.11+ |
| 10 | +- Django Language Server (`djls`) installed on your system. See [Installation](../index.md#installation). |
| 11 | + |
| 12 | +## Configuration |
| 13 | + |
| 14 | +The server can be configured using Neovim's built-in [`vim.lsp.config()`](https://neovim.io/doc/user/lsp.html#lsp-config). |
| 15 | + |
| 16 | +You can define the configuration inline in your `init.lua`: |
| 17 | + |
| 18 | +```lua |
| 19 | +-- In init.lua |
| 20 | +vim.lsp.config('djls', { |
| 21 | + cmd = { 'djls', 'serve' }, |
| 22 | + filetypes = { 'htmldjango', 'html', 'python' }, |
| 23 | + root_markers = { 'manage.py', 'pyproject.toml' }, |
| 24 | + single_file_support = true, |
| 25 | +}) |
| 26 | + |
| 27 | +vim.lsp.enable('djls') |
| 28 | +``` |
| 29 | + |
| 30 | +Or create a dedicated config file in your runtimepath at `lsp/djls.lua`: |
| 31 | + |
| 32 | +```lua |
| 33 | +-- In <rtp>/lsp/djls.lua (e.g., ~/.config/nvim/lsp/djls.lua) |
| 34 | +return { |
| 35 | + cmd = { 'djls', 'serve' }, |
| 36 | + filetypes = { 'htmldjango', 'html', 'python' }, |
| 37 | + root_markers = { 'manage.py', 'pyproject.toml' }, |
| 38 | + single_file_support = true, |
| 39 | +} |
| 40 | +``` |
| 41 | + |
| 42 | +Then just enable it in your `init.lua`: |
| 43 | + |
| 44 | +```lua |
| 45 | +-- In init.lua |
| 46 | +vim.lsp.enable('djls') |
| 47 | +``` |
| 48 | + |
| 49 | +### Django Settings |
| 50 | + |
| 51 | +For Django project settings and other server options, see [Configuration](../configuration.md). |
| 52 | + |
| 53 | +To pass settings via Neovim's LSP client, use `init_options`: |
| 54 | + |
| 55 | +```lua |
| 56 | +vim.lsp.config('djls', { |
| 57 | + -- ... basic config from above |
| 58 | + init_options = { |
| 59 | + django_settings_module = "myproject.settings", |
| 60 | + venv_path = "/path/to/venv", |
| 61 | + }, |
| 62 | +}) |
| 63 | +``` |
| 64 | + |
| 65 | +### File Type Detection |
| 66 | + |
| 67 | +Django templates need the correct filetype for LSP activation. The simplest approach is a pattern match: |
| 68 | + |
| 69 | +```lua |
| 70 | +vim.filetype.add({ |
| 71 | + pattern = { |
| 72 | + [".*/templates/.*%.html"] = "htmldjango", |
| 73 | + }, |
| 74 | +}) |
| 75 | +``` |
| 76 | + |
| 77 | +For more intelligent detection that checks if an HTML file is actually in a Django project by walking up the directory tree from the file's location to find Django project markers (`manage.py` or `pyproject.toml` with django): |
| 78 | + |
| 79 | +```lua |
| 80 | +-- Detect Django projects |
| 81 | +local function is_django_project(path) |
| 82 | + local current = path |
| 83 | + while current ~= "/" do |
| 84 | + -- Check for manage.py |
| 85 | + if vim.fn.filereadable(current .. "/manage.py") == 1 then |
| 86 | + return true |
| 87 | + end |
| 88 | + |
| 89 | + -- Check for pyproject.toml with django dependency |
| 90 | + -- Note: This is a naive check that just searches for "django" in the file. |
| 91 | + -- A more robust approach would parse the TOML and check dependencies properly. |
| 92 | + local pyproject = current .. "/pyproject.toml" |
| 93 | + if vim.fn.filereadable(pyproject) == 1 then |
| 94 | + local content = vim.fn.readfile(pyproject) |
| 95 | + for _, line in ipairs(content) do |
| 96 | + if line:match("django") then |
| 97 | + return true |
| 98 | + end |
| 99 | + end |
| 100 | + end |
| 101 | + |
| 102 | + current = vim.fn.fnamemodify(current, ":h") |
| 103 | + end |
| 104 | + return false |
| 105 | +end |
| 106 | + |
| 107 | +-- Auto-detect htmldjango filetype for Django projects |
| 108 | +vim.api.nvim_create_autocmd({ "BufRead", "BufNewFile" }, { |
| 109 | + pattern = "*.html", |
| 110 | + callback = function(args) |
| 111 | + local file_dir = vim.fn.fnamemodify(args.file, ":p:h") |
| 112 | + if is_django_project(file_dir) then |
| 113 | + vim.bo[args.buf].filetype = "htmldjango" |
| 114 | + end |
| 115 | + end, |
| 116 | +}) |
| 117 | +``` |
| 118 | + |
| 119 | +## Using with nvim-lspconfig |
| 120 | + |
| 121 | +[nvim-lspconfig](https://github.com/neovim/nvim-lspconfig) does not currently include a `djls` configuration, but we plan to submit one. Once available, if you have nvim-lspconfig installed, it will provide the configuration automatically and you can skip defining it yourself. |
| 122 | + |
| 123 | +## Troubleshooting |
| 124 | + |
| 125 | +Run `:checkhealth vim.lsp` to diagnose issues. |
| 126 | + |
| 127 | +Common problems: |
| 128 | + |
| 129 | +- **Server not starting**: Verify `djls` is installed (`which djls`) |
| 130 | +- **No completions**: Check that `DJANGO_SETTINGS_MODULE` is configured |
| 131 | +- **Wrong filetype**: Verify with `:set filetype?` that templates show `htmldjango` or `django-html` |
0 commit comments