Module: RBeautify

Defined in:
ext/ae-editor/lib/rbeautify.rb

Constant Summary collapse

TabStr =

user-customizable values

" "
TabSize =
2
IndentExp =

indent regexp tests

[
   /^module\b/,
   /^class\b/,
   /^if\b/,
   /(=\s*|^)until\b/,
   /(=\s*|^)for\b/,
   /^unless\b/,
   /(=\s*|^)while\b/,
   /(=\s*|^)begin\b/,
   /(^| )case\b/,
   /\bthen\b/,
   /^rescue\b/,
   /^def\b/,
   /\bdo\b/,
   /^else\b/,
   /^elsif\b/,
   /^ensure\b/,
   /\bwhen\b/,
   /\{[^\}]*$/,
   /\[[^\]]*$/
]
OutdentExp =

outdent regexp tests

[
   /^rescue\b/,
   /^ensure\b/,
   /^elsif\b/,
   /^end\b/,
   /^else\b/,
   /\bwhen\b/,
   /^[^\{]*\}/,
   /^[^\[]*\]/
]

Class Method Summary collapse

Class Method Details

.beautify_file(path) ⇒ Object

beautify_string


186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'ext/ae-editor/lib/rbeautify.rb', line 186

def RBeautify.beautify_file(path)
   error = false
   if(path == '-') # stdin source
      source = STDIN.read
      dest,error = beautify_string(source,"stdin")
      print dest
   else # named file source
      source = File.read(path)
      dest,error = beautify_string(source,path)
      if(source != dest)
         # make a backup copy
         File.open(path + "~","w") { |f| f.write(source) }
         # overwrite the original
         File.open(path,"w") { |f| f.write(dest) }
      end
   end
   return error
end

.beautify_string(source, path = "") ⇒ Object


81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'ext/ae-editor/lib/rbeautify.rb', line 81

def RBeautify.beautify_string(source, path = "")
   comment_block = false
   in_here_doc = false
   here_doc_term = ""
   program_end = false
   multiLine_array = []
   multiLine_str = ""
   tab = 0
   output = []
   source.each do |line|
      line.chomp!
      if(!program_end)
         # detect program end mark
         if(line =~ /^__END__$/)
            program_end = true
         else
            # combine continuing lines
            if(!(line =~ /^\s*#/) && line =~ /[^\\]\\\s*$/)
               multiLine_array.push line
               multiLine_str += line.sub(/^(.*)\\\s*$/,"\\1")
               next
            end

            # add final line
            if(multiLine_str.length > 0)
               multiLine_array.push line
               multiLine_str += line.sub(/^(.*)\\\s*$/,"\\1")
            end

            tline = ((multiLine_str.length > 0)?multiLine_str:line).strip
            if(tline =~ /^=begin/)
               comment_block = true
            end
            if(in_here_doc)
               in_here_doc = false if tline =~ %r{\s*#{here_doc_term}\s*}
            else # not in here_doc
               if tline =~ %r{=\s*<<}
                  here_doc_term = tline.sub(%r{.*=\s*<<-?\s*([_|\w]+).*},"\\1")
                  in_here_doc = here_doc_term.size > 0
               end
            end
         end
      end
      if(comment_block || program_end || in_here_doc)
         # add the line unchanged
         output << line
      else
         comment_line = (tline =~ /^#/)
         if(!comment_line)
            # throw out sequences that will
            # only sow confusion
            while tline.gsub!(/\{[^\{]*?\}/,"")
            end
            while tline.gsub!(/\[[^\[]*?\]/,"")
            end
            while tline.gsub!(/'.*?'/,"")
            end
            while tline.gsub!(/".*?"/,"")
            end
            while tline.gsub!(/\`.*?\`/,"")
            end
            while tline.gsub!(/\([^\(]*?\)/,"")
            end
            while tline.gsub!(/\/.*?\//,"")
            end
            while tline.gsub!(/%r(.).*?\1/,"")
            end
            # delete end-of-line comments
            tline.sub!(/#[^\"]+$/,"")
            # convert quotes
            tline.gsub!(/\\\"/,"'")
            OutdentExp.each do |re|
               if(tline =~ re)
                  tab -= 1
                  break
               end
            end
         end
         if (multiLine_array.length > 0)
            multiLine_array.each do |ml|
               output << rb_add_line(ml,tab)
            end
            multiLine_array.clear
            multiLine_str = ""
         else
            output << rb_add_line(line,tab)
         end
         if(!comment_line)
            IndentExp.each do |re|
               if(tline =~ re && !(tline =~ /\s+end\s*$/))
                  tab += 1
                  break
               end
            end
         end
      end
      if(tline =~ /^=end/)
         comment_block = false
      end
   end
   error = (tab != 0)
   STDERR.puts "Error: indent/outdent mismatch: #{tab}." if error
   return output.join("\n") + "\n",error
end

.mainObject

beautify_file


205
206
207
208
209
210
211
212
213
214
215
216
# File 'ext/ae-editor/lib/rbeautify.rb', line 205

def RBeautify.main
   error = false
   if(!ARGV[0])
      STDERR.puts "usage: Ruby filenames or \"-\" for stdin."
      exit 0
   end
   ARGV.each do |path|
      error = (beautify_file(path))?true:error
   end
   error = (error)?1:0
   exit error
end

.rb_add_line(line, tab) ⇒ Object


75
76
77
78
79
# File 'ext/ae-editor/lib/rbeautify.rb', line 75

def RBeautify.rb_add_line(line,tab)
   line.strip!
   line = rb_make_tab(tab) + line if line.length > 0
   return line
end

.rb_make_tab(tab) ⇒ Object


71
72
73
# File 'ext/ae-editor/lib/rbeautify.rb', line 71

def RBeautify.rb_make_tab(tab)
   return (tab < 0)?"":TabStr * TabSize * tab
end