Skip to content

Configuring shfmt #1246

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

Closed
ambroisie opened this issue Dec 18, 2024 · 4 comments
Closed

Configuring shfmt #1246

ambroisie opened this issue Dec 18, 2024 · 4 comments

Comments

@ambroisie
Copy link

Code editor

NeoVim

Platform

Linux (NixOS)

Version

5.4.0

What steps will reproduce the bug?

There's no "Discussion" template, this isn't really a bug, just a question.

The documentation states:

The indentation used by shfmt is whatever has been configured for the current editor session, so there is no shfmt-specific configuration variable for this. If your editor is configured for two-space indents then that's what it will use. If you're using tabs for indentation then shfmt will use that.

I'm not sure I understand how I can configure shfmt the way I used to when I ran it standalone in my editor (-i 4 -s -ci). The README mentions EditorConfig, but that doesn't give me a global configuration, and is less convenient than relying on my default configuration for random files.

How often does it reproduce? Is there a required condition?

No response

What is the expected behavior?

No response

What do you see instead?

N/A

Additional information

No response

@ambroisie
Copy link
Author

ambroisie commented Dec 18, 2024

I found out I could use settings when configuring the LSP server to set various options for shfmt:

        settings = {
            bashIde = {
                shfmt = {
                    languageDialect = "auto",
                    simplifyCode = true,
                    caseIndent = true,
                },
            },
        },

But indentation is not set correctly: I have my editor set to use 4 spaces for indentation, and my scripts get reformatted to use 8 spaces instead...

@chris-reeves
Copy link
Contributor

Yes, setting options via LSP server config options would be my recommendation as it is the most configurable. .editorconfig is also supported, however some options are not configurable (only simplify, IIRC). If any shfmt-specific options are set in .editorconfig then LSP config will be ignored.

The exception is indentation. Indentation always comes from the client via the language server. If you're seeing 8-space indents, then that is what the client is sending through when requesting document formatting (the default is to use tabs, so the client must have explicitly requested 8-space indents).

Perhaps some experimentation to understand behaviour might be a good place to start? For example, what if you set your editor to 3-space indents - does that result in 6-space indents, or do you get 8-space regardless? I think I'd need to see a capture of the messages between your client and the server in order to see where the problem lies, and I'm not sure I can help you with how to do that for your particular client.

@ambroisie
Copy link
Author

It looks like the server must be sending tabs, and my editor is expending them to spaces:

  • By default, tabstop (the width of a tab) is set to 8.
  • Changing it to 4 or 12 changes the width used in formatting through LSP.

@ambroisie
Copy link
Author

I think it might actually be an issue with the format-on-save plug-in that I've been using, see lukas-reineke/lsp-format.nvim/issues/94.

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