Skip to content

What is a structural editor

Nikolaus Knop edited this page Jan 21, 2019 · 4 revisions

What is a structural editor?

When I was thinking about this question, I noticed that it is more difficult to answer than I had thought. I tried four different definitions of which I like the last one best.

  1. A structural editor is an editor which is cognizant of the documents structure.

This is in fact the wikipedia definition and on the first glance it looks pretty logical, but when you think about it, it becomes clear that this definition also accepts common IDEs as structure editors. Without knowing the structure of the file being edited they couldn't do syntax highlighting or highlight errors.

  1. A structural editor is an editor which you can interact with in a non-textual way.

This also sounds pretty good, but in fact also accepts IDEs as structural editors. You can for example let IntelliJ insert implementations of equals or hashCode and you haven't typed a single character. So this definition can't count either.

  1. A structural editor is an editor which you can interact with in a visual way.

This is pretty imprecise because text is entirely as visual as dragging images or viewing a dependency graph. Also a structural editor could be directed by speech and wouldn't be accepted by this definition.

  1. A structural editor is a computer program that allows editing an artifact (whatever this is) in a way abstracted from text.

It is crucial to understand the difference between this definition and the second one. Mentioning the example from the second definition, when applying some intentation to some part of a program common IDEs still finally modify text and not the Abstract Syntax Tree.
This abstraction from text has one pretty fundamental advantage:
The view on a document is entirely independent from the internal representation.
This for example allows the user to reorder some definitions without actually changing the program!

Another advantage of structural editors is that they allow Live Programming. At them moment the user hits "run" the editor doesn't have to first compile all changed files but can instead instantly start to run the program. The unification of compiler and IDE actually enables compiling or optimizing a program while the user edits another part of the program.

Last but not least the implementation of structural editors is far easier than that of a full-blown IDE. Hextant tries to make a solid base for rapidly developing support for new languages.

The big disadvantage (and solutions)

There were many earlier approaches to developing a structural editors, but the one thing they had in common was the lack of adoption. This is partly because of social factors - developers are experienced in writing programs in a textual way - but also because of the fact that structural editors for programming languages make it very difficult to perform some changes that are easy to perform in a text based way.
For example in a language with an if-construct supporting an optional else clause: The user has typed two if statements, one of them with an additional else-clause, the other one without:

if (some condition here) {
    do some thing
} else {
    do some other thing
}

if (some other condition here) {
    do yet another thing
}

Now the user wants to add the else clause of the first statement to the second if statement.

if (some condition here) {
    do some thing
} else {
    do some other thing
}

if (some other condition here) {
    do yet another thing
} else {
    do some other thing
}

In a text based editing environment this is as easy as copying the else statement to the other if statement, but in a strict structural editor this wasn't possible, as the else clause isn't a complete statement and such may not be selected for copying it individually. One could find a lot of other cases where structural editors are just cumbersome to use. But as I promised there are (partial) solutions for this problem:

  • Structural editors could allow switching between editing text and editing the AST.
    This would complicate the whole thing a lot since you needed a parser converting the text back to an AST and an "unparser" doing the whole thing backwards. There also is the unanswered question how much editing support there should be in the textual editor.
  • There could be commands that you could apply in different contexts for such common operations. This currently is my favourite solutions and my aim is to simplify creating such commands in Hextant as much as possible.