Class: PuppetLanguageServer::Manifest::DocumentFormattingProvider

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet-languageserver/manifest/document_formatting_provider.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.instanceObject



10
11
12
# File 'lib/puppet-languageserver/manifest/document_formatting_provider.rb', line 10

def instance
  @instance ||= new
end

Instance Method Details

#format(content, formatting_options, max_filesize = 4096) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/puppet-languageserver/manifest/document_formatting_provider.rb', line 15

def format(content, formatting_options, max_filesize = 4096)
  return [] unless formatting_options['insertSpaces'] == true
  return [] if !max_filesize.zero? && (content.length > max_filesize)

  lexer = PuppetLint::Lexer.new
  tokens = lexer.tokenise(content)
  edits = {}

  tokens.select { |token| token.type == :FARROW }.each do |token|
    FormatOnTypeProvider.instance.format(
      content,
      token.line - 1,
      token.column,
      '>',
      formatting_options,
      max_filesize
    ).each do |edit|
      edits[edit.to_h] = edit
    end
  end

  edits.values.sort_by do |edit|
    [edit.range.start.line, edit.range.start.character, edit.range.end.line, edit.range.end.character]
  end
end