11module PuppetLanguageServer
22 module PuppetParserHelper
3- def self . remove_char_at ( content , line_offsets , line_num , char_num )
3+ def self . remove_chars_starting_at ( content , line_offsets , line_num , char_num , num_chars_to_remove )
44 line_offset = line_offsets [ line_num ]
55 raise if line_offset . nil?
66
77 # Remove the offending character
8- new_content = content . slice ( 0 , line_offset + char_num - 1 ) + content . slice ( line_offset + char_num , content . length - 1 )
8+ new_content = content . slice ( 0 , line_offset + char_num - num_chars_to_remove ) + content . slice ( line_offset + char_num , content . length - num_chars_to_remove )
99
1010 new_content
1111 end
1212
13+ def self . remove_char_at ( content , line_offsets , line_num , char_num )
14+ remove_chars_starting_at ( content , line_offsets , line_num , char_num , 1 )
15+ end
16+
17+ def self . get_char_at ( content , line_offsets , line_num , char_num )
18+ line_offset = line_offsets [ line_num ]
19+ raise if line_offset . nil?
20+
21+ absolute_offset = line_offset + ( char_num - 1 )
22+
23+ content [ absolute_offset ]
24+ end
25+
1326 def self . insert_text_at ( content , line_offsets , line_num , char_num , text )
1427 # Insert text after where the cursor is
1528 # This helps due to syntax errors like `$facts[]` or `ensure =>`
@@ -43,7 +56,7 @@ def self.get_line_at(content, line_offsets, line_num)
4356 end
4457 end
4558
46- def self . object_under_cursor ( content , line_num , char_num , multiple_attempts = false )
59+ def self . object_under_cursor ( content , line_num , char_num , multiple_attempts = false , disallowed_classes = [ ] )
4760 # Use Puppet to generate the AST
4861 parser = Puppet ::Pops ::Parser ::Parser . new
4962
@@ -53,14 +66,25 @@ def self.object_under_cursor(content, line_num, char_num, multiple_attempts = fa
5366
5467 result = nil
5568 move_offset = 0
56- %i[ noop remove_char try_quotes try_quotes_and_comma ] . each do |method |
69+ %i[ noop remove_word try_quotes try_quotes_and_comma remove_char ] . each do |method |
5770 new_content = nil
5871 case method
5972 when :noop
6073 new_content = content
6174 when :remove_char
6275 new_content = remove_char_at ( content , line_offsets , line_num , char_num )
6376 move_offset = -1
77+ when :remove_word
78+ next_char = get_char_at ( content , line_offsets , line_num , char_num )
79+
80+ while /[[:word:]]/ . match ( next_char )
81+ move_offset -= 1
82+ next_char = get_char_at ( content , line_offsets , line_num , char_num + move_offset )
83+
84+ break if char_num + move_offset < 0
85+ end
86+
87+ new_content = remove_chars_starting_at ( content , line_offsets , line_num , char_num , -move_offset )
6488 when :try_quotes
6589 # Perhaps try inserting double quotes. Useful in empty arrays or during variable assignment
6690 # Grab the line up to the cursor character + 1
@@ -98,7 +122,7 @@ def self.object_under_cursor(content, line_num, char_num, multiple_attempts = fa
98122 # If during paring we modified the source we may need to change the cursor location
99123 abs_offset = result . line_offsets [ line_num ] + char_num + move_offset
100124 # Typically we're completing after something was typed, so go back one char
101- abs_offset -= 1 if abs_offset > 0
125+ abs_offset -= 1
102126
103127 # Enumerate the AST looking for items that span the line/char we want.
104128 # Once we have all valid items, sort them by the smallest span. Typically the smallest span
@@ -107,7 +131,7 @@ def self.object_under_cursor(content, line_num, char_num, multiple_attempts = fa
107131 # TODO: Should probably walk the AST and only look for the deepest child, but integer sorting
108132 # is so much easier and faster.
109133 valid_models = result . model . eAllContents . select do |item |
110- !item . offset . nil? && !item . length . nil? && abs_offset >= item . offset && abs_offset <= item . offset + item . length
134+ !item . offset . nil? && !item . length . nil? && abs_offset >= item . offset && abs_offset <= item . offset + item . length && ! disallowed_classes . include? ( item . class )
111135 end
112136 valid_models . sort! { |a , b | a . length - b . length }
113137
0 commit comments