psych-comments 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ba5e728c57571dade021e0eddcd6518b9a3aadfb015d084136371ec5fd2b7742
4
- data.tar.gz: a10c10213b15b1751d3b140a14faae0add32ec1cc4b3c17ffb08da55dd19ef6b
3
+ metadata.gz: 818bc094529602b346feec2de45c209a97846747142399a673984b27cf311153
4
+ data.tar.gz: c20108df72182dbe158485de105ab764c5e6f6e5f6bc9f0fe714d3fb8bdd717a
5
5
  SHA512:
6
- metadata.gz: 4f891304d71be4e3e4649a53fb64c2a84a108e41f3d34e7420da2a14ba8660b94e762032dc939f1dba94f0e054ff0538b723d805122ce21e96dfc5d29058a4c7
7
- data.tar.gz: 2971dd17da1740003f85a6fb9789369677f1ee110b803a71340ad8134c94ea0f09c30606536f38f844de8cc5bf78dcfa71d1fed1d08060ff808dcc8a97715c43
6
+ metadata.gz: c21f780021ef4b2f86ef7c090624c8d06f252922ce6539998edb5768b310536aa412c61a6d8e33370f58401560bbca92d8006dfc7ee3ca80d3efb398598e1370
7
+ data.tar.gz: 77fd723a8d83a8a8071ea87e60548efa88be32f5c88cb242c52548df56950e8c3ba0a29fccfd0e5c7b7ff29c4be8ca8d39b492f0d0cbed3f24e506fc8adfeb98
data/CHANGELOG.md CHANGED
@@ -1,5 +1,10 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.1.1] - 2022-08-25
4
+
5
+ - Parser: distinguish comments before a bullet `- ` and after it
6
+ - Emitter: various improvements
7
+
3
8
  ## [0.1.0] - 2022-08-24
4
9
 
5
10
  - Initial release
@@ -16,7 +16,7 @@ module Psych
16
16
  end
17
17
  end
18
18
 
19
- module_function def stringify_adjust_scalar(node, indent = 0)
19
+ module_function def stringify_adjust_scalar(node, indent_str = 0)
20
20
  node2 = Psych::Nodes::Scalar.new(node.value, nil, nil, node.plain, node.quoted, node.style)
21
21
  if node.tag
22
22
  if node.style == Psych::Nodes::Scalar::PLAIN
@@ -30,7 +30,7 @@ module Psych
30
30
  if node.style == Psych::Nodes::Scalar::DOUBLE_QUOTED || node.style == Psych::Nodes::Scalar::SINGLE_QUOTED || node.style == Psych::Nodes::Scalar::PLAIN
31
31
  s = s.gsub(/\s*\n\s*/, " ")
32
32
  else
33
- s = s.gsub(/\n/, "\n#{INDENT * indent}")
33
+ s = s.gsub(/\n/, "\n#{indent_str}")
34
34
  end
35
35
  s.gsub(/\n\s+$/, "\n")
36
36
  end
@@ -108,8 +108,7 @@ module Psych
108
108
  @comment_lookahead.shift
109
109
  else
110
110
  node.leading_comments.each do |comment|
111
- print comment
112
- newline!
111
+ emit_comment(comment)
113
112
  end
114
113
  end
115
114
  if has_anchor(node)
@@ -130,7 +129,7 @@ module Psych
130
129
  if node.is_a?(Psych::Nodes::Alias)
131
130
  print "*#{node.anchor}"
132
131
  else
133
- print stringify_adjust_scalar(node, @indent)
132
+ print stringify_adjust_scalar(node, INDENT * @indent)
134
133
  end
135
134
  when Psych::Nodes::Mapping
136
135
  set_flow(flow?(node)) do
@@ -155,14 +154,12 @@ module Psych
155
154
  emit(key)
156
155
  print ":"
157
156
  space!
158
- if single_line(value)
159
- emit(value)
160
- elsif has_bullet(value)
157
+ if single_line?(value) || has_bullet(value)
161
158
  emit(value)
162
159
  else
163
- @indent += 1
164
- emit(value)
165
- @indent -= 1
160
+ indented do
161
+ emit(value)
162
+ end
166
163
  end
167
164
  newline!
168
165
  end
@@ -188,8 +185,12 @@ module Psych
188
185
  emit_lookahead_comments(subnode) unless @flow
189
186
  print "- "
190
187
  @state = :pseudo_indent
191
- indented do
188
+ if single_line?(subnode)
192
189
  emit(subnode)
190
+ else
191
+ indented do
192
+ emit(subnode)
193
+ end
193
194
  end
194
195
  newline!
195
196
  end
@@ -204,7 +205,7 @@ module Psych
204
205
  unless node.implicit
205
206
  newline!
206
207
  print "---"
207
- newline!
208
+ space!
208
209
  end
209
210
  set_tagmap(node) do
210
211
  emit(node.root)
@@ -212,8 +213,8 @@ module Psych
212
213
  unless node.implicit_end
213
214
  newline!
214
215
  print "..."
215
- newline!
216
216
  end
217
+ newline!
217
218
  when Psych::Nodes::Stream
218
219
  node.children.each do |subnode|
219
220
  emit(subnode)
@@ -221,18 +222,26 @@ module Psych
221
222
  else
222
223
  raise TypeError, node
223
224
  end
225
+ node.trailing_comments.each do |comment|
226
+ emit_comment(comment)
227
+ end
224
228
  end
225
229
 
226
230
  def emit_lookahead_comments(node)
231
+ return if node.equal?(@comment_lookahead[0])
232
+
227
233
  node.leading_comments.each do |comment|
228
- print comment
229
- newline!
234
+ emit_comment(comment)
230
235
  end
231
236
  @comment_lookahead.push(node)
232
- case node
233
- when Psych::Nodes::Mapping, Psych::Nodes::Sequence
234
- emit_lookahead_comments(node.children[0]) unless flow?(node)
237
+ end
238
+
239
+ def emit_comment(comment)
240
+ unless /\A#[^\r\n]*\z/.match?(comment)
241
+ raise ArgumentError, "Invalid comment: #{comment.inspect}"
235
242
  end
243
+ print comment
244
+ newline!
236
245
  end
237
246
 
238
247
  def indented(&block)
@@ -253,12 +262,20 @@ module Psych
253
262
  end
254
263
  end
255
264
 
265
+ def single_line?(node)
266
+ flow?(node) && node.leading_comments.empty? && node.trailing_comments.empty?
267
+ end
268
+
256
269
  def flow?(node)
257
270
  case node
271
+ when Psych::Nodes::Scalar, Psych::Nodes::Alias
272
+ true
258
273
  when Psych::Nodes::Mapping
259
274
  @flow || node.style == Psych::Nodes::Mapping::FLOW || node.children.empty?
260
275
  when Psych::Nodes::Sequence
261
276
  @flow || node.style == Psych::Nodes::Sequence::FLOW || node.children.empty?
277
+ else
278
+ false
262
279
  end
263
280
  end
264
281
 
@@ -4,6 +4,7 @@ module Psych
4
4
  def initialize(text)
5
5
  @lines = text.lines.to_a
6
6
  @last = [0, 0]
7
+ @bullet_owner = nil
7
8
  end
8
9
 
9
10
  def sublines(sl, sc, el, ec)
@@ -25,7 +26,20 @@ module Psych
25
26
  def read_comments(line, column)
26
27
  s = sublines(*@last, line, column)
27
28
  @last = [line, column]
28
- s.scan(/#.*?$/)
29
+ comments = []
30
+ s.scan(/-|#.*?$/) do |token|
31
+ case token
32
+ when "-"
33
+ if @bullet_owner
34
+ @bullet_owner.leading_comments.push(*comments)
35
+ comments = []
36
+ end
37
+ else
38
+ comments << token
39
+ end
40
+ end
41
+ @bullet_owner = nil
42
+ comments
29
43
  end
30
44
 
31
45
  def visit(node)
@@ -35,8 +49,11 @@ module Psych
35
49
  @last = [node.end_line, node.end_column]
36
50
  when Psych::Nodes::Sequence, Psych::Nodes::Mapping
37
51
  has_delim = /[\[{]/.match?(char_at(node.start_line, node.start_column))
52
+ has_bullet = node.is_a?(Psych::Nodes::Sequence) && !has_delim
53
+ # Special-case on `- #foo\n bar: baz`
38
54
  node.leading_comments.push(*read_comments(node.start_line, node.start_column)) if has_delim
39
55
  node.children.each do |subnode|
56
+ @bullet_owner = subnode if has_bullet
40
57
  visit(subnode)
41
58
  end
42
59
  if has_delim
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Psych
4
4
  module Comments
5
- VERSION = "0.1.0"
5
+ VERSION = "0.1.1"
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: psych-comments
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Masaki Hara
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-08-24 00:00:00.000000000 Z
11
+ date: 2022-08-25 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Brings comment-aware YAML parsing to Psych
14
14
  email: