When moving a block of code, you can use toString on each node and "paste" the result by using the appropriate method. There needs to be a way to preserve blank lines between nodes, so we can preserve the author's style.
The first idea that comes to mind is a method that returns a string containing only newlines (and maybe indentation?) corresponding to the number of blank lines before or after the node (so two variants). This string can then be "pasted" just like the toString result.
As an example:
foo()
// comment 1
bar()
// comment 2
foo()
Assume the bar() expression is represented by node:
node.spaceBefore() // => "\n\n// comment 1\n"
node.toString() // => "bar()"
node.spaceAfter() // => "\n\n\n// comment 2\n"
In the above example, the \n that begins bar()'s line is included in the spaceBefore result. And the \n that begins the line after that is included in the spaceAfter result. I'm not certain, but I think that's the expected behavior.
I'm also undecided if indentation should be included or not. I'm leaning towards no, because toString currently strips indentation and we should match that behavior.
Another thing: maybe toString(true) can include the result of spaceAfter (and even the result of spaceBefore if we're the first sibling of a BlockStatement) to make things easier?
These decisions could easily be a source of many bugs if we aren't careful.
When moving a block of code, you can use
toStringon each node and "paste" the result by using the appropriate method. There needs to be a way to preserve blank lines between nodes, so we can preserve the author's style.The first idea that comes to mind is a method that returns a string containing only newlines (and maybe indentation?) corresponding to the number of blank lines before or after the node (so two variants). This string can then be "pasted" just like the
toStringresult.As an example:
Assume the
bar()expression is represented bynode:In the above example, the
\nthat beginsbar()'s line is included in thespaceBeforeresult. And the\nthat begins the line after that is included in thespaceAfterresult. I'm not certain, but I think that's the expected behavior.I'm also undecided if indentation should be included or not. I'm leaning towards no, because
toStringcurrently strips indentation and we should match that behavior.Another thing: maybe
toString(true)can include the result ofspaceAfter(and even the result ofspaceBeforeif we're the first sibling of aBlockStatement) to make things easier?These decisions could easily be a source of many bugs if we aren't careful.