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

formatexpr=Fixedqg silently overrides user set formatprg #209

Open
cideM opened this issue Oct 20, 2020 · 6 comments
Open

formatexpr=Fixedqg silently overrides user set formatprg #209

cideM opened this issue Oct 20, 2020 · 6 comments

Comments

@cideM
Copy link

cideM commented Oct 20, 2020

I originally reported this problem in Neovim since that's where the runtime file is, but I think it's just taken from yats or at least parts of it are.

typescript.vim sets up formatexpr which has priority over formatprg for the gq mapping, as is explained in the Neovim :h

gq{motion}		Format the lines that {motion} moves over.
			Formatting is done with one of three methods:
			1. If 'formatexpr' is not empty the expression is
			   evaluated.  This can differ for each buffer.
			2. If 'formatprg' is not empty an external program
			   is used.
			3. Otherwise formatting is done internally.

The same happens in yats.vim, obviously, since that's where the runtime file in Neovim comes from.

I'm reporting this is kind of a bug since this behavior is quite challenging to debug especially for people who are new to Neovim. The thing is that there's nothing warning you about this behavior and at least in my experience formatexpr is a pretty rare sight. If you search for "how to format a file in Neovim" you'll most likely come across various plugins, but also recommendations to leverage formatprg and gq.

Now I was wondering... what's the purpose of the Fixedgq function and can this maybe be handled separately? For example, users who want the behavior of that function could just wrap gq with it, or bind it to a key as a <Plug> mapping, or it could at least be behind a global and/or buffer local option.

@equt
Copy link
Contributor

equt commented Oct 27, 2020

This is exactly the problem I came across these few days. Instead of using Vimscript to rewrite one, I would prefer using prettier, the de facto standard formatting tool for javascript or typescript.

So it would lead to the following settings in my ftplugin/typescript.vim.

let s:prettier_config = {
	" ...
\ }

let &l:formatexpr = 'prettier' . utils#map_as_cli_options(s:prettier_config)

But all stuff in after/ftplugin loads after after/indent, so I have to create my after/indent for overwriting that.

I would suggest removing the formatexpr setting from indent or moving it into ftplugin. typescript seems to be the only language I found to set the formatexpr inside the indent. Python, JavaScript, OCaml and Rust maintained by the Rust lang team only place indention related settings inside the indent directory.

c.c. neovim/neovim#13113

@equt
Copy link
Contributor

equt commented Oct 28, 2020

I would suggest removing the formatexpr setting from indent or moving it into ftplugin.

Now moved to the ftplugin.

For example, users who want the behavior of that function could just wrap gq with it, or bind it to a key as a mapping, or it could at least be behind a global and/or buffer local option.

Instead of adding a <Plug> mapping or a global option, I simply add code to check whether the format tool has been set at that moment. Also, it's been set to formatprg for a lower priority.

PR #212

@cideM
Copy link
Author

cideM commented Oct 28, 2020

Wow that was quick, thanks!

@BenoitZugmeyer
Copy link
Contributor

👋 It seems that the change brought by PR #212 is breaking gqq (or gq in visual mode): this command now inserts zsh:1: unknown file attribute: v in the buffer instead of formating lines.

See user reports:

@equt
Copy link
Contributor

equt commented Nov 9, 2020

I don't know about NeoVim, but in Vim, &formatprg is an external command, and you can't really expect external commands to have access to Vim's internal variables.

Yes, this should be exactly the point here, and it's hard to believe that I even never notice that before. Sorry for the disruption, @BenoitZugmeyer, and I have fixed that in PR #216.

@David-Else
Copy link

I am unsure how this repo relates to Neovim official, but I just wasted hours with this horrible bug! Does the fact it is now fixed here mean it will be fixed in Neovim, or does someone need to say something to the Neovim people? Cheers!

cideM added a commit to cideM/dotfiles that referenced this issue Sep 13, 2021
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

4 participants