Code formatting - preserving newlines and comments

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Code formatting - preserving newlines and comments

Peter Uhnak
Is it possible to configure formatter to preserve empty lines and line comments?

e.g.
--------------
myMethod
someCode.
< empty line that I would like to preserve >
moreCode.
---------------

the formatter also automatically puts comments on the end of the previous statement.
e.g.
------
someCode.
"a comment"
moreCode.
----
ends up as
----
someCode. "a comment"
moreCode.
-----

Both those behaviors seems odd to me.

I've seen empty newlines and line comments in Pharo code... so either I missed some setting or people just don't use the formatter?

Thanks,
Peter
Reply | Threaded
Open this post in threaded view
|

Re: Code formatting - preserving newlines and comments

stepharo
Hi peter

Is it possible to configure formatter to preserve empty lines and line comments?

I do not know :)


e.g.
--------------
myMethod
someCode.
< empty line that I would like to preserve >
moreCode.
---------------
But I think that should be customizable.


the formatter also automatically puts comments on the end of the previous statement.
e.g.
------
someCode.
"a comment"
moreCode.

If I recall, the problem is that comments are not a node but an attribute of a node.
We should try to fix that. It is not clear where you always want to put them.

----
ends up as
----
someCode. "a comment"
moreCode.
-----

Both those behaviors seems odd to me.

I've seen empty newlines and line comments in Pharo code... so either I missed some setting or people just don't use the formatter?
I use the formatter and I would like to use it much more systemaitcally.

Thanks,
Peter

Reply | Threaded
Open this post in threaded view
|

Re: Code formatting - preserving newlines and comments

Peter Uhnak
Ah!

RBConfigurableFormatter class>>settingsOn: is missing one setting
----
(aBuilder setting: #retainBlankLinesBetweenStatements)
label: 'Retain blank lines between statements'.
----


As for the comments, I looked at RBConfigurableFormatter>>formatStatementCommentsFor:
one idea might be to look at the space between end of the statement and beginning of the comment and if there is newline present then preserve it.

something like
--------------------------
formatStatementCommentsFor: aStatementNode
originalSource isNil
ifTrue: [ ^ self ].
FormatCommentWithStatements
ifFalse: [ ^ self ].
aStatementNode statementComments
do: [ :each | 
(self newLinesBeforeStartingAt: each first) > 0
ifTrue: [ self newLine ]
ifFalse: [ codeStream tab ].

codeStream nextPutAll: (originalSource copyFrom: each first to: each last) ]
---------------------------

There should be also an accompanying setting #retainBlankLineBeforeComments.

Peter


On Wed, Aug 20, 2014 at 7:45 PM, stepharo <[hidden email]> wrote:
Hi peter


Is it possible to configure formatter to preserve empty lines and line comments?

I do not know :)



e.g.
--------------
myMethod
someCode.
< empty line that I would like to preserve >
moreCode.
---------------
But I think that should be customizable.



the formatter also automatically puts comments on the end of the previous statement.
e.g.
------
someCode.
"a comment"
moreCode.

If I recall, the problem is that comments are not a node but an attribute of a node.
We should try to fix that. It is not clear where you always want to put them.


----
ends up as
----
someCode. "a comment"
moreCode.
-----

Both those behaviors seems odd to me.

I've seen empty newlines and line comments in Pharo code... so either I missed some setting or people just don't use the formatter?
I use the formatter and I would like to use it much more systemaitcally.

Thanks,
Peter


Reply | Threaded
Open this post in threaded view
|

Re: Code formatting - preserving newlines and comments

Peter Uhnak
actually it should be:
-------------
formatStatementCommentsFor: aStatementNode
originalSource isNil
ifTrue: [ ^ self ].
FormatCommentWithStatements
ifFalse: [ ^ self ].
aStatementNode statementComments
do: [ :each | 
| count | 
count := self newLinesBeforeStartingAt: each first.
(count) > 0
ifTrue: [ self newLines: count ]
ifFalse: [ codeStream tab ].

codeStream nextPutAll: (originalSource copyFrom: each first to: each last) ]
--------------
to allow having comments on top of sections of code.


On Wed, Aug 20, 2014 at 10:20 PM, Peter Uhnák <[hidden email]> wrote:
Ah!

RBConfigurableFormatter class>>settingsOn: is missing one setting
----
(aBuilder setting: #retainBlankLinesBetweenStatements)
label: 'Retain blank lines between statements'.
----


As for the comments, I looked at RBConfigurableFormatter>>formatStatementCommentsFor:
one idea might be to look at the space between end of the statement and beginning of the comment and if there is newline present then preserve it.

something like
--------------------------
formatStatementCommentsFor: aStatementNode
originalSource isNil
ifTrue: [ ^ self ].
FormatCommentWithStatements
ifFalse: [ ^ self ].
aStatementNode statementComments
do: [ :each | 
(self newLinesBeforeStartingAt: each first) > 0
ifTrue: [ self newLine ]
ifFalse: [ codeStream tab ].

codeStream nextPutAll: (originalSource copyFrom: each first to: each last) ]
---------------------------

There should be also an accompanying setting #retainBlankLineBeforeComments.

Peter


On Wed, Aug 20, 2014 at 7:45 PM, stepharo <[hidden email]> wrote:
Hi peter


Is it possible to configure formatter to preserve empty lines and line comments?

I do not know :)



e.g.
--------------
myMethod
someCode.
< empty line that I would like to preserve >
moreCode.
---------------
But I think that should be customizable.



the formatter also automatically puts comments on the end of the previous statement.
e.g.
------
someCode.
"a comment"
moreCode.

If I recall, the problem is that comments are not a node but an attribute of a node.
We should try to fix that. It is not clear where you always want to put them.


----
ends up as
----
someCode. "a comment"
moreCode.
-----

Both those behaviors seems odd to me.

I've seen empty newlines and line comments in Pharo code... so either I missed some setting or people just don't use the formatter?
I use the formatter and I would like to use it much more systemaitcally.

Thanks,
Peter



Reply | Threaded
Open this post in threaded view
|

Re: Code formatting - preserving newlines and comments

Davide Varvello
In reply to this post by Peter Uhnak
Thanks Peter, really useful for readability
Davide
Reply | Threaded
Open this post in threaded view
|

Re: Code formatting - preserving newlines and comments

Pharo Smalltalk Users mailing list
In reply to this post by Peter Uhnak
Thanks Peter, really useful for readability
Davide



--
View this message in context: http://forum.world.st/Code-formatting-preserving-newlines-and-comments-tp4773967p4815079.html
Sent from the Pharo Smalltalk Users mailing list archive at Nabble.com.