Skip to content

Use Neoformat for formatexpr #442

Open

Description

@alecandido

I would like to do more or less the opposite of #36, i.e. use Neoformat as formatexpr, such that when I select some text and press gq it gets formatted as if I issued :Neoformat, but without a custom mapping.

I'd need a function to invoke, possibly passing parameters available to formatexpr:

          The v:lnum  variable holds the first line to be formatted.
          The v:count variable holds the number of lines to be formatted.
          The v:char  variable holds the character that is going to be
                        inserted if the expression is being evaluated due to
                        automatic formatting.  This can be empty.  Don't insert
                        it yet!

I believe it should be possible just setting formatexpr in my configurations, but I'm not familiar with Neoformat internals, that's why I'm asking for help.

Activity

  1. sbdchd commented on Jan 11, 2023

    @sbdchd
    Owner

    Hmm I think it's tricky because a lot of the underlying formatters don't support that sort of thing

  2. alecandido commented on Jan 11, 2023

    @alecandido
    Author

    But :Neoformat is already able to format a visual selection. What is available to the command that would not be available while invoking through formatexpr.

    For the time being, I just mapped gQ on :Neoformat (with the effect of shadowing Ex mode on Neovim), but now I have two formatters, and in most situations I'd only need a single one.

  3. iamFIREcracker commented on Jan 11, 2023

    @iamFIREcracker
    Contributor

    @alecandido: you can give the following a try (I have been using it for months without much problem):

    function! NeoformatExpr() abort " {{{
        " if v:char != ''
        "     return
        " endif
        let line1 = v:lnum
        let line2 = v:lnum + v:count - 1
        execute ':' . line1 . ',' . line2 . 'Neoformat'
    endfunction "}}}

    PS. to enable it, just run setl formatexpr=NeoformatExpr() inside the filetype specific autogroup.

  4. alecandido commented on Jan 11, 2023

    @alecandido
    Author

    Thanks, that is exactly what I had in mind (the concept, I was completely missing the implementation)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions