Class: Dispersion::Annotator

Inherits:
Prism::Visitor
  • Object
show all
Defined in:
lib/dispersion/annotator.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(code) ⇒ Annotator

Returns a new instance of Annotator.



11
12
13
14
# File 'lib/dispersion/annotator.rb', line 11

def initialize(code)
	@code = code
	@annotations = Array.new(code.count("\n")) { [] }
end

Class Method Details

.call(code) ⇒ Object



7
8
9
# File 'lib/dispersion/annotator.rb', line 7

def self.call(code)
	new(code).call
end

Instance Method Details

#callObject



16
17
18
19
20
21
22
# File 'lib/dispersion/annotator.rb', line 16

def call
	parsed = Prism.parse(@code)
	parsed.comments.each { |c| visit_comment(c) }
	visit(parsed.value)

	@annotations.map(&:sort!)
end

#highlight(type, loc) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/dispersion/annotator.rb', line 24

def highlight(type, loc)
	return unless loc
	return if loc.start_line == loc.end_line && loc.start_column == loc.end_column

	@annotations[loc.start_line - 1] << Dispersion::Annotation.new(
		type,
		loc.start_character_column,
	)

	@annotations[loc.end_line - 1] << Dispersion::Annotation.new(
		:reset,
		loc.end_character_column,
	)
end

#visit_alternation_pattern_node(node) ⇒ Object



312
313
314
315
# File 'lib/dispersion/annotator.rb', line 312

def visit_alternation_pattern_node(node)
	highlight :operator, node.operator_loc
	super
end

#visit_and_node(node) ⇒ Object



187
188
189
190
# File 'lib/dispersion/annotator.rb', line 187

def visit_and_node(node)
	highlight :operator, node.operator_loc
	super
end

#visit_array_node(node) ⇒ Object



263
264
265
266
267
# File 'lib/dispersion/annotator.rb', line 263

def visit_array_node(node)
	highlight :bracket, node.opening_loc
	highlight :bracket, node.closing_loc
	super
end

#visit_array_pattern_node(node) ⇒ Object



306
307
308
309
310
# File 'lib/dispersion/annotator.rb', line 306

def visit_array_pattern_node(node)
	highlight :bracket, node.opening_loc
	highlight :bracket, node.closing_loc
	super
end

#visit_assoc_node(node) ⇒ Object



269
270
271
272
# File 'lib/dispersion/annotator.rb', line 269

def visit_assoc_node(node)
	highlight :operator, node.operator_loc
	super
end

#visit_block_argument_node(node) ⇒ Object



182
183
184
185
# File 'lib/dispersion/annotator.rb', line 182

def visit_block_argument_node(node)
	highlight :operator, node.operator_loc
	super
end

#visit_block_node(node) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/dispersion/annotator.rb', line 118

def visit_block_node(node)
	if node.opening_loc.length == 1
		highlight :bracket, node.opening_loc
	else
		highlight :keyword, node.opening_loc
	end

	if node.closing_loc.length == 1
		highlight :bracket, node.closing_loc
	else
		highlight :keyword, node.closing_loc
	end

	super
end

#visit_block_parameter_node(node) ⇒ Object



225
226
227
228
# File 'lib/dispersion/annotator.rb', line 225

def visit_block_parameter_node(node)
	highlight :operator, node.operator_loc
	super
end

#visit_call_node(node) ⇒ Object



97
98
99
100
# File 'lib/dispersion/annotator.rb', line 97

def visit_call_node(node)
	highlight :call, node.message_loc
	super
end

#visit_capture_pattern_node(node) ⇒ Object



328
329
330
331
# File 'lib/dispersion/annotator.rb', line 328

def visit_capture_pattern_node(node)
	highlight :operator, node.operator_loc
	super
end

#visit_case_match_node(node) ⇒ Object



140
141
142
143
144
# File 'lib/dispersion/annotator.rb', line 140

def visit_case_match_node(node)
	highlight :keyword, node.case_keyword_loc
	highlight :keyword, node.end_keyword_loc
	super
end

#visit_case_node(node) ⇒ Object



134
135
136
137
138
# File 'lib/dispersion/annotator.rb', line 134

def visit_case_node(node)
	highlight :keyword, node.case_keyword_loc
	highlight :keyword, node.end_keyword_loc
	super
end

#visit_class_node(node) ⇒ Object



91
92
93
94
95
# File 'lib/dispersion/annotator.rb', line 91

def visit_class_node(node)
	highlight :keyword, node.class_keyword_loc
	highlight :keyword, node.end_keyword_loc
	super
end

#visit_comment(node) ⇒ Object



39
40
41
# File 'lib/dispersion/annotator.rb', line 39

def visit_comment(node)
	highlight :comment, node.location
end

#visit_constant_read_node(node) ⇒ Object



86
87
88
89
# File 'lib/dispersion/annotator.rb', line 86

def visit_constant_read_node(node)
	highlight :constant, node.location
	super
end

#visit_def_node(node) ⇒ Object



43
44
45
46
47
48
49
50
# File 'lib/dispersion/annotator.rb', line 43

def visit_def_node(node)
	highlight :keyword, node.def_keyword_loc
	highlight :keyword, node.end_keyword_loc
	highlight :call, node.name_loc
	highlight :bracket, node.lparen_loc
	highlight :bracket, node.rparen_loc
	super
end

#visit_else_node(node) ⇒ Object



170
171
172
173
174
175
176
177
178
179
180
# File 'lib/dispersion/annotator.rb', line 170

def visit_else_node(node)
	if node.else_keyword.length == 1
		highlight :operator, node.else_keyword_loc
	else
		highlight :keyword, node.else_keyword_loc
	end

	highlight :keyword, node.end_keyword_loc

	super
end

#visit_embedded_statements_node(node) ⇒ Object



283
284
285
286
287
# File 'lib/dispersion/annotator.rb', line 283

def visit_embedded_statements_node(node)
	highlight :bracket, node.opening_loc
	highlight :bracket, node.closing_loc
	super
end

#visit_float_node(node) ⇒ Object



252
253
254
255
# File 'lib/dispersion/annotator.rb', line 252

def visit_float_node(node)
	highlight :numeric, node.location
	super
end

#visit_forwarding_parameter_node(node) ⇒ Object



274
275
276
277
# File 'lib/dispersion/annotator.rb', line 274

def visit_forwarding_parameter_node(node)
	highlight :operator, node.location
	super
end

#visit_forwarding_super_node(node) ⇒ Object



279
280
281
# File 'lib/dispersion/annotator.rb', line 279

def visit_forwarding_super_node(node)
	highlight :keyword, node.location
end

#visit_hash_node(node) ⇒ Object



257
258
259
260
261
# File 'lib/dispersion/annotator.rb', line 257

def visit_hash_node(node)
	highlight :bracket, node.opening_loc
	highlight :bracket, node.closing_loc
	super
end

#visit_if_node(node) ⇒ Object



152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/dispersion/annotator.rb', line 152

def visit_if_node(node)
	highlight :keyword, node.if_keyword_loc
	highlight :keyword, node.end_keyword_loc

	if node.then_keyword&.length == 1
		highlight :operator, node.then_keyword_loc
	else
		highlight :keyword, node.then_keyword_loc
	end

	super
end

#visit_in_node(node) ⇒ Object



300
301
302
303
304
# File 'lib/dispersion/annotator.rb', line 300

def visit_in_node(node)
	highlight :keyword, node.in_loc
	highlight :keyword, node.then_loc
	super
end

#visit_instance_variable_and_write_node(node) ⇒ Object



69
70
71
72
73
# File 'lib/dispersion/annotator.rb', line 69

def visit_instance_variable_and_write_node(node)
	highlight :ivar, node.name_loc
	highlight :operator, node.operator_loc
	super
end

#visit_instance_variable_or_write_node(node) ⇒ Object



75
76
77
78
79
# File 'lib/dispersion/annotator.rb', line 75

def visit_instance_variable_or_write_node(node)
	highlight :ivar, node.name_loc
	highlight :operator, node.operator_loc
	super
end

#visit_instance_variable_read_node(node) ⇒ Object



81
82
83
84
# File 'lib/dispersion/annotator.rb', line 81

def visit_instance_variable_read_node(node)
	highlight :ivar, node.location
	super
end

#visit_instance_variable_write_node(node) ⇒ Object



63
64
65
66
67
# File 'lib/dispersion/annotator.rb', line 63

def visit_instance_variable_write_node(node)
	highlight :ivar, node.name_loc
	highlight :operator, node.operator_loc
	super
end

#visit_integer_node(node) ⇒ Object



247
248
249
250
# File 'lib/dispersion/annotator.rb', line 247

def visit_integer_node(node)
	highlight :numeric, node.location
	super
end

#visit_interpolated_string_node(node) ⇒ Object



107
108
109
110
111
# File 'lib/dispersion/annotator.rb', line 107

def visit_interpolated_string_node(node)
	highlight :string, node.opening_loc
	highlight :string, node.closing_loc
	super
end

#visit_keyword_rest_parameter_node(node) ⇒ Object



220
221
222
223
# File 'lib/dispersion/annotator.rb', line 220

def visit_keyword_rest_parameter_node(node)
	highlight :operator, node.operator_loc
	super
end

#visit_local_variable_and_write_node(node) ⇒ Object



203
204
205
206
207
# File 'lib/dispersion/annotator.rb', line 203

def visit_local_variable_and_write_node(node)
	highlight :var, node.name_loc
	highlight :operator, node.operator_loc
	super
end

#visit_local_variable_or_write_node(node) ⇒ Object



197
198
199
200
201
# File 'lib/dispersion/annotator.rb', line 197

def visit_local_variable_or_write_node(node)
	highlight :var, node.name_loc
	highlight :operator, node.operator_loc
	super
end

#visit_local_variable_read_node(node) ⇒ Object



58
59
60
61
# File 'lib/dispersion/annotator.rb', line 58

def visit_local_variable_read_node(node)
	highlight :var, node.location
	super
end

#visit_local_variable_write_node(node) ⇒ Object



52
53
54
55
56
# File 'lib/dispersion/annotator.rb', line 52

def visit_local_variable_write_node(node)
	highlight :var, node.name_loc
	highlight :operator, node.operator_loc
	super
end

#visit_module_node(node) ⇒ Object



146
147
148
149
150
# File 'lib/dispersion/annotator.rb', line 146

def visit_module_node(node)
	highlight :keyword, node.module_keyword_loc
	highlight :keyword, node.end_keyword_loc
	super
end

#visit_next_node(node) ⇒ Object



242
243
244
245
# File 'lib/dispersion/annotator.rb', line 242

def visit_next_node(node)
	highlight :keyword, node.keyword_loc
	super
end

#visit_optional_parameter_node(node) ⇒ Object



322
323
324
325
326
# File 'lib/dispersion/annotator.rb', line 322

def visit_optional_parameter_node(node)
	highlight :var, node.name_loc
	highlight :operator, node.operator_loc
	super
end

#visit_or_node(node) ⇒ Object



192
193
194
195
# File 'lib/dispersion/annotator.rb', line 192

def visit_or_node(node)
	highlight :operator, node.operator_loc
	super
end

#visit_parentheses_node(node) ⇒ Object



289
290
291
292
293
# File 'lib/dispersion/annotator.rb', line 289

def visit_parentheses_node(node)
	highlight :bracket, node.opening_loc
	highlight :bracket, node.closing_loc
	super
end

#visit_range_node(node) ⇒ Object



295
296
297
298
# File 'lib/dispersion/annotator.rb', line 295

def visit_range_node(node)
	highlight :operator, node.operator_loc
	super
end

#visit_required_parameter_node(node) ⇒ Object



317
318
319
320
# File 'lib/dispersion/annotator.rb', line 317

def visit_required_parameter_node(node)
	highlight :var, node.location
	super
end

#visit_rescue_node(node) ⇒ Object



209
210
211
212
213
# File 'lib/dispersion/annotator.rb', line 209

def visit_rescue_node(node)
	highlight :keyword, node.keyword_loc
	highlight :keyword, node.operator_loc
	super
end

#visit_rest_parameter_node(node) ⇒ Object



215
216
217
218
# File 'lib/dispersion/annotator.rb', line 215

def visit_rest_parameter_node(node)
	highlight :operator, node.operator_loc
	super
end

#visit_string_node(node) ⇒ Object



102
103
104
105
# File 'lib/dispersion/annotator.rb', line 102

def visit_string_node(node)
	highlight :string, node.location
	super
end

#visit_symbol_node(node) ⇒ Object



113
114
115
116
# File 'lib/dispersion/annotator.rb', line 113

def visit_symbol_node(node)
	highlight :symbol, node.location
	super
end

#visit_until_node(node) ⇒ Object



236
237
238
239
240
# File 'lib/dispersion/annotator.rb', line 236

def visit_until_node(node)
	highlight :keyword, node.keyword_loc
	highlight :keyword, node.closing_loc
	super
end

#visit_when_node(node) ⇒ Object



165
166
167
168
# File 'lib/dispersion/annotator.rb', line 165

def visit_when_node(node)
	highlight :keyword, node.keyword_loc
	super
end

#visit_while_node(node) ⇒ Object



230
231
232
233
234
# File 'lib/dispersion/annotator.rb', line 230

def visit_while_node(node)
	highlight :keyword, node.keyword_loc
	highlight :keyword, node.closing_loc
	super
end