varikoz272
varikoz272

Reputation: 95

Remove auto format and autofix from ZLS COMPLETELY (with nvim kickstart)

ZLS version is for 0.12.0 (I put in into PATH and lua script and checked if some plugin puts builtin LSP which I dont need).

In config_gen/config.json:

        {
            "name": "enable_autofix",
            "description": "Whether to automatically fix errors on save. Currently supports adding and removing discards.",
            "type": "bool",
            "default": false
        },

And it still compresses my struct into a single line:

//before
const Container = struct {
    childs: std.ArrayList(),
    content: []u8
};

//after saving
const Container = struct { childs: std.ArrayList(), content: []u8 };

I tried to configure it in init.lua (I installed kickstart and did almost no changes):

        zls = {
          cmd = { 'C:/zig/lsp/1.12.0/zig-out/bin/zls.exe' },
          settings = {
            Lua = {
              format_on_save = false,
            },
          },
        },

And copypasted in Autoformat section right after lua's formater

It is better if I fix it not only for nvim, but in ZLS config or whatever

Upvotes: 0

Views: 690

Answers (2)

As mentioned above, you can just add comma, but if you still want to disable it:

According to https://zigtools.org/zls/editors/vim/nvim-lspconfig/ , you can use this global option:

-- disable format-on-save from `ziglang/zig.vim`
vim.g.zig_fmt_autosave = 0

To disable "fix all" you can try this, but I didn't tried myself:

vim.api.nvim_create_autocmd('BufWritePre',{
  pattern = {"*.zig", "*.zon"},
  callback = function(ev)
    vim.lsp.buf.code_action({
      -- Try to make that *only* array-like table empty
      -- original lines:
      -- context = { only = { "source.fixAll" } },
      -- apply = true,
      context = { only = {} },
      apply = false,
    })
  end
})

P.S. Also it should be zls instead of Lua for ZLS config of nvim-lspconfig in your code example (Lua is for Lua LS*):

local lspconfig = require('lspconfig')
lspconfig.zls.setup {
  -- Server-specific settings. See `:help lspconfig-setup`

  -- omit the following line if `zls` is in your PATH
  -- === IMO you can use PATH btw ^ ===
  cmd = { '/path/to/zls_executable' },
  -- There are two ways to set config options:
  --   - edit your `zls.json` that applies to any editor that uses ZLS
  --   - set in-editor config options with the `settings` field below.
  --
  -- Further information on how to configure ZLS:
  -- https://zigtools.org/zls/configure/
  settings = {
    zls = {
      -- Example from link above:
      -- Neovim already provides basic syntax highlighting
      semantic_tokens = "partial",

      -- omit the following line if `zig` is in your PATH
      zig_exe_path = '/path/to/zig_executable'
    }
  }
}

Upvotes: 1

chrboesch
chrboesch

Reputation: 314

If you also put a comma in the last line, the formatting will be implemented accordingly:

const Container = struct {
    childs: std.ArrayList(),
    content: []u8,
};

This also applies, for example, to parameters in functions, etc.:

fn foo(
    p1: u8,
    p2: u8,
    p3: u8,
) void {

Upvotes: 0

Related Questions