django-language-server/docs/editor-setup/neovim.md
Josh Thomas 5c821d8591
add basic documentation (#42)
* add basic documentation

* remove leading slash
2024-12-16 16:47:58 -06:00

1.7 KiB

Neovim

Using https://github.com/folke/lazy.nvim and https://github.com/neovim/nvim-lspconfig:

{
  "neovim/nvim-lspconfig",
  opts = {
    servers = {
      djls = {},
    },
    setup = {
      djls = function(_, opts)
        local configs = require("lspconfig.configs")
        local util = require("lspconfig.util")

        if not configs.djls then
          configs.djls = {
            default_config = {
              cmd = { "djls", "serve" },
              filetypes = { "htmldjango" },
              root_dir = function(fname)
                local root = util.root_pattern("manage.py", "pyproject.toml")(fname)
                vim.notify("LSP root dir: " .. (root or "nil"))
                return root or vim.fn.getcwd()
              end,
              handlers = {
                ["window/logMessage"] = function(_, params, _)
                  local message_type = {
                    [1] = vim.log.levels.ERROR,
                    [2] = vim.log.levels.WARN,
                    [3] = vim.log.levels.INFO,
                    [4] = vim.log.levels.DEBUG,
                  }
                  vim.notify(params.message, message_type[params.type], {
                    title = "djls",
                  })
                end,
              },
              on_attach = function(client, bufnr)
                vim.notify("djls attached to buffer: " .. bufnr)
              end,
            },
          }
        end
        require("lspconfig").djls.setup({})
      end,
    },
  },
}

!!! note

This configuration is copied straight from my Neovim setup and includes a logging setup that sends LSP messages to Neovim's notification system. You can remove all the references to `vim.notify` if you don't care about this functionality.