Skip to content

Commit 16e9d31

Browse files
author
Ian
committed
[>] [2018-02-23] Sync with main branch.
2 parents cfce39b + 0f7edad commit 16e9d31

File tree

213 files changed

+1713
-1488
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

213 files changed

+1713
-1488
lines changed

get_formatter_ast.lua

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
local get_params = request('get_formatter_ast.get_params')
22
local convert = request('!.file.convert')
33
local get_ast = request('!.lua.code.get_ast')
4-
local formatter_preprocess = request('!.formats.lua.save.formatter.preprocess')
4+
local transform_ast = request('!.formats.lua.transform_ast')
55

66
local parse =
77
function(s)
88
local result
99
result = get_ast(s)
10-
result = formatter_preprocess(result)
10+
result =
11+
transform_ast(
12+
result,
13+
{
14+
keep_comments = true,
15+
keep_unparsed_tail = true,
16+
}
17+
)
1118
return result
1219
end
1320

lcf-scm-1.rockspec

Lines changed: 133 additions & 145 deletions
Large diffs are not rendered by default.

readme.md

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
# Description
2+
3+
Formats any valid Lua 5.3 code.
4+
5+
Lines with code are wrapped to fit inside given margins.
6+
7+
Files with invalid Lua syntax may lose the content after the syntax
8+
error! lcf's parser is more permissible than Lua's, so make sure to
9+
verify the correctness of the file before running lcf.
10+
For example via `$ luac -p <filename>`.
11+
12+
Installation script deploys three command-line scripts:
13+
14+
* lua.reformat
15+
* lua.get_ast
16+
* lua.get_formatter_ast
17+
18+
Last two for people who love tinkering.
19+
20+
21+
# Requirements
22+
23+
Sole requirement is Lua v5.3. Earlier versions will not work.
24+
25+
It may or may not work under Windows. I've not tested it there.
26+
27+
For Lua v5.1 checkout `5.1` branch.
28+
29+
30+
# Installation
31+
32+
$ sudo luarocks make lcf-scm-1.rockspec
33+
34+
(also works `$ sudo luarocks install lcf`)
35+
36+
37+
# Usage
38+
39+
## From command-line
40+
41+
$ lua.reformat <f_in>
42+
43+
You can pass formatter parameters in command line. For available
44+
options call `$ lua.reformat`.
45+
46+
47+
## From Lua interpreter
48+
49+
Suppose you have a string with Lua code and wish to get another string
50+
with formatted code.
51+
52+
do
53+
local lua_code_str = 'do return end' -- < fill it
54+
55+
require('lcf.workshop.base')
56+
local get_ast = request('!.lua.code.get_ast')
57+
local get_formatted_code = request('!.formats.lua.save')
58+
59+
return get_formatted_code(get_ast(lua_code_str))
60+
end
61+
62+
63+
### Passing formatting parameters
64+
65+
You may override default parameters by passing a table with new values
66+
of changed parameters:
67+
68+
get_formatted_code(
69+
get_ast(lua_code_str),
70+
{
71+
indent_chunk = ' ',
72+
right_margin = 96,
73+
max_text_width = math.huge,
74+
keep_unparsed_tail = true,
75+
keep_comments = true,
76+
}
77+
)
78+
79+
* `indent_chunk` is a string using for building one indent. You may try
80+
value `|..` to see it's effect.
81+
82+
* `right_margin` limits length of line with indent. Setting it makes
83+
sense for printing.
84+
85+
* `max_text_width` limits length of line without indent, i.e. length of
86+
text in line. Setting it makes sense for windowed viewing in editor.
87+
88+
* `keep_unparsed_tail` is a flag to keep text after point where we
89+
failed to parse source.
90+
91+
Syntactically incorrect code may lose significant parts even with
92+
this flag. For example `f() = a` is formatted as `f()`. (Because
93+
it's parsed as assignment but formatted as function call.) But text
94+
like `1; f() = a` will fail to parse and will remain intact.
95+
96+
* `keep_comments` is a flag to keep comments. Comment text is not changed
97+
so comments may last beyond right margin.
98+
99+
Comments are "sinked-up" to statements level. So text
100+
101+
function(a, --parameter "a"
102+
b) --parameter "b"
103+
end
104+
105+
is formatted as
106+
107+
--parameter "a"
108+
function(a, b)
109+
--parameter "b"
110+
end
111+
112+
This is done to keep formatting routines simple.
113+
114+
---
115+
116+
* Based on my own generic stream processing core,
117+
which is extended to generic strings parser,
118+
which uses my own loseless Lua syntax representation,
119+
produces AST (annotated syntax tree),
120+
which is further simplified and structured for code formatter,
121+
which produces code layout,
122+
trying all possible variants to fit code under given margins
123+
and maintain indentation.
124+
125+
* It uses local copy of my ["workshop"](https://github.com/martin-eden/workshop) code hive.
126+
127+
* See also [my repositories contents](https://github.com/martin-eden/contents).
128+
129+
---
130+
```
131+
2016-08-16
132+
2017-01-28
133+
2017-09-26
134+
2018-02-23
135+
```

readme.txt

Lines changed: 0 additions & 117 deletions
This file was deleted.

reformat.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
local get_params = request('reformat.get_params')
22
local get_ast = request('!.lua.code.get_ast')
3-
local serialize_ast = request('!.formats.lua.save')
3+
local serialize_ast = request('!.lua.code.ast_as_code')
44
local convert = request('!.file.convert')
55

66
return

reformat/get_params.lua

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,24 @@ cmdline_processor.allowed_params =
1414
{
1515
{name = 'f_in_name', type = 'string'},
1616
{name = 'f_out_name', type = 'string'},
17+
{name = 'indent', type = 'key_str'},
1718
{name = 'right-margin', type = 'key_int'},
1819
{name = 'max-text-width', type = 'key_int'},
1920
{name = 'keep-comments', type = 'flag'},
20-
{name = 'indent', type = 'key_str'},
21+
{name = 'keep-unparsed-tail', type = 'flag'},
2122
}
2223

2324
--[[
2425
Although original default parameters are stored in
25-
[lua.save.formatter.interface] I override it here.
26+
[lua.formatter.interface] I override it here.
2627
]]
2728
local default_formatter_options =
2829
{
29-
right_margin = 120,
30-
max_text_width = 100,
3130
indent_chunk = ' ',
31+
right_margin = 96,
32+
max_text_width = math.huge,
3233
keep_comments = true,
34+
keep_unparsed_tail = true,
3335
}
3436

3537
local table_to_str = request('!.formats.lua_table.save')
@@ -39,23 +41,24 @@ return
3941
assert_table(args)
4042
if not args[1] or (args[1] == '--help') then
4143
print(usage_text)
42-
print('Default options:')
44+
print('Defaults:')
4345
print(table_to_str(default_formatter_options))
46+
print('\n-- Martin, 2018-02')
4447
return
4548
end
4649

4750
local params = cmdline_processor:run(args)
4851

49-
local f_in_name, f_out_name
50-
f_in_name = params.f_in_name
52+
local f_in_name = params.f_in_name
5153
if not f_in_name then
5254
print(usage_text)
5355
return
5456
end
55-
f_out_name = params.f_out_name or (f_in_name .. '.formatted')
5657

57-
params['max-text-width'] = params['max-text-width'] or params['right-margin']
58-
params['right-margin'] = params['right-margin'] or params['max-text-width']
58+
local f_out_name = params.f_out_name
59+
if not f_out_name then
60+
f_out_name = f_in_name .. '.formatted'
61+
end
5962

6063
local formatter_options =
6164
new(
@@ -65,6 +68,7 @@ return
6568
right_margin = params['right-margin'],
6669
max_text_width = params['max-text-width'],
6770
keep_comments = params['keep-comments'],
71+
keep_unparsed_tail = params['keep-unparsed-tail'],
6872
}
6973
)
7074

reformat/usage_text.lua

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,24 @@ return
33
Reformat Lua 5.3 code.
44
55
lua.reformat <f_in> [<f_out>]
6-
^ ^ ^
7-
| | |
8-
+-- [<options>] -----+
6+
7+
8+
└── [<options>] ─────┘
99
1010
<options>:
11-
--right-margin=<int> -- Right margin, indent chars counts.
12-
--max-text-width=<int> -- Maximum text length, ignoring indent chars.
13-
--keep-comments -- Keep comments.
14-
--~keep-comments -- Remove comments.
15-
--indent=<str> -- Use given string as indent chunk
1611
17-
Notes
18-
* In case when only one parameter of (right-margin, max-text-width)
19-
is given, another parameter is set to same value. This is done
20-
because these parameters have similar effect and in other case
21-
you wish to set another parameter to near value manually.
12+
--indent=<str> Use given string as indent chunk.
2213
23-
* If <f_out> name is not given, it's generated using <f_in> name.
14+
--right-margin=<int> Right margin, including indent part.
15+
--max-text-width=<int> Maximum text length, excluding indent part.
2416
25-
-- Martin, 2017-10-14
17+
--keep-comments Keep comments.
18+
--~keep-comments Remove comments.
19+
20+
--keep-unparsed-tail Keep unparsed file end.
21+
--~keep-unparsed-tail Remove unparsed file end.
22+
23+
<f_out>:
24+
25+
If not given, file name is generated using <f_in> prefix.
2626
]]

0 commit comments

Comments
 (0)