class CodeWriter

Overview

TODO Write documentation for CodeWriter

Defined in:

code_writer.cr
code_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

Instance Method Summary

Constructor Detail

def self.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"]) #

[View source]

Class Method Detail

def self.escape_char(str : String, char : Char) #

[View source]
def self.escape_quote(str : String, quote : Char) #

[View source]
def self.get_string(string : String | -> String) #

[View source]

Instance Method Detail

def blank_line #

Add a blank line to the internal buffer.


[View source]
def blank_line_if_last_not #

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


[View source]
def 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

[View source]
def block_end(*args, **options) #

[View source]
def block_end(*args, **options, &) #

[View source]
def block_start(*args, **options) #

[View source]
def block_start(*args, **options, &) #

[View source]
def 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

[View source]
def 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

[View source]
def comment_start(*args, **options) #

[View source]
def comment_start(*args, **options, &) #

[View source]
def dedent(amount : Int32 = 1) #

Subtract from the indentation level for the next lines.


[View source]
def dedent(amount : Int32 = 1, &) #

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


[View source]
def indent(amount : Int32 = 1) #

Set the indentation level for the next lines.


[View source]
def indent(amount : Int32 = 1, &) #

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


[View source]
def indent_text #

Get the indentation string given the current indent level.


[View source]
def 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 }

[View source]
def inline_block_end(*args, **options) #

[View source]
def inline_block_end(*args, **options, &) #

[View source]
def inline_block_start(*args, **options) #

[View source]
def inline_block_start(*args, **options, &) #

[View source]
def language_settings : LanguageSettings #

[View source]
def language_settings=(language_settings : LanguageSettings) #

[View source]
def last_n(bytes : Int32 = 1) #

Get the last n bytes from the internal buffer.


[View source]
def multiline_comment_end(*args, **options) #

[View source]
def multiline_comment_end(*args, **options, &) #

[View source]
def multiline_comment_middle(*args, **options) #

[View source]
def multiline_comment_middle(*args, **options, &) #

[View source]
def multiline_comment_start(*args, **options) #

[View source]
def multiline_comment_start(*args, **options, &) #

[View source]
def newline #

Add a newline to the internal buffer.


[View source]
def newline_if_last_not #

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


[View source]
def pos #

Get the position of the internal buffer.


[View source]
def print(str) #

Write text to the internal buffer. Respects the current indentation level, and will indent the line if the last_newline flag is set.


[View source]
def print(str, *args) #

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.


[View source]
def print_if(condition, str) #

Writes text, only if the condition is true.


[View source]
def print_if(condition, str, *args) #

[View source]
def puts(str) #

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


[View source]
def 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.


[View source]
def puts_if(condition, str) #

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


[View source]
def puts_if(condition, str, *args) #

[View source]
def reset! #

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


[View source]
def set_indent(level : Int32) #

Set the indent level.


[View source]
def set_pos(pos) #

Set the position of the internal buffer.


[View source]
def size #

Get the size of the internal buffer.


[View source]
def space(count : Int32 = 1) #

Add a space to the internal buffer.


[View source]
def space_before_block_start(*args, **options) #

[View source]
def space_before_block_start(*args, **options, &) #

[View source]
def supports_inline_block?(*args, **options) #

[View source]
def supports_inline_block?(*args, **options, &) #

[View source]
def supports_multiline_comments?(*args, **options) #

[View source]
def supports_multiline_comments?(*args, **options, &) #

[View source]
def to_s #

Get the contents of the internal buffer.


[View source]