dispersion 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ec8d56fde7ef494b2635dcedec943cf152f726d933f689b2cadaf5c86103281d
4
- data.tar.gz: 35634702904c1aa084fdac4c2bf7c433bafa25686ee2de9cea476a08cdcef9e7
3
+ metadata.gz: c6d839f727b08a163dc893736afaed93d3563f26c5bc500b7ade8fa3b06803f4
4
+ data.tar.gz: 6065a58ba57342d09f600831d521cdce62db0effc33491944075e9c87b79128e
5
5
  SHA512:
6
- metadata.gz: 2e4ebdf10603779a04c21a43e6914c79ce6ddf9372e069058172ffb7db7e1eaaf6f1994a65d847813b842673cb8e9f129bcd197e47f936d6c46bff5c5562da45
7
- data.tar.gz: 0d7ec6d70fb9ec42e5ad9f2e8fd890e5ce1665c026d48f719abb24a723105143504dfd80b73180d85968cac5ba8ec976195163755f21d786247b938a34f90fdb
6
+ metadata.gz: 41dc3511ba6378dca079e6984c2a19ba3013894ae654d7e31483a2d0385939d553c430574b9123fbf277c73f3b8aa5e2389fc60691e6de80e4ae7f5a3b306b51
7
+ data.tar.gz: d7ce2287bc8fe50d0cf03ea5c47ca6e6defd09fa37e7929040c777539431ef955d5fe1b728f9e933b8160968c297fad11f2b6124300556dfe27ced241a102bd4
@@ -3,331 +3,325 @@
3
3
  require "prism"
4
4
 
5
5
  module Dispersion
6
- class Annotator < Prism::Visitor
7
- def self.call(code)
8
- new(code).call
9
- end
10
-
11
- def initialize(code)
12
- @code = code
13
- @annotations = Array.new(code.count("\n")) { [] }
14
- end
15
-
16
- def call
17
- parsed = Prism.parse(@code)
18
- parsed.comments.each { |c| visit_comment(c) }
19
- visit(parsed.value)
20
-
21
- @annotations.map(&:sort!)
22
- end
23
-
24
- def highlight(type, loc)
25
- return unless loc
26
- return if loc.start_line == loc.end_line && loc.start_column == loc.end_column
27
-
28
- @annotations[loc.start_line - 1] << Dispersion::Annotation.new(
29
- type,
30
- loc.start_character_column,
31
- )
32
-
33
- @annotations[loc.end_line - 1] << Dispersion::Annotation.new(
34
- :reset,
35
- loc.end_character_column,
36
- )
37
- end
38
-
39
- def visit_comment(node)
40
- highlight :comment, node.location
41
- end
42
-
43
- def visit_def_node(node)
44
- highlight :keyword, node.def_keyword_loc
45
- highlight :keyword, node.end_keyword_loc
46
- highlight :call, node.name_loc
47
- highlight :bracket, node.lparen_loc
48
- highlight :bracket, node.rparen_loc
49
- super
50
- end
51
-
52
- def visit_local_variable_write_node(node)
53
- highlight :var, node.name_loc
54
- highlight :operator, node.operator_loc
55
- super
56
- end
57
-
58
- def visit_local_variable_read_node(node)
59
- highlight :var, node.location
60
- super
61
- end
62
-
63
- def visit_instance_variable_write_node(node)
64
- highlight :ivar, node.name_loc
65
- highlight :operator, node.operator_loc
66
- super
67
- end
68
-
69
- def visit_instance_variable_and_write_node(node)
70
- highlight :ivar, node.name_loc
71
- highlight :operator, node.operator_loc
72
- super
73
- end
74
-
75
- def visit_instance_variable_or_write_node(node)
76
- highlight :ivar, node.name_loc
77
- highlight :operator, node.operator_loc
78
- super
79
- end
80
-
81
- def visit_instance_variable_read_node(node)
82
- highlight :ivar, node.location
83
- super
84
- end
85
-
86
- def visit_constant_read_node(node)
87
- highlight :constant, node.location
88
- super
89
- end
90
-
91
- def visit_class_node(node)
92
- highlight :keyword, node.class_keyword_loc
93
- highlight :keyword, node.end_keyword_loc
94
- super
95
- end
96
-
97
- def visit_call_node(node)
98
- highlight :call, node.message_loc
99
- super
100
- end
101
-
102
- def visit_string_node(node)
103
- highlight :string, node.location
104
- super
105
- end
106
-
107
- def visit_interpolated_string_node(node)
108
- highlight :string, node.opening_loc
109
- highlight :string, node.closing_loc
110
- super
111
- end
112
-
113
- def visit_symbol_node(node)
114
- highlight :symbol, node.location
115
- super
116
- end
117
-
118
- def visit_block_node(node)
119
- if node.opening_loc.length == 1
120
- highlight :bracket, node.opening_loc
121
- else
122
- highlight :keyword, node.opening_loc
123
- end
124
-
125
- if node.closing_loc.length == 1
126
- highlight :bracket, node.closing_loc
127
- else
128
- highlight :keyword, node.closing_loc
129
- end
130
-
131
- super
132
- end
133
-
134
- def visit_case_node(node)
135
- highlight :keyword, node.case_keyword_loc
136
- highlight :keyword, node.end_keyword_loc
137
- super
138
- end
139
-
140
- def visit_case_match_node(node)
141
- highlight :keyword, node.case_keyword_loc
142
- highlight :keyword, node.end_keyword_loc
143
- super
144
- end
145
-
146
- def visit_module_node(node)
147
- highlight :keyword, node.module_keyword_loc
148
- highlight :keyword, node.end_keyword_loc
149
- super
150
- end
151
-
152
- def visit_if_node(node)
153
- highlight :keyword, node.if_keyword_loc
154
- highlight :keyword, node.end_keyword_loc
155
-
156
- if node.then_keyword&.length == 1
157
- highlight :operator, node.then_keyword_loc
158
- else
159
- highlight :keyword, node.then_keyword_loc
160
- end
161
-
162
- super
163
- end
164
-
165
- def visit_when_node(node)
166
- highlight :keyword, node.keyword_loc
167
- super
168
- end
169
-
170
- def visit_else_node(node)
171
- if node.else_keyword.length == 1
172
- highlight :operator, node.else_keyword_loc
173
- else
174
- highlight :keyword, node.else_keyword_loc
175
- end
176
-
177
- highlight :keyword, node.end_keyword_loc
178
-
179
- super
180
- end
181
-
182
- def visit_block_argument_node(node)
183
- highlight :operator, node.operator_loc
184
- super
185
- end
186
-
187
- def visit_and_node(node)
188
- highlight :operator, node.operator_loc
189
- super
190
- end
191
-
192
- def visit_or_node(node)
193
- highlight :operator, node.operator_loc
194
- super
195
- end
196
-
197
- def visit_local_variable_or_write_node(node)
198
- highlight :var, node.name_loc
199
- highlight :operator, node.operator_loc
200
- super
201
- end
202
-
203
- def visit_local_variable_and_write_node(node)
204
- highlight :var, node.name_loc
205
- highlight :operator, node.operator_loc
206
- super
207
- end
208
-
209
- def visit_rescue_node(node)
210
- highlight :keyword, node.keyword_loc
211
- highlight :keyword, node.operator_loc
212
- super
213
- end
214
-
215
- def visit_rest_parameter_node(node)
216
- highlight :operator, node.operator_loc
217
- super
218
- end
219
-
220
- def visit_keyword_rest_parameter_node(node)
221
- highlight :operator, node.operator_loc
222
- super
223
- end
224
-
225
- def visit_block_parameter_node(node)
226
- highlight :operator, node.operator_loc
227
- super
228
- end
229
-
230
- def visit_while_node(node)
231
- highlight :keyword, node.keyword_loc
232
- highlight :keyword, node.closing_loc
233
- super
234
- end
235
-
236
- def visit_until_node(node)
237
- highlight :keyword, node.keyword_loc
238
- highlight :keyword, node.closing_loc
239
- super
240
- end
241
-
242
- def visit_next_node(node)
243
- highlight :keyword, node.keyword_loc
244
- super
245
- end
246
-
247
- def visit_integer_node(node)
248
- highlight :numeric, node.location
249
- super
250
- end
251
-
252
- def visit_float_node(node)
253
- highlight :numeric, node.location
254
- super
255
- end
256
-
257
- def visit_hash_node(node)
258
- highlight :bracket, node.opening_loc
259
- highlight :bracket, node.closing_loc
260
- super
261
- end
262
-
263
- def visit_array_node(node)
264
- highlight :bracket, node.opening_loc
265
- highlight :bracket, node.closing_loc
266
- super
267
- end
268
-
269
- def visit_assoc_node(node)
270
- highlight :operator, node.operator_loc
271
- super
272
- end
273
-
274
- def visit_forwarding_parameter_node(node)
275
- highlight :operator, node.location
276
- super
277
- end
278
-
279
- def visit_forwarding_super_node(node)
280
- highlight :keyword, node.location
281
- end
282
-
283
- def visit_embedded_statements_node(node)
284
- highlight :bracket, node.opening_loc
285
- highlight :bracket, node.closing_loc
286
- super
287
- end
288
-
289
- def visit_parentheses_node(node)
290
- highlight :bracket, node.opening_loc
291
- highlight :bracket, node.closing_loc
292
- super
293
- end
294
-
295
- def visit_range_node(node)
296
- highlight :operator, node.operator_loc
297
- super
298
- end
299
-
300
- def visit_in_node(node)
301
- highlight :keyword, node.in_loc
302
- highlight :keyword, node.then_loc
303
- super
304
- end
305
-
306
- def visit_array_pattern_node(node)
307
- highlight :bracket, node.opening_loc
308
- highlight :bracket, node.closing_loc
309
- super
310
- end
311
-
312
- def visit_alternation_pattern_node(node)
313
- highlight :operator, node.operator_loc
314
- super
315
- end
316
-
317
- def visit_required_parameter_node(node)
318
- highlight :var, node.location
319
- super
320
- end
321
-
322
- def visit_optional_parameter_node(node)
323
- highlight :var, node.name_loc
324
- highlight :operator, node.operator_loc
325
- super
326
- end
327
-
328
- def visit_capture_pattern_node(node)
329
- highlight :operator, node.operator_loc
330
- super
331
- end
332
- end
6
+ class Annotator < Prism::Visitor
7
+ def self.call(code)
8
+ new(code).call
9
+ end
10
+
11
+ def initialize(code)
12
+ @code = code
13
+ @annotations = []
14
+ end
15
+
16
+ def call
17
+ @prev_end = 0
18
+ parsed = Prism.parse(@code)
19
+ parsed.comments.each { |c| visit_comment(c) }
20
+ visit(parsed.value)
21
+
22
+ @annotations.sort_by(&:first)
23
+ end
24
+
25
+ def highlight(token, loc)
26
+ return unless loc
27
+ return if loc.start_line == loc.end_line && loc.start_column == loc.end_column
28
+
29
+ @annotations << [loc.start_offset, loc.end_offset, token].freeze
30
+ end
31
+
32
+ def visit_comment(node)
33
+ highlight Tokens::COMMENT, node.location
34
+ end
35
+
36
+ def visit_def_node(node)
37
+ highlight Tokens::KEYWORD, node.def_keyword_loc
38
+ highlight Tokens::FUNCTION, node.name_loc
39
+ highlight Tokens::PUNCTUATION, node.lparen_loc
40
+ highlight Tokens::PUNCTUATION, node.rparen_loc
41
+ highlight Tokens::KEYWORD, node.end_keyword_loc
42
+ super
43
+ end
44
+
45
+ def visit_local_variable_write_node(node)
46
+ highlight Tokens::LOCAL_VARIABLE, node.name_loc
47
+ highlight Tokens::OPERATOR, node.operator_loc
48
+ super
49
+ end
50
+
51
+ def visit_local_variable_read_node(node)
52
+ highlight Tokens::LOCAL_VARIABLE, node.location
53
+ super
54
+ end
55
+
56
+ def visit_instance_variable_write_node(node)
57
+ highlight Tokens::INSTANCE_VARIABLE, node.name_loc
58
+ highlight Tokens::OPERATOR, node.operator_loc
59
+ super
60
+ end
61
+
62
+ def visit_instance_variable_and_write_node(node)
63
+ highlight Tokens::INSTANCE_VARIABLE, node.name_loc
64
+ highlight Tokens::OPERATOR, node.operator_loc
65
+ super
66
+ end
67
+
68
+ def visit_instance_variable_or_write_node(node)
69
+ highlight Tokens::INSTANCE_VARIABLE, node.name_loc
70
+ highlight Tokens::OPERATOR, node.operator_loc
71
+ super
72
+ end
73
+
74
+ def visit_instance_variable_read_node(node)
75
+ highlight Tokens::INSTANCE_VARIABLE, node.location
76
+ super
77
+ end
78
+
79
+ def visit_constant_read_node(node)
80
+ highlight Tokens::CONSTANT, node.location
81
+ super
82
+ end
83
+
84
+ def visit_class_node(node)
85
+ highlight Tokens::KEYWORD, node.class_keyword_loc
86
+ highlight Tokens::KEYWORD, node.end_keyword_loc
87
+ super
88
+ end
89
+
90
+ def visit_call_node(node)
91
+ unless node.name in :[] | :[]=
92
+ highlight Tokens::FUNCTION, node.message_loc
93
+ end
94
+
95
+ super
96
+ end
97
+
98
+ def visit_string_node(node)
99
+ highlight Tokens::STRING, node.location
100
+ super
101
+ end
102
+
103
+ def visit_interpolated_string_node(node)
104
+ highlight Tokens::PUNCTUATION, node.opening_loc
105
+ highlight Tokens::PUNCTUATION, node.closing_loc
106
+ super
107
+ end
108
+
109
+ def visit_symbol_node(node)
110
+ highlight Tokens::SYMBOL, node.location
111
+ super
112
+ end
113
+
114
+ def visit_block_node(node)
115
+ if node.opening_loc.length == 1
116
+ highlight Tokens::PUNCTUATION, node.opening_loc
117
+ else
118
+ highlight Tokens::KEYWORD, node.opening_loc
119
+ end
120
+
121
+ if node.closing_loc.length == 1
122
+ highlight Tokens::PUNCTUATION, node.closing_loc
123
+ else
124
+ highlight Tokens::KEYWORD, node.closing_loc
125
+ end
126
+
127
+ super
128
+ end
129
+
130
+ def visit_case_node(node)
131
+ highlight Tokens::KEYWORD, node.case_keyword_loc
132
+ highlight Tokens::KEYWORD, node.end_keyword_loc
133
+ super
134
+ end
135
+
136
+ def visit_case_match_node(node)
137
+ highlight Tokens::KEYWORD, node.case_keyword_loc
138
+ highlight Tokens::KEYWORD, node.end_keyword_loc
139
+ super
140
+ end
141
+
142
+ def visit_module_node(node)
143
+ highlight Tokens::KEYWORD, node.module_keyword_loc
144
+ highlight Tokens::KEYWORD, node.end_keyword_loc
145
+ super
146
+ end
147
+
148
+ def visit_if_node(node)
149
+ highlight Tokens::KEYWORD, node.if_keyword_loc
150
+ highlight Tokens::KEYWORD, node.end_keyword_loc
151
+
152
+ if node.then_keyword&.length == 1
153
+ highlight Tokens::OPERATOR, node.then_keyword_loc
154
+ else
155
+ highlight Tokens::KEYWORD, node.then_keyword_loc
156
+ end
157
+
158
+ super
159
+ end
160
+
161
+ def visit_when_node(node)
162
+ highlight Tokens::KEYWORD, node.keyword_loc
163
+ super
164
+ end
165
+
166
+ def visit_else_node(node)
167
+ if node.else_keyword.length == 1
168
+ highlight Tokens::OPERATOR, node.else_keyword_loc
169
+ else
170
+ highlight Tokens::KEYWORD, node.else_keyword_loc
171
+ end
172
+
173
+ super
174
+ end
175
+
176
+ def visit_block_argument_node(node)
177
+ highlight Tokens::OPERATOR, node.operator_loc
178
+ super
179
+ end
180
+
181
+ def visit_and_node(node)
182
+ highlight Tokens::OPERATOR, node.operator_loc
183
+ super
184
+ end
185
+
186
+ def visit_or_node(node)
187
+ highlight Tokens::OPERATOR, node.operator_loc
188
+ super
189
+ end
190
+
191
+ def visit_local_variable_or_write_node(node)
192
+ highlight Tokens::LOCAL_VARIABLE, node.name_loc
193
+ highlight Tokens::OPERATOR, node.operator_loc
194
+ super
195
+ end
196
+
197
+ def visit_local_variable_and_write_node(node)
198
+ highlight Tokens::LOCAL_VARIABLE, node.name_loc
199
+ highlight Tokens::OPERATOR, node.operator_loc
200
+ super
201
+ end
202
+
203
+ def visit_rescue_node(node)
204
+ highlight Tokens::KEYWORD, node.keyword_loc
205
+ highlight Tokens::KEYWORD, node.operator_loc
206
+ super
207
+ end
208
+
209
+ def visit_rest_parameter_node(node)
210
+ highlight Tokens::OPERATOR, node.operator_loc
211
+ super
212
+ end
213
+
214
+ def visit_keyword_rest_parameter_node(node)
215
+ highlight Tokens::OPERATOR, node.operator_loc
216
+ super
217
+ end
218
+
219
+ def visit_block_parameter_node(node)
220
+ highlight Tokens::OPERATOR, node.operator_loc
221
+ super
222
+ end
223
+
224
+ def visit_while_node(node)
225
+ highlight Tokens::KEYWORD, node.keyword_loc
226
+ highlight Tokens::KEYWORD, node.closing_loc
227
+ super
228
+ end
229
+
230
+ def visit_until_node(node)
231
+ highlight Tokens::KEYWORD, node.keyword_loc
232
+ highlight Tokens::KEYWORD, node.closing_loc
233
+ super
234
+ end
235
+
236
+ def visit_next_node(node)
237
+ highlight Tokens::KEYWORD, node.keyword_loc
238
+ super
239
+ end
240
+
241
+ def visit_integer_node(node)
242
+ highlight Tokens::INTEGER, node.location
243
+ super
244
+ end
245
+
246
+ def visit_float_node(node)
247
+ highlight Tokens::FLOAT, node.location
248
+ super
249
+ end
250
+
251
+ def visit_hash_node(node)
252
+ highlight Tokens::PUNCTUATION, node.opening_loc
253
+ highlight Tokens::PUNCTUATION, node.closing_loc
254
+ super
255
+ end
256
+
257
+ def visit_array_node(node)
258
+ highlight Tokens::PUNCTUATION, node.opening_loc
259
+ highlight Tokens::PUNCTUATION, node.closing_loc
260
+ super
261
+ end
262
+
263
+ def visit_assoc_node(node)
264
+ highlight Tokens::OPERATOR, node.operator_loc
265
+ super
266
+ end
267
+
268
+ def visit_forwarding_parameter_node(node)
269
+ highlight Tokens::OPERATOR, node.location
270
+ super
271
+ end
272
+
273
+ def visit_forwarding_super_node(node)
274
+ highlight Tokens::KEYWORD, node.location
275
+ end
276
+
277
+ def visit_embedded_statements_node(node)
278
+ highlight Tokens::PUNCTUATION, node.opening_loc
279
+ highlight Tokens::PUNCTUATION, node.closing_loc
280
+ super
281
+ end
282
+
283
+ def visit_parentheses_node(node)
284
+ highlight Tokens::PUNCTUATION, node.opening_loc
285
+ highlight Tokens::PUNCTUATION, node.closing_loc
286
+ super
287
+ end
288
+
289
+ def visit_range_node(node)
290
+ highlight Tokens::OPERATOR, node.operator_loc
291
+ super
292
+ end
293
+
294
+ def visit_in_node(node)
295
+ highlight Tokens::KEYWORD, node.in_loc
296
+ highlight Tokens::KEYWORD, node.then_loc
297
+ super
298
+ end
299
+
300
+ def visit_array_pattern_node(node)
301
+ highlight Tokens::PUNCTUATION, node.opening_loc
302
+ highlight Tokens::PUNCTUATION, node.closing_loc
303
+ super
304
+ end
305
+
306
+ def visit_alternation_pattern_node(node)
307
+ highlight Tokens::OPERATOR, node.operator_loc
308
+ super
309
+ end
310
+
311
+ def visit_required_parameter_node(node)
312
+ highlight Tokens::LOCAL_VARIABLE, node.location
313
+ super
314
+ end
315
+
316
+ def visit_optional_parameter_node(node)
317
+ highlight Tokens::LOCAL_VARIABLE, node.name_loc
318
+ highlight Tokens::OPERATOR, node.operator_loc
319
+ super
320
+ end
321
+
322
+ def visit_capture_pattern_node(node)
323
+ highlight Tokens::OPERATOR, node.operator_loc
324
+ super
325
+ end
326
+ end
333
327
  end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Dispersion
4
+ module Tokens
5
+ COMMENT = :COMMENT
6
+ KEYWORD = :KEYWORD
7
+ CONSTANT = :CONSTANT
8
+ FUNCTION = :FUNCTION
9
+ PUNCTUATION = :PUNCTUATION
10
+ LOCAL_VARIABLE = :LOCAL_VARIABLE
11
+ INSTANCE_VARIABLE = :INSTANCE_VARIABLE
12
+ OPERATOR = :OPERATOR
13
+ STRING = :STRING
14
+ SYMBOL = :SYMBOL
15
+ INTEGER = :INTEGER
16
+ FLOAT = :FLOAT
17
+ end
18
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Dispersion
4
- VERSION = "0.1.0"
4
+ VERSION = "0.2.0"
5
5
  end
data/lib/dispersion.rb CHANGED
@@ -1,9 +1,43 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "prism"
4
+
3
5
  module Dispersion
4
- autoload :Annotator, "dispersion/annotator"
5
- autoload :Annotation, "dispersion/annotation"
6
- autoload :Formatters, "dispersion/formatters"
7
- autoload :Highlighter, "dispersion/highlighter"
8
- autoload :VERSION, "dispersion/version"
6
+ autoload :Annotator, "dispersion/annotator"
7
+ autoload :Tokens, "dispersion/tokens"
8
+
9
+ THEME = {
10
+ Tokens::COMMENT => "\e[90m",
11
+ Tokens::KEYWORD => "\e[31m",
12
+ Tokens::CONSTANT => "\e[34m",
13
+ Tokens::FUNCTION => "\e[33m",
14
+ Tokens::PUNCTUATION => "\e[37m",
15
+ Tokens::LOCAL_VARIABLE => "\e[32m",
16
+ Tokens::INSTANCE_VARIABLE => "\e[35m",
17
+ Tokens::OPERATOR => "\e[33m",
18
+ Tokens::STRING => "\e[32m",
19
+ Tokens::SYMBOL => "\e[36m",
20
+ Tokens::INTEGER => "\e[33m",
21
+ Tokens::FLOAT => "\e[33m",
22
+ }.freeze
23
+
24
+ def self.annotate(code)
25
+ Annotator.call(code)
26
+ end
27
+
28
+ def self.ansi(code)
29
+ code.dup.tap do |buffer|
30
+ offset = 0
31
+
32
+ for start_offset, end_offset, token in annotate(code)
33
+ start = THEME.fetch(token)
34
+
35
+ buffer.bytesplice(start_offset + offset, 0, start)
36
+ offset += start.bytesize
37
+
38
+ buffer.bytesplice(end_offset + offset, 0, "\e[0m")
39
+ offset += 4
40
+ end
41
+ end
42
+ end
9
43
  end
metadata CHANGED
@@ -1,51 +1,39 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dispersion
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joel Drapper
8
- autorequire:
9
8
  bindir: bin
10
9
  cert_chain: []
11
- date: 2023-12-03 00:00:00.000000000 Z
10
+ date: 2025-01-31 00:00:00.000000000 Z
12
11
  dependencies:
13
12
  - !ruby/object:Gem::Dependency
14
13
  name: prism
15
14
  requirement: !ruby/object:Gem::Requirement
16
15
  requirements:
17
- - - "~>"
16
+ - - ">="
18
17
  - !ruby/object:Gem::Version
19
- version: '0.18'
18
+ version: '0'
20
19
  type: :runtime
21
20
  prerelease: false
22
21
  version_requirements: !ruby/object:Gem::Requirement
23
22
  requirements:
24
- - - "~>"
23
+ - - ">="
25
24
  - !ruby/object:Gem::Version
26
- version: '0.18'
27
- description:
25
+ version: '0'
28
26
  email:
29
27
  - joel@drapper.me
30
28
  executables: []
31
29
  extensions: []
32
30
  extra_rdoc_files: []
33
31
  files:
34
- - CHANGELOG.md
35
- - CODE_OF_CONDUCT.md
36
- - Gemfile
37
- - Gemfile.lock
38
32
  - LICENSE.txt
39
33
  - README.md
40
- - Rakefile
41
- - dispersion.gemspec
42
34
  - lib/dispersion.rb
43
- - lib/dispersion/annotation.rb
44
35
  - lib/dispersion/annotator.rb
45
- - lib/dispersion/formatters.rb
46
- - lib/dispersion/formatters/ansi.rb
47
- - lib/dispersion/formatters/html.rb
48
- - lib/dispersion/highlighter.rb
36
+ - lib/dispersion/tokens.rb
49
37
  - lib/dispersion/version.rb
50
38
  homepage: https://github.com/joeldrapper/dispersion
51
39
  licenses:
@@ -53,9 +41,8 @@ licenses:
53
41
  metadata:
54
42
  homepage_uri: https://github.com/joeldrapper/dispersion
55
43
  source_code_uri: https://github.com/joeldrapper/dispersion
56
- changelog_uri: https://github.com/joeldrapper/dispersion/blob/main/CHANGELOG.md
44
+ changelog_uri: https://github.com/joeldrapper/dispersion
57
45
  funding_uri: https://github.com/sponsors/joeldrapper
58
- post_install_message:
59
46
  rdoc_options: []
60
47
  require_paths:
61
48
  - lib
@@ -70,8 +57,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
70
57
  - !ruby/object:Gem::Version
71
58
  version: '0'
72
59
  requirements: []
73
- rubygems_version: 3.4.10
74
- signing_key:
60
+ rubygems_version: 3.6.2
75
61
  specification_version: 4
76
- summary: Prism-powered syntax highlighter for Ruby
62
+ summary: Syntac Highlighter for Ruby
77
63
  test_files: []
data/CHANGELOG.md DELETED
@@ -1,5 +0,0 @@
1
- ## [Unreleased]
2
-
3
- ## [0.1.0] - 2023-10-09
4
-
5
- - Initial release
data/CODE_OF_CONDUCT.md DELETED
@@ -1,84 +0,0 @@
1
- # Contributor Covenant Code of Conduct
2
-
3
- ## Our Pledge
4
-
5
- We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
-
7
- We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
-
9
- ## Our Standards
10
-
11
- Examples of behavior that contributes to a positive environment for our community include:
12
-
13
- * Demonstrating empathy and kindness toward other people
14
- * Being respectful of differing opinions, viewpoints, and experiences
15
- * Giving and gracefully accepting constructive feedback
16
- * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
- * Focusing on what is best not just for us as individuals, but for the overall community
18
-
19
- Examples of unacceptable behavior include:
20
-
21
- * The use of sexualized language or imagery, and sexual attention or
22
- advances of any kind
23
- * Trolling, insulting or derogatory comments, and personal or political attacks
24
- * Public or private harassment
25
- * Publishing others' private information, such as a physical or email
26
- address, without their explicit permission
27
- * Other conduct which could reasonably be considered inappropriate in a
28
- professional setting
29
-
30
- ## Enforcement Responsibilities
31
-
32
- Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
-
34
- Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
-
36
- ## Scope
37
-
38
- This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
-
40
- ## Enforcement
41
-
42
- Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at joel@drapper.me. All complaints will be reviewed and investigated promptly and fairly.
43
-
44
- All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
-
46
- ## Enforcement Guidelines
47
-
48
- Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
-
50
- ### 1. Correction
51
-
52
- **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
-
54
- **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
-
56
- ### 2. Warning
57
-
58
- **Community Impact**: A violation through a single incident or series of actions.
59
-
60
- **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
-
62
- ### 3. Temporary Ban
63
-
64
- **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
-
66
- **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
-
68
- ### 4. Permanent Ban
69
-
70
- **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
-
72
- **Consequence**: A permanent ban from any sort of public interaction within the community.
73
-
74
- ## Attribution
75
-
76
- This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
- available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
-
79
- Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
-
81
- [homepage]: https://www.contributor-covenant.org
82
-
83
- For answers to common questions about this code of conduct, see the FAQ at
84
- https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile DELETED
@@ -1,8 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- source "https://rubygems.org"
4
-
5
- # Specify your gem's dependencies in dispersion.gemspec
6
- gemspec
7
-
8
- gem "green_dots", path: "../green_dots"
data/Gemfile.lock DELETED
@@ -1,34 +0,0 @@
1
- PATH
2
- remote: ../green_dots
3
- specs:
4
- green_dots (0.1.0)
5
- concurrent-ruby
6
- prism
7
- rouge
8
- tty-tree
9
- zeitwerk
10
-
11
- PATH
12
- remote: .
13
- specs:
14
- dispersion (0.1.0)
15
- prism (~> 0.18)
16
-
17
- GEM
18
- remote: https://rubygems.org/
19
- specs:
20
- concurrent-ruby (1.2.2)
21
- prism (0.18.0)
22
- rouge (4.1.3)
23
- tty-tree (0.4.0)
24
- zeitwerk (2.6.12)
25
-
26
- PLATFORMS
27
- arm64-darwin-22
28
-
29
- DEPENDENCIES
30
- dispersion!
31
- green_dots!
32
-
33
- BUNDLED WITH
34
- 2.4.12
data/Rakefile DELETED
@@ -1,12 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "bundler/gem_tasks"
4
- require "rake/testtask"
5
-
6
- Rake::TestTask.new(:test) do |t|
7
- t.libs << "test"
8
- t.libs << "lib"
9
- t.test_files = FileList["test/**/test_*.rb"]
10
- end
11
-
12
- task default: :test
data/dispersion.gemspec DELETED
@@ -1,29 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative "lib/dispersion/version"
4
-
5
- Gem::Specification.new do |spec|
6
- spec.name = "dispersion"
7
- spec.version = Dispersion::VERSION
8
- spec.authors = ["Joel Drapper"]
9
- spec.email = ["joel@drapper.me"]
10
-
11
- spec.summary = "Prism-powered syntax highlighter for Ruby"
12
- spec.homepage = "https://github.com/joeldrapper/dispersion"
13
- spec.license = "MIT"
14
- spec.required_ruby_version = ">= 3.0.0"
15
-
16
- spec.metadata["homepage_uri"] = spec.homepage
17
- spec.metadata["source_code_uri"] = spec.homepage
18
- spec.metadata["changelog_uri"] = "https://github.com/joeldrapper/dispersion/blob/main/CHANGELOG.md"
19
- spec.metadata["funding_uri"] = "https://github.com/sponsors/joeldrapper"
20
-
21
- spec.files = Dir.chdir(__dir__) do
22
- `git ls-files -z`.split("\x0").reject do |f|
23
- (File.expand_path(f) == __FILE__) || f.start_with?(*%w[bin/ test/ spec/ features/ .git .circleci appveyor])
24
- end
25
- end
26
-
27
- spec.require_paths = ["lib"]
28
- spec.add_dependency "prism", "~> 0.18"
29
- end
@@ -1,29 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Dispersion
4
- class Annotation
5
- include Comparable
6
-
7
- def initialize(type, location)
8
- @type = type
9
- @location = location
10
- end
11
-
12
- attr_reader :type, :location
13
-
14
- def <=>(other)
15
- comparison_value <=> other.comparison_value
16
- end
17
-
18
- def comparison_value
19
- [
20
- location,
21
- (type == :reset ? 0 : 1)
22
- ]
23
- end
24
-
25
- def to_ary
26
- [type, location]
27
- end
28
- end
29
- end
@@ -1,89 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Dispersion
4
- module Formatters
5
- class ANSI
6
- Regular = "\e[22m"
7
- Bold = "\e[1m"
8
- Faint = "\e[2m"
9
- Reset = "\e[0m"
10
- Italic = "\e[3m"
11
- Underline = "\e[4m"
12
-
13
- # Basic Colors
14
- Black = "\e[30m"
15
- Red = "\e[31m"
16
- Green = "\e[32m"
17
- Yellow = "\e[33m"
18
- Blue = "\e[34m"
19
- Magenta = "\e[35m"
20
- Cyan = "\e[36m"
21
- White = "\e[37m"
22
-
23
- # Bright Colors
24
- BrightBlack = "\e[90m"
25
- BrightRed = "\e[91m"
26
- BrightGreen = "\e[92m"
27
- BrightYellow = "\e[93m"
28
- BrightBlue = "\e[94m"
29
- BrightMagenta = "\e[95m"
30
- BrightCyan = "\e[96m"
31
- BrightWhite = "\e[97m"
32
-
33
- DEFAULT_ANSI = {
34
- call: Red,
35
- string: Yellow,
36
- keyword: Cyan,
37
- comment: Italic + Magenta,
38
- var: "",
39
- constant: Blue,
40
- ivar: Blue,
41
- symbol: Green,
42
- numeric: Green,
43
- bracket: "",
44
- operator: "\e[36m",
45
- }
46
-
47
- def initialize(code, annotations:, theme:)
48
- @code = code
49
- @annotations = annotations
50
- @marks = DEFAULT_ANSI
51
- end
52
-
53
- def call
54
- lines = @code.lines
55
- current_symbol = nil
56
- buffer = +""
57
-
58
- lines.each_with_index do |line, line_number|
59
- cursor = 0
60
-
61
- @annotations[line_number].each do |(symbol, column)|
62
- buffer << line[cursor...column]
63
- cursor = column
64
-
65
- # Always reset the current symbol
66
- if current_symbol
67
- buffer << Reset
68
- current_symbol = nil
69
- end
70
-
71
- unless symbol == :reset
72
- buffer << @marks.fetch(symbol)
73
- current_symbol = symbol
74
- end
75
- end
76
-
77
- buffer << line[cursor..]
78
- end
79
-
80
- if current_symbol
81
- buffer << Reset
82
- current_symbol = nil
83
- end
84
-
85
- buffer.gsub(/\t/, " ")
86
- end
87
- end
88
- end
89
- end
@@ -1,51 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "erb"
4
-
5
- module Dispersion
6
- module Formatters
7
- class HTML
8
- def initialize(code, annotations:)
9
- @code = code
10
- @annotations = annotations
11
- end
12
-
13
- def call
14
- lines = @code.lines
15
- current_symbol = nil
16
- buffer = +""
17
-
18
- lines.each_with_index do |line, line_number|
19
- cursor = 0
20
-
21
- @annotations[line_number].each do |(symbol, column)|
22
- buffer << ERB::Escape.html_escape(line[cursor...column])
23
- cursor = column
24
-
25
- # Always reset the current symbol
26
- if current_symbol
27
- buffer << "</span>"
28
- current_symbol = nil
29
- end
30
-
31
- unless symbol == :reset
32
- buffer << %(<span data-dispersion-symbol="#{symbol.name}">)
33
- current_symbol = symbol
34
- end
35
-
36
- # binding.irb
37
- end
38
-
39
- buffer << ERB::Escape.html_escape(line[cursor..])
40
- end
41
-
42
- if current_symbol
43
- buffer << "</span>"
44
- current_symbol = nil
45
- end
46
-
47
- buffer.gsub(/\t/, " ")
48
- end
49
- end
50
- end
51
- end
@@ -1,8 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Dispersion
4
- module Formatters
5
- autoload :HTML, "dispersion/formatters/html"
6
- autoload :ANSI, "dispersion/formatters/ansi"
7
- end
8
- end
@@ -1,19 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Dispersion
4
- class Highlighter
5
- def initialize(code)
6
- @code = code
7
- @annotations = Dispersion::Annotator.call(code)
8
- freeze
9
- end
10
-
11
- def to_ansi(theme: nil)
12
- Dispersion::Formatters::ANSI.new(@code, annotations: @annotations, theme:).call
13
- end
14
-
15
- def to_html
16
- Dispersion::Formatters::HTML.new(@code, annotations: @annotations).call
17
- end
18
- end
19
- end