Class: RubyLsp::Requests::Formatting
- Defined in:
- lib/ruby_lsp/requests/formatting.rb
Overview
The [formatting](microsoft.github.io/language-server-protocol/specification#textDocument_formatting) request uses RuboCop to fix auto-correctable offenses in the document. This requires enabling format on save and registering the ruby-lsp as the Ruby formatter.
Defined Under Namespace
Classes: Error
Class Method Summary collapse
-
.provider ⇒ Object
: -> TrueClass.
Instance Method Summary collapse
-
#initialize(global_state, document) ⇒ Formatting
constructor
: (GlobalState global_state, RubyDocument document) -> void.
-
#perform ⇒ Object
: -> (Array & Object)?.
Constructor Details
#initialize(global_state, document) ⇒ Formatting
: (GlobalState global_state, RubyDocument document) -> void
20 21 22 23 24 25 |
# File 'lib/ruby_lsp/requests/formatting.rb', line 20 def initialize(global_state, document) super() @document = document @active_formatter = global_state.active_formatter #: Support::Formatter? @uri = document.uri #: URI::Generic end |
Class Method Details
.provider ⇒ Object
: -> TrueClass
14 15 16 |
# File 'lib/ruby_lsp/requests/formatting.rb', line 14 def provider true end |
Instance Method Details
#perform ⇒ Object
: -> (Array & Object)?
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/ruby_lsp/requests/formatting.rb', line 29 def perform return unless @active_formatter return if @document.syntax_error? # We don't format erb documents yet formatted_text = @active_formatter.run_formatting(@uri, @document) return unless formatted_text lines = @document.source.lines size = @document.source.size return if formatted_text.size == size && formatted_text == @document.source [ Interface::TextEdit.new( range: Interface::Range.new( start: Interface::Position.new(line: 0, character: 0), end: Interface::Position.new(line: lines.size, character: 0), ), new_text: formatted_text, ), ] end |