lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Hello list!

I'm glad to announce lua code formatter. I've represent it's code
as lualocks package "lcf". It comes for lua 5.1 and 5.3 versions.
It installs shell script "lua.reformat" with <f_in> <f_out> parameters.

I'm willing to get responses on its usage, probably syntax
constructions it cannot parse correctly.

I've tested it on a 100MiB bunch of World of Warcraft addons as
example of badly written and formatted code where one-style
reformatting really helps to understand logic.

Further plans is to use "lua-parser" package to speed-up parsing
phase for production use.


--[[ readme.txt ]]

Description

  Formats any valid lua 5.3 code.

  Main advantage is that it wraps code lines to maintain readability.
  Efforts are made to represent code in more easy to understand form,
  not the shortest one.


Requirements

  Sole requirement is lua v5.3. Earlier versions will not work.
  (I've made a backport for lua v5.1. See "5.1" branch.)


Installation

  sudo luarocks install lcf


Usage

  From command-line

    lua.reformat <fIn> <fOut>


  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_formatted_ast = request('lcf.workshop.load_from.lua')
      local get_formatted_code = request('lcf.workshop.save_to.lua')

      return get_formatted_code(get_formatted_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_formatted_ast(lua_code_str),
        {
          indent_chunk = '  ',
          right_margin = 100,
          max_text_width = 65,
        }
      )

      <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 printring.
      <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.

--
* Built on my own general strings parser,
  which uses my own lua syntax representation,
  produces AST (annotated syntax tree),
  which is further structured for code formatter,
  which gets this structured AST and produce code layout,
  trying all possible variants to fit code in 70-symbol lines
  and maintain indentation.

* Currently comments are stripped from code. I'll add support
  for them someday (or will not and explain why).

* 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

-- Martin

Attachment: 0xB91B958F.asc
Description: application/pgp-keys