public Task OnLoadAsync(Kernel kernel)
        {
            Formatter.Register <DateTime>((date, writer) =>
            {
                writer.Write(date.DrawSvgClock());
            }, "text/html");

            Formatter.Register <DateTimeOffset>((date, writer) =>
            {
                writer.Write(date.DrawSvgClock());
            }, "text/html");


            var clockCommand = new Command("#!clock", "Displays a clock showing the current or specified time.")
            {
                new Option <int>(new[] { "-o", "--hour" },
                                 "The position of the hour hand"),
                new Option <int>(new[] { "-m", "--minute" },
                                 "The position of the minute hand"),
                new Option <int>(new[] { "-s", "--second" },
                                 "The position of the second hand")
            };

            clockCommand.Handler = CommandHandler.Create(
                (int hour, int minute, int second, KernelInvocationContext invocationContext) =>
            {
                invocationContext.Display(SvgClock.DrawSvgClock(hour, minute, second));
            });

            kernel.AddDirective(clockCommand);

            if (KernelInvocationContext.Current is { } context)
            {
                PocketView view = div(
                    code(nameof(ClockExtension)),
                    " is loaded. It adds visualizations for ",
                    code(typeof(System.DateTime)),
                    " and ",
                    code(typeof(System.DateTimeOffset)),
                    ". Try it by running: ",
                    code("DateTime.Now"),
                    " or ",
                    code("#!clock -h")
                    );

                context.Display(view);
            }

            return(Task.CompletedTask);
        }
        public async Task OnLoadAsync(IKernel kernel)
        {
            Formatter <DateTime> .Register((d, writer) =>
            {
                writer.Write(d.DrawSvgClock());
            }, "text/html");

            Formatter <DateTimeOffset> .Register((d, writer) =>
            {
                writer.Write(d.DrawSvgClock());
            }, "text/html");

            if (kernel is KernelBase kernelBase)
            {
                var clockCommand = new Command("#!clock", "Displays a clock showing the current or specified time.")
                {
                    new Option <int>(new[] { "-o", "--hour" },
                                     "The position of the hour hand"),
                    new Option <int>(new[] { "-m", "--minute" },
                                     "The position of the minute hand"),
                    new Option <int>(new[] { "-s", "--second" },
                                     "The position of the second hand")
                };

                clockCommand.Handler = CommandHandler.Create(
                    async(int hour, int minute, int second, KernelInvocationContext context) =>
                {
                    await context.DisplayAsync(SvgClock.DrawSvgClock(hour, minute, second));
                });

                kernelBase.AddDirective(clockCommand);
            }

            if (KernelInvocationContext.Current is {} context)
            {
                await context.DisplayAsync($"`{nameof(ClockExtension)}` is loaded. It adds visualizations for `System.DateTime` and `System.DateTimeOffset`. Try it by running: `display(DateTime.Now);` or `#!clock -h`", "text/markdown");
            }
        }
Example #3
0
        public async Task OnLoadAsync(Kernel kernel)
        {
            System.Diagnostics.Debugger.Launch();



            //var kernels = new List<FSharpKernel>();

            //var c = kernel as CompositeKernel;
            //if(c!=null)
            //{
            //    kernels.AddRange(c.ChildKernels.Where(k => k is FSharpKernel).Select(k => (FSharpKernel)k));
            //}

            //if (kernel is FSharpKernel)
            //{
            //    kernels.Add((FSharpKernel)kernel);
            //}
            //foreach(var fs in kernels)
            //{

            //    var allFlags = System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance;
            //    var scripty = (Lazy<FSharpScript>)fs.GetType().GetField("script", allFlags).GetValue(fs);
            //    var fsi = scripty.Value.Fsi;

            //    var checkerField = fsi.GetType().GetField("checker", allFlags);
            //    var old = (FSharpChecker)checkerField.GetValue(fsi);

            //    if (!checkers.TryGetValue(old, out var foo))
            //    {
            //        var resolver = (FSharp.Compiler.ReferenceResolver.Resolver)old.GetType().GetField("legacyReferenceResolver", allFlags).GetValue(old);
            //        var nc = FSharpChecker.Create(null, FSharpOption<bool>.Some(true), null, FSharpOption<FSharp.Compiler.ReferenceResolver.Resolver>.Some(resolver), null, null, null, null);
            //        checkerField.SetValue(fsi, nc);
            //        checkers.Add(nc, nc);
            //    }
            //}



            kernel.AddMiddleware((a, b, c) =>
            {
                var fs = b.HandlingKernel as FSharpKernel;
                if (fs != null)
                {
                    Func <string, System.Threading.Tasks.Task> cont = null;
                    string code = null;

                    switch (b.Command)
                    {
                    case SubmitCode v:
                        code = v.Code;
                        cont = n => c(new SubmitCode(n, v.TargetKernelName, v.SubmissionType), b);
                        break;

                    case RequestHoverText v:
                        code = v.Code;
                        cont = n => c(new RequestHoverText(n, v.LinePosition, v.TargetKernelName), b);
                        break;

                    case RequestCompletions v:
                        code = v.Code;
                        cont = n => c(new RequestCompletions(n, v.LinePosition, v.TargetKernelName), b);
                        break;

                    case RequestDiagnostics v:
                        code = v.Code;
                        cont = n => c(new RequestDiagnostics(n, v.TargetKernelName), b);
                        break;
                    }

                    if (code != null)
                    {
                        var allFlags = System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance;
                        var scripty  = (Lazy <FSharpScript>)fs.GetType().GetField("script", allFlags).GetValue(fs);
                        var trip     = FSharpAsync.RunSynchronously(scripty.Value.Fsi.ParseAndCheckInteraction(code), FSharpOption <int> .None, FSharpOption <CancellationToken> .None);

                        typeof(FSharpCheckFileResults).GetField("keepAssemblyContents", allFlags).SetValue(trip.Item2, true);

                        var newCode = Adaptify.Compiler.Adaptify.getReplacementCode(Adaptify.Compiler.Log.empty, false, trip.Item2, code);

                        var rx = new System.Text.RegularExpressions.Regex(@"FSI_[0-9][0-9][0-9][0-9]\.");

                        newCode = newCode.Replace("Stdin.", "");
                        newCode = rx.Replace(newCode, "");

                        return(cont(newCode));
                    }
                }
                return(c(a, b));
            });

            Formatter <DateTime> .Register((date, writer) =>
            {
                writer.Write(date.DrawSvgClock());
            }, "text/html");

            Formatter <DateTimeOffset> .Register((date, writer) =>
            {
                writer.Write(date.DrawSvgClock());
            }, "text/html");


            var clockCommand = new Command("#!clock", "Displays a clock showing the current or specified time.")
            {
                new Option <int>(new[] { "-o", "--hour" },
                                 "The position of the hour hand"),
                new Option <int>(new[] { "-m", "--minute" },
                                 "The position of the minute hand"),
                new Option <int>(new[] { "-s", "--second" },
                                 "The position of the second hand")
            };

            clockCommand.Handler = CommandHandler.Create(
                async(int hour, int minute, int second, KernelInvocationContext context) =>
            {
                await context.DisplayAsync(SvgClock.DrawSvgClock(hour, minute, second));
            });

            kernel.AddDirective(clockCommand);

            if (KernelInvocationContext.Current is { } context)
            {
                context.Display(kernel.GetType().FullName);
                await context.DisplayAsync($"asdasdasdasdasd `{nameof(ClockExtension)}` is loaded. It adds visualizations for `System.DateTime` and `System.DateTimeOffset`. Try it by running: `display(DateTime.Now);` or `#!clock -h`", "text/markdown");
            }
        }