Created
April 7, 2012 00:33
-
-
Save hbt/2324282 to your computer and use it in GitHub Desktop.
script to align code without using a parter + tokenizer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# add("zi", Zoom["in"]); | |
# add("zo", Zoom.out); | |
# add("zm", Zoom.more); | |
# add("zr", Zoom.reduce); | |
# add("zz", Zoom.reset); | |
# | |
# add("zI", Zoom.current_in); | |
# add("zO", Zoom.current_out); | |
# | |
# add("zM", Zoom.current_more); | |
# add("zR", Zoom.current_reduce); | |
# add("zZ", Zoom.current_reset); | |
class Part | |
attr_accessor :value | |
def initialize(str) | |
@value = str | |
end | |
def is_alphanumeric | |
is_symbol == false | |
end | |
def is_symbol | |
res = true | |
@value.each_char do |c| | |
res = res && ['{', '}', '[', ']', '+', ')', ',', ';'].include?(c) | |
end | |
res | |
end | |
def type | |
# ln = letter+number | |
# s = symbol | |
is_alphanumeric ? 'ln' : 's' | |
end | |
def to_str | |
@value | |
end | |
end | |
class PartUtils | |
class << self | |
def extract_parts(line) | |
res = [] | |
current_value = [] | |
prev_type = nil | |
line.each_char do |c| | |
p = Part.new c | |
if p.type == prev_type || prev_type == nil | |
current_value << c | |
elsif p.type != prev_type && prev_type != nil | |
res << Part.new(current_value.join) | |
current_value = [c] | |
end | |
prev_type = p.type | |
end | |
res << Part.new(current_value.join) if current_value.size > 0 | |
res | |
end | |
def extract_parts_attrs(parts, method) | |
res = [] | |
parts.each do |p| | |
res << p.send(method) | |
end | |
res | |
end | |
def is_alphanumeric(c) | |
(c =~ /[a-zA-Z]/ || c =~ /[0-9]/) === 0 | |
end | |
def is_symbol(c) | |
is_alphanumeric == false | |
end | |
def compare_parts(*parts) | |
res = parts.size > 1 | |
original_part = parts[0] | |
original_types = extract_parts_attrs(original_part, 'type') | |
original_values = extract_parts_attrs(original_part, 'value') | |
parts.each do |p| | |
types = extract_parts_attrs(p, 'type') | |
values = extract_parts_attrs(p, 'value') | |
# check we have same pattern | |
types.each_with_index do |t, i| | |
res = false if original_types[i] != t | |
# check we have same symbols | |
res = false if t == 's' && original_values[i] != values[i] | |
end | |
end | |
res | |
end | |
def format_pattern(lines) | |
all_parts = [] | |
lines.each do |line| | |
all_parts << extract_parts(line) | |
end | |
for i in (0...all_parts[0].size) | |
# find largest part | |
big_size = 0; | |
for j in (0...all_parts.size) | |
value = all_parts[j][i].value | |
big_size = value.size if value.size > big_size | |
end | |
# add spaces to other parts | |
for j in (0...all_parts.size) | |
all_parts[j][i].value = all_parts[j][i].value.ljust(big_size) | |
end | |
for j in (0...all_parts.size) | |
# puts all_parts[j][i].value | |
end | |
end | |
formatted = [] | |
all_parts.each do |row| | |
formatted << extract_parts_attrs(row, 'value').join() | |
end | |
formatted | |
end | |
end | |
end | |
lines = [ | |
' add("zM", Zoom.current_more);', | |
' add("zR", Price.current_reduce);', | |
' mm();' | |
] | |
content = File.read('../src/frontend/main.js') | |
lines = content.split("\n") | |
prev_line_parts = nil | |
current_pattern = [] | |
formatted_line_parts = [] | |
lines.each do |line| | |
line_parts = PartUtils.extract_parts line | |
if prev_line_parts == nil | |
prev_line_parts = line_parts | |
current_pattern << line | |
next | |
end | |
same_pattern = PartUtils.compare_parts(line_parts, prev_line_parts) and line_parts.size > 1 | |
prev_line_parts = line_parts | |
if !same_pattern | |
if current_pattern.size > 1 | |
current_pattern = PartUtils.format_pattern current_pattern | |
end | |
formatted_line_parts << current_pattern | |
current_pattern = [] | |
end | |
current_pattern << line | |
end | |
formatted_line_parts << current_pattern | |
formatted_line_parts.flatten! | |
content = formatted_line_parts.join("\n") | |
tmpfile = File.new '../src/frontend/main.js', "w" | |
tmpfile.write content | |
tmpfile.flush |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment