public async Task TestAsyncAPICall() { var api = new NvimAPI(); var result = await api.Eval("2 + 2"); Assert.AreEqual(4L, result); }
public async Task TestPluginExports() { const string pluginPath = "/path/to/plugin.sln"; var api = new NvimAPI(); await PluginHost.RegisterPlugin <TestPlugin>(api, pluginPath); await api.Command( $"let g:result = {nameof(TestPlugin.AddNumbers)}(1, 2)"); var result = await api.GetVar("result"); Assert.AreEqual(3L, result); await api.Command($"{nameof(TestPlugin.TestCommand1)} a b c"); CollectionAssert.AreEqual(new[] { "a", "b", "c" }, TestPlugin.Command1Args); await api.Command($"{nameof(TestPlugin.TestCommand2)} 1 2 3"); Assert.AreEqual("1 2 3", TestPlugin.Command2Args); await api.Command("edit test.cs"); Assert.IsTrue(TestPlugin.AutocmdCalled); await api.Command($"call {nameof(TestPlugin.CountLines)}()"); Assert.IsTrue(TestPlugin.CountLinesReturn == 1); }
private static void Main() { Log.WriteLine("Plugin host started"); var nvim = new NvimAPI(Console.OpenStandardOutput(), Console.OpenStandardInput()); nvim.OnUnhandledRequest += (sender, request) => { // Load the plugin and get the handler asynchronously Task.Run(() => { var handler = GetPluginHandler(nvim, request.MethodName); if (handler == null) { var error = $"Could not find request handler for {request.MethodName}"; request.SendResponse(null, error); Log.WriteLine(error); return; } Log.WriteLine($"Loaded handler for \"{request.MethodName}\""); try { var result = handler(request.Arguments); request.SendResponse(result); } catch (Exception exception) { request.SendResponse(null, exception); } }); }; nvim.OnUnhandledNotification += (sender, notification) => { // Load the plugin and get the handler asynchronously Task.Run(() => { GetPluginHandler(nvim, notification.MethodName) ?.Invoke(notification.Arguments); }); }; nvim.RegisterHandler("poll", args => "ok"); nvim.RegisterHandler("specs", args => { var slnFilePath = (string)args.First(); var pluginType = GetPluginFromSolutionPath(slnFilePath); return(pluginType == null ? null : PluginHost.GetPluginSpecs(pluginType)); }); nvim.WaitForDisconnect(); Log.WriteLine("Plugin host stopping"); }
public static async Task RegisterPlugin(NvimAPI api, string pluginPath, Type pluginType) { var pluginAttribute = pluginType.GetCustomAttribute <NvimPluginAttribute>(); if (pluginAttribute == null) { throw new Exception( $"Type \"{pluginType}\" must have the NvimPlugin attribute"); } var pluginInstance = Activator.CreateInstance(pluginType, api); var methodsDictionary = new Dictionary <string, Dictionary <string, object> >(); var exports = GetPluginExports(pluginType, pluginPath, pluginInstance) .ToArray(); foreach (var export in exports) { export.Register(api); if (export is NvimPluginFunction function) { methodsDictionary[function.Name] = new Dictionary <string, object> { { "async", !function.Sync }, { "nargs", function.Method.GetParameters().Length } }; } } await RegisterPlugin(api, pluginPath, exports); var version = new Version(pluginAttribute.Version); await api.SetClientInfo(pluginAttribute.Name ?? pluginType.Name, new Dictionary <string, int> { { "major", version.Major }, { "minor", version.Minor }, { "patch", version.Build } }, "plugin", methodsDictionary, new Dictionary <string, string> { { "website", pluginAttribute.Website }, { "license", pluginAttribute.License }, { "logo", pluginAttribute.Logo } }); var channelID = (long)(await api.GetApiInfo())[0]; await api.CallFunction("remote#host#Register", new object[] { PluginHostName, "*", channelID }); }
private static async Task RegisterPlugin(NvimAPI api, string pluginPath, IEnumerable <NvimPluginExport> exports) { await api.CallFunction("remote#host#RegisterPlugin", new object[] { PluginHostName, pluginPath, exports.Select(export => export.GetSpec()).ToArray() }); }
public async Task TestLocalSocket() { var nvimStdio = new NvimAPI(); var serverAddress = (string)await nvimStdio.CallFunction("serverstart", new object[0]); var nvimLocalSocket = new NvimAPI(serverAddress); Assert.IsNotNull(await nvimLocalSocket.CommandOutput("version")); }
public async Task TestTCPSocket() { var nvimStdio = new NvimAPI(); var serverAddress = (string)await nvimStdio.CallFunction("serverstart", new object[] { System.Net.IPAddress.Loopback + ":" }); var nvimTCPSocket = new NvimAPI(serverAddress); Assert.IsNotNull(await nvimTCPSocket.CommandOutput("version")); }
public static NvimPluginExport[] RegisterPluginExports(NvimAPI api, string pluginPath, Type pluginType) { var pluginInstance = Activator.CreateInstance(pluginType, api); var exports = GetPluginExports(pluginType, pluginPath, pluginInstance) .ToArray(); foreach (var export in exports) { export.Register(api); } return(exports); }
public async Task TestNvimUIEvent() { const string testString = "hello_world"; var titleSetEvent = new ManualResetEvent(false); var api = new NvimAPI(); await api.UiAttach(100, 200, new Dictionary <string, string>()); api.SetTitle += (sender, args) => { if (args.Title == testString) { titleSetEvent.Set(); } }; await api.Command($"set titlestring={testString} | set title"); Assert.IsTrue(titleSetEvent.WaitOne(TimeSpan.FromSeconds(5))); }
private static Func <object[], object> GetPluginHandler(NvimAPI nvim, string methodName) { var methodNameSplit = methodName.Split(':'); // On Windows absolute paths contain a colon after // the drive letter, so the first two elements must // be joined together to obtain the file path. var slnFilePath = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? $"{methodNameSplit[0]}:{methodNameSplit[1]}" : methodNameSplit[0]; var pluginType = GetPluginFromSolutionPath(slnFilePath); var exports = PluginHost.RegisterPluginExports(nvim, slnFilePath, pluginType); return(exports.FirstOrDefault(export => export.HandlerName == methodName)?.Handler); }
public async Task TestCallAndReply() { var api = new NvimAPI(); api.RegisterHandler("client-call", args => { CollectionAssert.AreEqual(new[] { 1L, 2L, 3L }, args); return(new[] { 4, 5, 6 }); }); var objects = await api.GetApiInfo(); var channelID = (long)objects.First(); await api.Command( $"let g:result = rpcrequest({channelID}, 'client-call', 1, 2, 3)"); var result = (object[])await api.GetVar("result"); CollectionAssert.AreEqual(new[] { 4L, 5L, 6L }, result); }
RegisterPlugin <T>(NvimAPI api, string pluginPath) => await RegisterPlugin(api, pluginPath, typeof(T));
internal void Register(NvimAPI nvim) => nvim.RegisterHandler(HandlerName, Handler);
public TestPlugin(NvimAPI nvim) => _nvim = nvim;