ohnoimdead

ohnoimdead

Getting Heex and ~H working in nvim with AstroNvim

Heya, apologies in advance if there’s a better place to ask this, happy to move it if so.

I’m just playing around with AstroNvim (GitHub - AstroNvim/AstroNvim: AstroNvim is an aesthetic and feature-rich neovim config that is extensible and easy to use with a great set of plugins · GitHub) and trying to get all of elixir, heex, and TailwindCSS working similarly to VSCode with the usual plugins. I’m 90% sure this is my lack of understanding of how to configure neovim to use the right language server and/or treesitter for .ex and .heex, but I’m a bit stuck at the moment. Curious if anyone else has gotten this all working? And to head off the simple questions: I have tailwindcss and elixirls with lsp and elixir and heex with ts. All the elixirls stuff works great in .ex, and all of tailwindcss works in .html, but it doesn’t all want to work together in .ex inside ~H sigils or in .heex.

I’ll keep poking at it until I find the right knobs and switches, but if someone is like: oh yeah, just do [obvious thing that I’m missing], then that would be super helpful.

Cheers!

Marked As Solved

ohnoimdead

ohnoimdead

The elixir-phoenix pack was added to the astrocommunity repo making it very easy to get up and running with AstroNvim.

  1. Install the latest AstroNvim per the instructions at https://docs.astronvim.com/
  2. Remove the first line in ~/.config/nvim/lua/community.lua which looks like if true then return {} end to enable astrocommunity
  3. Add { import = "astrocommunity.pack.elixir-phoenix" }
  4. Profit :magic_wand:

Here’s what your astrocommunity.lua file might contain, for reference:

---@type LazySpec
return {
  "AstroNvim/astrocommunity",
  { import = "astrocommunity.pack.elixir-phoenix" },
  { import = "astrocommunity.pack.lua" },
  -- import/override with your plugins folder
}

Also Liked

groig

groig

I use neovim configured from scratch and it wasn’t difficult to get it all working. Here are the relevant parts of my config:

Treesitter options:

{
        ensure_installed = "all",
        highlight = {
            enable = true,
        },
}

Lsp configs for tailwind and elixir-ls

    lspconfig.elixirls.setup(vim.tbl_extend("force", lsp_options, {
      cmd = { "elixir-ls" },
      settings = { elixirLS = { dialyzerEnabled = false } },
    }))

    lspconfig.tailwindcss.setup(vim.tbl_extend("force", lsp_options, {
      filetypes = { "html", "elixir", "eelixir", "heex" },
      init_options = {
        userLanguages = {
          elixir = "html-eex",
          eelixir = "html-eex",
          heex = "html-eex",
        },
      },
      settings = {
        tailwindCSS = {
          experimental = {
            classRegex = {
              'class[:]\\s*"([^"]*)"',
            },
          },
        },
      },
    }))

Some screenshots showing the highlight and the elixir-ls and tailwind completions both inside ~H sigils and heex files:

Here is a very trimmed down version of kickstart.nvim with working treesitter highlight, completion and snippets similar to how it works on my config

17
Post #8
ohnoimdead

ohnoimdead

Well no worries either way because, thanks to your help, I got it to work!

For those that want to get this to work today (because this will hopefully become out of date quickly and just work out-of-the-box):

  1. Install neovim 0.8+, AstroNvim, and the required deps, see GitHub - AstroNvim/AstroNvim: AstroNvim is an aesthetic and feature-rich neovim config that is extensible and easy to use with a great set of plugins · GitHub
  2. Use LspInstall (type :LspInstall then space, tab to autocomplete) to install: elixir-ls, emmet-ls, and tailwindcss-language-server
  3. Use TSInstall (type :TSInstall then space, tab to autocomplete) to install: elixir, heex, and optionally eex
  4. Edit ~/.config/nvim/init.lua and add the following under line 18 which starts with require("astronvim.utils").conditional_func...
local lspconfig = require("lspconfig")
local capabilities = vim.lsp.protocol.make_client_capabilities()

lspconfig.tailwindcss.setup({
  capabilities = capabilities,
  filetypes = { "html", "elixir", "eelixir", "heex" },
  init_options = {
    userLanguages = {
      elixir = "html-eex",
      eelixir = "html-eex",
      heex = "html-eex",
    },
  },
  settings = {
    tailwindCSS = {
      experimental = {
        classRegex = {
          'class[:]\\s*"([^"]*)"',
        },
      },
    },
  },
})

lspconfig.emmet_ls.setup({
  capabilities = capabilities,
  filetypes = { "html", "css", "elixir", "eelixir", "heex" },
})

And go to town! My only minor complaint is the lack of color swatches for Tailwind colors. :stuck_out_tongue: However, there’s a few things that are even better than VSCode imo, like the first-class ripgrep support. And, of course, true vim support in everything, including the file explorer. And hell, it looks great!


There is one SUPER STRANGE gotcha, which is that the tailwindcss ls can’t resolve the root directory for Phoenix projects unless it’s in git (there’s a .git folder). So if you don’t see tailwindcss in the status line and lsp says root directory: Not found. for tailwindcss, make sure to run git init. :person_shrugging:

Bonus: here’s a screen shot from Pop! OS with the same setup (which is where I’m at most of the time)

groig

groig

Awesome!

Take a look here.

The root directory detection is configurable. Try adding this to your tailwind server options, maybe below filetypes:

root_dir = lspconfig.util.root_pattern('tailwind.config.js', 'tailwind.config.ts', 'postcss.config.js', 'postcss.config.ts', 'package.json', 'node_modules', '.git', 'mix.exs'),

Where Next?

Popular in Questions Top

beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> somethi...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
JulienCorb
I am trying to implement my new.html.eex file to create new posts on my website. new.html.eex: <h1>Create Post</h1> <%= ...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: The documentation above suggests that while ...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

Other popular topics Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 39523 209
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New
New

We're in Beta

About us Mission Statement