LazyVim / LazyVim

Neovim config for the lazy
https://lazyvim.github.io/
Apache License 2.0
16.3k stars 1.14k forks source link

feature: Improve documentation (for formatting) for newbies #3136

Closed tf42src closed 3 months ago

tf42src commented 3 months ago

Did you check the docs?

Is your feature request related to a problem? Please describe.

I am trying to format html. It is not fully clear to me what the required steps are. Do I need to install conform.nvim? Do I need to install a language server, do I need to have npm, do I need prettier?

I started with the StarterTemplate which works great. I am really enjoying it and I am getting used more and more how to use everything. What I couldn't figure out so far what steps do I need to do to enable formatting (on save and on shortcut) for html.

<leader>cf (while a html file is open) currently gives me "No formatter available". So I head over to Formatting which tells me I should use conform.nvim - which seems to be installed (see screenshot).

Formatting, also gives some configuration options: opts and I kind of understand that I need to add sth. like html = {"prettier"} to formatters_by_ft. Makes sense, but where?

I cannot find conform.nvim anywhere in my .config/nvim/ folder except for one entry in ./lazy-lock.json. I followed this well-explained youtube video to enable formatting which lead to "Don't override plugin.config directly, since this will break LazyVim formatting."

I also tried copying the "Full Spec" from Formatting into a new file in my plugins directory which also didn't work (the built-in lua LSP hinted at all sorts of errors).

Apologies for the lengthy description, but hopefully it makes it a bit clear what a new user of lazyvim might go through and struggle with.

Screenshot from 2024-05-11 09-59-38

Describe the solution you'd like

A simple example how/where to enable formatting for anything but what is enabled already.

Describe alternatives you've considered

Youtube :)

Additional context

NVIM v0.9.4
Build type: RelWithDebInfo
LuaJIT 2.1.1692716794
Compilation: /usr/bin/cc -O2 -g -Og -g -Wall -Wextra -pedantic -Wno-unused-parameter -Wstrict-prototypes -std=gnu99 -Wshadow -Wconversion -Wvla -Wdouble-promotion -Wmissing-noreturn -Wmissing-format-attribute -Wmissing-prototypes -fno-common -Wno-unused-result -Wimplicit-fallthrough -fdiagnostics-color=always -fstack-protector-strong -DUNIT_TESTING -DINCLUDE_GENERATED_DECLARATIONS -D_GNU_SOURCE -I/build/nvim/parts/nvim/build/.deps/usr/include/luajit-2.1 -I/usr/include -I/build/nvim/parts/nvim/build/.deps/usr/include -I/build/nvim/parts/nvim/build/build/src/nvim/auto -I/build/nvim/parts/nvim/build/build/include -I/build/nvim/parts/nvim/build/build/cmake.config -I/build/nvim/parts/nvim/build/src -I/usr/include -I/build/nvim/parts/nvim/build/.deps/usr/include -I/build/nvim/parts/nvim/build/.deps/usr/include -I/build/nvim/parts/nvim/build/.deps/usr/include -I/build/nvim/parts/nvim/build/.deps/usr/include -I/build/nvim/parts/nvim/build/.deps/usr/include -I/build/nvim/parts/nvim/build/.deps/usr/include

   system vimrc file: "$VIM/sysinit.vim"
  fall-back for $VIM: "/usr/share/nvim"

Run :checkhealth for more info
dpetka2001 commented 3 months ago

Formatting, also gives some configuration options: opts and I kind of understand that I need to add sth. like html = {"prettier"} to formatters_by_ft. Makes sense, but where? I cannot find conform.nvim anywhere in my .config/nvim/ folder except for one entry in ./lazy-lock.json. I followed this well-explained youtube video to enable formatting which lead to "Don't override plugin.config directly, since this will break LazyVim formatting."

It's mentioned in the docs where you should place your own customization right here. So you just create a file in /lua/plugins/ directory and inside the file you make the changes you want for the specific plugin spec you're interested in. So, you would do something like

return {
{
  "stevearc/conform.nvim",
  opts = {
    formatters_by_ft = {
      ["html"] = { "prettier" },
    },
  },
},
}

I also tried copying the "Full Spec" from Formatting into a new file in my plugins directory which also didn't work (the built-in lua LSP hinted at all sorts of errors).

You should never copy the entire from what's in the docs. Usually there's a warning about this in the docs, but for some reason in the formatting page it's missing.

caution You don't need to copy the default settings to your config. They are only shown here for reference.

So, in summary, you create files in ~/.config/nvim/lua/plugins/ directory and there you make changes to the specs of the plugins you want to change, as is mentioned in the docs.

tf42src commented 3 months ago

Thanks for the quick reply! I have added the "opts" in my plugin directory like this:

Screenshot from 2024-05-12 01-22-36

(I have removed the "format_on_save" again - because that wasn't allowed). Nevertheless, navigating to an html file and pressing <leader>cf still shows, I don't have a formatter:

Screenshot from 2024-05-12 01-22-20

I have double checked, that prettier is installed.

Screenshot from 2024-05-12 01-28-17

The only hint I could find was this (but it doesn't tell me what the error is):

cat  ~/.local/state/nvim/conform.log
01:23:21[ERROR] Formatter 'prettier' error: 
01:23:38[ERROR] Formatter 'prettier' error: 
01:23:40[ERROR] Formatter 'prettier' error: 
01:23:45[ERROR] Formatter 'prettier' error: 

I can run prettier manually and it works:

~/node_modules/.bin/prettier ~/projects/testproj/intro.html

tf42src commented 3 months ago

Did a bit more digging and found this, which made me check what I have installed, but everything looks fine:

Screenshot from 2024-05-12 01-54-09

dpetka2001 commented 3 months ago

What does :!which prettier show inside Neovim?

tf42src commented 3 months ago

Screenshot from 2024-05-12 15-53-02

I can execute prettier directly from the command line too (it's added to $PATH).

dpetka2001 commented 3 months ago

What does LazyFormatInfo and ConformInfo show?