Thursday, 16 May 2013

Creating a REPL editor that is linked to a Code editor (with detailed step-by-step description of how I use O2 to 'evolve an C# UI via quick REPL')

Today I needed to script the creation of a C# script needed a C# REPL that was linked into a Code Editor (to make code development faster).

Since there wasn’t such script in the O2.Platform.Scripts library, I quickly created one.

This post shows how I created this script



image

which when executed will create this GUI:

image

where the left-hand side is the script editor controlling the right-hand side code viewer:

image

Note: this script has now been added to the O2.Platform.Scripts as the script: “Util - REPL Script a Code Editor.h2”

image

How does it work:

The C# script that creates this GUI is quite simple.

The first line creates an popup window with size 1200x500, sets the title of the form to “Util – REPL Script a Code Editor” and puts the return Panel object into the topPanel variable:

image

Which looks like this:

image

Next we add a source code editor and set its color coding scheme to C#:

image

which looks like this:

image

Next step is to call the insert_Left extension method of the Windows.Forms.Controls.Control type:

image

which adds a SplitContainer object to the parent of topPanel, add a new Panel object to the left pane and puts the topPanel object on the right pane :

image

Next we add a script_Me control to the left-hand-side panel, (which is an C# REPL editor with the right-hand-side code editor object passed as a reference, under the variable name “codeEditor”):

image

which looks like this:

image

And the final step is to set the code editor to execute the script every time the script compiles OK, and to set a first script (which will change the right-hand-side editor):

image

which looks like this:

image

For reference here is the code that creates this GUI:
   1: //var topPanel = panel.clear().add_Panel();
   2: var topPanel = "Util - REPL Script a Code Editor".popupWindow(1200,500);
   3: var codeEditor = topPanel.add_SourceCodeEditor().csharp_Colors();
   4:  
   5: var firstScript = "codeEditor.set_Code(\"Created by Scripted Code\");".line() +
   6:                   "return codeEditor;";
   7:  
   8: topPanel.insert_Left()
   9:         .add_Script_Me(codeEditor,"codeEditor")
  10:         .executeOnCompile()
  11:         .set_Code(firstScript);;
  12:         
  13: return "ok";