Coder Social home page Coder Social logo

codeblockwritersharp's Introduction

CodeBlockWriterSharp

Build Status

Code writer that assists with formatting and visualizing blocks of JavaScript or TypeScript code.

Conversion of code-block-writer to C# (.net standard and .net framework).

Todo

  • Convert tests from code-block-writer to c#.

Install

Available on nuget.

dotnet add package CodeBlockWriterSharp
# or
Install-Package CodeBlockWriterSharp
# or
nuget install CodeBlockWriterSharp

Example

using CodeBlockWriterSharp;

// ...etc...

var writer = new CodeBlockWriter(new Options {
    // optional options
    NewLine = "\r\n",         // default: "\n"
    IndentNumberOfSpaces = 2, // default: 4
    UseTabs = false,          // default: false
    UseSingleQuote = true     // default: false
});

writer.Write("class MyClass extends OtherClass").Block(() => {
    writer.WriteLine("@MyDecorator(1, 2)");
    writer.Write("myMethod(myParam: any)").Block(() => {
        writer.Write("return this.post(").Quote("myArgument").Write(");");
    });
});

Console.WriteLine(writer.ToString());

Outputs (using "\r\n" for newlines):

class MyClass extends OtherClass {
  @MyDecorator(1, 2)
  myMethod(myParam: any) {
    return this.post('myArgument');
  }
}

Methods

  • Block(Action block = null) - Indents all the code written within and surrounds it in braces.
  • InlineBlock(Action block = null) - Same as block, but doesn't add a space before the first brace and doesn't add a newline at the end.
  • GetLength() - Get the current number of characters.
  • WriteLine(string text) - Writes some text and adds a newline.
  • NewLine() - Writes a newline.
  • NewLineIfLastNot() - Writes a newline if what was written last wasn't a newline.
  • BlankLine() - Writes a blank line. Does not allow consecutive blank lines.
  • BlankLineIfLastNot() - Writes a blank line if what was written last wasn't a blank line.
  • Quote() - Writes a quote character.
  • Quote(string text) - Writes text surrounded in quotes.
  • Indent(times = 1) - Indents the current line. Optionally writes multiple indents when providing a number.
  • IndentBlock(Action block = null) - Indents a block of code.
  • Space(int times = 1) - Writes a space. Optionally writes multiple spaces when providing a number.
  • SpaceIfLastNot() - Writes a space if the last was not a space.
  • Tab(int times = 1) - Writes a tab. Optionally writes multiple tabs when providing a number.
  • TabIfLastNot() - Writes a tab if the last was not a tab.
  • Write(string text) - Writes some text.
  • ConditionalNewLine(bool condition) - Writes a newline if the condition is matched.
  • ConditionalBlankLine(bool condition) - Writes a blank line if the condition is matched.
  • ConditionalWrite(bool condition, string text) - Writes if the condition is matched.
  • ConditionalWrite(bool condition, Func<string> textFunc) - Writes if the condition is matched.
  • ConditionalWriteLine(bool condition, string text) - Writes some text and adds a newline if the condition is matched.
  • ConditionalWriteLine(bool condition, Func<string> textFunc) - Writes some text and adds a newline if the condition is matched.
  • SetIndentationLevel(int indentationLevel) - Sets the current indentation level.
  • SetIndentationLevel(string whitespaceText) - Sets the current indentation level based on the provided whitespace text.
  • WithIndentationLevel(int indentationLevel, Action action) - Sets the indentation level within the provided action.
  • WithIndentationLevel(string whitespaceText, Action action) - Sets the indentation level based on the provided whitespace text within the action.
  • GetIndentationLevel() - Gets the current indentation level.
  • QueueIndentationLevel(int indentationLevel) - Queues an indentation level to be used once a new line is written.
  • QueueIndentationLevel(string whitespaceText) - Queues an indentation level to be used once a new line is written based on the provided whitespace text.
  • WithHangingIndentation(Action action) - Writes the code within the action with hanging indentation.
  • CloseComment() - Writes text to exit a comment if in a comment.
  • IsInComment() - Gets if the writer is currently in a comment.
  • IsAtStartOfFirstLineOfBlock() - Gets if the writer is currently at the start of the first line of the text, block, or indentation block.
  • IsOnFirstLineOfBlock() - Gets if the writer is currently on the first line of the text, block, or indentation block.
  • IsInString() - Gets if the writer is currently in a string.
  • IsLastNewLine() - Gets if the writer last wrote a newline.
  • IsLastBlankLine() - Gets if the writer last wrote a blank line.
  • IsLastSpace() - Gets if the writer last wrote a space.
  • IsLastTab() - Gets if the writer last wrote a tab.
  • GetLastChar() - Gets the last character written.
  • GetOptions() - Gets the writer options.
  • ToString() - Gets the string.

Other Features

  • Does not indent within strings.
  • Escapes newlines within double and single quotes created with .Quote(text).

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.