Class: RubyLsp::Requests::Formatting
- Extended by:
- T::Sig
- Defined in:
- lib/ruby_lsp/requests/formatting.rb
Overview

The 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.
The rubyLsp.formatter setting specifies which formatter to use.
If set to auto then it behaves as follows:
- It will use RuboCop if it is part of the bundle.
- If RuboCop is not available, and
syntax_treeis a direct dependency, it will use that. - Otherwise, no formatting will be applied.
Example
def say_hello
puts "Hello" # --> formatting: fixes the indentation on save
end
Defined Under Namespace
Classes: Error
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(global_state, document) ⇒ Formatting
constructor
A new instance of Formatting.
- #perform ⇒ Object
Constructor Details
#initialize(global_state, document) ⇒ Formatting
Returns a new instance of Formatting.
44 45 46 47 48 49 |
# File 'lib/ruby_lsp/requests/formatting.rb', line 44 def initialize(global_state, document) super() @document = document @active_formatter = T.let(global_state.active_formatter, T.nilable(Support::Formatter)) @uri = T.let(document.uri, URI::Generic) end |
Class Method Details
Instance Method Details
#perform ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/ruby_lsp/requests/formatting.rb', line 52 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 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: size, character: size), ), new_text: formatted_text, ), ] end |