Class: ThemeCheck::LanguageServer::VariableLookupFinder::LiquidFixer

Inherits:
Object
  • Object
show all
Includes:
Constants
Defined in:
lib/theme_check/language_server/variable_lookup_finder/liquid_fixer.rb

Overview

Attempt to turn the code of the token until the cursor position into valid liquid code.

Constant Summary

Constants included from Constants

Constants::ANY_ENDING_TAG, Constants::ANY_STARTING_TAG, Constants::ENDS_IN_BRACKET_POSITION_THAT_CANT_BE_COMPLETED, Constants::ENDS_WITH_BLANK_POTENTIAL_LOOKUP, Constants::ENDS_WITH_POTENTIAL_LOOKUP, Constants::SYMBOLS_PRECEDING_POTENTIAL_LOOKUPS, Constants::UNCLOSED_SQUARE_BRACKET, Constants::VARIABLE_LOOKUP, Constants::VARIABLE_LOOKUP_CHARACTERS, Constants::VARIABLE_START

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(content, cursor = nil) ⇒ LiquidFixer

Returns a new instance of LiquidFixer.



15
16
17
18
# File 'lib/theme_check/language_server/variable_lookup_finder/liquid_fixer.rb', line 15

def initialize(content, cursor = nil)
  @content = content
  @cursor = cursor || content.size
end

Instance Attribute Details

#contentObject (readonly)

Returns the value of attribute content.



13
14
15
# File 'lib/theme_check/language_server/variable_lookup_finder/liquid_fixer.rb', line 13

def content
  @content
end

#cursorObject (readonly)

Returns the value of attribute cursor.



13
14
15
# File 'lib/theme_check/language_server/variable_lookup_finder/liquid_fixer.rb', line 13

def cursor
  @cursor
end

Instance Method Details

#parsableObject



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
# File 'lib/theme_check/language_server/variable_lookup_finder/liquid_fixer.rb', line 20

def parsable
  # Welcome to Hackcity
  @markup = content[0...cursor]

  catch(:empty_lookup_markup) do
    # close open delimiters
    @markup += "'" if @markup.count("'").odd?
    @markup += '"' if @markup.count('"').odd?
    @markup += "]" if @markup =~ UNCLOSED_SQUARE_BRACKET

    @ends_with_blank_potential_lookup = @markup =~ ENDS_WITH_BLANK_POTENTIAL_LOOKUP
    @markup = last_line if liquid_tag?

    @markup = "{% #{@markup}" unless has_start_tag?

    # close the tag
    @markup += tag_end unless has_end_tag?

    # close if statements
    @markup += '{% endif %}' if tag?('if')

    # close unless statements
    @markup += '{% endunless %}' if tag?('unless')

    # close elsif statements
    @markup = "{% if x %}#{@markup}{% endif %}" if tag?('elsif')

    # close case statements
    @markup += '{% endcase %}' if tag?('case')

    # close when statements
    @markup = "{% case x %}#{@markup}{% endcase %}" if tag?('when')

    # close for statements
    @markup += '{% endfor %}' if tag?('for')

    # close tablerow statements
    @markup += '{% endtablerow %}' if tag?('tablerow')

    @markup
  end
end