Giter Club home page Giter Club logo

lua_code_formatter's Introduction

Description

  Formats any valid Lua 5.3 code.

  Lines with code are wrapped to fit inside given margins.

  Installation script deploys three command-line scripts:

    lua.reformat
    lua.get_ast
    lua.get_formatter_ast

  Last two for that guys that love tinkering.


Requirements

  Sole requirement is Lua v5.3. Earlier versions will not work.

  It may or may not work under Windows. I've not tested it there.

  For Lua v5.1 checkout "5.1" branch.


Installation

  > sudo luarocks make lcf-scm-1.rockspec

  (also works "sudo luarocks install lcf")


Usage

  From command-line

    > lua.reformat <f_in>

    You can pass formatter parameters in command line. For
    syntax call "lua.reformat" without parameters.


  From Lua interpreter

    Suppose you have a string with Lua code and wish to get another
    string with formatted code.

    do
      local lua_code_str = 'do return end' -- < fill it

      require('lcf.workshop.base')
      local get_ast = request('!.lua.code.get_ast')
      local get_formatted_code = request('!.formats.lua.save')

      return get_formatted_code(get_ast(lua_code_str))
    end


    Passing formatting parameters

      You may override default parameters by passing a table with
      new values of changed parameters:

      get_formatted_code(
        get_ast(lua_code_str),
        {
          indent_chunk = '  ',
          right_margin = 100,
          max_text_width = 65,
          keep_comments = true,
        }
      )

      <indent_chunk> is a string using for building one indent.
        You may try '|..' to see it's effect.
      <right_margin> limits length of line with indent. Setting it
        makes sense for printing.
      <max_text_width> limits length of line without indent, i.e.
        length of text in line. Setting it makes sense for windowed
        viewing in editor.
      <keep_comments> is a flag to keep comments. Comment text is
        not changed so kept comments may last beyond right margin.

        ! Comments are "sinked-up" to statements level. So text

          function(a, --parameter "a"
            b) --parameter "b"
          end

        is treated as

          --parameter "a"
          function(a, b)
            --parameter "b"
          end

        This is done to keep formatting routines simple.


--
* Based on my own generic stream processing core,
  which is extended to generic strings parser,
  which uses my own loseless Lua syntax representation,
  produces AST (annotated syntax tree),
  which is further simplified and structured for code formatter,
  which produces code layout,
  trying all possible variants to fit code under given margins
  and maintain indentation.

* It uses local copy of my "workshop" code hive.
  Current version: https://github.com/martin-eden/workshop

* See also https://github.com/martin-eden/contents

--
2016-08-16
2017-01-28
2017-09-26

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.