X-Git-Url: http://nitlanguage.org diff --git a/src/examples/nitlight_as_a_service.nit b/src/examples/nitlight_as_a_service.nit index fa95f6c..e117268 100644 --- a/src/examples/nitlight_as_a_service.nit +++ b/src/examples/nitlight_as_a_service.nit @@ -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 @@ -76,6 +76,32 @@ class HLCode # The pseudo source-file var source: SourceFile + + # JavaScript code to update an existing codemirror editor. + fun code_mirror_update: Template + do + + var res = new Template + 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 = " {{{m.text.html_escape}}}"; + var w = editor.addLineWidget({{{m.location.line_start-1}}}, l); + widgets.push(w); +""" + end + res.add """});}""" + return res + end end # Nitcorn service to hightlight code @@ -88,13 +114,33 @@ class HighlightAction do var hl = new HighlightVisitor var page = new Template + + # There is code? Process it var code = http_request.post_args.get_or_null("code") + 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 """ {{{hl.head_content}}} + """ # Add the form+textarea @@ -102,10 +148,7 @@ class HighlightAction

""" - if code != null then - # There is code? Process it - var hlcode = hightlightcode(hl, code) - + if hlcode != null then # Inject highlight page.add "
"
 			page.add hl.html.write_to_string
@@ -117,10 +160,46 @@ class HighlightAction
 				page.add "
  • {m.location.as(not null)}: {m.text}
  • " end page.add "" + else + page.add "
    " end page.add hl.foot_content + + # Call codemirror + page.add """ + + """