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
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
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
|
# This script generates a tags file using Prism to parse the Ruby files.
require "prism"
# This visitor is responsible for visiting the nodes in the AST and generating
# the appropriate tags. The tags are stored in the entries array as strings.
class TagsVisitor < Prism::Visitor
# This represents an entry in the tags file, which is a tab-separated line. It
# houses the logic for how an entry is constructed.
class Entry
attr_reader :parts
def initialize(name, filepath, pattern, type)
@parts = [name, filepath, pattern, type]
end
def attribute(key, value)
parts << "#{key}:#{value}"
end
def attribute_class(nesting, names)
return if nesting.empty? && names.length == 1
attribute("class", [*nesting, names].flatten.tap(&:pop).join("."))
end
def attribute_inherits(names)
attribute("inherits", names.join(".")) if names
end
def to_line
parts.join("\t")
end
end
private_constant :Entry
attr_reader :entries, :filepath, :lines, :nesting, :singleton
# Initialize the visitor with the given parameters. The first three parameters
# are constant throughout the visit, while the last two are controlled by the
# visitor as it traverses the AST. These are treated as immutable by virtue of
# the visit methods constructing new visitors when they need to change.
def initialize(entries, filepath, lines, nesting = [], singleton = false)
@entries = entries
@filepath = filepath
@lines = lines
@nesting = nesting
@singleton = singleton
end
# Visit a method alias node and generate the appropriate tags.
#
# alias m2 m1
#
def visit_alias_method_node(node)
enter(node.new_name.unescaped.to_sym, node, "a") do |entry|
entry.attribute_class(nesting, [nil])
end
super
end
# Visit a method call to attr_reader, attr_writer, or attr_accessor without a
# receiver and generate the appropriate tags. Note that this ignores the fact
# that these methods could be overridden, which is a limitation of this
# script.
#
# attr_accessor :m1
#
def visit_call_node(node)
if !node.receiver && %i[attr_reader attr_writer attr_accessor].include?(name = node.name)
(node.arguments&.arguments || []).grep(Prism::SymbolNode).each do |argument|
if name != :attr_writer
enter(:"#{argument.unescaped}", argument, singleton ? "F" : "f") do |entry|
entry.attribute_class(nesting, [nil])
end
end
if name != :attr_reader
enter(:"#{argument.unescaped}=", argument, singleton ? "F" : "f") do |entry|
entry.attribute_class(nesting, [nil])
end
end
end
end
super
end
# Visit a class node and generate the appropriate tags.
#
# class C1
# end
#
def visit_class_node(node)
if (names = names_for(node.constant_path))
enter(names.last, node, "c") do |entry|
entry.attribute_class(nesting, names)
entry.attribute_inherits(names_for(node.superclass))
end
node.body&.accept(copy_visitor([*nesting, names], singleton))
end
end
# Visit a constant path write node and generate the appropriate tags.
#
# C1::C2 = 1
#
def visit_constant_path_write_node(node)
if (names = names_for(node.target))
enter(names.last, node, "C") do |entry|
entry.attribute_class(nesting, names)
end
end
super
end
# Visit a constant write node and generate the appropriate tags.
#
# C1 = 1
#
def visit_constant_write_node(node)
enter(node.name, node, "C") do |entry|
entry.attribute_class(nesting, [nil])
end
super
end
# Visit a method definition node and generate the appropriate tags.
#
# def m1; end
#
def visit_def_node(node)
enter(node.name, node, (node.receiver || singleton) ? "F" : "f") do |entry|
entry.attribute_class(nesting, [nil])
end
super
end
# Visit a module node and generate the appropriate tags.
#
# module M1
# end
#
def visit_module_node(node)
if (names = names_for(node.constant_path))
enter(names.last, node, "m") do |entry|
entry.attribute_class(nesting, names)
end
node.body&.accept(copy_visitor([*nesting, names], singleton))
end
end
# Visit a singleton class node and generate the appropriate tags.
#
# class << self
# end
#
def visit_singleton_class_node(node)
case node.expression
when Prism::SelfNode
node.body&.accept(copy_visitor(nesting, true))
when Prism::ConstantReadNode, Prism::ConstantPathNode
if (names = names_for(node.expression))
node.body&.accept(copy_visitor([*nesting, names], true))
end
else
node.body&.accept(copy_visitor([*nesting, nil], true))
end
end
private
# Generate a new visitor with the given dynamic options. The static options
# are copied over automatically.
def copy_visitor(nesting, singleton)
TagsVisitor.new(entries, filepath, lines, nesting, singleton)
end
# Generate a new entry for the given name, node, and type and add it into the
# list of entries. The block is used to add additional attributes to the
# entry.
def enter(name, node, type)
line = lines[node.location.start_line - 1].chomp
pattern = "/^#{line.gsub("\\", "\\\\\\\\").gsub("/", "\\/")}$/;\""
entry = Entry.new(name, filepath, pattern, type)
yield entry
entries << entry.to_line
end
# Retrieve the names for the given node. This is used to construct the class
# attribute for the tags.
def names_for(node)
case node
when Prism::ConstantPathNode
names = names_for(node.parent)
return unless names
names << node.name
when Prism::ConstantReadNode
[node.name]
when Prism::SelfNode
[:self]
else
# dynamic
end
end
end
# Parse the Ruby file and visit all of the nodes in the resulting AST. Once all
# of the nodes have been visited, the entries array should be populated with the
# tags.
result = Prism.parse_stream(DATA)
result.value.accept(TagsVisitor.new(entries = [], __FILE__, result.source.lines))
# Print the tags to STDOUT.
puts "!_TAG_FILE_FORMAT 2 /extended format; --format=1 will not append ;\" to lines/"
puts "!_TAG_FILE_SORTED 1 /0=unsorted, 1=sorted, 2=foldcase/"
puts entries.sort
# =>
# !_TAG_FILE_FORMAT 2 /extended format; --format=1 will not append ;" to lines/
# !_TAG_FILE_SORTED 1 /0=unsorted, 1=sorted, 2=foldcase/
# C1 sample/prism/make_tags.rb /^ class C1$/;" c class:M1.M2
# C2 sample/prism/make_tags.rb /^ class C2 < Object$/;" c class:M1.M2.C1 inherits:Object
# C6 sample/prism/make_tags.rb /^ C6 = 1$/;" C class:M1
# C7 sample/prism/make_tags.rb /^ C7 = 2$/;" C class:M1
# C9 sample/prism/make_tags.rb /^ C8::C9 = 3$/;" C class:M1.C8
# M1 sample/prism/make_tags.rb /^module M1$/;" m
# M2 sample/prism/make_tags.rb /^ module M2$/;" m class:M1
# M4 sample/prism/make_tags.rb /^ module M3::M4$/;" m class:M1.M3
# M5 sample/prism/make_tags.rb /^ module self::M5$/;" m class:M1.self
# m1 sample/prism/make_tags.rb /^ def m1; end$/;" f class:M1.M2.C1.C2
# m10 sample/prism/make_tags.rb /^ attr_accessor :m10, :m11$/;" f class:M1.M3.M4
# m10= sample/prism/make_tags.rb /^ attr_accessor :m10, :m11$/;" f class:M1.M3.M4
# m11 sample/prism/make_tags.rb /^ attr_accessor :m10, :m11$/;" f class:M1.M3.M4
# m11= sample/prism/make_tags.rb /^ attr_accessor :m10, :m11$/;" f class:M1.M3.M4
# m12 sample/prism/make_tags.rb /^ attr_reader :m12, :m13, :m14$/;" f class:M1.M3.M4
# m13 sample/prism/make_tags.rb /^ attr_reader :m12, :m13, :m14$/;" f class:M1.M3.M4
# m14 sample/prism/make_tags.rb /^ attr_reader :m12, :m13, :m14$/;" f class:M1.M3.M4
# m15= sample/prism/make_tags.rb /^ attr_writer :m15$/;" f class:M1.M3.M4
# m2 sample/prism/make_tags.rb /^ def m2; end$/;" f class:M1.M2.C1.C2
# m3 sample/prism/make_tags.rb /^ alias m3 m1$/;" a class:M1.M2.C1.C2
# m4 sample/prism/make_tags.rb /^ alias :m4 :m2$/;" a class:M1.M2.C1.C2
# m5 sample/prism/make_tags.rb /^ def self.m5; end$/;" F class:M1.M2.C1.C2
# m6 sample/prism/make_tags.rb /^ def m6; end$/;" F class:M1.M2.C1.C2
# m7 sample/prism/make_tags.rb /^ def m7; end$/;" F class:M1.M2.C1.C2.C3
# m8 sample/prism/make_tags.rb /^ def m8; end$/;" F class:M1.M2.C1.C2.C4.C5
# m9 sample/prism/make_tags.rb /^ def m9; end$/;" F class:M1.M2.C1.C2.
__END__
module M1
module M2
class C1
class C2 < Object
def m1; end
def m2; end
alias m3 m1
alias :m4 :m2
def self.m5; end
class << self
def m6; end
end
class << C3
def m7; end
end
class << C4::C5
def m8; end
end
class << c
def m9; end
end
end
end
end
module M3::M4
attr_accessor :m10, :m11
attr_reader :m12, :m13, :m14
attr_writer :m15
end
module self::M5
end
C6 = 1
C7 = 2
C8::C9 = 3
end
|