[scala-tools] Scala tool for formatting the source code

classic Classic list List threaded Threaded
25 messages Options
12
Reply | Threaded
Open this post in threaded view
|

[scala-tools] Scala tool for formatting the source code

Samira Tasharofi
Hi,

I read the "Scala Style Guide" at http://davetron5000.github.com/scala-style/index.html
and tried to use the formatting option of Eclipse for automatic
formatting of my Scala codes. But it seems that Eclipse does not
support reformatting of Scala source codes. Is there any tool that I
can use for automatic formatting of my Scala codes? How the programmer
of Standard library maintain their code in a consistent format?

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

[scala-tools] Re: Scala tool for formatting the source code

Philippe Lhoste
On 18/02/2011 17:08, Samira Tasharofi wrote:
> How the programmer
> of Standard library maintain their code in a consistent format?

Perhaps by paying attention to the coding rules set in the project? ;-)

(but admittedly, I use sometime Eclipse's reformatter, on Java code, when some previous
programmer was sloppy (eg. bad indent size) or when the SCM messed up indentation with its
automatic merge (matching wrong level of braces). So it is still convenient.)

Note: there are other IDEs (NetBeans, JetBrains) which might (or not) implement this feature.

--
Philippe Lhoste
--  (near) Paris -- France
--  http://Phi.Lho.free.fr
--  --  --  --  --  --  --  --  --  --  --  --  --  --

Reply | Threaded
Open this post in threaded view
|

[scala-tools] Re: Scala tool for formatting the source code

Jesper Nordenberg
In reply to this post by Samira Tasharofi
Samira Tasharofi skrev 2011-02-18 17:08:
> Hi,
>
> I read the "Scala Style Guide" at http://davetron5000.github.com/scala-style/index.html
> and tried to use the formatting option of Eclipse for automatic
> formatting of my Scala codes. But it seems that Eclipse does not
> support reformatting of Scala source codes. Is there any tool that I
> can use for automatic formatting of my Scala codes? How the programmer
> of Standard library maintain their code in a consistent format?

This might be what you're looking for:

https://github.com/mdr/scalariform

/Jesper Nordenberg

Reply | Threaded
Open this post in threaded view
|

Re: [scala-tools] Re: Scala tool for formatting the source code

David Bernard-3
Eclipse ScalaIDE use scalariform to format code. IIRC to use reformat you should select the code to reformat.
If it doesn't work, please open a bug ticket.

/davidB

On Fri, Feb 18, 2011 at 19:24, Jesper Nordenberg <[hidden email]> wrote:
Samira Tasharofi skrev 2011-02-18 17:08:

Hi,

I read the "Scala Style Guide" at http://davetron5000.github.com/scala-style/index.html
and tried to use the formatting option of Eclipse for automatic
formatting of my Scala codes. But it seems that Eclipse does not
support reformatting of Scala source codes. Is there any tool that I
can use for automatic formatting of my Scala codes? How the programmer
of Standard library maintain their code in a consistent format?

This might be what you're looking for:

https://github.com/mdr/scalariform

/Jesper Nordenberg


Reply | Threaded
Open this post in threaded view
|

Re: [scala-tools] Re: Scala tool for formatting the source code

Naftoli Gugenheim
It definitely works, but I think it always runs on the entire file, selection or not.
What's your plugin version and update site?



On Fri, Feb 18, 2011 at 4:27 PM, David Bernard <[hidden email]> wrote:
Eclipse ScalaIDE use scalariform to format code. IIRC to use reformat you should select the code to reformat.
If it doesn't work, please open a bug ticket.

/davidB


On Fri, Feb 18, 2011 at 19:24, Jesper Nordenberg <[hidden email]> wrote:
Samira Tasharofi skrev 2011-02-18 17:08:

Hi,

I read the "Scala Style Guide" at http://davetron5000.github.com/scala-style/index.html
and tried to use the formatting option of Eclipse for automatic
formatting of my Scala codes. But it seems that Eclipse does not
support reformatting of Scala source codes. Is there any tool that I
can use for automatic formatting of my Scala codes? How the programmer
of Standard library maintain their code in a consistent format?

This might be what you're looking for:

https://github.com/mdr/scalariform

/Jesper Nordenberg



Reply | Threaded
Open this post in threaded view
|

[scala-tools] Re: Scala tool for formatting the source code

Samira Tasharofi
Thank you all for your help. scalariform now "almost" works in my
eclipse. By "almost" I mean sometimes it cannot reformat my code. But
in most cases, it works.
The problem is that it does not work for "save action". Is it supposed
to work on save actions?



On Feb 18, 3:45 pm, Naftoli Gugenheim <[hidden email]> wrote:

> It definitely works, but I think it always runs on the entire file,
> selection or not.
> What's your plugin version and update site?
>
> On Fri, Feb 18, 2011 at 4:27 PM, David Bernard
> <[hidden email]>wrote:
>
>
>
>
>
>
>
> > Eclipse ScalaIDE use scalariform to format code. IIRC to use reformat you
> > should select the code to reformat.
> > If it doesn't work, please open a bug ticket.
>
> > /davidB
>
> > On Fri, Feb 18, 2011 at 19:24, Jesper Nordenberg <[hidden email]>wrote:
>
> >> Samira Tasharofi skrev 2011-02-18 17:08:
>
> >>  Hi,
>
> >>> I read the "Scala Style Guide" at
> >>>http://davetron5000.github.com/scala-style/index.html
> >>> and tried to use the formatting option of Eclipse for automatic
> >>> formatting of my Scala codes. But it seems that Eclipse does not
> >>> support reformatting of Scala source codes. Is there any tool that I
> >>> can use for automatic formatting of my Scala codes? How the programmer
> >>> of Standard library maintain their code in a consistent format?
>
> >> This might be what you're looking for:
>
> >>https://github.com/mdr/scalariform
>
> >> /Jesper Nordenberg
Reply | Threaded
Open this post in threaded view
|

Re: [scala-tools] Re: Scala tool for formatting the source code

David Bernard-3


On Fri, Feb 18, 2011 at 23:20, Samira Tasharofi <[hidden email]> wrote:
Thank you all for your help. scalariform now "almost" works in my
eclipse. By "almost" I mean sometimes it cannot reformat my code. But
in most cases, it works.
The problem is that it does not work for "save action". Is it supposed
to work on save actions?

No you should call it explicitly.
There is no save hook (on save action) like for java.
On save, only builder is called.

/davidB 



On Feb 18, 3:45 pm, Naftoli Gugenheim <[hidden email]> wrote:
> It definitely works, but I think it always runs on the entire file,
> selection or not.
> What's your plugin version and update site?
>
> On Fri, Feb 18, 2011 at 4:27 PM, David Bernard
> <[hidden email]>wrote:
>
>
>
>
>
>
>
> > Eclipse ScalaIDE use scalariform to format code. IIRC to use reformat you
> > should select the code to reformat.
> > If it doesn't work, please open a bug ticket.
>
> > /davidB
>
> > On Fri, Feb 18, 2011 at 19:24, Jesper Nordenberg <[hidden email]>wrote:
>
> >> Samira Tasharofi skrev 2011-02-18 17:08:
>
> >>  Hi,
>
> >>> I read the "Scala Style Guide" at
> >>>http://davetron5000.github.com/scala-style/index.html
> >>> and tried to use the formatting option of Eclipse for automatic
> >>> formatting of my Scala codes. But it seems that Eclipse does not
> >>> support reformatting of Scala source codes. Is there any tool that I
> >>> can use for automatic formatting of my Scala codes? How the programmer
> >>> of Standard library maintain their code in a consistent format?
>
> >> This might be what you're looking for:
>
> >>https://github.com/mdr/scalariform
>
> >> /Jesper Nordenberg

Reply | Threaded
Open this post in threaded view
|

[scala-tools] Re: Scala tool for formatting the source code

Samira Tasharofi
But in https://github.com/mdr/scalariform
it is mentioned that it works for save action.

On Feb 18, 4:34 pm, David Bernard <[hidden email]> wrote:

> On Fri, Feb 18, 2011 at 23:20, Samira Tasharofi
> <[hidden email]>wrote:
>
> > Thank you all for your help. scalariform now "almost" works in my
> > eclipse. By "almost" I mean sometimes it cannot reformat my code. But
> > in most cases, it works.
> > The problem is that it does not work for "save action". Is it supposed
> > to work on save actions?
>
> No you should call it explicitly.
> There is no save hook (on save action) like for java.
> On save, only builder is called.
>
> /davidB
>
>
>
>
>
>
>
>
>
> > On Feb 18, 3:45 pm, Naftoli Gugenheim <[hidden email]> wrote:
> > > It definitely works, but I think it always runs on the entire file,
> > > selection or not.
> > > What's your plugin version and update site?
>
> > > On Fri, Feb 18, 2011 at 4:27 PM, David Bernard
> > > <[hidden email]>wrote:
>
> > > > Eclipse ScalaIDE use scalariform to format code. IIRC to use reformat
> > you
> > > > should select the code to reformat.
> > > > If it doesn't work, please open a bug ticket.
>
> > > > /davidB
>
> > > > On Fri, Feb 18, 2011 at 19:24, Jesper Nordenberg <[hidden email]
> > >wrote:
>
> > > >> Samira Tasharofi skrev 2011-02-18 17:08:
>
> > > >>  Hi,
>
> > > >>> I read the "Scala Style Guide" at
> > > >>>http://davetron5000.github.com/scala-style/index.html
> > > >>> and tried to use the formatting option of Eclipse for automatic
> > > >>> formatting of my Scala codes. But it seems that Eclipse does not
> > > >>> support reformatting of Scala source codes. Is there any tool that I
> > > >>> can use for automatic formatting of my Scala codes? How the
> > programmer
> > > >>> of Standard library maintain their code in a consistent format?
>
> > > >> This might be what you're looking for:
>
> > > >>https://github.com/mdr/scalariform
>
> > > >> /Jesper Nordenberg
Reply | Threaded
Open this post in threaded view
|

[scala-tools] Re: Scala tool for formatting the source code

Matt Russell
In reply to this post by Samira Tasharofi
On Friday, February 18, 2011 10:20:05 PM UTC, Samira Tasharofi wrote:
 
Thank you all for your help. scalariform now "almost" works in my
eclipse. By "almost" I mean sometimes it cannot reformat my code. But
in most cases, it works.

The formatter only works if it can parse the source successfully, which might account for some of what you're seeing. Certainly, though, if you've got Scala 2.8 source that compiles successfully but can't be reformatted, that's a bug -- it'd be much appreciated if you could file an issue on Github:

 
The problem is that it does not work for "save action". Is it supposed
to work on save actions?

Yes -- it used to, unfortunately it seems it stopped working at some point. I'll see if I can get it working again.

-- Matt
Reply | Threaded
Open this post in threaded view
|

Re: [scala-tools] Re: Scala tool for formatting the source code

Randall R Schulz-2
In reply to this post by Jesper Nordenberg
On Friday February 18 2011, Jesper Nordenberg wrote:
> ...
>
> This might be what you're looking for:
>
> https://github.com/mdr/scalariform

Well, that's an alarming development.

I cannot fathom why programmers want to surrender the precise layout and
appearance of their code to a program. There's certainly no way a
program is going to be able to format code the way I want it. I make
decisions about each line, block and file and while there are rules I
use, their are many exceptions, many cases were simple uniformity
cannot produce well presented code. Now, if a program could do that,
I'd use it, but given the vanishingly small probability of such a thing
existing (while Scala is still in use), I will not hold my breath.


> /Jesper Nordenberg


Randall ZSchulz
Reply | Threaded
Open this post in threaded view
|

Re: [scala-tools] Re: Scala tool for formatting the source code

Jim Balter-2
On Fri, Feb 18, 2011 at 9:05 PM, Randall R Schulz <[hidden email]> wrote:
On Friday February 18 2011, Jesper Nordenberg wrote:
> ...
>
> This might be what you're looking for:
>
> https://github.com/mdr/scalariform

Well, that's an alarming development.

Prettyprinting has been around for at least five decades.
 

I cannot fathom

Inability to comprehend is not a virtue.
 
why programmers want to surrender the precise layout and
appearance of their code to a program.

You're allowed to modify the program's output, y'know. And interactive formatters allow you to selectively choose what code to format -- no surrendering is required.
 
There's certainly no way a
program is going to be able to format code the way I want it. I make
decisions about each line, block and file and while there are rules I
use, their are many exceptions, many cases were simple uniformity
cannot produce well presented code. Now, if a program could do that,
I'd use it, but given the vanishingly small probability of such a thing
existing (while Scala is still in use), I will not hold my breath.


Sounds like a case of making the perfect the enemy of the good.

-- Jim

Reply | Threaded
Open this post in threaded view
|

Re: [scala-tools] Re: Scala tool for formatting the source code

Matt Russell
In reply to this post by Randall R Schulz-2
On 19 February 2011 05:05, Randall R Schulz <[hidden email]> wrote:
>> https://github.com/mdr/scalariform
>
> Well, that's an alarming development.
>
> I cannot fathom why programmers want to surrender the precise layout and
> appearance of their code to a program

Each to their own, of course, but I wrote Scalariform because I find coding without a formatter to be a pretty harsh experience:

* It eases some of the mechanics of manipulating code text. I'm constantly messing stuff up as I add / edit code, and I find it unbearably tedious to manually reindent / fix spacing that way.

* You do indeed surrender the precise layout of your program. YMMV, but I find an automatic formatter can get close enough to an acceptable style often enough that this is not an issue.

* You can selectively disable an automatic formatter for regions of the source

* Mumble mumble...something about enforcing consistency of code style across a team...mumble mumble. Probably not a bad idea, to be honest, but not a particularly big issue for me.

Would you be willing to try it across some of your source? I'd be interested to hear how much you find unacceptable -- both cases where an automatic rule would have solved things, and cases where no simple automatic rule could plausibly have done the right thing.

-- Matt
Reply | Threaded
Open this post in threaded view
|

Re: [scala-tools] Re: Scala tool for formatting the source code

Randall R Schulz-2
On Saturday February 19 2011, Matt Russell wrote:
> On 19 February 2011 05:05, Randall R Schulz <[hidden email]> wrote:
> ...
>
> Would you be willing to try it across some of your source? I'd be
> interested to hear how much you find unacceptable -- both cases where
> an automatic rule would have solved things, and cases where no simple
> automatic rule could plausibly have done the right thing.

I really don't have any interest in a program that leaves my code
untouched and I want to write my code the way I want to write it, not
write it in a manner I dislike and have a tool make it into one I do
like.

I really don't think this sort of program is a useful thing and I don't
think it's good for people to have it or to use it. I very strongly
believe that we should write our code the way we want it written. What
is the real motivation for writing code in a form other than the way
you want it?


> -- Matt


Randall Schulz
Reply | Threaded
Open this post in threaded view
|

Re: [scala-tools] Re: Scala tool for formatting the source code

Matt Russell
On Saturday, February 19, 2011 3:01:01 PM UTC, Randall R Schulz wrote:

> I really don't think this sort of program is a useful thing and I don't 
> think it's good for people to have it or to use it. 

You're entitled to your opinion, but I would ask that you at least try out the tool before making an assertion like that.

> I very strongly believe that we should write our code the way we want it written. What 
> is the real motivation for writing code in a form other than the way you want it?

I use a code formatter to get code into the form I want it. As above, to "ease some of the mechanics of manipulating code text". No-one, I assume, writes perfect code in a linear fashion. We add bits, move bits, refactor, and so forth. In doing so, the formatting gets messed up. 

Take, for example, moving a local def block to a containing scope, which will then require a change of indentation. Rather than reindenting by hand, I get a tool to do the job. My mode of operation is to invoke a formatter interactively very frequently during editing. I find that that way, I don't have to worry so much about maintaining the layout of the source and can concentrate on the more interesting parts of coding.

-- Matt
Reply | Threaded
Open this post in threaded view
|

Re: [scala-tools] Re: Scala tool for formatting the source code

Jim Powers
In reply to this post by Randall R Schulz-2

On Feb 19, 2011 10:01 AM, "Randall R Schulz" <[hidden email]> wrote:

> I really don't have any interest in a program that leaves my code
> untouched and I want to write my code the way I want to write it, not
> write it in a manner I dislike and have a tool make it into one I do
> like.
>
> I really don't think this sort of program is a useful thing and I don't
> think it's good for people to have it or to use it. I very strongly
> believe that we should write our code the way we want it written. What
> is the real motivation for writing code in a form other than the way
> you want it?

One of the virtues of programming languages are their constrained grammars. Because of this fact it is possible do do interesting things with the resulting parse tree, compiling for one, pretty printing is another. Automatic pretty-printing of code is really quite useful. You see, the way you format your code may be great for you but could suck for me. I often automatically reformat code in my emacs buffers (using emacs' scala-mode and scaliform in combination) because the results generally put the various constructs into predictable visual locations lowering my cognitive load while reading. Of course you are free to keep your source code in the way that is most comfortable to you, but others may not share your tastes this is where automated tools like pretty-printing with personal customizations comes into play. Since I use code formatters to help me digest code and I've not yet witnessed the collapse of the universe doing so I don't think the existence and use of such tools harms anyone.

--
Jim Powers

Reply | Threaded
Open this post in threaded view
|

Re: [scala-tools] Re: Scala tool for formatting the source code

Peter C. Chapin-3
In reply to this post by Randall R Schulz-2
On Sat, 19 Feb 2011, Randall R Schulz wrote:

> I really don't think this sort of program is a useful thing and I don't
> think it's good for people to have it or to use it. I very strongly
> believe that we should write our code the way we want it written. What
> is the real motivation for writing code in a form other than the way
> you want it?

You aren't always the one writing the code.

I work with students in group projects. It is desirable that we all produce
consistently formatted code and students are often not very good at that.
One way to handle the issue is to agree on a style and then encode that
agreement into the configuration of a code formatting tool. Then the rule is
simple: run the code through the formatter before committing it to the
repository. In fact, we could even create a pre-commit hook that does this
automatically (although we haven't). This is an easy and effective way for
everyone to generate consistently styled code when working on the same
project, despite differences (legitimate or otherwise) in personal
preferences.

I agree that code formatters don't do a perfect job. However, a good
formatter does a good enough job to make it very worthwhile in the situation
I described. I definitely support their use.

Peter

Reply | Threaded
Open this post in threaded view
|

Re: [scala-tools] Re: Scala tool for formatting the source code

Daniel Sobral
In reply to this post by Randall R Schulz-2
Code formatters have two main goals:

1. Automatically formatting code which, for some reason, got badly formatting. In fact, the most basic operation -- indentation adjustment -- is found on all IDEs and code editors with varying degrees of sophistication.

2. Reformatting other people's code to something that doesn't offend your sensibility. Perhaps you are very concerned about what the code looks like, but the fact is that there are many programmers out there -- good and bad -- which don't give much of a damn. 

On Sat, Feb 19, 2011 at 03:05, Randall R Schulz <[hidden email]> wrote:
On Friday February 18 2011, Jesper Nordenberg wrote:
> ...
>
> This might be what you're looking for:
>
> https://github.com/mdr/scalariform

Well, that's an alarming development.

I cannot fathom why programmers want to surrender the precise layout and
appearance of their code to a program. There's certainly no way a
program is going to be able to format code the way I want it. I make
decisions about each line, block and file and while there are rules I
use, their are many exceptions, many cases were simple uniformity
cannot produce well presented code. Now, if a program could do that,
I'd use it, but given the vanishingly small probability of such a thing
existing (while Scala is still in use), I will not hold my breath.


> /Jesper Nordenberg


Randall ZSchulz



--
Daniel C. Sobral

I travel to the future all the time.
Reply | Threaded
Open this post in threaded view
|

[scala-tools] Re: Scala tool for formatting the source code

Philippe Lhoste
In reply to this post by Matt Russell
On 19/02/2011 10:22, Matt Russell wrote:
> Each to their own, of course

Sure, it is better to have a tool and not use it... than not have the
tool when we need it! ;-)

> but I wrote Scalariform because I find
> coding without a formatter to be a pretty harsh experience:

Not me, but I send back to your initial sentence...

> * It eases some of the mechanics of manipulating code text. I'm
> constantly messing stuff up as I add / edit code, and I find it
> unbearably tedious to manually reindent / fix spacing that way.

Not me, again, but I have a good editor... :-) And I am quite maniacal
with the look of my code.

> * You do indeed surrender the precise layout of your program. YMMV, but
> I find an automatic formatter can get close enough to an acceptable
> style often enough that this is not an issue.

What I don't like is "acceptable style".
I appreciate Eclipse's formatter because you can tweak it to your
heart's content, to fit exactly to your taste or to the team rules.
Last time I checked (perhaps too quickly) Scalariform, it was enforcing
what seems to be the consensus around "good coding style for Scala" and
that's all. No flexibility. I can have missed options, though.

Again, nothing against your tool or its usage, just a remark of a
potential user.

--
Philippe Lhoste
--  (near) Paris -- France
--  http://Phi.Lho.free.fr
--  --  --  --  --  --  --  --  --  --  --  --  --  --

Reply | Threaded
Open this post in threaded view
|

[scala-tools] Re: Scala tool for formatting the source code

Philippe Lhoste
In reply to this post by Randall R Schulz-2
On 19/02/2011 16:01, Randall R Schulz wrote:
> I really don't think this sort of program is a useful thing and I don't
> think it's good for people to have it or to use it. I very strongly
> believe that we should write our code the way we want it written. What
> is the real motivation for writing code in a form other than the way
> you want it?

Sometime, you get code that isn't your. It can be written by a messy
coder or one using conventions that irks you; it can have been messed up
by HTML formatters, or imperfect merge tools, etc.
Such tool can be useful to get at least something readable, for code
inspection / study.
Or just to format to your own rules, when you accept code useful for
your open source project, from a programmer not disciplined enough to
follow your coding style.

--
Philippe Lhoste
--  (near) Paris -- France
--  http://Phi.Lho.free.fr
--  --  --  --  --  --  --  --  --  --  --  --  --  --

Reply | Threaded
Open this post in threaded view
|

Re: [scala-tools] Re: Scala tool for formatting the source code

Kevin Wright-3
In reply to this post by Samira Tasharofi


On 20 Feb 2011 10:10, "Philippe Lhoste" <[hidden email]> wrote:
>
> On 19/02/2011 16:01, Randall R Schulz wrote:
>>
>> I really don't think this sort of program is a useful thing and I don't
>> think it's good for people to have it or to use it. I very strongly
>> believe that we should write our code the way we want it written. What
>> is the real motivation for writing code in a form other than the way
>> you want it?
>
>
> Sometime, you get code that isn't your. It can be written by a messy coder or one using conventions that irks you; it can have been messed up by HTML formatters, or imperfect merge tools, etc.
> Such tool can be useful to get at least something readable, for code inspection / study.
> Or just to format to your own rules, when you accept code useful for your open source project, from a programmer not disciplined enough to follow your coding style.
>

There's also the whole category of code generators, Java->Scala converters, decompilers, etc. Where it's nice to have a common tool that every such project can share, instead of having to reinvent the wheel each time...

>
> --
> Philippe Lhoste
> --  (near) Paris -- France
> --  http://Phi.Lho.free.fr
> --  --  --  --  --  --  --  --  --  --  --  --  --  --
>

12