ctrlpvim / ctrlp.vim

Active fork of kien/ctrlp.vim—Fuzzy file, buffer, mru, tag, etc finder.
ctrlpvim.github.com/ctrlp.vim
Other
5.57k stars 259 forks source link

add async api support #543

Closed prabirshrestha closed 4 years ago

prabirshrestha commented 4 years ago

This is a PR to solve the issue I mentioned at https://github.com/ctrlpvim/ctrlp.vim/issues/200. The idea comes from quickpick.vim

You can find a working example here. https://github.com/prabirshrestha/vim-lsp/pull/826

This requires changes to ctrlp. View demo at https://asciinema.org/a/333925.

ctrlp-vim-lsp

Most of the heavy lifting is already done via lsp servers so it is very fast and responsive.

call ctrlp#set([{ 'label': 'hello' }])
mattn commented 4 years ago

Sorry nit-picks. Totally LGTM.

prabirshrestha commented 4 years ago

@mattn no worries. I'm completely new to ctrlp code base. I did notice it but was more interested in getting proof of concept working.

For LSP servers we get the following response. Currently I'm getting the text from reading the file but I then loose this object. What is a better way to convert this to line also also preserve this entire object so when I go to the accept callback we can get this raw object.

{
  "location": {
    "uri": "file:///home/prabirshrestha/code/others/typescript-language-server/node_modules/typescript/lib/lib.es5.d.ts",
    "range": {
      "end": {
        "character": 16,
        "line": 111
      },
      "start": {
        "character": 4,
        "line": 111
      }
    }
  },
  "name": "get",
  "kind": 6
}
mattn commented 4 years ago

I worried that the "lines" can be modified from user's code.

prabirshrestha commented 4 years ago

Makes sense. But for other providers I see them doing the copy instead.

fu! ctrlp#bookmarkdir#init()
    cal s:setentries()
    cal s:syntax()
    retu s:process(copy(s:bookmarks[1]), ':.')
endf

When would one change "lines". I was hoping g:ctrlp_lines is actually internal since it wasn't documented.

mattn commented 4 years ago

Ah. Right. I don't want to make g:ctrlp_lines be private variable since someone may already use it. So ctrlp#set and ctrlp#get is not required?

prabirshrestha commented 4 years ago

I'm ok removing ctrlp#set and ctrlp#get if we say g:ctrlp_lines is the way to go and is ok to be public. I had thought of this too but wasn't sure. For now I will remove get and set methods. this would avoid introducing new apis.

Any idea on how to attach a metadata to a line? Something like user_data for for completion items. One option would be to use syntax but not the best way.

1| method helloWorld() hello.ts
2| method serve.start() server.ts
prabirshrestha commented 4 years ago

Also what is this function used for? not sure how to use it though. it does seem to modify g:ctrlp_lines

fu! ctrlp#setlines(...)
    cal call('s:setlines_pre', a:000)
    cal s:setlines_post()
endf
prabirshrestha commented 4 years ago

Apparently seems like we can do this currently without touching g:ctrlp_lines based on the code.

let s:items = []
function! ctrlp#lsp#workspace_symbol#init() abort
    return s:items
endfunction

" for onchange. `setlines()` seems to call init.

function! ctrlp#lsp#workspace_symbol#change() abort
    let s:items = ['a', 'b', 'c']
    call ctrlp#setlines()
    call ctrlp#update()
endfunction

This might actually be better though it does get a bit ugly with script level variables.

Updated PR to remove ctrlp#get() and ctrlp#set() and also used ctrlp#setlines() in vim-lsp.

mattn commented 4 years ago

Looks better.

prabirshrestha commented 4 years ago

Actually non of the functions seems to be documented. So this is good to be merged now. Docs can come later.

I also renamed change to be search instead since I would like to bring quickpick's another feature that is listen for selection changes. This allows to implement another features such as live colorscheme changes or popup to provide more info.

Colorscheme Picker

mattn commented 4 years ago

Thank you!

prabirshrestha commented 4 years ago

Thanks for the quick reviews and merge. Exiting to see what others will now build with these apis.