예제 #1
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
            });
        }
예제 #2
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);
        }