Sha256: 44b368524644b21a91dd431cbfed1caec24a6d93103b73a2b84627dbefc09a61

Contents?: true

Size: 1.81 KB

Versions: 8

Compression:

Stored size: 1.81 KB

Contents

# typed: strict
# frozen_string_literal: true

module RubyLsp
  module Requests
    # The [range formatting](https://microsoft.github.io/language-server-protocol/specification#textDocument_rangeFormatting)
    # is used to format a selection or to format on paste.
    class RangeFormatting < Request
      extend T::Sig

      sig { params(global_state: GlobalState, document: RubyDocument, params: T::Hash[Symbol, T.untyped]).void }
      def initialize(global_state, document, params)
        super()
        @document = document
        @uri = T.let(document.uri, URI::Generic)
        @params = params
        @active_formatter = T.let(global_state.active_formatter, T.nilable(Support::Formatter))
      end

      sig { override.returns(T.nilable(T::Array[Interface::TextEdit])) }
      def perform
        return unless @active_formatter
        return if @document.syntax_error?

        target = @document.locate_first_within_range(@params[:range])
        return unless target

        location = target.location

        formatted_text = @active_formatter.run_range_formatting(
          @uri,
          target.slice,
          location.start_column / 2,
        )
        return unless formatted_text

        code_units_cache = @document.code_units_cache

        [
          Interface::TextEdit.new(
            range: Interface::Range.new(
              start: Interface::Position.new(
                line: location.start_line - 1,
                character: location.cached_start_code_units_column(code_units_cache),
              ),
              end: Interface::Position.new(
                line: location.end_line - 1,
                character: location.cached_end_code_units_column(code_units_cache),
              ),
            ),
            new_text: formatted_text.strip,
          ),
        ]
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
ruby-lsp-0.22.1 lib/ruby_lsp/requests/range_formatting.rb
ruby-lsp-0.22.0 lib/ruby_lsp/requests/range_formatting.rb
ruby-lsp-0.21.3 lib/ruby_lsp/requests/range_formatting.rb
ruby-lsp-0.21.2 lib/ruby_lsp/requests/range_formatting.rb
ruby-lsp-0.21.1 lib/ruby_lsp/requests/range_formatting.rb
ruby-lsp-0.21.0 lib/ruby_lsp/requests/range_formatting.rb
ruby-lsp-0.20.1 lib/ruby_lsp/requests/range_formatting.rb
ruby-lsp-0.20.0 lib/ruby_lsp/requests/range_formatting.rb