mhartington/formatter.nvim

Issue for "any type" formatting with a default LSP formatting configuration

Closed this issue · 1 comments

Shell command and output (optional):

Which configuration?
Type (custom or builtin): Custom
Filetype: HTML
Formatter: Prettier + LSP default

Configuration(s) (post all the variants you tried)

  formatter.setup({
    logging = true,
    filetype = {
      ["*"] = {
        function()
          vim.lsp.buf.format({ async = false, timeout_ms = 10000 })
          return nil
          -- The code under this comment simply does not work
          --return {
          --  exe = vim.lsp.buf.format,
          --  args = {{ async = false }},
          --  stdin = true,
          --}
        end
      },
      typescript = require('formatter.filetypes.typescript').prettier,
      javascript = require('formatter.filetypes.javascript').prettier,
      html = require('formatter.filetypes.html').prettier,
      css = require('formatter.filetypes.html').prettier,
    },
  })

Expected behavior

I would like to configure the formatter to run the default LSP formatter when there is no custom one applied to a filetype

Actual behaviour

On some files, notably HTML ones, an error occurs :

E5108: Error executing lua: /home/max/.config/nvim/after/plugin/mappings.lua:21: Vim(lua):E5108: Error executing lua ...ack/packer/start/formatter.nvim/lua/formatter/format.lua:62: Index out of bounds
stack traceback:
        [C]: in function 'get_lines'
        ...ack/packer/start/formatter.nvim/lua/formatter/format.lua:62: in function 'start_task'
        ...ack/packer/start/formatter.nvim/lua/formatter/format.lua:49: in function 'format'
        [string ":lua"]:1: in main chunk
        [C]: in function 'Format'
        /home/max/.config/nvim/after/plugin/mappings.lua:21: in function </home/max/.config/nvim/after/plugin/mappings.lua:10>
stack traceback:
        [C]: in function 'Format'
        /home/max/.config/nvim/after/plugin/mappings.lua:21: in function </home/max/.config/nvim/after/plugin/mappings.lua:10>

Additional context

I have tried to debug (keep in mind this is all new to me, I made my neovim and learnt Lua on Friday) and I think that this is because formatters are being applied one after the other, and what happens is that a previous formatter updates the file content, and the lsp.format call is not aware of those changes, and has an error on starting/ending lines.

The HTML file I am trying to format is this one (look for the comment) :

  <mat-sidenav-container class="h-full">
    <mat-sidenav mode="over">
      <div class="h-full flex flex-col items-stretch justify-start gap-4 p-2">
        <app-profile class="flex-lock" />

        <mat-action-list
          class="flex-full !flex flex-col items-stretch justify-start"
        >
          @if (connected()) {
            <button mat-list-item routerLink="/trips/add">Nouvelle balade</button>
            <button mat-list-item routerLink="/trips">Trouver une balade</button>
            <span class="flex-full"></span>
          } @else {
            <button mat-list-item routerLink="/">Accueil</button>
            <span class="flex-full"></span>
          }
        </mat-action-list>
      </div>
    </mat-sidenav>

    <mat-sidenav-content class="!flex flex-col items-stretch justify-start">
      <mat-toolbar class="flex-lock !relative">
        <div class="__container flex flex-row items-center justify-start gap-4">
          <button mat-icon-button (click)="sidenav.open()">
            <mat-icon fontIcon="menu"></mat-icon>
          </button>
          <span> Potes à motos </span>
          <span class="flex-1"></span>
        </div>

        @if (requesting()) {
          <!-- Indent this line more/less and try to format -->
          <mat-progress-bar
            mode="indeterminate"
            class="!absolute left-0 right-0 top-0"
          ></mat-progress-bar>
        }
      </mat-toolbar>

      <div class="__container flex-full p-4 overflow-hidden">
        <router-outlet></router-outlet>
      </div>
    </mat-sidenav-content>
  </mat-sidenav-container>
tjex commented

duplicate of #305.

Please close this issue and comment on the other issue that you're having the same issue.