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 = []
return result unless trigger_character == '>'
return result unless formatting_options['insertSpaces'] == true
return result if !max_filesize.zero? && (content.length > max_filesize)
lexer = PuppetLint::Lexer.new
tokens = lexer.tokenise(content)
cursor_token = find_token_by_location(tokens, line, char)
return result if cursor_token.nil?
return result unless cursor_token.type == :FARROW
start_brace = cursor_token.prev_token_of(%i[LBRACE SEMIC], skip_blocks: true)
end_brace = cursor_token.next_token_of(%i[RBRACE SEMIC], skip_blocks: true)
return result if end_brace.nil? || start_brace.nil? || end_brace.line - start_brace.line <= 2
farrows = []
farrow_token = start_brace
lines = []
loop do
farrow_token = farrow_token.next_token_of(:FARROW, skip_blocks: true)
break if farrow_token.nil? || farrow_token.line > end_brace.line
break if farrow_token.line == end_brace.line && farrow_token.character > end_brace.character
return result if lines.include?(farrow_token.line)
lines << farrow_token.line
farrows << { token: farrow_token }
end
farrows.each do |item|
item.merge!(calculate_indentation_info(item[:token]))
end
max_indent = -1
farrows.each do |info|
max_indent = info[:indent] if info[:indent] > max_indent
end
return result if max_indent == -1
farrows.each do |info|
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
next if begin_farrow_token - end_name_token == new_whitespace
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
|