Giter VIP home page Giter VIP logo

luaformatter's Introduction

Lua Formatter

This library beautify Lua code:

  • Indent block
  • Indent expressions
  • Trim trailing white spaces

It is plain Lua 5.1.

Prerequisites

  • Lua 5.1
  • Metalua >= 0.7
  • Penlight ~> 0.9

Installation

$ luarocks install --local formatter

How does it work?

Library

$ lua
Lua 5.1.5  Copyright (C) 1994-2012 Lua.org, PUC-Rio
> formatter = require 'formatter'
> code = [[do
>> local var = 
>> 42
>> end]]
> formattedcode = formatter.indentcode(code, '\n', true, '  ')
> print( formattedcode )
do
  local var =
    42
end

Command line

$ cat sample.lua
do
local var =
42
end
$ luaformatter sample.lua
do
  local var =
    42
end

Want more options?

$ luaformatter --help
Formats Lua code.
  -a, --autosave Flush formatted Lua in given file instead of stdout.
  -s, --spaces (default 2) Spaces to use as indentation.
  -t, --tabs   (default 0) Tabulation(s) to use as indentation.
  -d, --delimiter (default unix) Type of new line to detect and use while formatting:
    * unix: '\n' LF Line feed.
    * windows: '\r\n' CR+LF
    * mac: '\r' CR Carriage Return of Macs before OSX.
  -h, --help This help.
  [files] Files to format.

luaformatter's People

Contributors

kinfoo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

luaformatter's Issues

Variable table formating

Hi,

I encountered some strange table formating:

$ cat /tmp/sampletable.lua 
local sometable = {
-- TODO
}
$ luaformatter /tmp/sampletable.lua 
local sometable = {
  -- TODO
  }

It is different when there is a value in table:

$ cat /tmp/sampletablewithvalue.lua 
local sometable = {
-- TODO
42
}
$ luaformatter /tmp/sampletablewithvalue.lua 
local sometable = {
  -- TODO
  42
}

It would be nice to have consistent formating.

Kevin

module 'pl.lapp' not found

user@ubuntu-3:~/$ sudo luarocks install formatter
user@ubuntu-3:~/$ luarocks list formatter

Installed rocks:
----------------

formatter
   0.1.1-1 (installed) - /usr/local/lib/luarocks/rocks

user@ubuntu-3:~/$ luaformatter
lua: /usr/local/bin/luaformatter:19: module 'pl.lapp' not found:
        no field package.preload['pl.lapp']
        no file './pl/lapp.lua'
        no file '/usr/local/share/lua/5.1/pl/lapp.lua'
        no file '/usr/local/share/lua/5.1/pl/lapp/init.lua'
        no file '/usr/local/lib/lua/5.1/pl/lapp.lua'
        no file '/usr/local/lib/lua/5.1/pl/lapp/init.lua'
        no file '/usr/share/lua/5.1/pl/lapp.lua'
        no file '/usr/share/lua/5.1/pl/lapp/init.lua'
        no file './pl/lapp.so'
        no file '/usr/local/lib/lua/5.1/pl/lapp.so'
        no file '/usr/lib/i386-linux-gnu/lua/5.1/pl/lapp.so'
        no file '/usr/lib/lua/5.1/pl/lapp.so'
        no file '/usr/local/lib/lua/5.1/loadall.so'
        no file './pl.so'
        no file '/usr/local/lib/lua/5.1/pl.so'
        no file '/usr/lib/i386-linux-gnu/lua/5.1/pl.so'
        no file '/usr/lib/lua/5.1/pl.so'
        no file '/usr/local/lib/lua/5.1/loadall.so'
stack traceback:
        [C]: in function 'require'
        /usr/local/bin/luaformatter:19: in main chunk
        [C]: ?


user@ubuntu-3:~/$ luarocks list penlight

Installed rocks:
----------------

penlight
   0.9.8-1 (installed) - /home/user/.luarocks/lib/luarocks/rocks

Shebang is not handled

Formatter is unable to handle code starting with a shebang:

$ lua
Lua 5.1.5  Copyright (C) 1994-2012 Lua.org, PUC-Rio
> formatter = require 'formatter'
> code = [[#!/usr/bin/env lua
>>          print 'Hi']]
> print( formatter.indentcode(code, '\n', true, '  ') )
./formatter.lua:487: attempt to index local 'linetodepth' (a nil value)
stack traceback:
    ./formatter.lua:487: in function 'indentcode'
    stdin:1: in main chunk
    [C]: ?

parsing failure if file contains the word "goto"

/usr/local/opt/[email protected]/bin/lua5.1: ...luarocks/share/lua/5.1/metalua/grammar/generator.lua:176: line 1506, char 8: Identifier expected
>>> function goto(uid)
>>>         ^
stack traceback:
	[C]: in function 'error'
	...luarocks/share/lua/5.1/metalua/grammar/generator.lua:176: in function 'parse_error'
	...rocks/share/lua/5.1/metalua/compiler/parser/misc.lua:96: in function <...rocks/share/lua/5.1/metalua/compiler/parser/misc.lua:95>
	(tail call): ?
	...luarocks/share/lua/5.1/metalua/grammar/generator.lua:607: in function <...luarocks/share/lua/5.1/metalua/grammar/generator.lua:591>
	(tail call): ?
	...luarocks/share/lua/5.1/metalua/grammar/generator.lua:113: in function 'raw_parse_sequence'
	...luarocks/share/lua/5.1/metalua/grammar/generator.lua:216: in function <...luarocks/share/lua/5.1/metalua/grammar/generator.lua:212>
	(tail call): ?
	(tail call): ?
	...
	...rocks/share/lua/5.1/metalua/compiler/parser/misc.lua:164: in function 'e'
	...luarocks/share/lua/5.1/metalua/grammar/generator.lua:113: in function 'raw_parse_sequence'
	...luarocks/share/lua/5.1/metalua/grammar/generator.lua:216: in function <...luarocks/share/lua/5.1/metalua/grammar/generator.lua:212>
	(tail call): ?
	...kelmana/.luarocks/share/lua/5.1/metalua/compiler.lua:104: in function 'f'
	...kelmana/.luarocks/share/lua/5.1/metalua/compiler.lua:153: in function 'src_to_ast'
	/Users/kelmana/.luarocks/share/lua/5.1/formatter.lua:367: in function 'getindentlevel'
	/Users/kelmana/.luarocks/share/lua/5.1/formatter.lua:512: in function 'indentcode'
	...uarocks/rocks-5.1/formatter/0.1.1-1/bin/luaformatter:79: in main chunk
	[C]: ?

Handle properly errors

When a given file had an error. A stacktrace from metalua is displayed.
A clear error message instead should be better.

Unable to pass "\t" as `indentation`

$ ./commandline.lua -i "\t" luarocks/luaformatter-0.1.0-2.rockspec                                                                                                                                                                     1 ↵
package = 'LuaFormatter'
version = '0.1.0-2'
description = {
\tsummary = 'Beautifies Lua code.',
\tdetailed = [[
    This library beautify Lua code:
    * Indent block
    * Indent expressions
    * Trim trailing white spaces
    It is plain Lua 5.1.
  ]],
\tlicense = 'EPL'
}
source = {
\turl = 'git://github.com/LuaDevelopmentTools/luaformatter.git',
\ttag = 'v0.1.0-2'
}
dependencies = {
\t'metalua-compiler == 0.7.2',
\t'penlight ~> 0.9.8'
}
build = {
\ttype = 'builtin',
\tinstall = {
\t\tbin = {
\t\t\tluaformatter = 'commandline.lua'
\t\t},
\t},
\tmodules = {
\t\tformatter = 'formatter.lua'
\t}
}

`-d` just does not work

$ ./commandline.lua -d windows luarocks/luaformatter-0.1.0-2.rockspec > res.lua

Resulting res.lua line endings are regular Unix \n.

Crashes with comment only files

When a file contains only comments (use case: ldoc API definitions), the formatter crashes:

printf -- '-- this is just a comment\n-- more comments\n' | luaformatter /dev/stdin
/usr/bin/lua5.1: ...ome/julien/.luaenv/check/share/lua/5.1/formatter.lua:57: attempt to index local 'node' (a nil value)
stack traceback:
	...ome/julien/.luaenv/check/share/lua/5.1/formatter.lua:57: in function 'getlastline'
	...ome/julien/.luaenv/check/share/lua/5.1/formatter.lua:373: in function 'getindentlevel'
	...ome/julien/.luaenv/check/share/lua/5.1/formatter.lua:512: in function 'indentcode'
	...uarocks/rocks-5.1/formatter/0.1.1-1/bin/luaformatter:79: in main chunk
	[C]: ?

Interestingly, the issue doesn't exist with a single line.

luarocks install formatter fails with `Missing dependencies`

$ luarocks install formatter
Installing https://rocks.moonscript.org/formatter-0.1.0-2.src.rock...
Using https://rocks.moonscript.org/formatter-0.1.0-2.src.rock... switching to 'build' mode

Missing dependencies for formatter:
metalua-compiler == 0.7.2
penlight ~> 0.9.8

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.