I need a code beautifier or formatter

Hello,

I have some source code that is terribly formatted. It needs some good
indentation at least. But I am too lazy to do it all by hand.

Does anyone know a tool to do the job ?

Thanks in advance,
Joao

Vim can format Ruby code (visual select then press '=').

Regards,

  Michael

···

On Sat, Jun 14, 2003 at 10:01:36PM +0900, Joao Pedrosa wrote:

Hello,

I have some source code that is terribly formatted. It needs some good
indentation at least. But I am too lazy to do it all by hand.

Does anyone know a tool to do the job ?

joaopedrosa@yahoo.com (Joao Pedrosa) writes:

I have some source code that is terribly formatted. It needs some good
indentation at least. But I am too lazy to do it all by hand.

Does anyone know a tool to do the job ?

I use XEmacs in Ruby mode. It has an indent function, which you can
invoke in batch mode from the command line

xemacs -batch myprog.rb -eval ‘(indent-region (point-min) (point-max)
nil)’ -f save-buffer -kill

I hope this helps,

Tim

joaopedrosa@yahoo.com (Joao Pedrosa) wrote in message news:d518d51a.0306140458.42b21a5b@posting.google.com

Hello,

I have some source code that is terribly formatted. It needs some good
indentation at least. But I am too lazy to do it all by hand.

Does anyone know a tool to do the job ?

Thanks in advance,
Joao

Hi Joao,

You could try a small program I wrote to do that: it’s by no means
complete, and will only handle a subset off all possible ruby programs
correctly. If I remember correctly it only handles programs that use
do…end instead of {…}, but maybe you can adapt it.

Cheers,

Han Holl

#!/usr/bin/ruby

class Pprint
attr_reader :arr
def initialize(s)
if ! build(s)
puts “Pretty print problem”
build(s, true)
exit
end
end

def build(s, debug = false)
indent = 0;
@arr =
comment = false
waitforeof = false
wf = nil
s.each do |line|
next if line =~ /^\s*$/
if line =~ /^=begin/
comment = true
end
if comment || line =~ /^\s*#[^{]/
@arr << line
if line =~ /^=end/
comment = false
end
next
end
if wf && line =~ wf
arr << $1
wf = nil
next
end
if line =~ /<<([A-Z0-9_])$/
waitforeof = true
wf = /^\s
(#{$1})\s*$/
end
ft = line.split

puts line

puts “HHH: #{ft[0]} #{ft[-1]}”

  if ft[0] =~ /^(?:def|module|class)$/ && ft[-1] == "end"
    @arr << "#{"  " * indent}#{line.strip}"
    next
  end        

@arr << “” if ft[0] == “def”

  indent -= 1 if ft[0] =~ /^(else|end|when|rescue|ensure)$/
  if indent < 0
    break
  end
  @arr << "#{"  " * indent}#{line.strip}"
  puts "#{indent}: #{line}" if debug
 
  case ft[0]
  when /^(class|module|case|def|if|while|for|else|when|unless|begin|rescue|ensure)$/
    indent += 1
  else
    
    if ft.find { |w| w == "do" } && ! ft.find { |w| w == "end" }
      indent += 1
    end
  end
end
indent == 0

end

end

if FILE == $0
ARGV.each do |file|
if FileTest.exists? file
f = nil
pp = Pprint.new((f = File.open(file)).readlines)
f.close
puts pp.arr
end
end
end

The emacs mode that comes with the distribution does a pretty good job as well.

···

On Sat, Jun 14, 2003 at 10:01:36PM +0900, Joao Pedrosa wrote:

Hello,

I have some source code that is terribly formatted. It needs some good
indentation at least. But I am too lazy to do it all by hand.

Does anyone know a tool to do the job ?

Vim can format Ruby code (visual select then press ‘=’).

for me this simply lines eveything up in a column - could you post the
relevant bits of your .vimrc/.gvimrc or forward them to me directly?

this sounds like a great feature…

-a

···

On Sat, 14 Jun 2003, Michael Neumann wrote:

On Sat, Jun 14, 2003 at 10:01:36PM +0900, Joao Pedrosa wrote:

Hello,

I have some source code that is terribly formatted. It needs some good
indentation at least. But I am too lazy to do it all by hand.

Does anyone know a tool to do the job ?

Vim can format Ruby code (visual select then press ‘=’).

Ara Howard
NOAA Forecast Systems Laboratory
Information and Technology Services
Data Systems Group
R/FST 325 Broadway
Boulder, CO 80305-3328
Email: ara.t.howard@noaa.gov
Phone: 303-497-7238
Fax: 303-497-7259
~ > ruby -e ‘p(%.\x2d\x29…intern)’
====================================

Nothing special in there, except the usual "syn on".

Make sure that the file is recognized as a Ruby source file. If it does
not end with .rb then ":setf ruby" will do it.

Hope this helps.

Regards,

  Michael

···

On Sat, Jun 14, 2003 at 11:43:16PM +0900, ahoward wrote:

On Sat, 14 Jun 2003, Michael Neumann wrote:

> On Sat, Jun 14, 2003 at 10:01:36PM +0900, Joao Pedrosa wrote:
> > Hello,
> >
> > I have some source code that is terribly formatted. It needs some good
> > indentation at least. But I am too lazy to do it all by hand.
> >
> > Does anyone know a tool to do the job ?
>
> Vim can format Ruby code (visual select then press '=').

for me this simply lines eveything up in a column - could you post the
relevant bits of your .vimrc/.gvimrc or forward them to me directly?

···

On Sat, Jun 14, 2003 at 11:43:16PM +0900, ahoward wrote:

On Sat, 14 Jun 2003, Michael Neumann wrote:

On Sat, Jun 14, 2003 at 10:01:36PM +0900, Joao Pedrosa wrote:

Hello,

I have some source code that is terribly formatted. It needs some good
indentation at least. But I am too lazy to do it all by hand.

Does anyone know a tool to do the job ?

Vim can format Ruby code (visual select then press ‘=’).

for me this simply lines eveything up in a column - could you post the
relevant bits of your .vimrc/.gvimrc or forward them to me directly?

this sounds like a great feature…


_ _

__ __ | | ___ _ __ ___ __ _ _ __
'_ \ / | __/ __| '_ _ \ / ` | ’ \
) | (| | |
__ \ | | | | | (| | | | |
.__/ _,
|_|/| || ||_,|| |_|
Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com

Turn right here. No! NO! The OTHER right!

i've got all that - which vim version?

-a

···

On Sat, 14 Jun 2003, Michael Neumann wrote:

Nothing special in there, except the usual "syn on".

Make sure that the file is recognized as a Ruby source file. If it does
not end with .rb then ":setf ruby" will do it.

Hope this helps.

--
  ====================================
  > Ara Howard
  > NOAA Forecast Systems Laboratory
  > Information and Technology Services
  > Data Systems Group
  > R/FST 325 Broadway
  > Boulder, CO 80305-3328
  > Email: ara.t.howard@noaa.gov
  > Phone: 303-497-7238
  > Fax: 303-497-7259
  > ~ > ruby -e 'p(%.\x2d\x29..intern)'
  ====================================

Hi,

Hello,

I have some source code that is terribly formatted. It needs some good
indentation at least. But I am too lazy to do it all by hand.

Does anyone know a tool to do the job ?

Vim can format Ruby code (visual select then press ‘=’).

for me this simply lines eveything up in a column - could you post the
relevant bits of your .vimrc/.gvimrc or forward them to me directly?

this sounds like a great feature…

http://www.rubygarden.org/ruby?VimRubySupport

Nice link.

When I added the lacking bits to .vimrc it began formatting ok.

But it had some problem with

----- snip
def listjoin(channels)
channels.each do |c|^M
s = “join ##{c}”^M
@irconnector.sendToStatus s^M
@irconnector.sendToServer s^M
end^M
end^M
----- snip

But now it is pretty usable.

Thanks all,
Joao

···

Em Sáb, 2003-06-14 às 11:54, Mauricio Fernández escreveu:

On Sat, Jun 14, 2003 at 11:43:16PM +0900, ahoward wrote:

On Sat, 14 Jun 2003, Michael Neumann wrote:

On Sat, Jun 14, 2003 at 10:01:36PM +0900, Joao Pedrosa wrote:

I think I found the bug in indent/ruby.vim responsible for that (assuming
that you had in fact one or more spaces after ‘|c|’ and before the EOL):

— ruby.vim.bak 2003-06-14 18:00:03.000000000 +0200
+++ ruby.vim 2003-06-14 18:02:47.000000000 +0200
@@ -52,7 +52,7 @@
" Add a ‘shiftwidth’ after lines beginning with module, class, def,…
" or ending in {, do (with optional |…| and comment).
let begin_words = ‘(module|class|def|if|for|while|until|else|elsif|case|when|unless|begin|ensure|rescue)>’

  • if line =~ ‘\v^\s*’ . begin_words || line =~ ‘\v({|)\s*(|[^|]|)?(\s#.*)?$’
  • if line =~ ‘\v^\s*’ . begin_words || line =~ ‘\v({|)\s*(|[^|]|)?\s(#.*)?$’
    let ind = ind + &sw
    let flag = 1
    endif

somebody (in fact, now I think it has happened to me in the past) could
put spaces after the block args. and indentation would bomb…

···

On Sun, Jun 15, 2003 at 12:24:58AM +0900, Joao Pedrosa wrote:

But it had some problem with

----- snip
def listjoin(channels)
channels.each do |c|^M
s = “join ##{c}”^M
@irconnector.sendToStatus s^M
@irconnector.sendToServer s^M
end^M
end^M
----- snip


_ _

__ __ | | ___ _ __ ___ __ _ _ __
'_ \ / | __/ __| '_ _ \ / ` | ’ \
) | (| | |
__ \ | | | | | (| | | | |
.__/ _,
|_|/| || ||_,|| |_|
Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com

We are using Linux daily to UP our productivity - so UP yours!
– Adapted from Pat Paulsen by Joe Sloan

You need this line in your .vimrc file:

  filetype plugin indent on

Regards,

  Michael

···

On Sat, Jun 14, 2003 at 03:09:25PM +0000, ahoward wrote:

On Sat, 14 Jun 2003, Michael Neumann wrote:

> Nothing special in there, except the usual "syn on".
>
> Make sure that the file is recognized as a Ruby source file. If it does
> not end with .rb then ":setf ruby" will do it.
>
> Hope this helps.

i've got all that - which vim version?

Hi,

···

Em Sáb, 2003-06-14 às 13:19, Mauricio Fernández escreveu:

On Sun, Jun 15, 2003 at 12:24:58AM +0900, Joao Pedrosa wrote:

But it had some problem with

----- snip
def listjoin(channels)
channels.each do |c|^M
s = “join ##{c}”^M
@irconnector.sendToStatus s^M
@irconnector.sendToServer s^M
end^M
end^M
----- snip

I think I found the bug in indent/ruby.vim responsible for that (assuming
that you had in fact one or more spaces after ‘|c|’ and before the EOL):

— ruby.vim.bak 2003-06-14 18:00:03.000000000 +0200
+++ ruby.vim 2003-06-14 18:02:47.000000000 +0200
@@ -52,7 +52,7 @@
" Add a ‘shiftwidth’ after lines beginning with module, class, def,…
" or ending in {, do (with optional |…| and comment).
let begin_words = ‘(module|class|def|if|for|while|until|else|elsif|case|when|unless|begin|ensure|rescue)>’

  • if line =~ ‘\v^\s*’ . begin_words || line =~ ‘\v({|)\s*(|[^|]|)?(\s#.*)?$’
  • if line =~ ‘\v^\s*’ . begin_words || line =~ ‘\v({|)\s*(|[^|]|)?\s(#.*)?$’
    let ind = ind + &sw
    let flag = 1
    endif

somebody (in fact, now I think it has happened to me in the past) could
put spaces after the block args. and indentation would bomb…

Right on target :slight_smile:

I am CCing to Gavin, as he probably would like to fix it.

Thanks,
Joao