Skip to content

feat: format autocmd events#108

Merged
xiaoshihou514 merged 11 commits intonvimdev:mainfrom
xiaoshihou514:main
Oct 13, 2023
Merged

feat: format autocmd events#108
xiaoshihou514 merged 11 commits intonvimdev:mainfrom
xiaoshihou514:main

Conversation

@xiaoshihou514
Copy link
Copy Markdown
Member

@xiaoshihou514 xiaoshihou514 commented Sep 21, 2023

#62

@xiaoshihou514
Copy link
Copy Markdown
Member Author

Screencast.from.2023-09-23.19-19-09.webm

Made a little statusline component levereging this feature, sth like this:

        if data then
            if data.status == "pending" then
                is_formatting = true
            elseif data.status == "done" then
                is_formatting = false
            end
        end
        symbol = is_formatting and "" or ""

@xiaoshihou514
Copy link
Copy Markdown
Member Author

Currently there's only GuardFmt events, pre-format (before spawning ) sends data with status="pending" and using=<formatter config table>, post-format (after update_buffer) sends data with status="done" and results=new_lines. So:

  • Is sending new_lines a bit too expensive?
  • Is it necessary to add corresponding GuardLint events? Haven't thought of a good use case as of yet

@xiaoshihou514
Copy link
Copy Markdown
Member Author

xiaoshihou514 commented Sep 23, 2023

I also made do_lint update on GuardFmt (status=done), but doesn't seem to make much of a difference when I tested it on a really large python file. (TextChanged already triggers do_lint I think)

@xiaoshihou514 xiaoshihou514 changed the title feat: format and lint autocmds feat: format autocmd events Sep 23, 2023
@barrettruth
Copy link
Copy Markdown
Contributor

Would be nice if you add an example config with the status bar in the docs to demonstrate these features.

@xiaoshihou514
Copy link
Copy Markdown
Member Author

xiaoshihou514 commented Sep 24, 2023

Would be nice if you add an example config with the status bar in the docs to demonstrate these features.

@barrett-ruth

Screencast.from.2023-09-24.20-37-55.webm
-- Guard setup
vim.opt.rtp:append("/tmp/guard.nvim")
local filetype = require("guard.filetype")
filetype("lua"):fmt({
    cmd = "stylua",
    args = { "-" },
    stdin = true,
})
require("guard").setup()

local is_formatting = false
_G.guard_status = function()
    -- Only display icon if auto-format is enabled
    local au = vim.api.nvim_get_autocmds({
        group = "Guard",
        buffer = 0,
    })
    if filetype[vim.bo.ft] and #au ~= 0 then
        -- show formatting status
        return is_formatting and "" or ""
    end
    return ""
end

-- super simple statusline that uses the above function
vim.cmd("au BufEnter * lua vim.opt.stl = [[%f %h%m ]] .. guard_status()")
-- update statusline on GuardFmt event
vim.api.nvim_create_autocmd("User", {
    pattern = "GuardFmt",
    callback = function(opt)
        is_formatting = opt.data.status == "pending"
        vim.opt.stl = [[%f %h%m ]] .. guard_status()
    end,
})

I will put that in ADVANCED.md as an example

Comment thread lua/guard/format.lua Outdated
Comment thread lua/guard/init.lua
@xiaoshihou514
Copy link
Copy Markdown
Member Author

xiaoshihou514 commented Oct 7, 2023

#99 (comment)

Some potential for a GuardLint event
On a second thought, it could just be processed during output parsing

Comment thread lua/guard/format.lua
Copy link
Copy Markdown
Contributor

@barrettruth barrettruth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've been swamped with exams recently and just pulled this down to take a look and ensure it works with my config and neovim head. Glepnir as always did a good job here.

I'll be sure to get out weekly commits now. In the meantime, you've been doing good work @xiaoshihou514 . Thanks!

Copy link
Copy Markdown
Contributor

@barrettruth barrettruth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@xiaoshihou514 xiaoshihou514 marked this pull request as ready for review October 13, 2023 07:08
@xiaoshihou514 xiaoshihou514 merged commit c46f043 into nvimdev:main Oct 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants