Skip to content

Instantly share code, notes, and snippets.

@swarley
Created January 6, 2013 20:27
Show Gist options
  • Select an option

  • Save swarley/4470003 to your computer and use it in GitHub Desktop.

Select an option

Save swarley/4470003 to your computer and use it in GitHub Desktop.
def indent(input)
@lexer.set_input StringIO.new(input)
output = ""
last_seek = 0
seek = 0
while token = @lexer.token
# Check for newline
if token.is_a? RubyToken::TkNL
seek = token.seek
# Take all characters that were from the previous line and add them
# to the output. I'm not sure if eating the spaces at the start would
# be proper in this case.
output << input[last_seek..seek]
indent_level = @lexer.indent
# This check is because when RubyLex is complete, it returns a -1
# indent.
@indent_level = if indent_level > 0 then (SPACES * indent_level) else ('') end
# Add the indent prefix.
output << indent_level
# Shift a character.
last_seek = seek+1
end
end
output
end
# To get indent level, you would pass the string to indent, and then access the attribute
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment