Tree API (JEP 236): fine grained offsets (for eg. SwitchTree)

Emilian Bold emilian.bold at gmail.com
Wed Aug 3 09:08:15 UTC 2016


Hello,

Since JEP 236 is designed to help with editing I believe the Tree API
should provide more offsets info about the whole statement.

For example SwitchTree covers the switch statement:

switch ( expression ) {
     cases
}

but it is impossible to determine the offsets of the curly braces or of the
round brackets.

The closing curly brace is clearly located at SwitchTree.getEndPosition.

But the first curly brace has an unknown offset somewhere between
SwitchTree.getExpression(). getEndPosition() + 1 and the getStartPosition
of the 1st CaseTree.

Obviously exact offsets are necessary to provide matching brace
highlighting or code folding for the switch expression in an editor.

Ideally SwitchTree would have contained a (Switch)BlockTree which in turn
would have contained the list of CaseTree-s.

For the round brackets, SwitchTree.getExpression() could have returned
a ParenthesizedTree instead.

PS: I wonder, would it be possible to guarantee that there is a way to
re-generate the exact input string based on the (CompilationUnit)Tree?
Because this is what seems necessary for an editor or for
minimally-invasive source-code processing tools.

--emi


More information about the nashorn-dev mailing list