-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy patherb_formatter.rb
More file actions
185 lines (157 loc) · 4.69 KB
/
Copy patherb_formatter.rb
File metadata and controls
185 lines (157 loc) · 4.69 KB
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
# frozen_string_literal: true
require "erb"
class CustomScanner < ERB::Compiler::TrimScanner
def initialize(src)
super(src, "<>", false)
@scan_reg = /(.*?)(%>\r?\n|#{(stags + etags).join("|")}|\n|\z)/m
end
def stags
["<%-", "<%==", "<%+={0,2}"] + super
end
def etags
super + ["-%>"]
end
end
class Rufo::ErbFormatter
def self.format(code, **options)
new(code, **options).format
end
attr_reader :result
def initialize(code, **options)
@options = options
@scanner = CustomScanner.new(code)
@code_mode = false
@current_lineno = 0
@current_column = 0
end
def format
out = []
process_erb do |(type, content)|
if type == :code && !erb_comment?(content)
formatted_code = process_code(content)
indented_code = formatted_code.lines.join(" " * current_column)
out << " #{indented_code} "
else
out << content
end
update_lineno(out.last)
update_column(out.last)
end
@result = out.join("")
end
private
attr_reader :scanner, :code_mode
attr_accessor :current_lineno, :current_column
def update_lineno(token)
lines = token.count("\n")
if lines > 0
self.current_lineno = current_lineno + lines
end
end
def update_column(token)
last_newline_index = token.rindex("\n")
if last_newline_index == nil
self.current_column = current_column + token.length
else
self.current_column = token[last_newline_index..-1].length
end
end
def process_erb
code = []
scanner.scan do |token|
if token.is_a?(String) && token.end_with?("%>")
disable_code_mode
yield [:code, code.join("")]
yield [:text, token]
code = []
elsif code_mode
code << token
elsif token == :cr
yield [:text, "\n"]
else
yield [:text, token]
end
enable_code_mode if token.is_a?(String) && token.start_with?("<%")
end
end
def process_code(code_str)
sexps = Ripper.sexp(code_str) || Rufo::Parser.sexp_unparsable_code(code_str)
if sexps.nil?
prefix, suffix = determine_code_wrappers(code_str)
end
result = format_code("#{prefix} " + code_str + " #{suffix}")
unless suffix.nil?
result = result.chomp(suffix)
end
unless prefix.nil?
result = result.sub(prefix, "")
end
result.strip
end
def format_affix(affix, levels, type)
string = ""
case type
when :prefix
count = 0
while count < levels
count += 1
string += (" " * Rufo::Formatter::INDENT_SIZE * (count > 0 ? count - 1 : 0)) + affix
string += "\n" if count < levels
end
when :suffix
count = levels
while count > 0
count -= 1
string += "\n"
string += (" " * Rufo::Formatter::INDENT_SIZE * (count > 0 ? count - 1 : 0)) + affix
end
end
string
end
CODE_BLOCK_KEYWORDS = %w[BEGIN END begin case class def do else elsif end ensure for if module rescue unless until while]
private_constant :CODE_BLOCK_KEYWORDS
def code_block_token?(token)
_, kind, value = token
kind == :on_kw && CODE_BLOCK_KEYWORDS.include?(value)
end
def determine_code_wrappers(code_str)
keywords = Ripper.lex("#{code_str}").filter { |lex_token| code_block_token?(lex_token) }
lexical_tokens = keywords.map { |lex_token| lex_token[3].to_s }
state_tally = lexical_tokens.group_by(&:itself).transform_values(&:count)
beg_token = state_tally["BEG"] || state_tally["EXPR_BEG"] || 0
end_token = state_tally["END"] || state_tally["EXPR_END"] || 0
depth = beg_token - end_token
if depth > 0
affix = format_affix("end", depth.abs, :suffix)
return nil, affix if Ripper.sexp("#{code_str}#{affix}")
end
return nil, "}" if Ripper.sexp("#{code_str} }")
return "{", nil if Ripper.sexp("{ #{code_str}")
if depth < 0
affix = format_affix("begin", depth.abs, :prefix)
return affix, nil if Ripper.sexp("#{affix}#{code_str}")
end
return "begin\n", "\nend" if Ripper.sexp("begin\n#{code_str}\nend")
return "if a\n", "\nend" if Ripper.sexp("if a\n#{code_str}\nend")
return "case a\n", "\nend" if Ripper.sexp("case a\n#{code_str}\nend")
return nil, "\nwhen nil\nend" if Ripper.sexp("#{code_str}\nwhen nil\nend")
raise_syntax_error!(code_str)
end
def raise_syntax_error!(code_str)
format_code(code_str)
rescue Rufo::SyntaxError => e
raise Rufo::SyntaxError.new(e.message, current_lineno + e.lineno)
end
def format_code(str)
Rufo::Formatter.format(str, **@options).chomp
end
def enable_code_mode
@code_mode = true
end
def disable_code_mode
@code_mode = false
end
def erb_comment?(code)
code.start_with?("#")
end
end