Esempio n. 1
0
        public async Task TestAsyncAPICall()
        {
            var api    = new NvimAPI();
            var result = await api.Eval("2 + 2");

            Assert.AreEqual(4L, result);
        }
Esempio n. 2
0
        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);
        }
Esempio n. 3
0
        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");
        }
Esempio n. 4
0
        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
            });
        }
Esempio n. 5
0
 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()
     });
 }
Esempio n. 6
0
        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"));
        }
Esempio n. 7
0
        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"));
        }
Esempio n. 8
0
        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);
        }
Esempio n. 9
0
        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)));
        }
Esempio n. 10
0
        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);
        }
Esempio n. 11
0
        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);
        }
Esempio n. 12
0
 RegisterPlugin <T>(NvimAPI api, string pluginPath) =>
 await RegisterPlugin(api, pluginPath, typeof(T));
Esempio n. 13
0
 internal void Register(NvimAPI nvim) =>
 nvim.RegisterHandler(HandlerName, Handler);
Esempio n. 14
0
 public TestPlugin(NvimAPI nvim) => _nvim = nvim;