Using handler annotations + register commands / functions, etc...
vigoux opened this issue ยท 8 comments
Hi !
I'm currently trying to use different parts of neovim-java for a plugin I write, and i have few questions :
- Is there a way to register Commands / functions, using neovim-api or handler-annotations (like in pynvim )?
- How to use handler-annotations, as the usage example is not complete ?
Your work is really awesome btw, thanks for this great package !
Hi!
Thanks for the interest and you are welcome ๐
Unfortunately, this package is not a real plugin host, but that doesn't mean it is not possible to do what you are trying to. You will just have to define commands in vimscript and use rpcrequest()
and rpcnotify()
to communicate with java process over TCP or Unix socket.
You would then use handler-annotations
to register listeners for requests/notifications. The example provided in readme demonstrates usage for notifications:
class AHandlerClass {
@NeovimNotificationHandler("redraw")
public void handleRedraw(NotificationMessage message) {
System.out.println("Received a message: " + message);
}
}
redraw is the name of notification and all data is inside NotificationMessage
object. For requests you would use @NeovimRequestHandler
and method would have to take RequestMessage
. An object containing all of these methods has to be registered at NeovimHandlerManager
, which has to be attached to a working RPCStreamer
, as seen in example:
NeovimHandlerManager neovimHandlerManager = new NeovimHandlerManager();
neovimHandlerManager.registerNeovimHandler(new AHandlerClass());
neovimHandlerManager.attachToStream(neovimStream);
I haven't updated this in a while, so functions of API v4, v5 and v6 (as seen in issues) are missing, but it should not be hard to update library with these.
I hope this helps. I would be glad to help you with any further issues and I hope to make this a real plugin host eventually.
Oh okay, that's good to know !
Another thing is that i would like to use both neovim-api
and handler-notifications
but i didn't found any way to get the neovimStream
thing out of NeovimApi
, how can i do this ?
Sorry for asking so many stupid questions, and thank you for that quick answer !
I guess I need to make a better interface for such use cases. For now, you can look at how NeovimApis
is creating NeovimApi
instance and replicate such behavior on your side. Then you can use RPCClient
created for that to use with handler-notifications
.
public final class NeovimApis {
public static NeovimApi getApiForConnection(RPCConnection rpcConnection) {
var rpcClient = new RPCClient.Builder()
.withObjectMapper(NeovimJacksonModule.createNeovimObjectMapper()).build();
var reactiveRPCStreamer = ReactiveRPCClient.createDefaultInstanceWithCustomStreamer(rpcClient);
reactiveRPCStreamer.attach(rpcConnection);
return new NeovimStreamApi(reactiveRPCStreamer);
}
}
I'll try that !
Thank you for those real quick answers, i'll keep you in touch !
I'm back,
I now can receive notifications from neovim, and kinda register commands.
The problem I have is, when inside a NeovimNotificaionHandler
, it seems that API calls to neovim blocks the process, is this related to the fact that i'm using two things of the same stream ?
Thanks for your aswers.
If I got this right, it shouldn't be a big issue. Basically, listening to responses, requests and notifications, all happens in a single loop, running on an executor service. That is something I should probably fix, to dispatch handling to separate threads. For right now, you could fix your issue by configuring NeovimHandlerManager
to use a custom ExecutorService
(by default it just handles requests and notifications on the same thread they were received, which is the thread with that single loop).
Example:
NeovimHandlerManager customHandlerManager = new NeovimHandlerManager(
new NeovimHandlerProxy(Executors.newSingleThreadExecutor())
);
Thanks for all the feedback. I guess I have quite a few improvements to make on this ๐
And that fixed it again !
You are awesome