diff --git a/.rubocop.yml b/.rubocop.yml index 815ad1c..3da05b1 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -1,6 +1,6 @@ AllCops: NewCops: enable - TargetRubyVersion: 3.0 + TargetRubyVersion: 3.1 SuggestExtensions: false Exclude: - .vendor/**/* @@ -70,6 +70,7 @@ Style/BlockDelimiters: Style/GlobalVars: Exclude: - ext/tree_sitter/extconf.rb + Style/IfUnlessModifier: Enabled: false diff --git a/CHANGELOG.md b/CHANGELOG.md index f679e6c..187315d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## [unreleased] +- Drop support for Ruby < 3.1. - Deprarture from the `ruby/pretty_print` API with Wadler: - accept `indent` which will apply to `nest` and `group` implicitly if no `indent:` kwarg is passed. diff --git a/examples/oppen/oppen.rb b/examples/oppen/oppen.rb index 709776d..692cc4a 100755 --- a/examples/oppen/oppen.rb +++ b/examples/oppen/oppen.rb @@ -15,6 +15,6 @@ Oppen.eof, ] -puts Oppen.print(tokens: tokens) +puts Oppen.print(tokens:) # Hello, World! # How are you doing? diff --git a/examples/oppen_and_wadler_customization/new_line.rb b/examples/oppen_and_wadler_customization/new_line.rb index b1ba64f..ded5c71 100755 --- a/examples/oppen_and_wadler_customization/new_line.rb +++ b/examples/oppen_and_wadler_customization/new_line.rb @@ -5,7 +5,7 @@ # The new line String can be specified using the `new_line` parameter. new_line = '
' -printer = Oppen::Wadler.new(indent: 2, new_line: new_line) +printer = Oppen::Wadler.new(indent: 2, new_line:) printer.group { printer.text 'Hello, World!' diff --git a/examples/oppen_and_wadler_customization/whitespace.rb b/examples/oppen_and_wadler_customization/whitespace.rb index 2348028..d031ca4 100755 --- a/examples/oppen_and_wadler_customization/whitespace.rb +++ b/examples/oppen_and_wadler_customization/whitespace.rb @@ -9,7 +9,7 @@ # to prevent trailing whitespaces can be specified using the `whitespace` parameter. whitespace = '**' -printer = Oppen::Wadler.new(indent: 2, whitespace: whitespace) +printer = Oppen::Wadler.new(indent: 2, whitespace:) printer.group { printer.text '******Hello, World!******' diff --git a/examples/oppen_and_wadler_customization/width.rb b/examples/oppen_and_wadler_customization/width.rb index 8c4814f..785aef3 100755 --- a/examples/oppen_and_wadler_customization/width.rb +++ b/examples/oppen_and_wadler_customization/width.rb @@ -6,7 +6,7 @@ width = 5 printer_width_default = Oppen::Wadler.new(indent: 2) -printer_width_narrow = Oppen::Wadler.new(indent: 2, width: width) +printer_width_narrow = Oppen::Wadler.new(indent: 2, width:) test_block = ->(printer) { printer.group { printer.text 'Hello, World!' diff --git a/justfile b/justfile index f7b5eb8..7993c05 100644 --- a/justfile +++ b/justfile @@ -45,6 +45,10 @@ gem: lint: bundle exec rubocop --config .rubocop.yml +[group('lint')] +lint-fix: + bundle exec rubocop --config .rubocop.yml -A + [group('publish')] publish: gem -C {{PKG_OUT}} push {{GEM_FILE}} diff --git a/lib/oppen.rb b/lib/oppen.rb index 1a0c3c5..207c3ba 100644 --- a/lib/oppen.rb +++ b/lib/oppen.rb @@ -154,10 +154,10 @@ def self.oppen # @return [Config] def self.wadler(eager_print: true, trim_trailing_whitespaces: true, upsize_stack: true) new( - eager_print: eager_print, + eager_print:, indent_anchor: :current_offset, - trim_trailing_whitespaces: trim_trailing_whitespaces, - upsize_stack: upsize_stack, + trim_trailing_whitespaces:, + upsize_stack:, ) end end @@ -172,7 +172,7 @@ def self.wadler(eager_print: true, trim_trailing_whitespaces: true, upsize_stack # @return [Token::String] # a new String token. def self.string(value, width: value.length) - Token::String.new(value, width: width) + Token::String.new(value, width:) end # @return [Token::Whitespace] a new Whitespace token. @@ -196,7 +196,7 @@ def self.whitespace(value) # # @see Wadler#break example on `line_continuation`. def self.break(str = ' ', line_continuation: '', offset: 0, width: str.length) - Token::Break.new(str, width: width, line_continuation: line_continuation, offset: offset) + Token::Break.new(str, width:, line_continuation:, offset:) end # @param line_continuation [String] @@ -209,7 +209,7 @@ def self.break(str = ' ', line_continuation: '', offset: 0, width: str.length) # # @see Wadler#break example on `line_continuation`. def self.line_break(line_continuation: '', offset: 0) - Token::LineBreak.new(line_continuation: line_continuation, offset: offset) + Token::LineBreak.new(line_continuation:, offset:) end # In a consistent group, the presence of a new line inside the group will @@ -232,7 +232,7 @@ def self.line_break(line_continuation: '', offset: 0) # # @see Wadler#group def self.begin_consistent(offset: 2) - Token::Begin.new(break_type: :consistent, offset: offset) + Token::Begin.new(break_type: :consistent, offset:) end # In an inconsistent group, the presence of a new line inside the group will @@ -251,7 +251,7 @@ def self.begin_consistent(offset: 2) # # @see Wadler#group def self.begin_inconsistent(offset: 2) - Token::Begin.new(break_type: :inconsistent, offset: offset) + Token::Begin.new(break_type: :inconsistent, offset:) end # @return [Token::End] a new End token. diff --git a/lib/oppen/mixins.rb b/lib/oppen/mixins.rb index 2f30bb5..15f56c2 100644 --- a/lib/oppen/mixins.rb +++ b/lib/oppen/mixins.rb @@ -24,7 +24,7 @@ def upsize_circular_array(arr, offset) # @return [String] def tokens_to_wadler(tokens, base_indent: 0, printer_name: 'out', width: tokens.length * 3) - printer = Oppen::Wadler.new(base_indent: base_indent, indent: 2, width: width) + printer = Oppen::Wadler.new(base_indent:, indent: 2, width:) handle_break_token = ->(token) { if token.offset.positive? diff --git a/lib/oppen/print_stack.rb b/lib/oppen/print_stack.rb index 32aadbb..606385c 100644 --- a/lib/oppen/print_stack.rb +++ b/lib/oppen/print_stack.rb @@ -81,7 +81,7 @@ def print(token, token_width, trim_on_break: 0) in Token::End handle_end in Token::Break - handle_break token, token_width, trim_on_break: trim_on_break + handle_break token, token_width, trim_on_break: in Token::String handle_string token, token_width end diff --git a/lib/oppen/printer.rb b/lib/oppen/printer.rb index 4df5bcf..b2d1bb9 100644 --- a/lib/oppen/printer.rb +++ b/lib/oppen/printer.rb @@ -293,7 +293,7 @@ def advance_left(token, token_width) end trim_on_break ||= 0 - print_stack.print(token, token_width, trim_on_break: trim_on_break) + print_stack.print(token, token_width, trim_on_break:) case token when Token::Break diff --git a/lib/oppen/token.rb b/lib/oppen/token.rb index be06d90..1b9dea8 100644 --- a/lib/oppen/token.rb +++ b/lib/oppen/token.rb @@ -74,7 +74,7 @@ def length = 999_999 end def initialize(line_continuation: '', offset: 0) - super(LineBreakString.new, line_continuation: line_continuation, offset: offset) + super(LineBreakString.new, line_continuation:, offset:) end end diff --git a/lib/wadler/print.rb b/lib/wadler/print.rb index 2ead099..e04620d 100644 --- a/lib/wadler/print.rb +++ b/lib/wadler/print.rb @@ -77,12 +77,12 @@ def add_missing_begin_and_end def output add_missing_begin_and_end Oppen.print( - tokens: tokens, - new_line: new_line, - config: config, + tokens:, + new_line:, + config:, space: space_gen, - out: out, - width: width, + out:, + width:, ) end @@ -327,7 +327,7 @@ def text(value, width: value.length) end tokens << Oppen.whitespace(match) else - tokens << Oppen.string(value, width: width) + tokens << Oppen.string(value, width:) end self end @@ -345,7 +345,7 @@ def text(value, width: value.length) # # @see Wadler#break example on `line_continuation`. def breakable(str = ' ', line_continuation: '', width: str.length) - tokens << Oppen.break(str, width: width, line_continuation: line_continuation, offset: current_indent) + tokens << Oppen.break(str, width:, line_continuation:, offset: current_indent) self end @@ -370,7 +370,7 @@ def breakable(str = ' ', line_continuation: '', width: str.length) # # @return [self] def break(line_continuation: '') - tokens << Oppen.line_break(line_continuation: line_continuation, offset: current_indent) + tokens << Oppen.line_break(line_continuation:, offset: current_indent) self end @@ -483,11 +483,11 @@ def separate(args, sep, breakable: ' ', break_pos: :after, first = false elsif break_pos == :after text sep - breakable(breakable, line_continuation: line_continuation) if breakable && !force_break - self.break(line_continuation: line_continuation) if force_break + breakable(breakable, line_continuation:) if breakable && !force_break + self.break(line_continuation:) if force_break else - breakable(breakable, line_continuation: line_continuation) if breakable && !force_break - self.break(line_continuation: line_continuation) if force_break + breakable(breakable, line_continuation:) if breakable && !force_break + self.break(line_continuation:) if force_break text sep end yield(*as) @@ -505,7 +505,7 @@ def separate(args, sep, breakable: ' ', break_pos: :after, body.() } }.end - breakable('', line_continuation: line_continuation) if !line_continuation.empty? && !break_type + breakable('', line_continuation:) if !line_continuation.empty? && !break_type self end @@ -553,7 +553,7 @@ def surround(lft, rgt, **opts) text lft if lft indent = opts.fetch(:indent, @indent) - nest_open(indent: indent) + nest_open(indent:) lft_breakable = opts.fetch(:lft_breakable, '') lft_can_break = opts.fetch(:lft_can_break, true) @@ -598,8 +598,8 @@ def surround(lft, rgt, **opts) # This is a wrapper around {separate} where `breakable: true`. # # @see [separate] - def lines(*args, **kwargs, &block) - separate(*args, **kwargs.merge(force_break: true), &block) + def lines(*args, **kwargs, &) + separate(*args, **kwargs.merge(force_break: true), &) end # Concatenates args. @@ -607,8 +607,8 @@ def lines(*args, **kwargs, &block) # This is a wrapper around {separate} where `breakable: false`. # # @see [separate] - def concat *args, **kwargs, &block - separate(*args, **kwargs.merge(breakable: false), &block) + def concat(*args, **kwargs, &) + separate(*args, **kwargs.merge(breakable: false), &) end # @!endgroup @@ -634,15 +634,15 @@ def angles(padding: '', **kwargs, &block) # {surround} with `< >`. New lines cannot appear after and before the delimiters. # # @return [self] - def angles_break_both(**kwargs, &block) - angles(**kwargs.merge(lft_force_break: true, rgt_force_break: true), &block) + def angles_break_both(**kwargs, &) + angles(**kwargs.merge(lft_force_break: true, rgt_force_break: true), &) end # {surround} with `< >`. New lines will appear after and before the delimiters. # # @return [self] - def angles_break_none(**kwargs, &block) - angles(**kwargs.merge(lft_can_break: false, rgt_can_break: false), &block) + def angles_break_none(**kwargs, &) + angles(**kwargs.merge(lft_can_break: false, rgt_can_break: false), &) end # {surround} with `{ }`. New lines can appear after and before the delimiters. @@ -662,15 +662,15 @@ def braces(padding: '', **kwargs, &block) # {surround} with `{ }`. New lines cannot appear after and before the delimiters. # # @return [self] - def braces_break_both(**kwargs, &block) - braces(**kwargs.merge(lft_force_break: true, rgt_force_break: true), &block) + def braces_break_both(**kwargs, &) + braces(**kwargs.merge(lft_force_break: true, rgt_force_break: true), &) end # {surround} with `{ }`. New lines will appear after and before the delimiters. # # @return [self] - def braces_break_none(**kwargs, &block) - braces(**kwargs.merge(lft_can_break: false, rgt_can_break: false), &block) + def braces_break_none(**kwargs, &) + braces(**kwargs.merge(lft_can_break: false, rgt_can_break: false), &) end # {surround} with `[ ]`. New lines can appear after and before the delimiters. @@ -690,15 +690,15 @@ def brackets(padding: '', **kwargs, &block) # {surround} with `[ ]`. New lines cannot appear after and before the delimiters. # # @return [self] - def brackets_break_both(**kwargs, &block) - brackets(**kwargs.merge(lft_force_break: true, rgt_force_break: true), &block) + def brackets_break_both(**kwargs, &) + brackets(**kwargs.merge(lft_force_break: true, rgt_force_break: true), &) end # {surround} with `[ ]`. New lines will appear after and before the delimiters. # # @return [self] - def brackets_break_none(**kwargs, &block) - brackets(**kwargs.merge(lft_can_break: false, rgt_can_break: false), &block) + def brackets_break_none(**kwargs, &) + brackets(**kwargs.merge(lft_can_break: false, rgt_can_break: false), &) end # {surround} with `( )`. New lines can appear after and before the delimiters. @@ -718,39 +718,39 @@ def parens(padding: '', **kwargs, &block) # {surround} with `( )`. New lines cannot appear after and before the delimiters. # # @return [self] - def parens_break_both(**kwargs, &block) - parens(**kwargs.merge(lft_force_break: true, rgt_force_break: true), &block) + def parens_break_both(**kwargs, &) + parens(**kwargs.merge(lft_force_break: true, rgt_force_break: true), &) end # {surround} with `( )`. New lines will appear after and before the delimiters. # # @return [self] - def parens_break_none(**kwargs, &block) - parens(**kwargs.merge(lft_can_break: false, rgt_can_break: false), &block) + def parens_break_none(**kwargs, &) + parens(**kwargs.merge(lft_can_break: false, rgt_can_break: false), &) end # {surround} with `` ` ` ``. New lines cannot appear after and before the delimiters # unless you specify it with `rgt_can_break` and `lft_can_break`. # # @return [self] - def backticks(**kwargs, &block) - surround('`', '`', lft_can_break: false, rgt_can_break: false, **kwargs, &block) + def backticks(**kwargs, &) + surround('`', '`', lft_can_break: false, rgt_can_break: false, **kwargs, &) end # {surround} with `" "`. New lines cannot appear after and before the delimiters # unless you specify it with `rgt_can_break` and `lft_can_break`. # # @return [self] - def quote_double(**kwargs, &block) - surround('"', '"', lft_can_break: false, rgt_can_break: false, **kwargs, &block) + def quote_double(**kwargs, &) + surround('"', '"', lft_can_break: false, rgt_can_break: false, **kwargs, &) end # {surround} with `' '`. New lines cannot appear after and before the delimiters # unless you specify it with `rgt_can_break` and `lft_can_break`. # # @return [self] - def quote_single(**kwargs, &block) - surround("'", "'", lft_can_break: false, rgt_can_break: false, **kwargs, &block) + def quote_single(**kwargs, &) + surround("'", "'", lft_can_break: false, rgt_can_break: false, **kwargs, &) end # Open a consistent group. diff --git a/oppen.gemspec b/oppen.gemspec index adc25ba..4c79333 100644 --- a/oppen.gemspec +++ b/oppen.gemspec @@ -19,5 +19,5 @@ Gem::Specification.new do |spec| spec.files = Dir['lib/**/*', 'LICENSE', 'README.md'] spec.require_paths = ['lib'] - spec.required_ruby_version = Gem::Requirement.new('>= 3.0') + spec.required_ruby_version = Gem::Requirement.new('>= 3.1') end diff --git a/test/customization_test.rb b/test/customization_test.rb index 33400bf..87798fd 100644 --- a/test/customization_test.rb +++ b/test/customization_test.rb @@ -224,7 +224,7 @@ def nest_delimiter_build_output(printer, delim) printer.text 'foo()' } printer.break - printer.nest(delim: delim, indent: 2) { + printer.nest(delim:, indent: 2) { printer.group { printer.text 'Hello' printer.breakable(', ') @@ -304,7 +304,7 @@ def group_delimiter_build_output(printer, delim) printer.breakable printer.text 'foo()' } - printer.group(delim: delim, indent: 2) { + printer.group(delim:, indent: 2) { printer.group { printer.break printer.text 'Hello' @@ -385,12 +385,12 @@ def line_continuation_build_output(printer, break_type = :consistent, printer.group(break_type, indent: 2) { printer.breakable '' printer.text '1' - printer.breakable ', ', line_continuation: line_continuation + printer.breakable(', ', line_continuation:) printer.text '2' - printer.breakable ', ', line_continuation: line_continuation + printer.breakable(', ', line_continuation:) printer.text '3' } - printer.breakable '', line_continuation: line_continuation + printer.breakable('', line_continuation:) printer.text ']' } end diff --git a/test/indent_anchor_test.rb b/test/indent_anchor_test.rb index e9929ac..f5a7a33 100644 --- a/test/indent_anchor_test.rb +++ b/test/indent_anchor_test.rb @@ -5,11 +5,11 @@ require_relative 'lib' def check_difference_oppen_wadler(width, expected_oppen, expected_wadler, builder_block) - printer = Oppen::Wadler.new(width: width, config: Oppen::Config.oppen) + printer = Oppen::Wadler.new(width:, config: Oppen::Config.oppen) builder_block.(printer) _(printer.output).must_equal expected_oppen, 'Oppen failed the test' - printer = Oppen::Wadler.new(width: width, config: Oppen::Config.wadler) + printer = Oppen::Wadler.new(width:, config: Oppen::Config.wadler) builder_block.(printer) _(printer.output).must_equal expected_wadler, 'Wadler failed the test' end diff --git a/test/lib.rb b/test/lib.rb index 6cda1dc..bcd550a 100644 --- a/test/lib.rb +++ b/test/lib.rb @@ -41,7 +41,7 @@ def break end def assert_wadler(width, expected, builder_block, indent: 0) - printer = Oppen::Wadler.new(indent: indent, width: width) + printer = Oppen::Wadler.new(indent:, width:) builder_block.(printer) _(printer.output).must_equal expected, 'Oppen failed the test' end diff --git a/test/trim_trailing_whitespaces_test.rb b/test/trim_trailing_whitespaces_test.rb index b605025..6dcedeb 100644 --- a/test/trim_trailing_whitespaces_test.rb +++ b/test/trim_trailing_whitespaces_test.rb @@ -59,7 +59,7 @@ }, ].each do |test| it test[:title] do - printer = Oppen::Wadler.new(width: 5, whitespace: whitespace) + printer = Oppen::Wadler.new(width: 5, whitespace:) test[:block].(printer) _(printer.output).must_equal test[:expected] end @@ -191,11 +191,11 @@ }, ].each do |test| it test[:title] do - printer = Oppen::Wadler.new(width: 5, whitespace: whitespace) + printer = Oppen::Wadler.new(width: 5, whitespace:) test[:block].(printer) _(printer.output).must_equal test[:expected] - printer_no_trailing = Oppen::Wadler.new(width: 5, whitespace: whitespace) + printer_no_trailing = Oppen::Wadler.new(width: 5, whitespace:) test[:block].(printer_no_trailing, false) _(printer_no_trailing.output).must_equal test[:expected] end