Lua beautifier

Simple perl script beautifier for lua, by @pkulchenko
This commit is contained in:
Joost van Doorn 2016-05-14 16:34:57 +02:00
parent aae09ea463
commit 7096ceb195
13 changed files with 179 additions and 4 deletions

View File

@ -42,7 +42,7 @@ matrix:
env: env:
global: global:
- APM_TEST_PACKAGES="language-marko language-html-swig language-svg language-d mavensmate-atom" - APM_TEST_PACKAGES="language-marko language-html-swig language-svg language-d mavensmate-atom language-lua"
- PATH="/home/travis/gopath/bin:$HOME/.linuxbrew/bin:$PATH" - PATH="/home/travis/gopath/bin:$HOME/.linuxbrew/bin:$PATH"
addons: addons:

View File

@ -1,4 +1,5 @@
# dev # dev
- Add beautifier for the Lua language.
- Add [ocp-indent](https://github.com/OCamlPro/ocp-indent) beautifier for the OCaml language. - Add [ocp-indent](https://github.com/OCamlPro/ocp-indent) beautifier for the OCaml language.
- Add [elm-format](https://github.com/avh4/elm-format) beautifier for the Elm language. - Add [elm-format](https://github.com/avh4/elm-format) beautifier for the Elm language.
- Add [clang-format](http://clang.llvm.org/docs/ClangFormat.html) beautifier for C/C++/Obj-C languages. - Add [clang-format](http://clang.llvm.org/docs/ClangFormat.html) beautifier for C/C++/Obj-C languages.

View File

@ -74,6 +74,7 @@ Or Settings/Preferences ➔ Packages ➔ Search for `atom-beautify`
- [x] [Erlang](https://github.com/Glavin001/atom-beautify/pull/683) - [x] [Erlang](https://github.com/Glavin001/atom-beautify/pull/683)
- Requires erlang syntax_tools to be installed - Requires erlang syntax_tools to be installed
- [x] [Crystal](https://github.com/Glavin001/atom-beautify/pull/900) - [x] [Crystal](https://github.com/Glavin001/atom-beautify/pull/900)
- [x] [Lua](https://github.com/Glavin001/atom-beautify/pull/973)
## Usage ## Usage

View File

@ -126,7 +126,7 @@ build_script:
- cd %APPVEYOR_BUILD_FOLDER% - cd %APPVEYOR_BUILD_FOLDER%
# Install languages to Atom # Install languages to Atom
- apm install language-marko language-html-swig language-svg language-elm language-d mavensmate-atom - apm install language-marko language-html-swig language-svg language-elm language-d mavensmate-atom language-lua
# Show current PATH # Show current PATH
- echo %PATH% - echo %PATH%
# Run tests on package # Run tests on package

View File

@ -0,0 +1,33 @@
-- Ensure that that the element at i is in the right position,
-- and return a closure which can be used for continuing the sort.
function quicksorter(i, vec, low, high)
if low >= high then
return quicksorter
else -- low < high
-- partition the vector and initialize the child closures
local middle = partition(vec, low, high)
local left, right = quicksorter
-- Create the promise
local function self(i, vec, low, high)
if i < middle then
left = left(i, vec, low, middle-1)
return self
elseif i > middle then
right = right(i, vec, middle+1, high)
return self
end
end
-- Force the promise until i is in the right position
return self(i, vec, low, high)
end
end
function lazysort(vec, low, high)
local sorter = quicksorter
return function(i)
sorter = sorter(i, vec, low, high)
return vec[i]
end
end

View File

@ -0,0 +1,33 @@
-- Ensure that that the element at i is in the right position,
-- and return a closure which can be used for continuing the sort.
function quicksorter(i, vec, low, high)
if low >= high then
return quicksorter
else -- low < high
-- partition the vector and initialize the child closures
local middle = partition(vec, low, high)
local left, right = quicksorter
-- Create the promise
local function self(i, vec, low, high)
if i < middle then
left = left(i, vec, low, middle-1)
return self
elseif i > middle then
right = right(i, vec, middle+1, high)
return self
end
end
-- Force the promise until i is in the right position
return self(i, vec, low, high)
end
end
function lazysort(vec, low, high)
local sorter = quicksorter
return function(i)
sorter = sorter(i, vec, low, high)
return vec[i]
end
end

View File

@ -86,6 +86,10 @@
{ {
"name": "Patrick Steele-Idem", "name": "Patrick Steele-Idem",
"url": "https://github.com/psteeleidem" "url": "https://github.com/psteeleidem"
},
{
"name": "Joost van Doorn",
"url": "https://github.com/JoostvDoorn"
} }
], ],
"engines": { "engines": {
@ -192,7 +196,8 @@
"go", "go",
"golang", "golang",
"svg", "svg",
"elm" "elm",
"lua"
], ],
"devDependencies": { "devDependencies": {
"coffeelint": "^1.10.1", "coffeelint": "^1.10.1",

View File

@ -26,7 +26,7 @@ describe "BeautifyLanguages", ->
"mustache", "objective-c", "perl", "php", "mustache", "objective-c", "perl", "php",
"python", "ruby", "sass", "sql", "svg", "python", "ruby", "sass", "sql", "svg",
"xml", "csharp", "gfm", "marko", "xml", "csharp", "gfm", "marko",
"go", "html-swig" "go", "html-swig", "lua"
] ]
# All Atom packages that Atom Beautify is dependent on # All Atom packages that Atom Beautify is dependent on
dependentPackages = [ dependentPackages = [

View File

@ -49,6 +49,7 @@ module.exports = class Beautifiers extends EventEmitter
'fortran-beautifier' 'fortran-beautifier'
'js-beautify' 'js-beautify'
'jscs' 'jscs'
'lua-beautifier'
'ocp-indent' 'ocp-indent'
'perltidy' 'perltidy'
'php-cs-fixer' 'php-cs-fixer'

View File

@ -0,0 +1,57 @@
# Copyright 2011 Paul Kulchenko
# Credits: http://notebook.kulchenko.com/programming/lua-beautifier-in-55-lines-of-perl
use strict;
use warnings;
use constant INDENT => ' ';
my($currIndent, $nextIndent, $prevLength) = (0, 0, 0);
while (<>) {
chomp;
s/^\s+|\s+$//g; # remote all spaces on both ends
s/\s+/ /g; # replace all whitespaces inside the string with one space
my $orig = $_;
s/(['"])[^\1]*?\1//g; # remove all quoted fragments for proper bracket processing
s/\s*--.+//; # remove all comments; this ignores long bracket style comments
# open a level; increase next indentation; don't change current one
if (/^((local )?function|repeat|while)\b/ && !/\bend\s*[\),;]*$/
|| /\b(then|do)$/ && !/^elseif\b/ # only open on 'then' if there is no 'elseif'
|| /^if\b/ && /\bthen\b/ && !/\bend$/ # only open on 'if' if there is no 'end' at the end
|| /\bfunction\s*\([^\)]*\)$/) {
$nextIndent = $currIndent + 1;
}
# close the level; change both current and next indentation
elsif (/^until\b/
|| /^end\s*[\),;]*$/
|| /^end\s*\)\s*\.\./ # this is a special case of 'end).."some string"'
|| /^else(if)?\b/ && /\bend$/) {
$nextIndent = $currIndent = $currIndent - 1;
}
# keep the level; decrease the current indentation; keep the next one
elsif (/^else\b/
|| /^elseif\b/) {
($nextIndent, $currIndent) = ($currIndent, $currIndent-1);
}
my $brackets = y/(// - y/)//; # capture unbalanced brackets
my $curly = y/{// - y/}//; # capture unbalanced curly brackets
# close (curly) brackets if needed
$currIndent += $curly if $curly < 0 && /^\}/;
$currIndent += $brackets if $brackets < 0 && /^\)/;
warn "WARNING: negative indentation at line $.: $orig\n" if $currIndent < 0;
print((length($orig) ? (INDENT x $currIndent) : ''), $orig, "\n")
if $prevLength > 0 || length($orig) > 0; # this is to collapse empty lines
$nextIndent += $brackets + $curly;
$currIndent = $nextIndent;
$prevLength = length($orig);
}
warn "WARNING: positive indentation at the end\n" if $nextIndent > 0;

View File

@ -0,0 +1,21 @@
###
###
path = require("path")
"use strict"
Beautifier = require('../beautifier')
module.exports = class Lua extends Beautifier
name: "lua"
options: {
Lua: true
}
beautify: (text, language, options) ->
lua_beautifier = path.resolve(__dirname, "beautifier.pl")
@run("perl", [
lua_beautifier,
'<',
@tempFile("input", text)
])

View File

@ -40,6 +40,7 @@ module.exports = class Languages
"jsx" "jsx"
"latex" "latex"
"less" "less"
"lua"
"markdown" "markdown"
'marko' 'marko'
"mustache" "mustache"

22
src/languages/lua.coffee Normal file
View File

@ -0,0 +1,22 @@
module.exports = {
name: "Lua"
namespace: "lua"
###
Supported Grammars
###
grammars: [
"Lua"
]
###
Supported extensions
###
extensions: [
'lua'
]
defaultBeautifier: "lua"
}