Class: RubyLsp::Requests::RangeFormatting
- Defined in:
- lib/ruby_lsp/requests/range_formatting.rb
Overview
The [range formatting](microsoft.github.io/language-server-protocol/specification#textDocument_rangeFormatting) is used to format a selection or to format on paste.
Instance Method Summary collapse
-
#initialize(global_state, document, params) ⇒ RangeFormatting
constructor
: (GlobalState global_state, RubyDocument document, Hash[Symbol, untyped] params) -> void.
-
#perform ⇒ Object
: -> Array?.
Constructor Details
#initialize(global_state, document, params) ⇒ RangeFormatting
: (GlobalState global_state, RubyDocument document, Hash[Symbol, untyped] params) -> void
10 11 12 13 14 15 16 |
# File 'lib/ruby_lsp/requests/range_formatting.rb', line 10 def initialize(global_state, document, params) super() @document = document @uri = document.uri #: URI::Generic @params = params @active_formatter = global_state.active_formatter #: Support::Formatter? end |
Instance Method Details
#perform ⇒ Object
: -> Array?
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 |
# File 'lib/ruby_lsp/requests/range_formatting.rb', line 20 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 |