Skip to content

More ways to enter code and operators

Audience: everyone who edits · about 5 minutes · Prerequisites: Operators and side-transformations

The Space menu

Press Space at the end of a finished value (a name, a number, a closing bracket) to open a menu of what can come next: the operators of the language, . member access, a call, an index, a conditional, and the word operators that have no key (and, or, in, comparisons). Pick one with the arrow keys and Enter, or type to filter (an shows and). The menu wraps the value exactly as the key would, so a then Space then + gives a + _. The structural entries say what they build at that cell: = reads "Assign to a variable" in a statement and "Test equality" in an expression.

After a number in a language that has units of measure (KernelF), the menu leads with the units: 5, then Space, then kg wraps the number as quantity<kg>(5). Type to filter (km), and the units declared in the file come after the built-in ones. To change the unit of an existing quantity, put the caret in its unit and press Alt+↓: the same list opens and picking one replaces the unit (typing the unit as text works too).

  • Typing with spaces still works. While the menu is open and you have typed nothing, an operator key applies directly and closes the menu, and a second Space closes it, so a + b typed with spaces gives the same result as a+b.
  • The + chips beside the focused cell open the same menu: the right chip for what can follow, the left chip for putting the value on the right-hand side of an operator. They appear once the caret has rested on the cell for about a second, or at once while you hold Alt.
  • Not in text. In a comment or any free text, and in the middle of a cell, Space stays a space.
  • Optional parts are listed too. When a part of the construct is not there yet and shows as a small + name chip right after the cell (a comprehension's if, a function's ->, a declaration's =), the menu lists it under the word the construct itself draws for it, as "Add ...", and picking it adds the part and puts the caret in it.
  • Typing the introducer works the same way. At the end of a cell whose construct offers such a chip, the symbol it draws adds the part: = after a declaration's type adds its value, , after an assert condition adds the message (instead of making a tuple). Only a key that is not an operator there is taken over, and = as before.
  • Turn it off with the preference Space opens the what-can-come-next menu (spaceOpensContinuation).

Splitting and joining

Typing an operator key in the middle of a number or a name splits it: 1|0 then + gives 1 + 0, and sc|ore then + gives sc + ore (both halves must be valid: a name cannot start with a digit, and the name of a declaration does not split). Backspace at the start of the right operand takes the operator out again: two numbers or two names join back (1 + |0 gives 1|0), anything else keeps the left operand and drops the right one (undo restores it).

Parts that are not drawn at all

A few constructs draw nothing while an optional part is missing (a SQL select without where, a Surface template without reads), so there is no chip to reach it. Press Alt+Enter on the construct (or on anything inside it): the menu has an Add group with "Add where", "Add reads", and so on, and picking one adds the part and shows it.

Selecting first

Select a node (Alt+W, or Ctrl+Up, which grows the selection one level; Alt+Shift+W or Ctrl+Down shrinks it) and then type an operator key: the selected node becomes the left operand, so with gross * discount selected, + gives gross * discount + _. Holding Shift with the Right or Left arrow at the edge of a cell's text does the same growing step. Type a letter, digit, quote or bracket with a node selected and it replaces the node (Alt+Backspace restores it). F8 and the Problems panel land on a problem with its node selected, so you can retype it at once. In the Alt+Enter menu, an operator offers Replace with its operand (a - (b + c) on the +: b or c), and an operand offers Replace parent ... with this.

A stray Enter at the end of a body does not leave a blank line behind: if you move the caret away from the new, still empty last line without typing anything, the line is removed again (undo goes back to before the Enter). A blank line between two statements, or one you typed into, stays.

Editing a statement as text

If you would rather type a whole statement than build it cell by cell, press Alt+Shift+E with the caret in it. A text box with the statement's source opens over the line. Edit it like any text; Enter (or leaving the box) reads it back and replaces the statement in one undo step, Shift+Enter adds a line, and Escape drops your text. If the text cannot be read, the box stays open and says so, so nothing you typed is lost. The model is still the structure: the text is read once, when you leave the box.

Templates

A project can keep its own templates in a file templates.json in the project folder, so a team shares them through git:

{ "templates": [ { "trigger": "guard", "name": "Guard clause", "text": "if not ${condition}:\n    print(${message})" } ] }

text is source in the model's own language, with ${name} for each hole. In an empty statement or expression cell, type the trigger (or part of the name) and press Ctrl+Space (with ghost text off the menu opens by itself): the menu lists the template under templates. Choosing it inserts the construct as if you had pasted it, in one undo step, with every hole an empty cell and the cursor in the first; Tab moves from hole to hole, and from the last one on to the next cell after the construct. A template that does not read in the model's language is not offered usefully: it is pasted as commented-out text, like any text that cannot be read. The file is read when the editor opens and again every few seconds, so an edit shows in the next menu; a menu that is already open is drawn again when the list arrives. There is no template editor yet: write the file by hand.

Naming an argument

In a call, type the parameter's name and then = to pass that argument by name: in total( type rate and = and the argument becomes rate=… with the cursor in the value. It works where the language has named arguments (Core and its Python and JavaScript surfaces do), only when the cell holds a bare name, and only in an argument slot; after a bigger expression, or anywhere else, = keeps its usual meaning (a comparison inside an expression, an assignment at the start of a statement).

Pulling an expression into a variable

Press Alt+V with an expression selected, or with the caret anywhere inside one, to give that expression a name. The expression moves into a new variable on the line above, the place it came from now refers to the variable, and the new name is open for typing: type the name you want, Enter confirms and Escape keeps the suggested one. The name is checked against the names already used in the model, so the suggestion never clashes. Everything is one undo step. It is available in languages whose variable declaration says how to extract one (today Core, the Python-like language), and not on a whole statement or on a declaration's own name.

Choosing what to do with a selection

Ctrl+Space with a node (or a run of statements) selected lists what you can do with that selection instead of the completion menu of a cell: replace it with one of its parts, and Surround with … entries that put the selection inside a new construct, such as if or while, and leave it selected inside. Typing a letter over a selection still replaces it, as before; the list is for when you want to keep what is selected.