Automatic closing bracket insertion
Some people like it that when they type ‘(’, the bracket is automatically closed as well (useful to keep brackets balanced). This should work for a configurable set of brackets of course, and should probably a preference. Not everybody likes this feature, or so I hear.Submitted by Zef Hemel on 26 February 2010 at 14:22
(see also Spoofax/29)
The basic functionality for this should be easy: IAutoEditStrategy allows us to capture text insertion events and change the document in response. We also have the Syntax.esv file which describes the bracket structures in the language.
The main technical challenge in this proposal is with string literals and comments:
a = "( <- no closing ) should be inserted here
Without a tokenizer we would have to parse the file to determine if the brackets are not part of a string literal or comment. Not hard, but I’m not sure if the parser/error recovery performance is always sufficient to do this. (Bit ironic since this feature makes it easier for the error recovery.)
Fixed r20653. Uses the “old” AST from the parser (which might lag behind a bit) to find out if we’re in a comment or string literal. Seems to work well.
If you want to make it better, it should also remove matching braces:
I type (| is cursor):
I do backspace end end up with:
I think the Java editor has something like this. I’m not sure if this should always happen, or only with empty brace pairs.
And by the way, in the stratego editor when I type "( it does insert the closing ), I don’t think it should, should it?
zefhemel: yeah I considered the backspace thing (it’s a TODO in the source code). But it’s a little more complicated (read: more work) when dealing with multi-char brackets.
If you type in:
Then 4 backspaces should remove all of it. In the end, maybe just hitting undo works just as well.
Also it shouldn’t do the "() thing, but it’s likely either an interaction with error recovery or you typing too fast for it to make an informed decision about it.
See also: Spoofax/41: Improve closing bracket insertion[/edit]
Log in to post comments