yaml-sort 1.0.1 → 2.0.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: 232d936095f9aac0e3f4098fa2cc4f1d608ddc79be9cb4e757fb476fa75999e3
4
- data.tar.gz: be5949810ea40472c5d80e7f23d96886e964847b26d9526f67b337a77acfe490
3
+ metadata.gz: a923ba44e82aa9eba2dbaa2ba2c4c30a6c775a5a6d0e2f447ec6e0682dc119df
4
+ data.tar.gz: 637f1efc9b2104818816ce37a62385a149c657d079ad50a6cb8337a9c56a7eee
5
5
  SHA512:
6
- metadata.gz: e90a5d3ff86076d86b0629ec1a387aa179588f678d17c5f60fcae2d01ffda38eb28f308d5d3d7df1a29d6b1a194701caab02409d26d364f8c2c8fb7403ca2a0a
7
- data.tar.gz: e2fb3054b83242a9fad8c596c6f79ecfce1f66169234c7e1bc8a179214bd41288e08eb608981b08d2312dff721e3bfa18aadaa429dded7bae1eeb702dc06bdd3
6
+ metadata.gz: 04c75095cfe4fe25a42fdf0d0a4184a0324b1c64244ff3930d082e23f0efd393dbea5f4ea3f00f2364d48307ff37677b17348afa5f57ea367c525248ea3c4ecd
7
+ data.tar.gz: b6f2583c7170070daa6bd3dbc1067e280fc26c4bc88895158039a6e2ce5dc67177ef2210978bfbb332496a207070a61c4d2c56a5a4a2913ff6d9bf83f5d89293
data/CHANGELOG.md CHANGED
@@ -3,9 +3,17 @@ All notable changes to this project will be documented in this file.
3
3
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
4
4
  and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
5
5
 
6
- ## [1.0.1](https://github.com/smortex/yaml-sort/tree/1.0.1) (2022-04-24)
6
+ ## [2.0.0](https://github.com/smortex/yaml-sort/tree/2.0.0) (2022-04-25)
7
7
 
8
- [Full Changelog](https://github.com/smortex/yaml-sort/compare/v1.0.0...1.0.1)
8
+ [Full Changelog](https://github.com/smortex/yaml-sort/compare/v1.0.1...2.0.0)
9
+
10
+ **Merged pull requests:**
11
+
12
+ - Rework list sorting [\#7](https://github.com/smortex/yaml-sort/pull/7) ([smortex](https://github.com/smortex))
13
+
14
+ ## [v1.0.1](https://github.com/smortex/yaml-sort/tree/v1.0.1) (2022-04-24)
15
+
16
+ [Full Changelog](https://github.com/smortex/yaml-sort/compare/v1.0.0...v1.0.1)
9
17
 
10
18
  **Fixed bugs:**
11
19
 
@@ -20,13 +20,15 @@ module Yaml
20
20
  dict
21
21
  end
22
22
 
23
- def to_s
23
+ def to_s(skip_first_indent: false)
24
+ n = -1
24
25
  super + items.map do |k, v|
26
+ n += 1
25
27
  case v
26
28
  when List, Dictionary
27
- "#{k}\n#{v}"
29
+ "#{k.to_s(skip_first_indent: skip_first_indent && n.zero?)}\n#{v}"
28
30
  when Scalar
29
- "#{k} #{v}"
31
+ "#{k.to_s(skip_first_indent: skip_first_indent && n.zero?)} #{v}"
30
32
  end
31
33
  end.join("\n")
32
34
  end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "scalar"
4
+
5
+ module Yaml
6
+ module Sort
7
+ class Item < Scalar
8
+ def to_s(*)
9
+ comments + value
10
+ end
11
+ end
12
+ end
13
+ end
@@ -22,15 +22,13 @@ module Yaml
22
22
 
23
23
  def to_s
24
24
  super + items.map do |item|
25
- "#{item[0]}#{item[1]}"
25
+ "#{item[0]}#{item[1].to_s(skip_first_indent: true)}"
26
26
  end.join("\n")
27
27
  end
28
28
 
29
29
  def sort
30
- List.new(items.sort { |a, b| a[1] <=> b[1] })
31
- rescue ArgumentError
32
- # Non-comparable items
33
- self
30
+ # TODO: Add an option to sort scalar values
31
+ List.new(items.map { |i| [i[0], i[1].sort] })
34
32
  end
35
33
  end
36
34
  end
@@ -78,10 +78,10 @@ def scan(text)
78
78
  when s.scan(/\n[[:blank:]]*#.*/) then emit(:COMMENT, s.matched)
79
79
  when s.scan(/\n([[:blank:]]*-) */) then emit(:ITEM, s.matched, indent: s.captures[0])
80
80
  when s.scan(/\n[[:blank:]]*\.\.\./) then emit(:END_OF_DOCUMENT, s.matched)
81
- when s.scan(/\n?([[:blank:]]*)([^[[:space:]]\n]+):(?=[ \n])/)
81
+ when s.scan(/\n?([[:blank:]]*)([^[[:space:]]\n]+:)(?=[ \n])/)
82
82
  indent = s.captures[0]
83
83
  indent = last_indent_value + indent + " " unless s.matched.start_with?("\n")
84
- emit(:KEY, s.matched, indent: indent)
84
+ emit(:KEY, s.matched, indent: indent, value: s.captures[1])
85
85
 
86
86
  when s.scan(/\n\z/)
87
87
  # Done
@@ -110,16 +110,16 @@ def scan(text)
110
110
  @tokens
111
111
  end
112
112
 
113
- def emit(token, value, length: nil, indent: nil)
113
+ def emit(token, match, length: nil, indent: nil, value: nil)
114
114
  indent.gsub!("-", " ") if indent
115
115
  if token && length.nil?
116
- raise "length must be explicitly passed when value is not a String (#{value.class.name})" unless value.is_a?(String)
117
- length = value.length
116
+ raise "length must be explicitly passed when match is not a String (#{match.class.name})" unless match.is_a?(String)
117
+ length = match.length
118
118
  end
119
119
 
120
- if value.start_with?("\n")
120
+ if match.start_with?("\n")
121
121
  @lineno += 1
122
- value = value[1..-1]
122
+ match = match[1..-1]
123
123
  length -= 1
124
124
  @position = 0
125
125
  end
@@ -130,7 +130,7 @@ def emit(token, value, length: nil, indent: nil)
130
130
  end
131
131
 
132
132
  exvalue = {
133
- value: value,
133
+ value: value || match,
134
134
  lineno: @lineno,
135
135
  position: @position,
136
136
  length: length,
@@ -138,7 +138,7 @@ def emit(token, value, length: nil, indent: nil)
138
138
  }
139
139
  @tokens << [token, exvalue]
140
140
 
141
- @lineno += value.count("\n")
141
+ @lineno += match.count("\n")
142
142
 
143
143
  @position += length
144
144
  end
@@ -351,7 +351,7 @@ module_eval(<<'.,.,', 'parser.ra', 22)
351
351
 
352
352
  module_eval(<<'.,.,', 'parser.ra', 24)
353
353
  def _reduce_11(val, _values, result)
354
- result = [Scalar.new(val[0]), val[1]]
354
+ result = [Item.new(val[0]), val[1]]
355
355
  result
356
356
  end
357
357
  .,.,
@@ -3,12 +3,13 @@
3
3
  module Yaml
4
4
  module Sort
5
5
  class Scalar < Value
6
- attr_reader :value
6
+ attr_reader :value, :indent
7
7
 
8
8
  def initialize(value)
9
9
  super()
10
10
  @comment = value[:comment] || []
11
11
  @value = value[:value]
12
+ @indent = value[:indent] || ""
12
13
  end
13
14
 
14
15
  def <=>(other)
@@ -19,8 +20,12 @@ module Yaml
19
20
  end
20
21
  end
21
22
 
22
- def to_s
23
- super + value
23
+ def to_s(skip_first_indent: false)
24
+ if skip_first_indent
25
+ super + value
26
+ else
27
+ super + indent + value
28
+ end
24
29
  end
25
30
  end
26
31
  end
@@ -7,7 +7,11 @@ module Yaml
7
7
  @comment = []
8
8
  end
9
9
 
10
- def to_s
10
+ def to_s(*)
11
+ comments
12
+ end
13
+
14
+ def comments
11
15
  @comment.join
12
16
  end
13
17
 
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Yaml
4
4
  module Sort
5
- VERSION = "1.0.1"
5
+ VERSION = "2.0.0"
6
6
  end
7
7
  end
data/lib/yaml/sort.rb CHANGED
@@ -3,6 +3,7 @@
3
3
  require_relative "sort/parser"
4
4
  require_relative "sort/value"
5
5
  require_relative "sort/dictionary"
6
+ require_relative "sort/item"
6
7
  require_relative "sort/list"
7
8
  require_relative "sort/scalar"
8
9
  require_relative "sort/version"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yaml-sort
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Romain Tartière
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-04-24 00:00:00.000000000 Z
11
+ date: 2022-04-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: cri
@@ -86,6 +86,7 @@ files:
86
86
  - lib/yaml/sort.rb
87
87
  - lib/yaml/sort/cli.rb
88
88
  - lib/yaml/sort/dictionary.rb
89
+ - lib/yaml/sort/item.rb
89
90
  - lib/yaml/sort/list.rb
90
91
  - lib/yaml/sort/parser.rb
91
92
  - lib/yaml/sort/scalar.rb