Class: Rfmt::LSP::Formatter

Inherits:
Object
  • Object
show all
Defined in:
lib/rfmt/lsp/formatter.rb

Class Method Summary collapse

Class Method Details

.format_edits(source) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/rfmt/lsp/formatter.rb', line 8

def self.format_edits(source)
  formatted = source.empty? ? "\n" : Rfmt.format(source)
  return [] if formatted == source

  [
    {
      range: full_document_range(source),
      newText: formatted
    }
  ]
rescue Rfmt::Error
  []
end

.full_document_range(source) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/rfmt/lsp/formatter.rb', line 22

def self.full_document_range(source)
  return { start: { line: 0, character: 0 }, end: { line: 0, character: 0 } } if source.empty?

  lines = source.split("\n", -1)

  {
    start: { line: 0, character: 0 },
    end: {
      line: lines.length - 1,
      character: lines.last.length
    }
  }
end