Hook works only one time when using kak-lsp

hello everyone
i have hook to enable formatting on write:

hook global WinSetOption filetype=go %{ # if any window has filetype go set
    lsp-enable # turn on kak-lsp, without that line hook works properly
    nop %sh{echo filetypego >> log} # logging to debug hook
    set window formatcmd goimports # set formatcmd to goimports for that window
    hook window BufWritePre .* %{ # and use it before write
        nop %sh{echo bufwritepre >> log}
        format
    }
}

when it has lsp-enable it works only one time, how that happen?
on “only one time” i mean i get logging and formatcmd set only when opening first go file.
am i not undestand something about hooks or it is kak-lsp quirk? (kak-lsp works nice btw)

If you look at the source of the lsp-enable command:

define-command -hidden lsp-enable -docstring "Default integration with kak-lsp" %{
    try %{
        add-highlighter global/cquery_semhl ranges cquery_semhl
    } catch %{
        fail 'lsp-enable: already enabled'
    }
    ...

…you can see that if you try to call lsp-enable twice in the same session, it deliberately raises an error on the second and subsequent times. In your hook, the first time you open a Go file, it enables LSP integration for the whole session and runs your other commands; the second time, the lsp-enable command throws an error and your other commands never get run.

From the kak-lsp README, you probably want to use the lsp-enable-window command instead, which just enables LSP integration for the current buffer and window, so it’s OK (and expected) to call that command for each new file you open.

1 Like

Thank you
my fail - i just had to check debug buffer