Filter lsp-document-symbol with prompt menu

There is lsp-document-symbol command available as part of kakoune-lsp plugin. It lists a structure of code in *goto* buffer. Sometimes I need to find specific type but the list can be quite long. So I came up with a quick prompt menu which lists unique entries available in the list.

declare-option -hidden str-list lsp_document_symbols

define-command -hidden lsp-document-symbols-prompt %{
  evaluate-commands -draft %{
    execute-keys <percent>s^<percent>:<ret>glh<a-i>b
    evaluate-commands -itersel %{
      set-option -add buffer lsp_document_symbols %val{selection}
    }
  }
  prompt -menu -shell-script-candidates %{
    echo $kak_opt_lsp_document_symbols | tr ' ' \\n | runiq
  } -on-abort %{
    set-option buffer lsp_document_symbols
  } keep-entry: %{
    execute-keys "<percent><a-s><a-K>%val{text}<ret><a-d>gg"
    set-option buffer lsp_document_symbols
  }
}

hook global BufSetOption filetype=lsp-document-symbol %{
  map buffer normal <F4> ':lsp-document-symbols-prompt<ret>'
}

Note: To make entries unique I opt in runiq. You can try plain old uniq but there was some dupes when I tried it.

Prompt with unique menu entries on F4 :

Edit: make list unique by invoking set-option -remove first, thus no need for external r?uniq.

define-command -hidden lsp-document-symbols-prompt %{
  evaluate-commands -draft %{
    execute-keys <percent>s^<percent>:<ret>glh<a-i>b
    evaluate-commands -itersel %{
      set-option -remove buffer lsp_document_symbols %val{selection}
      set-option -add buffer lsp_document_symbols %val{selection}
    }
  }
  prompt -menu -shell-script-candidates %{
    printf %s\\n $kak_opt_lsp_document_symbols
  } keep-entry: %{
    execute-keys "<percent><a-s><a-K>%val{text}<ret><a-d>gg"
    set-option buffer lsp_document_symbols
  }
}

Nice. Note there is also lsp-goto-document-symbol which shows a menu though I barely ever use that. But I think scratch buffers are better than menus due to their flexibility.