@@ -56,13 +56,19 @@ def self.get_line_at(content, line_offsets, line_num)
5656 end
5757 end
5858
59- def self . object_under_cursor ( content , line_num , char_num , multiple_attempts = false , disallowed_classes = [ ] )
59+ def self . object_under_cursor ( content , line_num , char_num , options )
60+ options = {
61+ :multiple_attempts => false ,
62+ :disallowed_classes => [ ] ,
63+ :tasks_mode => false
64+ } . merge ( options )
65+
6066 # Use Puppet to generate the AST
6167 parser = Puppet ::Pops ::Parser ::Parser . new
6268
6369 # Calculating the line offsets can be expensive and is only required
6470 # if we're doing mulitple passes of parsing
65- line_offsets = line_offsets ( content ) if multiple_attempts
71+ line_offsets = line_offsets ( content ) if options [ : multiple_attempts]
6672
6773 result = nil
6874 move_offset = 0
@@ -72,9 +78,11 @@ def self.object_under_cursor(content, line_num, char_num, multiple_attempts = fa
7278 when :noop
7379 new_content = content
7480 when :remove_char
81+ next if line_num . zero? && char_num . zero?
7582 new_content = remove_char_at ( content , line_offsets , line_num , char_num )
7683 move_offset = -1
7784 when :remove_word
85+ next if line_num . zero? && char_num . zero?
7886 next_char = get_char_at ( content , line_offsets , line_num , char_num )
7987
8088 while /[[:word:]]/ =~ next_char
@@ -106,10 +114,10 @@ def self.object_under_cursor(content, line_num, char_num, multiple_attempts = fa
106114 next if new_content . nil?
107115
108116 begin
109- result = parser . parse_string ( new_content , '' )
117+ result = parser . singleton_parse_string ( new_content , options [ :tasks_mode ] , '' )
110118 break
111119 rescue Puppet ::ParseErrorWithIssue => _exception
112- next if multiple_attempts
120+ next if options [ : multiple_attempts]
113121 raise
114122 end
115123 end
@@ -138,14 +146,14 @@ def self.object_under_cursor(content, line_num, char_num, multiple_attempts = fa
138146 valid_models = [ ]
139147 if result . model . respond_to? :eAllContents
140148 valid_models = result . model . eAllContents . select do |item |
141- check_for_valid_item ( item , abs_offset , disallowed_classes )
149+ check_for_valid_item ( item , abs_offset , options [ : disallowed_classes] )
142150 end
143151
144152 valid_models . sort! { |a , b | a . length - b . length }
145153 else
146154 path = [ ]
147155 result . model . _pcore_all_contents ( path ) do |item |
148- if check_for_valid_item ( item , abs_offset , disallowed_classes ) # rubocop:disable Style/IfUnlessModifier Nicer to read like this
156+ if check_for_valid_item ( item , abs_offset , options [ : disallowed_classes] ) # rubocop:disable Style/IfUnlessModifier Nicer to read like this
149157 valid_models . push ( model_path_struct . new ( item , path . dup ) )
150158 end
151159 end
0 commit comments