Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

align.rb

View align.rb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 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 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
#!/usr/bin/env ruby
# Alignes the source
def align(text, regexp, width)
text.to_a.map do |line|
if offset = offsetOfRegexpInLine(line, regexp)
if shouldInsertBefore(line, regexp)
before = line[0..offset-1]
before + ' ' * (width - (before.length)) + line[offset..line.length-1]
else
before = line[0..offset]
before + ' ' * (width - (before.length-1)) + line[offset+1..line.length-1]
end
else
line
end
end.join
end
 
# Figures out if the spacing should be added before or after the match
def shouldInsertBefore(line, regexp)
offset = offsetOfRegexpInLine(line,regexp)
if line.chars.to_a[offset-1] =~ /\s/ then true else false end
end
 
# Finds the width of the line with the most text before the regexp.
def width(text, regexp)
text.split("\n").collect { |line| offsetOfRegexpInLine(line,regexp) }.max
end
 
# The offset of a regexp in a line of text. -1 if it doesn't match
def offsetOfRegexpInLine(line, regexp)
if match = regexp.match(line)
match.offset(match.size > 1 ? 1 : 0)[0]
else
-1
end
end
 
# Checks that the regexp matches every line
def hasMatchForAll(text, regexp)
text.to_a.select { |x| not x =~ /^\s*$/}.all? { |line| line =~ regexp }
end
 
def left_spacing(line)
line.chars.take_while { |char| char =~ /\s/ }.join
end
 
# squeeces all whitspace in the line (preserves the left spacing)
def trim_all_whitespace(text)
text.to_a.map do |line|
left_spacing(line) + line.squeeze(" ").squeeze(" ").lstrip #the 2. is a tab
end.join
end
 
# finds the minimum offset of the capture of a regexp across all lines in a text.
def minOffsetOfRegexp(text,regexp)
min_offset = text.split("\n").collect { |line| offsetOfRegexpInLine(line,regexp) }.min
{ "min_offset" => min_offset, "regexp" => regexp }
end
 
# Sorts and filters the regular expressions so the ones with the captures with the
# lowest offset comes first. The ones that aren't matched or doesn't need alignment
# are filtered out
def prioritizeRegexps(text, regexps)
regexps.
select { |r| hasMatchForAll(text, r) }.
collect { |r| minOffsetOfRegexp(text, r) }.
select { |d| not d['min_offset'].nil? }.
sort { |d1,d2| d1['min_offset'] <=> d2['min_offset'] }.
select { |d| d['min_offset'] > 0 }.
collect { |d| d['regexp'] }
end
 
# Finds blocks of code to align. It uses the following heuristics:
#
# - A line belongs to the previous block if it has the same indentation
# - A blank line ends a block (TODO)
#
# returns an array of dictionaries with the keys 'block', 'from', 'to' that
# expresses which lines the block spans.
def find_blocks(text, regexps)
lines = text.to_a
 
initial = { 'prev' => lines[0],
'blocks' => [{ 'lines' => [], 'from' => 0, 'to' => 0 }]}
 
lines.reduce(initial) do |reduced, line|
blocks = reduced['blocks']
current_block = blocks.last
if left_spacing(line) == left_spacing(reduced['prev'])
current_block['lines'] << line
current_block['to'] = current_block['to'] + 1
{ 'prev' => line, 'blocks' => blocks}
else
from = current_block['to']
blocks << { 'lines' => [line],'from' => from, 'to' => from + 1}
{ 'prev' => line, 'blocks' => blocks}
end
end['blocks']
end
 
# Formats a single block.
def format_block(block_dict, regexps)
text = trim_all_whitespace(block_dict['lines'].join)
prioritizeRegexps(text, regexps).
reduce(text) { |txt,regexp| align(txt, regexp, width(txt, regexp)) }
end
 
# Formats every block in the text
def format_all(text, regexps)
find_blocks(trim_all_whitespace(text), regexps).
map { |block| format_block(block, regexps) }
end
 
# Formats the source in the block that contains the given line. All other blocks
# are just returned w/o formatting.
def format_block_containing_line(text, line_number, regexps)
find_blocks(text, regexps).map do |block_dict|
if block_dict['from'] <= line_number && block_dict['to'] >= line_number
format_block(block_dict, regexps)
else
block_dict['lines'].join
end
end
end
 
=begin
Lets give it a try. Side-effecting code is now allowed.
=end
 
regexps = [ /(,)(?!$)/, /\}/, /<-/, /\s[-+\/*|]?(=)\s/, /\s(=>)\s/,/:/,/\/\// ]
 
text = DATA.read
 
print format_all(text, regexps)
# print format_block_containing_line(text, 10, regexps)
# print format_block_containing_line(text, 1, regexps)
 
 
__END__
val a = "aa"
val bb = "bb"
 
{ capitalizeWord, flag::insert },
{ changeCaseOfLetter, flag::insert },
{ changeCaseOfWord, flag::insert },
{ complete, flag::insert|flag::unselect },
{ deleteBackward, flag::erase },
{ deleteBackwardByDecomposingPreviousCharacter, flag::insert },
{ deleteForward, flag::erase },
{ deleteSubWordBackward, flag::erase },
{ deleteSubWordForward, flag::erase },
{ deleteToBeginningOfLine, flag::erase },
{ deleteToBeginningOfParagraph, flag::erase },
{ deleteToEndOfLine, flag::erase },
{ deleteToEndOfParagraph, flag::erase },
{ deleteToMark, flag::mark },
{ deleteWordBackward, flag::erase },
{ deleteWordForward, flag::erase },
{ indent, flag::insert },
{ insertBacktab, flag::insert|flag::unselect },
{ insertNewline, flag::insert|flag::unselect },
{ insertNewlineIgnoringFieldEditor, flag::insert|flag::unselect },
{ insertTab, flag::insert|flag::unselect },
{ insertTabIgnoringFieldEditor, flag::insert|flag::unselect },
{ moveBackward, flag::simple_movement },
{ moveDown, flag::require_continuous_selection|flag::simple_movement },
{ moveDownAndModifySelection, flag::require_continuous_selection },
{ moveForward, flag::simple_movement },
{ moveParagraphBackwardAndModifySelection, flag::require_continuous_selection },
{ moveParagraphForwardAndModifySelection, flag::require_continuous_selection },
{ moveToBeginningOfColumn, flag::require_continuous_selection },
{ moveToBeginningOfColumnAndModifySelection, flag::require_continuous_selection },
{ moveToBeginningOfDocument, flag::require_continuous_selection },
{ moveToBeginningOfDocumentAndModifySelection, flag::require_continuous_selection },
{ moveToBeginningOfParagraph, flag::require_continuous_selection },
{ moveToEndOfColumn, flag::require_continuous_selection },
{ moveToEndOfColumnAndModifySelection, flag::require_continuous_selection },
{ moveToEndOfDocument, flag::require_continuous_selection },
{ moveToEndOfDocumentAndModifySelection, flag::require_continuous_selection },
{ moveToEndOfParagraph, flag::require_continuous_selection },
{ moveUp, flag::require_continuous_selection|flag::simple_movement },
{ moveUpAndModifySelection, flag::require_continuous_selection },
{ moveWordBackward, flag::simple_movement },
{ moveWordForward, flag::simple_movement },
{ nextCompletion, flag::insert|flag::unselect },
{ pageDown, flag::require_continuous_selection },
{ pageDownAndModifySelection, flag::require_continuous_selection },
{ pageUp, flag::require_continuous_selection },
{ pageUpAndModifySelection, flag::require_continuous_selection },
{ previousCompletion, flag::insert|flag::unselect },
{ reformatText, flag::insert },
{ reformatTextAndJustify, flag::insert },
{ selectToMark, flag::mark },
{ shiftLeft, flag::insert },
{ shiftRight, flag::insert },
{ swapWithMark, flag::mark },
{ transpose, flag::insert },
{ unwrapText, flag::insert },
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.