File: range_formatting_test.rb

package info (click to toggle)
ruby-ruby-lsp 0.26.7-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 27,676 kB
  • sloc: ruby: 35,294; javascript: 29; sh: 7; makefile: 4
file content (60 lines) | stat: -rw-r--r-- 1,520 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# typed: true
# frozen_string_literal: true

require "test_helper"

class RangeFormattingTest < Minitest::Test
  def setup
    @global_state = RubyLsp::GlobalState.new
    @global_state.formatter = "syntax_tree"
    regular_formatter = RubyLsp::Requests::Support::SyntaxTreeFormatter.new
    @global_state.register_formatter("syntax_tree", regular_formatter)
    @global_state.stubs(:active_formatter).returns(regular_formatter)
    source = +<<~RUBY
      class Foo

        def foo
          [
          1,
          2,
          3,
          4,
          ]
        end
      end
    RUBY
    @document = RubyLsp::RubyDocument.new(
      source: source,
      version: 1,
      uri: URI::Generic.from_path(path: __FILE__),
      global_state: @global_state,
    )
  end

  def test_syntax_tree_supports_range_formatting
    # Note how only the selected array is formatted, otherwise the blank lines would be removed
    expect_formatted_range({ start: { line: 3, character: 2 }, end: { line: 8, character: 5 } }, <<~RUBY)
      class Foo

        def foo
          [1, 2, 3, 4]
        end
      end
    RUBY
  end

  private

  def expect_formatted_range(range, expected)
    edits = RubyLsp::Requests::RangeFormatting.new(@global_state, @document, { range: range }).perform #: as !nil

    @document.push_edits(
      edits.map do |edit|
        { range: edit.range.to_hash.transform_values(&:to_hash), text: edit.new_text }
      end,
      version: 2,
    )

    assert_equal(expected, @document.source)
  end
end