LunarVim / LunarVim

🌙 LunarVim is an IDE layer for Neovim. Completely free and community driven.
https://www.lunarvim.org
GNU General Public License v3.0
18.31k stars 1.51k forks source link

Cannot set/override formatoptions in config #2878

Closed pezcore closed 2 years ago

pezcore commented 2 years ago

Problem description

When opening a file with

lvim src/lib.rs

The formatoptions value (as shown by :echo &fo) is vqnlj, but it is desired for formaoptions to contain the much-needed flag c (otherwise formatting doc comments is very painful). This is the case despite attempts to set the c flag in config.lua on the advice of @abzcoding in #2232:

diff --git a/.config/lvim/config.lua b/.config/lvim/config.lua
index 036b8ec..5437700 100644
--- a/.config/lvim/config.lua
+++ b/.config/lvim/config.lua
@@ -179,3 +179,17 @@ lvim.builtin.treesitter.highlight.enabled = true
 --     require("nvim-treesitter.highlight").attach(0, "bash")
 --   end,
 -- })
+lvim.autocommands._formatoptions = {}
+vim.opt.formatoptions = {
+  q = true, -- continue comments with gq"
+  c = true, -- Auto-wrap comments using textwidth
+  r = true, -- Continue comments when pressing Enter
+  n = true, -- Recognize numbered lists
+  t = true, -- autowrap lines using text width value
+  j = true, -- remove a comment leader when joining lines.
+  -- Only break if the line was not longer than 'textwidth' when the insert
+  -- started and only at a white character that has been entered during the
+  -- current insert command.
+  l = true,
+  v = true,
+}

This is the only change to an otherwise completely stock, fresh install of lunarvim.

The reason for this undesired behavior (inability to have c flag set in formatoptions) is this code. In general it should be possible to override and/or disable any option or behavior provided by lunarvim by modifying config.lua, but it appears that because of the mentioned code block, this is not possible as the c, r, and o flags are always removed from formatoptions whenever a new file is opened.

LunarVim version

master-47e4e5b

Neovim version (>= 0.7.2)

NVIM v0.7.2

Operating system/version

Manjaro Linux 5.15.57-2-MANJARO x86_64 GNU/Linux

Steps to reproduce

  1. Add configuration code to config.lua to enable the c flag in formatoptions:
diff --git a/.config/lvim/config.lua b/.config/lvim/config.lua
index 036b8ec..5437700 100644
--- a/.config/lvim/config.lua
+++ b/.config/lvim/config.lua
@@ -179,3 +179,17 @@ lvim.builtin.treesitter.highlight.enabled = true
 --     require("nvim-treesitter.highlight").attach(0, "bash")
 --   end,
 -- })
+lvim.autocommands._formatoptions = {}
+vim.opt.formatoptions = {
+  q = true, -- continue comments with gq"
+  c = true, -- Auto-wrap comments using textwidth
+  r = true, -- Continue comments when pressing Enter
+  n = true, -- Recognize numbered lists
+  t = true, -- autowrap lines using text width value
+  j = true, -- remove a comment leader when joining lines.
+  -- Only break if the line was not longer than 'textwidth' when the insert
+  -- started and only at a white character that has been entered during the
+  -- current insert command.
+  l = true,
+  v = true,
+}
  1. open a file with lvim filename and check the result of :echo &fo, c is not present

support info

LspInfo

Language client log: /home/xxx/.cache/lvim/lsp.log
 Detected filetype:   rust

 1 client(s) attached to this buffer: 

 Client: rust_analyzer (id: 1, pid: 28615, bufnr: [1])
    filetypes:       rust
    autostart:       true
    root directory:  /home/xxx/src/rust/xxx
    cmd:             rust-analyzer

 Configured servers list: rust_analyzer

LvimInfo


     __                          _    ___         
    / /   __  ______  ____ _____| |  / (_)___ ___ 
   / /   / / / / __ \/ __ `/ ___/ | / / / __ `__ \
  / /___/ /_/ / / / / /_/ / /   | |/ / / / / / / /
 /_____/\__,_/_/ /_/\__,_/_/    |___/_/_/ /_/ /_/ 

 Buffer info
 * filetype:                rust
 * bufnr:                   1
 * treesitter status:       active

 Active client(s)
 * name:                      rust_analyzer
 * id:                        1
 * supported filetype(s):     rust
 * attached buffers:          1
 * root_dir pattern:          1
 * capabilities:              declarationProvider        | hoverProvider           | implementationProvider
                              callHierarchyProvider      | selectionRangeProvider  | foldingRangeProvider
                              definitionProvider         | referencesProvider      | typeDefinitionProvider
                              documentFormattingProvider | documentSymbolProvider
                              documentHighlightProvider  | workspaceSymbolProvider

 Formatters info
 * Active: 
 * Supported: rustfmt

 Linters info
 * Active: 
 * Supported: []

 Code actions info
 * Active: 

Screenshots

No response

pezcore commented 2 years ago

I believe I have found a workaround to this issue. Adding

vim.api.nvim_del_augroup_by_name("_format_options")

to the end of config.lua will delete the offending augroup (defined here), and allow setting formatoptions to contain c and/or r and/or o.

But I wonder why does this augroup exist in the first place? Are there any features that actually depend on it? It seems pretty detrimental to LunarVim because it basically hijacks user configurability of formatoptions and makes it impossible for the user to set c, r, or o in their formatoptions. There is no info in the LunarVim documentation regarding this behavior or how to disable/override it, and discovering this workaround was rather challenging and time consuming for me.

I think at the very least, the LunarVim Documentation should explain that this behavior exists (since it is not standard Nvim behavior) and give instructions on how to disable/override it properly in the user config.

Or perhaps the augroup definition should be removed from LunarVim altogether, I can't really see the benefit of it.

vimpunk commented 2 years ago

Just ran into this. I'd like to see that autogroup removed too, there is no reason for it to exist.

rebuilt commented 2 years ago

It does seem irregular

Stupremee commented 2 years ago

Hey,

This is really annoying for me, and I was wondering if there is any new information on this?

github-actions[bot] commented 2 years ago

This issue is stale because it has been open for 30 days with no activity.

kylo252 commented 2 years ago

this has changed in #2592

you can add this to your config.lua

pcall(vim.api.nvim_del_augroup_by_name, "_format_options")