mattn / efm-langserver

General purpose Language Server
MIT License
1.35k stars 61 forks source link

Format elixir file with `mix format` #153

Open collegeimprovements opened 3 years ago

collegeimprovements commented 3 years ago

Thanks a lot for this awesome plugin.

I'm trying to format elixir files with, the following config.

version: 2
log-level: 1

tools:
  eslint: &eslint
    lint-command: './node_modules/.bin/eslint -f unix --stdin'
    lint-ignore-exit-code: true
    lint-stdin: true

  mix_credo: &mix_credo
    lint-command: "mix credo suggest --format=flycheck --read-from-stdin ${INPUT}"
    lint-stdin: true
    lint-formats:
      - '%f:%l:%c: %t: %m'
      - '%f:%l: %t: %m'
    lint-category-map:
      R: N
      D: I
      F: E
      W: W
    root-markers:
      - mix.lock
      - mix.exs

  luafmt: &luafmt
    format-command: 'lua-format -i --no-keep-simple-function-one-line --no-break-after-operator --column-limit=150 --break-after-table-lb'
    format-stdin: true

  mix_format: &mix_format
    format-command: 'mix format'
    format-stdin: true

languages:
  elixir:
    - <<: *mix_credo
    - <<: *mix_format

But it doesn't seem to work.
Here is the relevant config from init.lua file:

require"lspconfig".efm.setup {
    init_options = {documentFormatting = true},
    filetypes = {"lua", "elixir"},
    settings = {
        rootMarkers = {".git/"},
        languages = {
            -- lua = {
            --     {
            --         formatCommand = "lua-format -i --no-keep-simple-function-one-line --no-break-after-operator --column-limit=150 --break-after-table-lb",
            --         formatStdin = true
            --     }
            -- }
        }
    }
}

However it's not working for elixir file but does work for lua files. Please help.

mattn commented 3 years ago

Sorry, I don't use neovim. Anyone, please help this.

AndreAugustoDev commented 3 years ago

Incredibly, this is simple and silly to solve I don't program in elixir, but I was able to test if the formatter was working. Just add an - at the end of the command, and it will work

---------------------------------------------
-- efm-langserver | General purpose LSP    --
---------------------------------------------

-- efm-langserver language modules--
-- mixformat | testing only --
local mixformat = {
        formatCommand = 'mix format -',
        formatStdin = true
    }

-- efm-langserver languages settings --
local efm_languages = {
    elixir = { mixformat }
}

-- Filetypes supported --
local efm_filetypes = {"elixir"}

local home = os.getenv("HOME")

-- efm-langserver Settings --
lspconfig.efm.setup{
    cmd = {'efm-langserver', '-logfile', home..'/.config/efm-langserver/efm.log', '-loglevel', '10'},
    filetypes = efm_filetypes,
    on_attach = on_attach,
    init_options = {documentFormatting = true},
    root_dir = function() return vim.fn.getcwd() end;
    settings = {
        rootMarkers = {".git/", vim.fn.getcwd()},
        languages = efm_languages
    }
}

Your format-command is "mix format" without the - at the end The correct is "mix format -" with - in the end.

According to what is in the documentation, the - is used to read output from stdin and write to stdout Docs at GitHub HexDocs reference