nvf icon indicating copy to clipboard operation
nvf copied to clipboard

Markdown; [LSP] Format request failed, no matching language servers.

Open sevenpkgs opened this issue 8 months ago • 13 comments

⚠️ Please verify that this bug has NOT been reported before.

  • [x] I checked all existing issues and didn't find a similar issue

Description

Pressing the keybind to format the text (leader+l+f) by default shows the following error message: [LSP] Format request failed, no matching language servers. But if toggle on save is enabled, saving the file formats the text as well.

Something worth noting is that everything works fine for .nix files. config for settings.vim.languages.nix

languages.nix =  {
            enable = true;
            lsp.enable = true;
            format.enable = true;
            format.type = "alejandra";
          };

👟 Reproduction steps

Relevant config:

       settings.vim.lsp = {
          formatOnSave = true;
        };
          settings.vim.languages.markdown = {
            enable = true;
            extensions.render-markdown-nvim.enable = true;
            format.enable = true;
            format.type = "deno_fmt";
            lsp.enable = true;
          };
  1. Open a markdown file
  2. Press the keybind to format the text (leader+l+f by default)

👀 Expected behavior

The text should get formatted.

😓 Actual Behavior

The error message pops up, and the text isn't formatted.

💻 Metadata

nix-shell -p nix-info --run "nix-info -m"

- system: `"x86_64-linux"`
 - host os: `Linux 6.6.85, NixOS, 24.11 (Vicuna), 24.11.20250330.7ffe0ed`
 - multi-user?: `yes`
 - sandbox: `yes`
 - version: `nix-env (Nix) 2.24.13`
 - nixpkgs: `/nix/store/szpwkdrxwjkd3hwi8cqviscfdgg2jzz5-source`

📝 Relevant log output

Nothing relevant in the logs.

sevenpkgs avatar Apr 04 '25 15:04 sevenpkgs

The difference between formatOnSave and <leader>lf is that the latter comes from the LSP module, and takes effect only if the active LSP provides formatting capabilities- which our current Markdown LSPs do not provide. Since I have always used formatOnSave, Since <leader>lf comes from LSP module itself, I don't like the idea of giving it the ability to format with conform, so I'd suggest setting a keybind to format with a formatter plugin (like conform) instead. Though I am open to other ideas.

NotAShelf avatar Apr 04 '25 15:04 NotAShelf

[...] takes effect only if the active LSP provides formatting capabilities- which our current Markdown LSPs do not provide.

But I've been using the <leader>lf keybind with no problems until I updated nvf this morning.

sevenpkgs avatar Apr 04 '25 15:04 sevenpkgs

Now that's interesting. Would you be able to bisect which commit has introduced this?

NotAShelf avatar Apr 04 '25 15:04 NotAShelf

Yeah, I hadn't updated in a while, and it's probably this commit. I could be wrong though, but it seems very likely.

Also, looking at the last few lines, it looks like nvf enables conform anyways (if format is enabled) so me not having conform enabled outside of that shouldn't be a problem right? The last few lines in question:

 (mkIf cfg.format.enable {
      vim.formatter.conform-nvim = {
        enable = true;
        setupOpts.formatters_by_ft.markdown = [cfg.format.type];
        setupOpts.formatters.${
          if cfg.format.type == "denofmt"
          then "deno_fmt"
          else cfg.format.type
        } = {
          command = getExe cfg.format.package;
        };
      };
    })

sevenpkgs avatar Apr 04 '25 15:04 sevenpkgs

<leader>lf requests only a LSP format, so conform cannot take effect. Previously it was handled by null-ls, which hands formatting to LSP as a fallback. @horriblename I think conform can do this as well, should we add a LSP fallback to languages that support LSP formatting?

NotAShelf avatar Apr 04 '25 15:04 NotAShelf

@horriblename I think conform can do this as well, should we add a LSP fallback to languages that support LSP formatting?

you mean by changing lf to call conform instead? probably we should. one thing to note is the formatting keybind is registered on lsp attach, we should add it "globally" instead

horriblename avatar Apr 05 '25 10:04 horriblename

I'm also seeing this for typescript, but for my case i'm getting mixed results that's kinda confusing

behaviour:

  1. load ts file, lsp loaded but no formatting diagnostic showing up
  2. save the file(which triggers formatOnSave, formatting diagnostic shows up). The file gets formatted properly
  3. <leader>formatkey doesn't work pre or post-file save
 programs.nvf.settings.vim.languages = {
    enableLSP = true;
    enableDAP = true;
    enableExtraDiagnostics = true;
    enableFormat = true;
    enableTreesitter = true;

    ts.enable = true;
  };

  programs.nvf.settings.vim.lsp = {
    enable = true;
    formatOnSave = true;
  }

mnpqraven avatar Apr 07 '25 09:04 mnpqraven

I'd suggest setting a keybind to format with a formatter plugin (like conform) instead.

@NotAShelf , apologies for the mention but I'd like to know how to add a keybind for that. I couldn't find in docs how to add keybinds for plugins.

sevenpkgs avatar Apr 21 '25 04:04 sevenpkgs

I guess i'm slightly confused on what the best option is here for writing markdown without getting bombarded with errors, I use the lsp.formatOnSave option because it seems fairly straightforward and works great for nix. When it comes to markdown is there an easy way to setup formatting through nvf where I don't have to bother hitting a different keybinding? And I guess this will be an issue for every language that doesn't offer formatting through the lsp?

Edit: Just to clarify I have tested if deno is actually formatting on markdown files, it seems it is so my main issue is just getting rid of "press enter or type command to continue" message every single time I save the file, which i'm sure is solvable.

cenunix avatar May 08 '25 08:05 cenunix

I'm also having this issue except even formatOnSave seems not to work for .ts or .js files

valyntyler avatar May 13 '25 15:05 valyntyler

#891 should have smoothed out the remaining issues with formatOnSave, perhaps an update would help.

the <leader>lf keybind will only work with LSP formatting, if you want a conform keybind, add your own, I'll show 2 options below. the <leader>l prefix is only used for LSP stuff and I'm not keen on changing that.

option 1: set LSP fromatting keybind to null and add custom keymap for conform:

vim.keymaps = [
  {
    key = "<leader>lf";
    mode = ['n'];
    lua = true;
    action = ''
      require('conform').format({
        lsp_format = "fallback"
      })
    '';
  }
];

option 2: on the other hand, if you prefer using gq, simply set

vim.options.formatexpr = "v:lua.require'conform'.formatexpr()"

horriblename avatar May 13 '25 21:05 horriblename

@NotAShelf I'll give you the final decision for whether we change <leader>lf to use conform. If not we can maybe make the conform formatexpr the default

horriblename avatar May 13 '25 21:05 horriblename

I feel like <leader>lf, as it's under LSP, should only contain LSP formatting. If anything I'd be more in favor of

  • A dedicated formatter bind that performs both LSP and conform formatting
  • Performing both on format-on-save (less favorable, might conflict)
  • Keep the two separate, but document how to consolidate the two.

No strong opition on any, feel free to implement what you feel is most appropriate.

NotAShelf avatar May 13 '25 21:05 NotAShelf