GithubHelp home page GithubHelp logo

koihik / luaformatter Goto Github PK

View Code? Open in Web Editor NEW
679.0 16.0 85.0 2.8 MB

Code formatter for Lua

License: Apache License 2.0

CMake 1.26% C++ 87.32% ANTLR 1.41% Shell 0.03% Dockerfile 0.11% Lua 9.88%
lua formatter codeformatter

luaformatter's Introduction

LuaFormatter

Reformats your Lua source code.

codecov build

Editor support

Install

LuaRocks

Requirements

  • c++ 17 compiler
  • luarocks 3.3.0+

The easiest way to install is to use LuaRocks.

luarocks install --server=https://luarocks.org/dev luaformatter

Build from source

Requirements

  • cmake 3.9+
  • c++ 17 compiler

Steps

    git clone --recurse-submodules https://github.com/Koihik/LuaFormatter.git
    cd LuaFormatter
    cmake .
    make
    make install

Usage

  ./lua-format [Lua scripts...] {OPTIONS}

    Reformats your Lua source code.

  OPTIONS:

      -h, --help                        Display this help menu
      -v, --verbose                     Turn on verbose mode
      -i                                Reformats in-place
      --dump-config                     Dumps the default style used to stdout
      -c[file], --config=[file]         Style config file
      --column-limit=[column limit]     Column limit of one line
      --indent-width=[indentation
      width]                            Number of spaces used for indentation
      --tab-width=[tab width]           Number of spaces used per tab
      --continuation-indent-width=[Continuation
      indentation width]                Indent width for continuations line
      --spaces-before-call=[spaces
      before call]                      Space on function calls
      --column-table-limit=[column
      table limit]                      Column limit of each line of a table
      --table-sep=[table separator]     Character to separate table fields
      --use-tab                         Use tab for indentation
      --no-use-tab                      Do not use tab for indentation
      --keep-simple-control-block-one-line
                                        keep block in one line
      --no-keep-simple-control-block-one-line
                                        Do not keep block in one line
      --keep-simple-function-one-line   keep function in one line
      --no-keep-simple-function-one-line
                                        Do not keep function in one line
      --align-args                      Align the arguments
      --no-align-args                   Do not align the arguments
      --break-after-functioncall-lp     Break after '(' of function call
      --no-break-after-functioncall-lp  Do not break after '(' of function call
      --break-before-functioncall-rp    Break before ')' of function call
      --no-break-before-functioncall-rp Do not break before ')' of function call
      --align-parameter                 Align the parameters
      --no-align-parameter              Do not align the parameters
      --chop-down-parameter             Chop down all parameters
      --no-chop-down-parameter          Do not chop down all parameters
      --break-after-functiondef-lp      Break after '(' of function def
      --no-break-after-functiondef-lp   Do not break after '(' of function def
      --break-before-functiondef-rp     Break before ')' of function def
      --no-break-before-functiondef-rp  Do not break before ')' of function def
      --align-table-field               Align fields of table
      --no-align-table-field            Do not align fields of table
      --break-after-table-lb            Break after '{' of table
      --no-break-after-table-lb         Do not break after '{' of table
      --break-before-table-rb           Break before '}' of table
      --no-break-before-table-rb        Do not break before '}' of table
      --chop-down-table                 Chop down any table
      --no-chop-down-table              Do not chop down any table
      --chop-down-kv-table              Chop down table if table contains key
      --no-chop-down-kv-table           Do not chop down table if table contains
                                        key
      --extra-sep-at-table-end          Add extra field separator at end of
                                        table
      --no-extra-sep-at-table-end       Do not add extra field separator at end
                                        of table
      --break-after-operator            Put break after operators
      --no-break-after-operator         Do not put break after operators
      --double-quote-to-single-quote    Transform string literals to use single
                                        quote
      --no-double-quote-to-single-quote Do not transform string literals to use
                                        single quote
      --single-quote-to-double-quote    Transform string literals to use double
                                        quote
      --no-single-quote-to-double-quote Do not transform string literals to use
                                        double quote
      --spaces-inside-functiondef-parens
                                        Put spaces on the inside of parens in
                                        function headers
      --no-spaces-inside-functiondef-parens
                                        Do not put spaces on the inside of
                                        parens in function headers
      --spaces-inside-functioncall-parens
                                        Put spaces on the inside of parens in
                                        function calls
      --no-spaces-inside-functioncall-parens
                                        Do not put spaces on the inside of
                                        parens in function calls
      --spaces-inside-table-braces      Put spaces on the inside of braces in
                                        table constructors
      --no-spaces-inside-table-braces   Do not put spaces on the inside of
                                        braces in table constructors
      --spaces-around-equals-in-field   Put spaces around the equal sign in
                                        key/value fields
      --no-spaces-around-equals-in-field
                                        Do not put spaces around the equal sign
                                        in key/value fields
      --line-breaks-after-function-body
                                        Line breakes after function body
      --line-separator=[line separator] input(determined by the input content),
                                        os(Use line ending of the current
                                        Operating system), lf(Unix style "\n"),
                                        crlf(Windows style "\r\n"), cr(classic
                                        Max style "\r")
      Lua scripts...                    Lua scripts to format
      "--" can be used to terminate flag options and force all following
      arguments to be treated as positional options

The program will attempt to automatically use the current directory's .lua-format file if no config file is passed in the command line. If none is found, it will try to locate a .lua-format file in a parent directory recursively. On Linux it will use $XDG_CONFIG_HOME/luaformatter/config.yaml if .lua-format does not exist. In case there's no file, it will fallback to the default configuration. The program will give the top priority to the configuration values given in the command-line, then to the configuration files and finally to the hard-coded default values.

Style configure file

Configuration parameters not specified fallback to their default values.

See this file

Default configuration

column_limit: 80
indent_width: 4
use_tab: false
tab_width: 4
continuation_indent_width: 4
spaces_before_call: 1
keep_simple_control_block_one_line: true
keep_simple_function_one_line: true
align_args: true
break_after_functioncall_lp: false
break_before_functioncall_rp: false
spaces_inside_functioncall_parens: false
spaces_inside_functiondef_parens: false
align_parameter: true
chop_down_parameter: false
break_after_functiondef_lp: false
break_before_functiondef_rp: false
align_table_field: true
break_after_table_lb: true
break_before_table_rb: true
chop_down_table: false
chop_down_kv_table: true
table_sep: ","
column_table_limit: 0
extra_sep_at_table_end: false
spaces_inside_table_braces: false
break_after_operator: true
double_quote_to_single_quote: false
single_quote_to_double_quote: false
spaces_around_equals_in_field: true
line_breaks_after_function_body: 1
line_separator: input

Disable formatting for a line or block

Sometimes it may be useful to disable automatic formatting. This is done be putting the code between LuaFormatter off and LuaFormatter on tags:

-- LuaFormatter off
matrix = {
   {1, 0, 0, 0},
   {1, 1, 0, 0},
   {1, 1, 1, 0},
   {1, 1, 1, 1}
}
-- LuaFormatter on

Limitations

  • Do not work when source file contains syntax error
  • Do not support 'Format selection'

luaformatter's People

Contributors

5lbbookofgre avatar akylzhan avatar andrejlevkovitch avatar brianhang avatar carlsmedstad avatar dansen avatar devalone avatar doug1234 avatar dramborleg avatar internetunexplorer avatar janfel avatar joclement avatar karanankit01 avatar ki-foobar avatar koihik avatar lexicality avatar mblayman avatar mzrinsky avatar no-more-secrets avatar rafauke avatar rangercyh avatar ruin0x11 avatar safeteewow avatar spacewander avatar tammela avatar thomasims avatar trk9001 avatar vladdoster avatar volucris1 avatar yut23 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  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  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  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

luaformatter's Issues

unexpected line break when the first value of table is shorter

Hi, the following are fomatted codes, there is an unexpected line break when the first value of table is shorter.

auto(update(path.base,
            {
    aaaaaaaa = 'aa',
    dddddddddddddddddddddddddddd = i,
    dddddddddddddddd = i
}))
auto(update(path.base, {
    aaaaaaaa = 'aaaaaaaaaaaaaaaaaaaaaaaaaa',
    dddddddddddddddddddddddddddd = i,
    dddddddddddddddd = i
}))

Compile and distribute binary in release pages

I have test this tool on macOS, it has a good format quality. I think it should be precompiled on major operating systems: Windows, macOS, Linux.
I think it should help people using this with no prerequisites such as Visual Studio, Xcode, clang and so on.

So would you add a continuous integration tool such as Travis CI or AppVeyor to build and release?

OSX 10.14 build fails

When I build with cmake I get this error:

Run Build Command(s):/usr/bin/make cmTC_1d318/fast && /Applications/Xcode.app/Contents/Developer/usr/bin/make -f CMakeFiles/cmTC_1d318.dir/build.make CMakeFiles/cmTC_1d318.dir/build
Building CXX object CMakeFiles/cmTC_1d318.dir/fs.cpp.o
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++    -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk -mmacosx-version-min=10.14   -std=c++1z -o CMakeFiles/cmTC_1d318.dir/fs.cpp.o -c /Users/pasi/installed/Lua/LuaFormatter/test/cpp17/fs.cpp
/Users/pasi/installed/Lua/LuaFormatter/test/cpp17/fs.cpp:7:21: error: 'path' is unavailable: introduced in macOS 10.15
   std::filesystem::path current = ".";

Fails to format some escaped strings

When I try to format code with the setting double_quote_to_single_quote set to true the formatting succeeds but breaks some escapes which cause unfinished string syntax errors.

Quick example:

local a = "\\"

Which incorrectly formats to:

local a = '\'

What about add support for vim?

It's very simple - I just get clang-format.py and rewrite the script for LuaFormatter. After this I add several lines in my .vimrc and all works. It'll be nice, if you add the repo in your README.md: vim-lua-format

Increase coverage of tests

The test suite is not covering some branches.

We shall increase our tests based on the reports from codecov.

Incorrect formatting of string prefixes

Hello! It's probably non-standard lua but I'll try anyway.

UI addons for Warhammer Online use L prefix (is it luamacro or something? I don't know) before string literals to denote wstrings, for example L"texthere". lua-format changes it to L "texthere" which results in an error.

Would it be possible to keep [A-Z] letters together with the string literal, as it (probably) is an error otherwise anyway?

It can be tested on this line of code:

mywstring = L"mytext"

Align Options Are Sometimes Ignored

Config:

column_limit: 100
use_tab: true
indent_width: 1
continuation_indent_width: 1
keep_simple_function_one_line: false
keep_simple_control_block_one_line: false
align_args: false
align_parameter: false
align_table_field: false
chop_down_table: true
chop_down_parameter: true
break_before_table_rb: true
break_after_table_lb: true
extra_sep_at_table_end: true
single_quote_to_double_quote: true

Problematic Results:

minetest.register_on_mods_loaded(function()
	applicable_furniture = table.search(ikea.registered_furniture,
                       		{includes = {size_x = 1, size_y = 1, size_z = 1}}) -- Formatter attempts to align using spaces _and_ adds tabs for intentation level, somehow failing at both it seems
end)

----------

function music.display_song_info(song)
	local message = music.tracks[song].title .. " by " .. music.tracks[song].author .. ", " ..
                		music.tracks[song].license -- Same here
	minetest.chat_send_all(message)
end

----------

assert(table.equals({"a", "b", c = "d", e = {"f", "g", "h", {"h"}}},
       	{"a", "b", c = "d", e = {"f", "g", "h", {"h"}}})) -- And Here

Expected Results:

minetest.register_on_mods_loaded(function()
	applicable_furniture = table.search(ikea.registered_furniture,
		{includes = {size_x = 1, size_y = 1, size_z = 1}})
end)

----------

function music.display_song_info(song)
	local message = music.tracks[song].title .. " by " .. music.tracks[song].author .. ", " ..
		music.tracks[song].license
	minetest.chat_send_all(message)
end

----------

assert(table.equals({"a", "b", c = "d", e = {"f", "g", "h", {"h"}}},
	{"a", "b", c = "d", e = {"f", "g", "h", {"h"}}}))

LuaRocks install fails on 'third_party/Catch2'

Install of a9aa804 (2020-01-11) using recommended command:

luarocks install --server=https://luarocks.org/dev luaformatter

fails with output:

error: Server does not allow request for unadvertised object e1c9d5569dc4135babb9c81891d70a8ba8ed938c
Fetched in submodule path 'third_party/Catch2', but it did not contain e1c9d5569dc4135babb9c81891d70a8ba8ed938c. Direct fetching of that commit failed.

Error: Failed to fetch submodules.

Reported first in #63 but that is about a different issue to this one. @tammela noted:

Luarocks internally uses a git command that should clone all submodules
commits as is. Github blocks this command on non tagged commits, so as a
requirement it is needed that all submodules must best in a tagged commit.

Unfortunately it looks like that something broke. After I caught this
problem myself I have double checked that all submodules were tagged.

As a workaround for now, you may install it following the Build from source guide.

Return non-zero status if formatting failed

Hi,

I use lua-format along with the vim plugin Neoformatter. I can't set vim to format on save because lua-format doesn't return an error code when formatting fails. If there is an error, I end up saving a blank file which is a problem!

For example, if I'm missing a brace, I get this error message:

formatting: init.lua
line 317:0 missing '}' at 'local'
Input contains syntax errors

This will cause the vim format plugin to clear the buffer because it thinks lua-format has succeeded.
Is it possible to return something other than zero if formatting fails?

chop_down_parameter might not work

I am trying to use the new chop_down_parameter option, but it does not work how I expected.

Relevant issue and PR:
#33
https://github.com/Koihik/LuaFormatter/pull/34/files

My config file:

column_limit: 120
indent_width: 4
continuation_indent_width: 4
use_tab: false
keep_simple_block_one_line: false
align_args: true
break_after_functioncall_lp: true
break_before_functioncall_rp: true
align_parameter: true
chop_down_parameter: true <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
break_after_functiondef_lp: true
break_before_functiondef_rp: true
align_table_field: true
break_after_table_lb: true
break_before_table_rb: true
chop_down_kv_table: true
table_sep: ','
extra_sep_at_table_end: false
break_after_operator: true

My lua test code

function add(a, adasdas, dfsdf, xzzxc, aewqe, asdas , zxczxc, asdasd, asdas, asdas,sas, as,asd,asd,asd,asd,zxv,qwe,cs,ryert,d,ds,xcv,asfd)
end

cli command

lua-format -so -c format test.lua

Output

function add(
    a, adasdas, dfsdf, xzzxc, aewqe, asdas, zxczxc, asdasd, asdas, asdas, sas, as, asd, asd, asd, asd, zxv, qwe, cs,
    ryert, d, ds, xcv, asfd
)
end

Expected output. (maybe not correctly indented here, but it's about the chopping down)

function add(
    a,
    adasdas,
    dfsdf,
    xzzxc,
    aewqe,
    asdas,
    zxczxc,
    asdasd,
    asdas,
    asdas,
    sas,
    as,
    asd,
    asd,
    asd,
    asd,
    zxv,
    qwe,
    cs,
    ryert,
    d,
    ds,
    xcv,
    asfd
)
end

Add an option chop_down_table

Input:

table = { variable1, variable2, variable3, variable4, variable5, variable6, variable7, variable8, variable9, variable10}

Current output:

table = {
    variable1, variable2, variable3, variable4, variable5, variable6, variable7, variable8,
    variable9, variable10,
}

Expected output with chop_down_table set to true:

table = {
    variable1,
    variable2,
    variable3,
    variable4,
    variable5,
    variable6,
    variable7,
    variable8,
    variable9,
    variable10,
}

[feature request] every argument on new line

Currently, when a function call/definition is too long, the arguments will be split over lines. However, as many arguments as possible are fit in each line. I propose an option that whenever the original line is too long, each argument will be put on a seperate line.

function add(a, lot, of, arguments, exceeding, column, limit)
    (...)

Is currently turned into

function add(a, lot, of, arguments,
             exceeding, column,
             limit)

I would like to see a option for

function add(a,
             lot,
             of,
             arguments,
             exceeding,
             column,
             limit)

Configure operators

I would like to change the operators ' .. ' to '..' (saves space) and ' ^ ' to '^' (shows the highest priority). We have huge codebase and we don't want to change every source file.

I tried to look from the source code but I could not find where I can do the change.

Or would it be a good idea to put all operators to config?

format crash

0 libsystem_kernel.dylib 0x00007fff6da0e33a __pthread_kill + 10
1 libsystem_pthread.dylib 0x00007fff6dacae60 pthread_kill + 430
2 libsystem_c.dylib 0x00007fff6d995808 abort + 120
3 libc++abi.dylib 0x00007fff6abf5458 abort_message + 231
4 libc++abi.dylib 0x00007fff6abe68a7 demangling_terminate_handler() + 238
5 libobjc.A.dylib 0x00007fff6c7215b1 _objc_terminate() + 104
6 libc++abi.dylib 0x00007fff6abf4887 std::__terminate(void ()()) + 8
7 libc++abi.dylib 0x00007fff6abf71a2 __cxxabiv1::failed_throw(__cxxabiv1::__cxa_exception
) + 27
8 libc++abi.dylib 0x00007fff6abf7169 __cxa_throw + 113
9 lua-format 0x00000001064f60b8 std::__1::__throw_range_error(char const*) + 56
10 lua-format 0x00000001064f412d antlrcpp::utf8_to_utf32(char const*, char const*) + 1437
11 lua-format 0x00000001064f3afa antlr4::ANTLRInputStream::load(std::__1::basic_string<char, std::__1::char_traits, std::__1::allocator > const&) + 106
12 lua-format 0x00000001064f5263 antlr4::ANTLRInputStream::load(std::__1::basic_istream<char, std::__1::char_traits >&) + 115
13 lua-format 0x000000010645f1a3 lua_format(std::__1::basic_istream<char, std::__1::char_traits >&, Config const&) + 99
14 lua-format 0x00000001064ad3e5 main + 9957
15 libdyld.dylib 0x00007fff6d8c6cc9 start + 1

Inconsistent line endings after format

It seems that when a file with \r\n line endings have mixed line endings after format.
I checked the result and found that not all line endings are converted to \n. Some of them are preserved.
I can use dos2unix or unix2dos to convert line endings after format, but I think the best solution is fixing it originally.

Comment duplicates when formatting long line

Using the VSCode plugin it seems recently comments at the end of long lines have started duplicating every time the file gets formatted.

An example can be the line:

local x = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa -- hello there

After being formatted once, it duplicates into 2 comments, then wraps to the next line. The 2nd time it becomes 4 comments, then 8 the third, etc.

These are the settings I'm using for formatting:

column_limit: 100
indent_width: 1
use_tab: true
tab_width: 4
continuation_indent_width: 0
spaces_before_call: 1
keep_simple_block_one_line: false
align_args: true
break_after_functioncall_lp: false
break_before_functioncall_rp: false
align_parameter: true
chop_down_parameter: false
break_after_functiondef_lp: false
break_before_functiondef_rp: false
align_table_field: true
break_after_table_lb: true
break_before_table_rb: true
chop_down_table: false
chop_down_kv_table: true
table_sep: ","
extra_sep_at_table_end: true
break_after_operator: true
double_quote_to_single_quote: true
single_quote_to_double_quote: false

No newline after multiline comment

If a statement is right after a multiline comment, the statement will be formatted right after the comment ends.

--[[
--
--]]
local lgi = 3

Becomes

--[[
--
--]] local lgi = 3

This does not seem like intended behaviour to me.

Lua 5.4 support

Lua 5.4 is just around the corner and introduces some new syntax tokens, please consider supporting it

Blank lines

Are blank lines kept when the formatting is done?

how to config format table like this?

local XMODEMCRC16Lookup = {
	0x0000,0x1021,0x2042,0x3063,0x4084,0x50a5,0x60c6,0x70e7,
	0x8108,0x9129,0xa14a,0xb16b,0xc18c,0xd1ad,0xe1ce,0xf1ef,
	0x1231,0x0210,0x3273,0x2252,0x52b5,0x4294,0x72f7,0x62d6,
	0x9339,0x8318,0xb37b,0xa35a,0xd3bd,0xc39c,0xf3ff,0xe3de,
	0x2462,0x3443,0x0420,0x1401,0x64e6,0x74c7,0x44a4,0x5485,
	0xa56a,0xb54b,0x8528,0x9509,0xe5ee,0xf5cf,0xc5ac,0xd58d,
	0x3653,0x2672,0x1611,0x0630,0x76d7,0x66f6,0x5695,0x46b4,
	0xb75b,0xa77a,0x9719,0x8738,0xf7df,0xe7fe,0xd79d,0xc7bc,
	0x48c4,0x58e5,0x6886,0x78a7,0x0840,0x1861,0x2802,0x3823,
	0xc9cc,0xd9ed,0xe98e,0xf9af,0x8948,0x9969,0xa90a,0xb92b,
	0x5af5,0x4ad4,0x7ab7,0x6a96,0x1a71,0x0a50,0x3a33,0x2a12,
	0xdbfd,0xcbdc,0xfbbf,0xeb9e,0x9b79,0x8b58,0xbb3b,0xab1a,
	0x6ca6,0x7c87,0x4ce4,0x5cc5,0x2c22,0x3c03,0x0c60,0x1c41,
	0xedae,0xfd8f,0xcdec,0xddcd,0xad2a,0xbd0b,0x8d68,0x9d49,
	0x7e97,0x6eb6,0x5ed5,0x4ef4,0x3e13,0x2e32,0x1e51,0x0e70,
	0xff9f,0xefbe,0xdfdd,0xcffc,0xbf1b,0xaf3a,0x9f59,0x8f78,
	0x9188,0x81a9,0xb1ca,0xa1eb,0xd10c,0xc12d,0xf14e,0xe16f,
	0x1080,0x00a1,0x30c2,0x20e3,0x5004,0x4025,0x7046,0x6067,
	0x83b9,0x9398,0xa3fb,0xb3da,0xc33d,0xd31c,0xe37f,0xf35e,
	0x02b1,0x1290,0x22f3,0x32d2,0x4235,0x5214,0x6277,0x7256,
	0xb5ea,0xa5cb,0x95a8,0x8589,0xf56e,0xe54f,0xd52c,0xc50d,
	0x34e2,0x24c3,0x14a0,0x0481,0x7466,0x6447,0x5424,0x4405,
	0xa7db,0xb7fa,0x8799,0x97b8,0xe75f,0xf77e,0xc71d,0xd73c,
	0x26d3,0x36f2,0x0691,0x16b0,0x6657,0x7676,0x4615,0x5634,
	0xd94c,0xc96d,0xf90e,0xe92f,0x99c8,0x89e9,0xb98a,0xa9ab,
	0x5844,0x4865,0x7806,0x6827,0x18c0,0x08e1,0x3882,0x28a3,
	0xcb7d,0xdb5c,0xeb3f,0xfb1e,0x8bf9,0x9bd8,0xabbb,0xbb9a,
	0x4a75,0x5a54,0x6a37,0x7a16,0x0af1,0x1ad0,0x2ab3,0x3a92,
	0xfd2e,0xed0f,0xdd6c,0xcd4d,0xbdaa,0xad8b,0x9de8,0x8dc9,
	0x7c26,0x6c07,0x5c64,0x4c45,0x3ca2,0x2c83,0x1ce0,0x0cc1,
	0xef1f,0xff3e,0xcf5d,0xdf7c,0xaf9b,0xbfba,0x8fd9,0x9ff8,
	0x6e17,0x7e36,0x4e55,0x5e74,0x2e93,0x3eb2,0x0ed1,0x1ef0
}

function indentation error

This is a strange error where variable names cause indentation error.

gridRec.coming_amount = fn.iter(purchaseTbl):filter(function(rec)
  return
end)

-- lua-format test1.lua -c config.txt > test1_fmt.lua

gridRec.coming_amount = fn.iter(purchaseTbl):filter(
                        		function(rec)
			return
		end)

-- config
column_limit: 100
indent_width: 1
use_tab: true
tab_width: 2
continuation_indent_width: 2
spaces_before_call: 1
keep_simple_control_block_one_line: false
keep_simple_function_one_line: false
align_args: true
break_after_functioncall_lp: false
break_before_functioncall_rp: false
align_parameter: true
chop_down_parameter: false
break_after_functiondef_lp: false
break_before_functiondef_rp: false
align_table_field: true
break_after_table_lb: true
break_before_table_rb: true
chop_down_table: true
chop_down_kv_table: true
table_sep: ","
extra_sep_at_table_end: false
break_after_operator: true
double_quote_to_single_quote: false
single_quote_to_double_quote: false

Uncorrect align of arguments

I have problem with uncorrect formatting function arguments.
My config file:

column_limit: 80
use_tab: true
indent_width: 1
continuation_indent_width: 1
keep_simple_block_one_line: false
align_args: false
break_after_functioncall_lp: false
break_before_functioncall_rp: false
align_parameter: false
break_after_functiondef_lp: false
break_before_functiondef_rp: false
align_table_field: false
break_after_table_lb: false
break_before_table_rb: false
chop_down_kv_table: true
table_sep: ','
extra_sep_at_table_end: true
break_after_operator: true

This is how work now:

function tmp()
	my_fund = alfa(beta, charlie, gamma, echo, foxtrot, lima, kilo, india, juliet,
												november)
	local body, mime = cache_images:get(config.CACHE.IMAGES_KEY_PREFIX .. cid ..
																																						type, beta, charlie)

end

But, of course, it is wrong. I think, I found wrong line:

// FormatVisitor.cpp:715
        firstArgsIndent = cur_columns() - indent_ - indentForAlign_;

Why here uses cur_columns()? I just remove it:

        firstArgsIndent = indent_ - indentForAlign_;

And now code looks like:

function tmp()
	my_fund = alfa(beta, charlie, gamma, echo, foxtrot, lima, kilo, india, juliet,
			november)
	local body, mime = cache_images:get(config.CACHE.IMAGES_KEY_PREFIX .. cid ..
			type, beta, charlie)

end

_FORTIFY_SOURCE seems to break compilation

Compiling with _FORTIFY_SOURCE, which might standard in some distros, seems to break the compilation process with an error on libc's function vnsprintf().

The following patch can fix the compilation:

-set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS} -Wall")
-set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O3")
+set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS} -U_FORTIFY_SOURCE -Wall")
+set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -U_FORTIFY_SOURCE -O3")

Might need more info as alpine uses musl.

Setup:
Alpine 39
gcc (Alpine 8.3.0) 8.3.0

Disable formatting for a line or block

Hi! Is it possible to disable auto-formatting for a block?
Something like this:

-- fmt: off
local paths = {
    root .. "/app/?.lua",
    root .. "/?.lua",
    root .. "/?/init.lua",
    root .. "/app/?/init.lua",
    root .. "/.rocks/share/lua/5.1/?.lua",
    root .. "/.rocks/share/lua/5.1/?/init.lua",
}

unexpected line break in long if condition

My binary is compiled from the lastest commit 21e0205

if aaaaaaa == 1 and bbbbb == 1 and bbbbb == 1 and bbbbb == 1 and bbbbb == 1 and
    b == -1 and bbbbb == 1 and bbbbb == 1 and bbbbb == 1 and bbbbb == 1 and bbbbb ==
        1 and bbbbb == 1 then print(1) end

is formated to

if aaaaaaa == 1 and bbbbb == 1 and bbbbb == 1 and bbbbb == 1 and bbbbb == 1 and
    b ==
    -1 and bbbbb == 1 and bbbbb == 1 and bbbbb == 1 and bbbbb == 1 and bbbbb ==
        1 and bbbbb == 1 then print(1) end

This line break is unexpected.

Change build instruction in README

What about to change this:

git clone --recurse-submodules https://github.com/Koihik/LuaFormatter.git
cd LuaFormatter
cmake .
make
make install

To this:

git clone --recurse-submodules https://github.com/Koihik/LuaFormatter.git
cd LuaFormatter
mkdir build
cd build
cmake ../
make
make install

So that tests work, now they don't pass because it's trying to read files from the parent directory.

chop down parameter for function call

#37 implemented the chopping down of parameters for function headers. It would be great to extend this to function calls as well.

Input

local str = test(123, 12312, 5345, 12312, 5345, 12312, 5345, 12312, 5345, 12312, 5345, 12312, 5345, 12312, 5345, 12312, 5345, 12312, 5345)

desired output

local str = test(123,
    12312,
    5345,
    12312,
    5345,
    12312,
    5345,
    12312,
    5345,
    12312,
    5345,
    12312,
    5345,
    12312,
    5345,
    12312,
    5345,
    12312,
    5345,
    12312,
    5345,)

current output

local str = test(
                123, 12312, 5345, 12312, 5345, 12312, 5345, 12312, 5345, 12312, 5345, 12312, 5345, 12312, 5345, 12312,
                5345, 12312, 5345
            )

Indentation error with long function parameters

Config:

break_after_functioncall_lp: true
break_before_functioncall_rp: true

Input:

print("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis non posuere nullam.")

Expected:

print(
    "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis non posuere nullam."
)

Actual:

print(
   
        "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis non posuere nullam."
)

Set configuration flags on the commandline.

Currently there exists no way to set configuration flags without a config file. This makes it unnecessarily hard for an editor plugin to dynamically change the formatting options.
Let’s take Emacs as an example. When indent-tabs-mode is true, I want LuaFormatter to set use_tab to true.
For this to work, I currently have to create a temporary configuration file for every invocation of LuaFormatter, which makes a local .lua-format (or global config.yaml, see PR #109) be ignored.

Many formatters deal with this problem by exposing their formatting options as long commandline switches, e.g. lua-format --use-tab --column-limit 100. Boolean switches are generally deactivated by prefixing them with “no” like in --no-use-tab.

Add Seperate Table Length Maximum

Sometimes you want tables to be broken earlier than the standard line limit, it'd be nice to have an extra setting to break tables earlier/later than other lines.

Examples:

Current:

some.really.long.function_call(with, many, parameters)
table = {that = should, really = be, broken = sooner}

Or:

some.really.long.function_call(with, many,
                               parameters)
table = {
  that = now,
  is = actually,
  broken = sooner,
}

Suggested:

some.really.long.function_call(with, many, parameters)
table = {
  that = now,
  is = actually,
  broken = sooner,
}

Note that chop_down_table only seems to "chop down" tables if they would be longer than the line length limit. This is sane behavior, just not exactly what is needed for my project.

[request] post on https://luarocks.org/

I think this is a great tool for the Lua community. One that has been lacking for a long time!!

Please consider posting it on luarocks and perhaps a mailing list announcement as well?

[bug] Lua 5.4 invalidates oddly crafted numbers

Lua 5.4 introduced a new parser for numbers.

In 5.3 and below code like below do run:

Lua 5.3.3  Copyright (C) 1994-2016 Lua.org, PUC-Rio
> x = 1.print(x)
1.0

The formatter correctly handles these odd numbers.

In 5.4 these are set to be invalid. Meaning that the parser will consume all tokens.
https://marc.info/?l=lua-l&m=156942228214223&w=2

The somewhat experimental support for 5.4 in LuaFormatter doesn't invalidate such numbers as it should.

chop_down_table and chop_down_kv_table not working

After last updates the functions chop_down_table and chop_down_kv_table is not working property.
Tested with versions 1.3.1 and 1.3.3.

With:

column_limit: 250
indent_width: 2
tab_width: 2
continuation_indent_width: 2
chop_down_table: true
chop_down_kv_table: true

Input:

test = {
  image = "test",
  list = {
    {
      ref = "testRef",
      tags = { "tagTest" },
      time = 10,

      materials = {
        {
          materialId = 123,
          count = 10
        }
      },
    },
  },
}

Output:

test = {image = 'test', list = {{ref = "testRef", tags = {"tagTest"}, time = 10, materials = {{materialId = 123, count = 10}}}}}

If column limit is set to 80 the output is:

test = {
  image = 'test',
  list = {
    {
      ref = "testRef",
      tags = {"tagTest"},
      time = 10,
      materials = {{materialId = 123, count = 10}},
    },
  },
}

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.