class RubyLsp::Requests::Formatting
“‘
end
puts “Hello” # –> formatting: fixes the indentation on save
def say_hello
“`ruby
# Example
* Otherwise, no formatting will be applied.
* If RuboCop is not available, and `syntax_tree` is a direct dependency, it will use that.
* It will use RuboCop if it is part of the bundle.
If set to `auto` then it behaves as follows:
The `rubyLsp.formatter` setting specifies which formatter to use.
registering the ruby-lsp as the Ruby formatter.
request uses RuboCop to fix auto-correctable offenses in the document. This requires enabling format on save and
The [formatting](microsoft.github.io/language-server-protocol/specification#textDocument_formatting)

def formatted_file
def formatted_file formatter_runner = Formatting.formatters[@formatter] raise InvalidFormatter, "Formatter is not available: #{@formatter}" unless formatter_runner formatter_runner.run(@uri, @document) end
def initialize(document, formatter: "auto")
def initialize(document, formatter: "auto") super() @document = document @uri = T.let(document.uri, URI::Generic) @formatter = formatter end
def perform
def perform return if @formatter == "none" return if @document.syntax_error? formatted_text = formatted_file 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
def register_formatter(identifier, instance)
def register_formatter(identifier, instance) @formatters[identifier] = instance end