class CodeWriter
- CodeWriter
- Reference
- Object
Overview
TODO Write documentation for CodeWriter
Defined in:
code_writer.crcode_writer/languages.cr
code_writer/utils.cr
Constant Summary
-
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
Class Method Summary
- .escape_char(str : String, char : Char)
- .escape_quote(str : String, quote : Char)
- .get_string(string : String | -> String)
Instance Method Summary
-
#blank_line
Add a blank line to the internal buffer.
-
#blank_line_if_last_not
Add a blank line to the internal buffer, only if the last character written was not a newline.
-
#block(first_line = nil, &block : -> )
Create a block using the current language's block syntax.
- #block_end(*args, **options)
- #block_end(*args, **options, &)
- #block_start(*args, **options)
- #block_start(*args, **options, &)
-
#comment(str)
Create a single comment using the current language's comment syntax.
-
#comment(*, pad : Bool = false, &)
Create a multiline comment using the current language's comment syntax.
- #comment_start(*args, **options)
- #comment_start(*args, **options, &)
-
#dedent(amount : Int32 = 1)
Subtract from the indentation level for the next lines.
-
#dedent(amount : Int32 = 1, &)
Subtract from the indentation level for the given block, then reset it once the block exits.
-
#indent(amount : Int32 = 1)
Set the indentation level for the next lines.
-
#indent(amount : Int32 = 1, &)
Set the indentation level for the given block, then reset it once the block exits.
-
#indent_text
Get the indentation string given the current indent level.
-
#inline_block(pre_block = nil, &block : -> )
Create an inline block using the current language's block syntax.
- #inline_block_end(*args, **options)
- #inline_block_end(*args, **options, &)
- #inline_block_start(*args, **options)
- #inline_block_start(*args, **options, &)
- #language_settings : LanguageSettings
- #language_settings=(language_settings : LanguageSettings)
-
#last_n(bytes : Int32 = 1)
Get the last n bytes from the internal buffer.
- #multiline_comment_end(*args, **options)
- #multiline_comment_end(*args, **options, &)
- #multiline_comment_middle(*args, **options)
- #multiline_comment_middle(*args, **options, &)
- #multiline_comment_start(*args, **options)
- #multiline_comment_start(*args, **options, &)
-
#newline
Add a newline to the internal buffer.
-
#newline_if_last_not
Add a newline to the internal buffer, only if the last character written was not a newline.
-
#pos
Get the position of the internal buffer.
-
#print(str)
Write text to the internal buffer.
-
#print(str, *args)
Write text to the internal buffer, using additional args as format arguments.
-
#print_if(condition, str)
Writes text, only if the condition is true.
- #print_if(condition, str, *args)
-
#puts(str)
Write text with a trailing newline, assuming the text doesn't already end with a newline.
-
#puts(str, *args)
Write text with a trailing newline, assuming the text doesn't already end with a newline.
-
#puts_if(condition, str)
Write text with a trailing newline, only if the condition is true.
- #puts_if(condition, str, *args)
-
#reset!
Reset the internal state of the writer.
-
#set_indent(level : Int32)
Set the indent level.
-
#set_pos(pos)
Set the position of the internal buffer.
-
#size
Get the size of the internal buffer.
-
#space(count : Int32 = 1)
Add a space to the internal buffer.
- #space_before_block_start(*args, **options)
- #space_before_block_start(*args, **options, &)
- #supports_inline_block?(*args, **options)
- #supports_inline_block?(*args, **options, &)
- #supports_multiline_comments?(*args, **options)
- #supports_multiline_comments?(*args, **options, &)
-
#to_s
Get the contents of the internal buffer.
Constructor Detail
Class Method Detail
Instance Method Detail
Add a blank line to the internal buffer, only if the last character written was not a newline.
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
Create a single comment using the current language's comment syntax.
writer.comment("This is a comment")
This will output:
# This is a comment
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
Subtract from the indentation level for the given block, then reset it once the block exits.
Set the indentation level for the given block, then reset it once the block exits.
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 }
Add a newline to the internal buffer, only if the last character written was not a newline.
Write text to the internal buffer. Respects the current indentation level, and will indent the line if the
last_newline
flag is set.
Write text to the internal buffer, using additional args as format arguments.
Respects the current indentation level, and will indent the line if the
last_newline
flag is set.
Write text with a trailing newline, assuming the text doesn't already end with a newline.
Write text with a trailing newline, assuming the text doesn't already end with a newline. Uses additional args as format arguments.