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
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
The emacs mode that comes with the distribution does a pretty good job as well.
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
--
====================================
| Ara Howard
| NOAA Forecast Systems Laboratory
| Information and Technology Services
| Data Systems Group
| R/FST 325 Broadway
| Boulder, CO 80305-3328
| Email: ara.t....@noaa.gov
| Phone: 303-497-7238
| Fax: 303-497-7259
| ~ > ruby -e 'p(%.\x2d\x29..intern)'
====================================
--- Michael Neumann <mneu...@ntecs.de> 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
> '=').
>
> Regards,
>
> Michael
Vim did format it, but not as expected. It doesn't even indent
correctly for me. Anyone can confirm that it doesn't work properly ?
Thanks Michael,
Joao
I tried the '=' in VIM and the indent then got screwed up..
Michael, How does your .vimrc look ?
--
Simon Strandgaard
You can try to update your indent file:
http://www.soyabean.com.au/gavin/vim/index.html
Works for me most of the time, but sometimes there's problems w/
def foo
class << anObject; self end
# thinks def foo is closed now and colors are wrong from now on
and
bla do |x|
#sometimes indentation fails here???
#and 'end' is mistakenly aligned with the outer scope, e.g. def foo
--
_ _
| |__ __ _| |_ ___ _ __ ___ __ _ _ __
| '_ \ / _` | __/ __| '_ ` _ \ / _` | '_ \
| |_) | (_| | |_\__ \ | | | | | (_| | | | |
|_.__/ \__,_|\__|___/_| |_| |_|\__,_|_| |_|
Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com
Are Linux users lemmings collectively jumping off of the cliff of
reliable, well-engineered commercial software?
-- Matt Welsh
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
http://www.rubygarden.org/ruby?VimRubySupport
--
_ _
| |__ __ _| |_ ___ _ __ ___ __ _ _ __
| '_ \ / _` | __/ __| '_ ` _ \ / _` | '_ \
| |_) | (_| | |_\__ \ | | | | | (_| | | | |
|_.__/ \__,_|\__|___/_| |_| |_|\__,_|_| |_|
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?
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:
> > > > 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
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(\{|<do>)\s*(\|[^|]*\|)?(\s*#.*)?$'
+ if line =~ '\v^\s*' . begin_words || line =~ '\v(\{|<do>)\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...
--
_ _
| |__ __ _| |_ ___ _ __ ___ __ _ _ __
| '_ \ / _` | __/ __| '_ ` _ \ / _` | '_ \
| |_) | (_| | |_\__ \ | | | | | (_| | | | |
|_.__/ \__,_|\__|___/_| |_| |_|\__,_|_| |_|
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
Right on target :-)
I am CCing to Gavin, as he probably would like to fix it.
Thanks,
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
I use vim for my ruby coding. I set smartindent on as well--not sure how
much difference that makes for ruby. It mostly does the right thing, but...
The problem is that it will end up completely in the weeds after it gets one
line wrong (since vim indents seem to be very much based on what the
previous line was). I've found I can't indent too many lines at once
without this happening. So I don't find I can use gg=G without being
unhappy.
Some things that aren't necessarily 'wrong', but aren't how I like them:
foo = "someverylongstring and some other "+
"something else"
The "something else" just lines up with the previous line. I'd like to see
it indented. Same goes for any non-complete line.
bar = some_method(afjdksajk, gjfrjeje, fjdkjska,
dfjkdsjk, ss)
I'd like for this to have the second line line up with the open paren.
Basically, I want things to work how ruby-mode for emacs works but I want to
use vim :)
There are just two things I miss from emacs now that I've made the switch:
psgml-mode, and the near certainty that emacs indent will tell me when I've
goofed something up and make it look just how I like it.
>xemacs -batch myprog.rb -eval '(indent-region (point-min) (point-max)
>nil)' -f save-buffer -kill
This thread is useful to me though, as I can just define a vim map that will
run the buffer through emacs batch mode and get it how I like it :) Thanks,
Tim, for the tip!
enjoy,
nikolai
--
::: name: Nikolai Weibull :: aliases: pcp / lone-star :::
::: born: Chicago, IL USA :: loc atm: Gothenburg, Sweden :::
::: page: www.pcppopper.org :: fun atm: gf,lps,ruby,php,war3 :::
main(){printf(&linux["\021%six\012\0"],(linux)["have"]+"fun"-97);}
FWIW, here's how the emacs ruby-mode indents that:
foo = "something long" +
"something else"
I thought about this not long ago, while working on a piece of software along
with several others, all with their own special little way of formatting (a
multi-language project, java, C/C++, C# and ruby mostly) trying to identify
important changes from cvs logs was a pain in the *** after the different
editors and different code styles messed things up. It ended up so bad
eventually that something had to be done, forcing everyone to write in the
same fashion would waste time and probably dampen productivity.
Everything that was to be commited to the cvs had to be passed through
indentation programs, astyle and gnu indent covered C/C++/java but we didn't
find any such utilities for Ruby and C# (which had a visual impact on the
project scheduled after analyzing progress reports).
afaik, there is no such application for Ruby (as a sidenote, any handwritten
ruby lexer in ruby? ;) perhaps it is time for the development of such an
application? Personally I'd love to be able to atleast format around the
differences in block structures ( {} compared to do ... etc.), the obvious
depth-indentation and probably a few other things I can't seem to remember
right now :-)
/ Björn Ståhl