Method: Hocon::Impl::ConfigNodeObject#indentation

Defined in:
lib/hocon/impl/config_node_object.rb

#indentationObject



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/hocon/impl/config_node_object.rb', line 125

def indentation
  seen_new_line = false
  indentation = []

  if @children.empty?
    return indentation
  end

  @children.each_index do |i|
    unless seen_new_line
      if @children[i].is_a?(Hocon::Impl::ConfigNodeSingleToken) && Tokens.newline?(@children[i].token)
        seen_new_line = true
        indentation.push(Hocon::Impl::ConfigNodeSingleToken.new(Tokens.new_line(nil)))
      end
    else
      if @children[i].is_a?(Hocon::Impl::ConfigNodeSingleToken) &&
          Tokens.ignored_whitespace?(@children[i].token) &&
          i + 1 < @children.size &&
          (@children[i + 1].is_a?(Hocon::Impl::ConfigNodeField) || @children[i + 1].is_a?(Hocon::Impl::ConfigNodeInclude))
        # Return the indentation of the first setting on its own line
        indentation.push(@children[i])
        return indentation
      end
    end
  end
  if indentation.empty?
    indentation.push(Hocon::Impl::ConfigNodeSingleToken.new(Tokens.new_ignored_whitespace(nil, " ")))
    return indentation
  else
    # Calculate the indentation of the ending curly-brace to get the indentation of the root object
    last = @children[-1]
    if last.is_a?(Hocon::Impl::ConfigNodeSingleToken) && last.token.equal?(Tokens::CLOSE_CURLY)
      beforeLast = @children[-2]
      indent = ""
      if beforeLast.is_a?(Hocon::Impl::ConfigNodeSingleToken) &&
          Tokens.ignored_whitespace?(beforeLast.token)
        indent = beforeLast.token.token_text
      end
      indent += "  "
      indentation.push(Hocon::Impl::ConfigNodeSingleToken.new(Tokens.new_ignored_whitespace(nil, indent)))
      return indentation
    end
  end

  # The object has no curly braces and is at the root level, so don't indent
  indentation
end