class

CodeWriter

Inherits Reference < Object

TODO: Write documentation for CodeWriter

Constants

CHARS = { back_slash: '\\', forward_slash: '/', newline: '\n', carriage_return: '\r', asterisk: '*', double_quote: '"', single_quote: '\'', back_tick: '`', open_brace: '{', close_brace: '}', dollar_sign: '$', space: ' ', tab: '\t', }
EXTENSION_TO_LANGUAGE = {"cr" => LANGUAGES["crystal"], "py" => LANGUAGES["python"], "js" => LANGUAGES["javascript"], "jsx" => LANGUAGES["javascript"], "ejs" => LANGUAGES["javascript"], "ts" => LANGUAGES["typescript"], "tsx" => LANGUAGES["typescript"], "c" => LANGUAGES["c"], "c++" => LANGUAGES["c++"], "c#" => LANGUAGES["c#"], "java" => LANGUAGES["java"]}
LANGUAGES = {"crystal" => LanguageSettings.new(block_start: "", block_end: "end", inline_block_start: "{", inline_block_end: "}", comment_start: "#", space_before_block_start: false), "python" => LanguageSettings.new(block_start: ":", block_end: "", comment_start: "#", space_before_block_start: false), "c" => LanguageSettings.new(block_start: "{", block_end: "}", inline_block_start: "{", inline_block_end: "}", comment_start: "//", multiline_comment_start: "/*", multiline_comment_end: "*/", space_before_block_start: true), "c++" => LanguageSettings.new(block_start: "{", block_end: "}", inline_block_start: "{", inline_block_end: "}", comment_start: "//", multiline_comment_start: "/*", multiline_comment_end: "*/", space_before_block_start: true), "c#" => LanguageSettings.new(block_start: "{", block_end: "}", inline_block_start: "{", inline_block_end: "}", comment_start: "//", multiline_comment_start: "/*", multiline_comment_end: "*/", space_before_block_start: true), "java" => LanguageSettings.new(block_start: "{", block_end: "}", inline_block_start: "{", inline_block_end: "}", comment_start: "//", multiline_comment_start: "/*", multiline_comment_end: "*/", space_before_block_start: true), "javascript" => LanguageSettings.new(block_start: "{", block_end: "}", inline_block_start: "{", inline_block_end: "}", comment_start: "//", multiline_comment_start: "/*", multiline_comment_middle: " *", multiline_comment_end: "*/", space_before_block_start: true), "typescript" => LanguageSettings.new(block_start: "{", block_end: "}", inline_block_start: "{", inline_block_end: "}", comment_start: "//", multiline_comment_start: "/*", multiline_comment_end: "*/", space_before_block_start: true)}
NEWLINE_RE = /(\r?\n)/
SHOULD_HANDLE = Set.new([CHARS["back_slash"], CHARS["forward_slash"], CHARS["new_line"], CHARS["carriage_return"], CHARS["asterisk"], CHARS["double_quote"], CHARS["single_quote"], CHARS["back_tick"], CHARS["open_brace"], CHARS["close_brace"]])

Constructors

new(*, buffer : IO = IO::Memory.new, newline_text : String = "\n", tab_count : Int32 = 4, indent_style : IndentStyle = IndentStyle::Spaces, quote_style : QuoteStyle = QuoteStyle::Double, language_settings : LanguageSettings = LANGUAGES["crystal"])
Source

Class methods

escape_char(str : String, char : Char)
Source
escape_quote(str : String, quote : Char)
Source
get_string(string : String | -> String)
Source

Instance methods

blank_line

Add a blank line to the internal buffer.

Source
blank_line_if_last_not

Add a blank line to the internal buffer, only if the last character written was not a newline.

Source
block(first_line = nil, &block : -> )

Create a block using the current language's block syntax. Sets the correct opening and closing characters, and indents the block contents.

writer.puts("def foo").block do
  writer.puts("# do something")
end

This will output:

def foo
  # do something
end
Source
block_end(*args, **options)
Source
block_end(*args, **options, &)
Source
block_start(*args, **options)
Source
block_start(*args, **options, &)
Source
comment(str)

Create a single comment using the current language's comment syntax.

writer.comment("This is a comment")

This will output:

# This is a comment
Source
comment(*, pad : Bool = false, &)

Create a multiline comment using the current language's comment syntax.

writer.comment do
  writer.print("This is a comment")
  writer.print("This is another comment")
end

This will output:

# This is a comment
# This is another comment
Source
comment_start(*args, **options)
Source
comment_start(*args, **options, &)
Source
dedent(amount : Int32 = 1)

Subtract from the indentation level for the next lines.

Source
dedent(amount : Int32 = 1, &)

Subtract from the indentation level for the given block, then reset it once the block exits.

Source
indent(amount : Int32 = 1)

Set the indentation level for the next lines.

Source
indent(amount : Int32 = 1, &)

Set the indentation level for the given block, then reset it once the block exits.

Source
indent_text

Get the indentation string given the current indent level.

Source
inline_block(pre_block = nil, &block : -> )

Create an inline block using the current language's block syntax. Sets the correct opening and closing characters, and keeps the block contents on the same line.

writer.print("foo.bar").inline_block do
  writer.print("|x| x + 1")
end

This will output:

foo.bar { |x| x + 1 }
Source
inline_block_end(*args, **options)
Source
inline_block_end(*args, **options, &)
Source
inline_block_start(*args, **options)
Source
inline_block_start(*args, **options, &)
Source
language_settings
Source
language_settings=(language_settings : LanguageSettings)
Source
last_n(bytes : Int32 = 1)

Get the last n bytes from the internal buffer.

Source
multiline_comment_end(*args, **options)
Source
multiline_comment_end(*args, **options, &)
Source
multiline_comment_middle(*args, **options)
Source
multiline_comment_middle(*args, **options, &)
Source
multiline_comment_start(*args, **options)
Source
multiline_comment_start(*args, **options, &)
Source
newline

Add a newline to the internal buffer.

Source
newline_if_last_not

Add a newline to the internal buffer, only if the last character written was not a newline.

Source
pos

Get the position of the internal buffer.

Source
puts(str)

Write text with a trailing newline, assuming the text doesn't already end with a newline.

Source
puts(str, *args)

Write text with a trailing newline, assuming the text doesn't already end with a newline. Uses additional args as format arguments.

Source
puts_if(condition, str)

Write text with a trailing newline, only if the condition is true.

Source
puts_if(condition, str, *args)
Source
reset!

Reset the internal state of the writer. Note: Does not clear the internal buffer.

Source
set_indent(level : Int32)

Set the indent level.

Source
set_pos(pos)

Set the position of the internal buffer.

Source
size

Get the size of the internal buffer.

Source
space(count : Int32 = 1)

Add a space to the internal buffer.

Source
space_before_block_start(*args, **options)
Source
space_before_block_start(*args, **options, &)
Source
supports_inline_block?(*args, **options)
Source
supports_inline_block?(*args, **options, &)
Source
supports_multiline_comments?(*args, **options)
Source
supports_multiline_comments?(*args, **options, &)
Source
to_s

Get the contents of the internal buffer.

Source

Nested types