Class: RBS::Writer
- Inherits:
-
Object
- Object
- RBS::Writer
- Defined in:
- lib/rbs/writer.rb
Instance Attribute Summary collapse
-
#indentation ⇒ Object
readonly
Returns the value of attribute indentation.
-
#out ⇒ Object
readonly
Returns the value of attribute out.
Instance Method Summary collapse
- #attribute(kind, attr) ⇒ Object
- #indent(size = 2) ⇒ Object
-
#initialize(out:) ⇒ Writer
constructor
A new instance of Writer.
- #method_name(name) ⇒ Object
- #name_and_args(name, args) ⇒ Object
- #name_and_params(name, params) ⇒ Object
- #prefix ⇒ Object
- #preserve!(preserve: true) ⇒ Object
- #preserve? ⇒ Boolean
- #preserve_empty_line(prev, decl) ⇒ Object
- #put_lines(lines, leading_spaces:) ⇒ Object
- #puts(string = "") ⇒ Object
- #write(decls) ⇒ Object
- #write_annotation(annotations) ⇒ Object
- #write_comment(comment) ⇒ Object
- #write_decl(decl) ⇒ Object
- #write_def(member) ⇒ Object
- #write_loc_source(located) ⇒ Object
- #write_member(member) ⇒ Object
Constructor Details
#initialize(out:) ⇒ Writer
Returns a new instance of Writer.
6 7 8 9 10 |
# File 'lib/rbs/writer.rb', line 6 def initialize(out:) @out = out @indentation = [] @preserve = false end |
Instance Attribute Details
#indentation ⇒ Object (readonly)
Returns the value of attribute indentation.
4 5 6 |
# File 'lib/rbs/writer.rb', line 4 def indentation @indentation end |
#out ⇒ Object (readonly)
Returns the value of attribute out.
3 4 5 |
# File 'lib/rbs/writer.rb', line 3 def out @out end |
Instance Method Details
#attribute(kind, attr) ⇒ Object
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 |
# File 'lib/rbs/writer.rb', line 312 def attribute(kind, attr) visibility = case attr.visibility when :public "public " when :private "private " else "" end var = case attr.ivar_name when nil "" when false "()" else "(#{attr.ivar_name})" end receiver = case attr.kind when :singleton "self." when :instance "" end "#{visibility}attr_#{kind} #{receiver}#{attr.name}#{var}: #{attr.type}" end |
#indent(size = 2) ⇒ Object
21 22 23 24 25 26 |
# File 'lib/rbs/writer.rb', line 21 def indent(size = 2) indentation.push(" " * size) yield ensure indentation.pop end |
#method_name(name) ⇒ Object
242 243 244 245 246 247 248 249 250 251 252 253 |
# File 'lib/rbs/writer.rb', line 242 def method_name(name) s = name.to_s case s when /\A(_?)[A-Za-z_]\w*(\?|!|=)?\Z/ s when *%w(| ^ & <=> == === =~ > >= < <= << >> + - * / % ** ~ +@ -@ [] []= ` ! != !~) s else "`#{s}`" end end |
#name_and_args(name, args) ⇒ Object
168 169 170 171 172 173 174 175 176 |
# File 'lib/rbs/writer.rb', line 168 def name_and_args(name, args) if name && args if args.empty? "#{name}" else "#{name}[#{args.join(", ")}]" end end end |
#name_and_params(name, params) ⇒ Object
156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/rbs/writer.rb', line 156 def name_and_params(name, params) if params.empty? "#{name}" else ps = params.each.map do |param| param.to_s end "#{name}[#{ps.join(", ")}]" end end |
#prefix ⇒ Object
28 29 30 |
# File 'lib/rbs/writer.rb', line 28 def prefix indentation.join() end |
#preserve!(preserve: true) ⇒ Object
16 17 18 19 |
# File 'lib/rbs/writer.rb', line 16 def preserve!(preserve: true) @preserve = preserve self end |
#preserve? ⇒ Boolean
12 13 14 |
# File 'lib/rbs/writer.rb', line 12 def preserve? @preserve end |
#preserve_empty_line(prev, decl) ⇒ Object
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 |
# File 'lib/rbs/writer.rb', line 342 def preserve_empty_line(prev, decl) # @type var decl: _Located return unless prev if (_ = decl).respond_to?(:comment) if comment = (_ = decl).comment decl = comment end end prev_loc = prev.location decl_loc = decl.location if prev_loc && decl_loc prev_end_line = prev_loc.end_line start_line = decl_loc.start_line if start_line - prev_end_line > 1 puts end else # When the signature is not constructed by the parser, # it always inserts an empty line. puts end end |
#put_lines(lines, leading_spaces:) ⇒ Object
178 179 180 181 182 183 184 185 186 |
# File 'lib/rbs/writer.rb', line 178 def put_lines(lines, leading_spaces:) lines.each_line.with_index do |line, index| line.chomp! line.rstrip! line.sub!(/\A( {,#{leading_spaces}})/, '') if index > 0 puts line end end |
#puts(string = "") ⇒ Object
32 33 34 35 36 37 38 |
# File 'lib/rbs/writer.rb', line 32 def puts(string = "") if string.size > 0 @out.puts("#{prefix}#{string}") else @out.puts end end |
#write(decls) ⇒ Object
71 72 73 74 75 76 77 78 |
# File 'lib/rbs/writer.rb', line 71 def write(decls) [nil, *decls].each_cons(2) do |prev, decl| raise unless decl preserve_empty_line(prev, decl) write_decl decl end end |
#write_annotation(annotations) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/rbs/writer.rb', line 40 def write_annotation(annotations) annotations.each do |annotation| string = annotation.string case when string !~ /\}/ puts "%a{#{string}}" when string !~ /\)/ puts "%a(#{string})" when string !~ /\]/ puts "%a[#{string}]" when string !~ /\>/ puts "%a<#{string}>" when string !~ /\|/ puts "%a|#{string}|" end end end |
#write_comment(comment) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/rbs/writer.rb', line 58 def write_comment(comment) if comment comment.string.lines.each do |line| line = line.chomp unless line.empty? puts "# #{line}" else puts "#" end end end end |
#write_decl(decl) ⇒ Object
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 |
# File 'lib/rbs/writer.rb', line 80 def write_decl(decl) case decl when AST::Declarations::Class super_class = if super_class = decl.super_class " < #{name_and_args(super_class.name, super_class.args)}" end write_comment decl.comment write_annotation decl.annotations puts "class #{name_and_params(decl.name, decl.type_params)}#{super_class}" indent do [nil, *decl.members].each_cons(2) do |prev, member| raise unless member preserve_empty_line prev, member write_member member end end puts "end" when AST::Declarations::Module self_type = unless decl.self_types.empty? " : #{decl.self_types.join(", ")}" end write_comment decl.comment write_annotation decl.annotations puts "module #{name_and_params(decl.name, decl.type_params)}#{self_type}" indent do decl.members.each.with_index do |member, index| if index > 0 puts end write_member member end end puts "end" when AST::Declarations::Constant write_comment decl.comment puts "#{decl.name}: #{decl.type}" when AST::Declarations::Global write_comment decl.comment puts "#{decl.name}: #{decl.type}" when AST::Declarations::Alias write_comment decl.comment write_annotation decl.annotations write_loc_source(decl) { puts "type #{name_and_params(decl.name, decl.type_params)} = #{decl.type}" } when AST::Declarations::Interface write_comment decl.comment write_annotation decl.annotations puts "interface #{name_and_params(decl.name, decl.type_params)}" indent do decl.members.each.with_index do |member, index| if index > 0 puts end write_member member end end puts "end" end end |
#write_def(member) ⇒ Object
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 |
# File 'lib/rbs/writer.rb', line 263 def write_def(member) visibility = case member.visibility when :public "public " when :private "private " else "" end name = case member.kind when :instance "#{method_name(member.name)}" when :singleton_instance "self?.#{method_name(member.name)}" when :singleton "self.#{method_name(member.name)}" end string = "" prefix = "#{visibility}def #{name}:" padding = " " * (prefix.size-1) string << prefix member.types.each.with_index do |type, index| if index > 0 string << padding string << "|" end string << " #{type}\n" end if member.overload if member.types.size > 0 string << padding string << "|" end string << " ...\n" end string.each_line do |line| puts line.chomp end end |
#write_loc_source(located) ⇒ Object
255 256 257 258 259 260 261 |
# File 'lib/rbs/writer.rb', line 255 def write_loc_source(located) if preserve? && loc = located.location put_lines(loc.source, leading_spaces: loc.start_column) else yield end end |
#write_member(member) ⇒ Object
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 |
# File 'lib/rbs/writer.rb', line 188 def write_member(member) case member when AST::Members::Include write_comment member.comment write_annotation member.annotations puts "include #{name_and_args(member.name, member.args)}" when AST::Members::Extend write_comment member.comment write_annotation member.annotations puts "extend #{name_and_args(member.name, member.args)}" when AST::Members::Prepend write_comment member.comment write_annotation member.annotations puts "prepend #{name_and_args(member.name, member.args)}" when AST::Members::AttrAccessor write_comment member.comment write_annotation member.annotations puts "#{attribute(:accessor, member)}" when AST::Members::AttrReader write_comment member.comment write_annotation member.annotations puts "#{attribute(:reader, member)}" when AST::Members::AttrWriter write_comment member.comment write_annotation member.annotations puts "#{attribute(:writer, member)}" when AST::Members::Public puts "public" when AST::Members::Private puts "private" when AST::Members::Alias write_comment member.comment write_annotation member.annotations new_name = member.singleton? ? "self.#{member.new_name}" : member.new_name old_name = member.singleton? ? "self.#{member.old_name}" : member.old_name puts "alias #{new_name} #{old_name}" when AST::Members::InstanceVariable write_comment member.comment puts "#{member.name}: #{member.type}" when AST::Members::ClassInstanceVariable write_comment member.comment puts "self.#{member.name}: #{member.type}" when AST::Members::ClassVariable write_comment member.comment puts "#{member.name}: #{member.type}" when AST::Members::MethodDefinition write_comment member.comment write_annotation member.annotations write_loc_source(member) { write_def member } else write_decl member end end |