conjure

:neovim:+:clj: https://github.com/Olical/conjure - If you're enjoying Conjure and want to say thanks: https://github.com/users/Olical/sponsorship :smile: (alt https://conjure.fun/discord)
2020-09-17T00:16:27.073700Z

👀 1
Aleh Atsman 2020-09-17T08:34:36.075Z

Hey guys, does anyone has an idea how hard is it to make competition source for https://github.com/nvim-lua/completion-nvim from conjure?

Olical 2020-09-17T08:40:19.077100Z

There's essentially a sort of promise implementation in Conjure to make the async work a little easier, you can use a sync complete function that blocks or the true async promise based one (highly recommended!). I would imagine asyncomplete is similar to completion-nvim?

Aleh Atsman 2020-09-17T08:42:30.077900Z

I think yes they are similar, will figure it out and see if I can make minimal working solution

Aleh Atsman 2020-09-17T08:42:45.078200Z

really want to have it with nvim-completition

Olical 2020-09-17T08:45:56.079Z

Sweet! If you do get it working (feel free to ask me some questions if you're really stuck but I think those examples should be enough) let me know and I'll add it to the lists of completion tools.

Aleh Atsman 2020-09-17T12:19:34.080200Z

function M.get_completions(prefix)
  local p = eval["completions-promise"](prefix)
  while not promise["done?"](p)
  do
    print("i am waiting") -- **what is the equivalent of time.sleep?**
  end

  result = promise["close"](p)

  local complete_items = {}
  for _, item in ipairs(result) do
    if item ~= "^" .. prefix then
      table.insert(complete_items, {
          word = item,
          kind = 'conjure',
          icase = 1,
          dup = 0,
          empty = 0,
        })
    end
  end

  print(vim.inspect(complete_items))
  return complete_items
end

Aleh Atsman 2020-09-17T12:19:45.080500Z

what is the equivalent of time sleep?

Aleh Atsman 2020-09-17T12:20:11.081Z

for lua code, or what is the proper way of handling async code?

Olical 2020-09-17T12:41:45.081400Z

I think you'll have to do something like this but from lua https://github.com/thecontinium/asyncomplete-conjure.vim/blob/master/autoload/asyncomplete/sources/conjure.vim

Olical 2020-09-17T12:41:54.081900Z

so you set up a timer then periodically check if the completions are done

Olical 2020-09-17T12:42:04.082300Z

Any other method (like sleeping) will block the editor

Olical 2020-09-17T12:42:25.082900Z

The reason the deoplete and coc implementations can sleep is because they're run in a separate process so they don't block nvim's ui.

Olical 2020-09-17T12:43:25.083500Z

You could actually use the conjure.timer module or maybe just copy parts of it that you need

(module conjure.timer
  {require {a conjure.aniseed.core
            nvim conjure.aniseed.nvim}})

(defn defer [f ms]
  (let [t (vim.loop.new_timer)]
    (t:start ms 0 (vim.schedule_wrap f))
    t))

(defn destroy [t]
  (when t
    (t:stop)
    (t:close))
  nil)

Olical 2020-09-17T12:44:06.084400Z

This uses the vim.loop.* functions which don't seem to be documented but actually map to libuv functions, so I just googled for "lib uv api" then guessed my way through to using new_timer, start, stop and close.

Olical 2020-09-17T12:44:19.084700Z

libuv is great for anything like this

Olical 2020-09-17T12:44:45.085300Z

So a combination of using / taking and modifying Conjure's timer module + the lib uv docs should be enough to get a repeating timer running where you can check if the promise is done.

Olical 2020-09-17T12:54:34.086800Z

Oh you have to return the results :thinking_face: that's not great, it's not really async in that way then, it's a sync function that they somehow run in an async way? You could try calling eval.completions-sync or using promise.await like that function does. I just think this will block neovim's ui which isn't great https://github.com/Olical/conjure/blob/566915cc545edd68bab2ad76eb59dfa43648a8b3/fnl/conjure/eval.fnl#L200-L203

Olical 2020-09-17T12:55:12.087400Z

Or maybe there's a way to sleep the process from libuv that I don't really know about.

Aleh Atsman 2020-09-17T13:24:01.088300Z

well yes looks like probably I can use sync api for my code, it is used later using timer, could that be the case

Olical 2020-09-18T08:42:24.000800Z

:thinking_face: maybe? I don't really understand the completion-nvim plugin right now, but it's worth checking what sort of APIs they give you. If you can write a callback based async completer (so you call a function when you're done) it'll be easier to check the promise every 5ms or something then call the callback with the results when it's done.

Aleh Atsman 2020-09-17T13:24:01.088500Z

?