uros-5 / jinja-lsp

Language Server for jinja
https://crates.io/crates/jinja-lsp
MIT License
91 stars 1 forks source link

Neovim support #6

Closed liketoeatcheese closed 8 months ago

liketoeatcheese commented 8 months ago

Hey mate,

I saw in your description that it supports nvim. Is there documentation somewhere on how to set it up? Great stuff :)

uros-5 commented 8 months ago

Hi,

jinja-lsp should be installed on your path.

init.lua:

local nvim_lsp = require('lspconfig')
local configs = require('lspconfig.configs')
if not configs.jinja_lsp then
configs.jinja_lsp = {
  default_config = {
    cmd = { 'PATH/jinja-lsp'},
    filetypes = {'jinja', 'rs'},
    root_dir = function(fname) 
      return lspconfig.util.find_git_ancestor(fname)
    end,
    settings = {
      templates = './TEMPLATES_DIR',
      backend = {'./BACKEND_DIR'},
      lang = "rust"
    }
  }
}
end

nvim_lsp.jinja_lsp.setup{}

Lsp should be available for Rust files too.

I haven't used Neovim in while now, maybe they changed how to add custom language server.

liketoeatcheese commented 8 months ago

Righttt. Yea, that makes sense now. Thanks for replying! And I assume the templates dir would be the directory of the templates starting from the project’s root and the backend dir would be the src folder?

uros-5 commented 8 months ago

Yeah, correct. In configuration, backend is array(table) of all directories that can contain variables, functions, filters for templates. It's array because you may want to use cargo-workspaces. Check example.

liketoeatcheese commented 8 months ago

Amazing. I got it set up. Thanks again! I appreciate it :)

liketoeatcheese commented 8 months ago

Hi uros, I can see the LSP server was attached to the buffer, but I'm not sure if it's working. I couldn't see the suggestions and all the fancy stuff that was showcased. Is there a way to confirm or do I need to do anything to trigger it? Note that I also tried your example folder but it didn't work as well.

local lspconfig = require("lspconfig")
local configs = require("lspconfig.configs")
if not configs.jinja_lsp then
configs.jinja_lsp = {
  default_config = {
      cmd = { "/home/wpham/.cargo/bin/jinja-lsp" },
      filetypes = { "html", "jinja", "rs" , "css"},
      root_dir = function(fname)
          print(fname)
          return lspconfig.util.find_git_ancestor(fname)
      end,
      settings = {
          templates = "./templates",
          backend = { "./src" },
          lang = "rust",
      },
  },
}
end

lspconfig.jinja_lsp.setup({})

Above is my config. Here's my tree where I start my folder:

.
├── Cargo.toml
├── Dockerfile
├── configurations
│   ├── base.toml
├── data
├── rust-toolchain
├── src
│   ├── domains
│   │   ├── constants.rs
│   │   ├── csrf_token.rs
│   │   ├── mod.rs
│   │   ├── new_user.rs
│   │   ├── token.rs
│   │   ├── user_email.rs
│   │   └── user_name.rs
│   ├── lib.rs
│   ├── main.rs
│   ├── middleware
│   │   ├── authed_user_loose_ui.rs
│   │   ├── authed_user_protected_ui.rs
│   │   ├── mod.rs
│   │   └── tracing_logger.rs
│   ├── resources
│   │   ├── fonts
│   │   │   └── noto_color_emojis
│   │   │       ├── NotoColorEmoji-Regular.woff2
│   │   │       └── OFL.txt
│   │   ├── img
│   │   │   └── spinning-circles.svg
│   │   └── js
│   │       ├── chii.js
│   │       ├── htmx.js
│   │       └── response-targets.js
│   ├── routes
│   │   ├── api
│   │   │   ├── healthcheck.rs
│   │   │   ├── login.rs
│   │   │   ├── logout.rs
│   │   │   ├── map_tiles.rs
│   │   │   ├── mod.rs
│   │   │   ├── signup.rs
│   │   ├── frontend
│   │   │   ├── common
│   │   │   │   ├── mod.rs
│   │   │   │   ├── remove.rs
│   │   │   │   └── toast.rs
│   │   │   ├── componentlib
│   │   │   │   ├── mod.rs
│   │   │   │   └── serve_template.rs
│   │   │   ├── login
│   │   │   │   ├── login_ui.rs
│   │   │   │   └── mod.rs
│   │   │   ├── mainpage
│   │   │   │   ├── main_ui.rs
│   │   │   │   └── mod.rs
│   │   │   └── mod.rs
│   │   └── mod.rs
│   ├── settings.rs
│   ├── startup.rs
│   ├── telemetry
│   │   └── mod.rs
│   └── utils
│       ├── errors
│       │   ├── actix_error_wrapper.rs
│       │   ├── debugger.rs
│       │   └── mod.rs
│       ├── general
│       │   ├── generate_error_toast.rs
│       │   ├── mod.rs
│       │   └── zero_pad_number.rs
│       ├── mod.rs
│       ├── tera
│       │   ├── meta_context.rs
│       │   └── mod.rs
│       └── test
│           ├── mod.rs
│           └── spawnapp.rs
├── templates
│   ├── 000_common
│   │   ├── colors.css
│   │   ├── cssreset.css
│   │   ├── layout.html
│   │   └── setTheme.js
│   ├── 001_fonts
│   │   └── noto_color_emoji.css
│   ├── 002_componentlib
│   │   ├── buttons
│   │   │   ├── buttons.css
│   │   │   └── index.html
│   │   ├── modal
│   │   │   ├── index.html
│   │   │   ├── modal.css
│   │   │   └── modal.js
│   │   ├── search_bar
│   │   │   ├── index.html
│   │   │   ├── search_bar.css
│   │   │   └── search_bar.js
│   │   ├── theme_switcher
│   │   │   ├── index.html
│   │   │   ├── theme_switcher.css
│   │   │   └── theme_switcher.js
│   │   └── toast
│   │       ├── alert.html
│   │       ├── index.html
│   │       └── toast.css
│   ├── map
│   │   └── index.html
│   └── pages
│       ├── 000_base
│       │   └── index.html
│       ├── login
│       │   ├── css
│       │   │   └── login.css
│       │   └── index.html
│       ├── main
│       │   ├── css
│       │   │   └── main.css
│       │   ├── index.html
│       │   └── js
│       │       └── js_map.html
│       └── redirect
│           └── index.html
├── test.txt

When I do LspInfo: image

Happy to provide any extra info if needed

uros-5 commented 8 months ago

Try this:

vim.filetype.add {
  extension = {
    jinja = 'jinja',
    jinja2 = 'jinja',
    j2 = 'jinja',
  },
}

-- uncomment this part if language server is not working
-- vim.lsp.set_log_level("debug")

local nvim_lsp = require('lspconfig')
local configs = require('lspconfig.configs')

-- try to remove this if statement at the moment and leave only that inner block
-- if it succeeds then add it again

if not configs.jinja_lsp then
configs.jinja_lsp = {
  default_config = {
    name = "jinja-lsp",
    cmd = { "/home/wpham/.cargo/bin/jinja-lsp" },
    filetypes = { "html", "jinja", "rs" , "css"},
    root_dir = function(fname)
      return "."
      --return nvim_lsp.util.find_git_ancestor(fname)
    end,
    init_options = {
      templates = './templates',
      backend = { './src' },
      lang = "rust"
    }
  }
}
end

nvim_lsp.jinja_lsp.setup {}
liketoeatcheese commented 8 months ago

https://github.com/uros-5/jinja-lsp/assets/88610355/7aa3ad7d-c0e1-4aff-b1fd-f19ee66781eb

vim.filetype.add({
    extension = {
        jinja = "jinja",
        jinja2 = "jinja",
        j2 = "jinja",
    },
})

local lspconfig = require("lspconfig")

local configs = require("lspconfig.configs")
if not configs.jinja_lsp then
    configs.jinja_lsp = {
        default_config = {
            cmd = { "/home/wpham/.cargo/bin/jinja-lsp" },
            filetypes = { "html", "jinja", "rs", "css" },
            root_dir = function(fname)
                                 return "."
            end,
            settings = {
                templates = "./templates",
                backend = { "./src" },
                lang = "rust",
            },
        },
    }
end

lspconfig.jinja_lsp.setup({})

The lsp does attach to the buffer, but it's not providing suggestions, etc... Attached is the video showing the example project

uros-5 commented 8 months ago

Replace your current configuration with one I mentioned in previous comment.

liketoeatcheese commented 8 months ago

Noiceee! I got it working <3 Thanks for that!

zilahir commented 4 months ago

Heyo!

unfortunately, i have the same issue, i cant seem to get this working. use the snippet you provided @uros-5 , the LSP gets attached to the buffer, but nothing really happening, no syntax highlight etc.

i am using a lazyvim, and created a file for the config. However, i needed to add a

return {}

at the end of the file, otherwise it failes as i am not returning a table, but a boolean.

Any tips would be greatly appreciated! 🙏

avk458 commented 1 month ago

Heyo!

unfortunately, i have the same issue, i cant seem to get this working. use the snippet you provided @uros-5 , the LSP gets attached to the buffer, but nothing really happening, no syntax highlight etc.

i am using a lazyvim, and created a file for the config. However, i needed to add a

return {}

at the end of the file, otherwise it failes as i am not returning a table, but a boolean.

Any tips would be greatly appreciated! 🙏

Hi. I'm using lazy.vim as well, but I setup the LSP by leveraging the nvm-lsconfig plugin.However the LSP doesn't seem to attached to the .html buffer, could you please provide your config file? Any tips would be greatly appreciated! 🙏