Class: PuppetLanguageServer::Manifest::FormatOnTypeProvider

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.instanceObject



9
10
11
# File 'lib/puppet-languageserver/manifest/format_on_type_provider.rb', line 9

def instance
  @instance ||= new
end

Instance Method Details

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



14
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/puppet-languageserver/manifest/format_on_type_provider.rb', line 14

def format(content, line, char, trigger_character, formatting_options, max_filesize = 4096)
  result = []
  # Abort if the user has pressed something other than `>`
  return result unless trigger_character == '>'
  # Abort if the formatting is tab based. Can't do that yet
  return result unless formatting_options['insertSpaces'] == true
  # Abort if content is too big
  return result if !max_filesize.zero? && (content.length > max_filesize)

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

  # Find where in the manifest the cursor is
  cursor_token = find_token_by_location(tokens, line, char)
  return result if cursor_token.nil?
  # The cursor should be at the end of a hashrocket, otherwise exit
  return result unless cursor_token.type == :FARROW

  # Find the start of the hash (or semicolon for multi-resource definitions) with respect to the cursor
  start_brace = cursor_token.prev_token_of(%i[LBRACE SEMIC], skip_blocks: true)
  # Find the end of the hash (or semicolon for multi-resource definitions) with respect to the cursor
  end_brace = cursor_token.next_token_of(%i[RBRACE SEMIC], skip_blocks: true)

  # The line count between the start and end brace needs to be at least 2 lines. Otherwise there's nothing to align to
  return result if end_brace.nil? || start_brace.nil? || end_brace.line - start_brace.line <= 2

  # Find all hashrockets '=>' between the hash braces, ignoring nested hashes
  farrows = []
  farrow_token = start_brace
  lines = []
  loop do
    farrow_token = farrow_token.next_token_of(:FARROW, skip_blocks: true)
    # if there are no more hashrockets, or we've gone past the end_brace, we can exit the loop
    break if farrow_token.nil? || farrow_token.line > end_brace.line
    # if there's a hashrocket AFTER the closing brace (why?) then we can also exit the loop
    break if farrow_token.line == end_brace.line && farrow_token.character > end_brace.character
    # Check for multiple hashrockets on the same line. If we find some, then we can't do any automated indentation
    return result if lines.include?(farrow_token.line)

    lines << farrow_token.line
    farrows << { token: farrow_token }
  end

  # Now we have a list of farrows, time for figure out the indentation marks
  farrows.each do |item|
    item.merge!(calculate_indentation_info(item[:token]))
  end

  # Now we have the list of indentations we can find the biggest
  max_indent = -1
  farrows.each do |info|
    max_indent = info[:indent] if info[:indent] > max_indent
  end
  # No valid indentations found
  return result if max_indent == -1

  # Now we have the indent size, generate all of the required TextEdits
  farrows.each do |info|
    # Ignore invalid hashrockets
    next if info[:indent] == -1

    end_name_token = info[:name_token].column + info[:name_token].to_manifest.length
    begin_farrow_token = info[:token].column
    new_whitespace = max_indent - end_name_token
    # If the whitespace is already what we want, then ignore it.
    next if begin_farrow_token - end_name_token == new_whitespace

    # Create the TextEdit
    result << LSP::TextEdit.new.from_h!(
      'newText' => ' ' * new_whitespace,
      'range' => LSP.create_range(info[:token].line - 1, end_name_token - 1, info[:token].line - 1, begin_farrow_token - 1)
    )
  end
  result
end