model: Make the `anchor` parameter of `MType::anchor_to` nullable
[nit.git] / src / examples / nitlight_as_a_service.nit
index 7007b70..e117268 100644 (file)
@@ -40,7 +40,7 @@ do
        var mb = new ModelBuilder(model, tc)
 
        # Parse the code
-       var source = new SourceFile.from_string("", content)
+       var source = new SourceFile.from_string("", content + "\n")
        var lexer = new Lexer(source)
        var parser = new Parser(lexer)
        var tree = parser.parse
@@ -85,12 +85,18 @@ class HLCode
                res.add """
        function nitmessage() {
                editor.operation(function(){
+                       for (var i = 0; i < widgets.length; ++i)
+                             editor.removeLineWidget(widgets[i]);
+                       widgets.length = 0;
+"""
+
                for m in source.messages do
                        res.add """
                        var l = document.createElement("div");
                        l.className = "lint-error"
                        l.innerHTML = "<span class='glyphicon glyphicon-warning-sign lint-error-icon'></span> {{{m.text.html_escape}}}";
                        var w = editor.addLineWidget({{{m.location.line_start-1}}}, l);
+                       widgets.push(w);
 """
                end
                res.add """});}"""
@@ -114,6 +120,19 @@ class HighlightAction
                var hlcode = null
                if code != null then hlcode = hightlightcode(hl, code)
 
+               if http_request.post_args.get_or_null("ajax") == "true" and hlcode != null then
+                       page.add hlcode.code_mirror_update
+                       page.add """
+                       document.getElementById("lightcode").innerHTML = "{{{hl.html.write_to_string.escape_to_c}}}";
+                       nitmessage();
+                       """
+
+                       var response = new HttpResponse(200)
+                       response.header["Content-Type"] = "application/javascript"
+                       response.body = page.write_to_string
+                       return response
+               end
+
                page.add """
                <!doctype html><html><head>{{{hl.head_content}}}
                <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.16.0/codemirror.css">
@@ -141,6 +160,8 @@ class HighlightAction
                                page.add "<li>{m.location.as(not null)}: {m.text}</li>"
                        end
                        page.add "</ul>"
+               else
+                       page.add "<pre id=light><code id=lightcode></code></pre>"
                end
 
                page.add hl.foot_content
@@ -161,8 +182,23 @@ class HighlightAction
                        page.add "function nitmessage()\{\}"
                end
                page.add """
+               var widgets = [];
                nitmessage();
 
+               function updatePage() {
+               $.post("", { ajax: true, code: editor.getValue()}, function(data) {
+                       eval(data);
+                       $(".popupable").popover({html:true, placement:'top'});
+               });
+               }
+
+               var waiting;
+               editor.on("change", function() {
+                       clearTimeout(waiting);
+                       waiting = setTimeout(updatePage, 500);
+               });
+               waiting = setTimeout(updatePage, 500);
+
                </script>
                </body></html>
                """