Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change indentation settings to use spaces instead of tabs #537

Open
fueripe-desu opened this issue Jan 10, 2025 · 2 comments
Open

Change indentation settings to use spaces instead of tabs #537

fueripe-desu opened this issue Jan 10, 2025 · 2 comments

Comments

@fueripe-desu
Copy link

I have tried setting the vim.opt.expandtab and it worked, when I :retab the tabs just turn into spaces, but the biggest problem is that I think the go formatter is setting the indentation to tabs when I format on save. Is there a way to override this setting?

This is my go.nvim setup using lazy.nvim:

return {
  "ray-x/go.nvim",
  dependencies = {  -- optional packages
    "ray-x/guihua.lua",
    "neovim/nvim-lspconfig",
    "nvim-treesitter/nvim-treesitter",
  },
  config = function()
    require("go").setup()
    -- Set tab and indentation options for Go files
    vim.api.nvim_create_autocmd("FileType", {
      pattern = "go",
      callback = function()
        vim.opt_local.expandtab = true
        vim.opt_local.tabstop = 2
        vim.opt_local.shiftwidth = 2
      end,
    })

    local format_sync_grp = vim.api.nvim_create_augroup("GoFormat", {})
    vim.api.nvim_create_autocmd("BufWritePre", {
      pattern = "*.go",
      callback = function()
      require('go.format').goimports()
      end,
      group = format_sync_grp,
    })

    vim.keymap.set("n", "<leader>t", ":GoTest<CR>")
    vim.keymap.set("n", "<leader>d", ":GoDebug<CR>")
    vim.keymap.set("n", "<leader>ds", ":GoDebug -s<CR>")
    vim.keymap.set("n", "<leader>db", ":GoBreakToggle<CR>")
  end,
  event = {"CmdlineEnter"},
  ft = {"go", 'gomod'},
  build = ':lua require("go.install").update_all_sync()' -- if you need to install/update all binaries
}
@endorama
Copy link

endorama commented Jan 15, 2025

I'm not the maintainer so I can confirm this with a 100% accuracy, but this plugin seems to default to using go fmt, https://github.com/ray-x/go.nvim?tab=readme-ov-file#code-format, for code formatting.

I would not expect there is a way to override this, as in Go the standard is to use tabs. This is enforced with go fmt, which automatically format the code using the defined language best practices.

Indentation
We use tabs for indentation and gofmt emits them by default. Use spaces only if you must.

Read more https://go.dev/doc/effective_go#formatting, https://go.dev/blog/gofmt

To achieve what you want you should disable the default formatting provided by this plugin and never run go fmt or gofumpt. (goimports may also apply tabs but I didn't check)

@fueripe-desu
Copy link
Author

Yeah, I was searching about this, and I have found it to be really frustrating, especially for people who don't like tabs as the default indentation. But anyways, thank you so much for replying!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants