elixir-tools / elixir-tools.nvim

Neovim plugin for Elixir
MIT License
395 stars 29 forks source link

Format-on-save insists on adding () to macros #179

Closed mikl closed 8 months ago

mikl commented 8 months ago

Working with the Phoenix application skeleton, you’ll have lots of macros for schemas, components and the like.

Phoenix does not use parens (()) around such invocations by default.

But as soon as I edit a file containing these while using elixir-tools.nvim, they are reformatted to add parents. So if I edit the core_components.ex that you get from the current mix phx.new, you get many screenfuls of diffs like these:

Screenshot 2023-11-02 at 05 49 23@2x

I haven’t been able to find a workaround – other than using VSCode for such files. For some reason, elixir-tools in VSCode does not seem to have this issue.

mhanberg commented 8 months ago

Is this an umbrella project?

Also are you using asdf?

mhanberg commented 8 months ago

Also, just to confirm, are you using Next LS or Elixir LS?

Next LS is enabled by default in elixir-tools.vecode, but it is not in elixir-tools.nvim

mikl commented 8 months ago

Umbrella: No asdf: yes Elixir LS or Next LS: tried both, problem seems to happen with both

I noticed in :LspInfo that every buffer seems to have two instances of the language server, dunno if that’s normal:

 Press q or <Esc> to close this window. Press <Tab> to view server doc.

 Language client log: /Users/mikl/.local/state/nvim/lsp.log
 Detected filetype:   elixir

 3 client(s) attached to this buffer: 

 Client: elixirls (id: 1, bufnr: [1, 164])
    filetypes:       elixir, eelixir, heex, surface
    autostart:       true
    root directory:  /Volumes/Work/employer/project
    cmd:             elixir-ls

 Client: NextLS (id: 2, bufnr: [1, 164])
    filetypes:       
    autostart:       false
    root directory:  /Volumes/Work/employer/project/packages/codename
    cmd:             /Users/mikl/.cache/elixir-tools/nextls/bin/nextls --stdio

 Client: NextLS (id: 3, bufnr: [1])
    filetypes:       
    autostart:       false
    root directory:  /Volumes/Work/employer/project/packages/codename
    cmd:             /Users/mikl/.cache/elixir-tools/nextls/bin/nextls --stdio

 Configured servers list: yamlls, lua_ls, jsonls, elixirls, tsserver, eslint

My config file looks like this:

-- % cat lua/plugins/elixir-tools.lua
return {
  "elixir-tools/elixir-tools.nvim",
  ft = { "elixir", "eex", "heex", "surface" },
  event = { "BufReadPre", "BufNewFile" },
  config = function()
    local elixir = require("elixir")
    local elixirls = require("elixir.elixirls")

    elixir.setup({
      nextls = { enable = true },
      credo = {},
      elixirls = {
        enable = false,
        settings = elixirls.settings({
          dialyzerEnabled = false,
          enableTestLenses = false,
        }),
        on_attach = function(_client, _bufnr)
          -- whatever keybinds you want, see below for more suggestions
          vim.keymap.set("n", "<space>fp", ":ElixirFromPipe<cr>", { buffer = true, noremap = true })
          vim.keymap.set("n", "<space>tp", ":ElixirToPipe<cr>", { buffer = true, noremap = true })
          vim.keymap.set("v", "<space>em", ":ElixirExpandMacro<cr>", { buffer = true, noremap = true })
        end,
      },
    })
  end,
  dependencies = {
    "nvim-lua/plenary.nvim",
  },
}

Also, I do not have the LazyVim lang-elixir extra enabled, so it should not be interfering here.

mhanberg commented 8 months ago

Can you disable elixir LS?

If you have both enabled, it'll run both formatters and you can't know which one is adding the parens.

mhanberg commented 8 months ago

Oh I can't read.

But I think that lsp info indicates elixir LS is being started by something else because the cmd is not what it would be if elixir tools started it

mhanberg commented 8 months ago

And it wouldn't say auto start true if it was started by elixir tools

mikl commented 8 months ago

Ah, finally found the extra elixir-ls instance.

I didn’t have elixir-ls anywhere in my config, but it turned out there was a copy of elixir-ls in ~/.local/share/nvim/mason/packages/elixir-ls. Mason apparently has its own package manager on top of Lazy, and a very old version of elixir-ls was installed there from a previous configuration.

After removing that, the formatter works as expected.

So not a elixir-tools problem at all, sorry for the confusion and thanks for all your hard work on elixir-tools.

mikl commented 8 months ago

Oh, and to anyone else having similar problems, if you still have Mason installed, there’s a :MasonUninstallAll command, should clean out any old stuff in there.