diff --git a/.travis.yml b/.travis.yml index 443ec69..b91ab6b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -42,7 +42,7 @@ matrix: env: 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" addons: diff --git a/CHANGELOG.md b/CHANGELOG.md index 8dcda17..24d98b1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,5 @@ # dev +- Add beautifier for the Lua 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 [clang-format](http://clang.llvm.org/docs/ClangFormat.html) beautifier for C/C++/Obj-C languages. diff --git a/README.md b/README.md index 81052ee..73e4ea6 100644 --- a/README.md +++ b/README.md @@ -74,6 +74,7 @@ Or Settings/Preferences ➔ Packages ➔ Search for `atom-beautify` - [x] [Erlang](https://github.com/Glavin001/atom-beautify/pull/683) - Requires erlang syntax_tools to be installed - [x] [Crystal](https://github.com/Glavin001/atom-beautify/pull/900) +- [x] [Lua](https://github.com/Glavin001/atom-beautify/pull/973) ## Usage diff --git a/appveyor.yml b/appveyor.yml index f8dd85d..5f7c79f 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -126,7 +126,7 @@ build_script: - cd %APPVEYOR_BUILD_FOLDER% # 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 - echo %PATH% # Run tests on package diff --git a/examples/simple-jsbeautifyrc/lua/expected/test.lua b/examples/simple-jsbeautifyrc/lua/expected/test.lua new file mode 100644 index 0000000..5c00e55 --- /dev/null +++ b/examples/simple-jsbeautifyrc/lua/expected/test.lua @@ -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 diff --git a/examples/simple-jsbeautifyrc/lua/original/test.lua b/examples/simple-jsbeautifyrc/lua/original/test.lua new file mode 100644 index 0000000..753deb3 --- /dev/null +++ b/examples/simple-jsbeautifyrc/lua/original/test.lua @@ -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 diff --git a/package.json b/package.json index 0cbd2b1..5cc377d 100644 --- a/package.json +++ b/package.json @@ -86,6 +86,10 @@ { "name": "Patrick Steele-Idem", "url": "https://github.com/psteeleidem" + }, + { + "name": "Joost van Doorn", + "url": "https://github.com/JoostvDoorn" } ], "engines": { @@ -192,7 +196,8 @@ "go", "golang", "svg", - "elm" + "elm", + "lua" ], "devDependencies": { "coffeelint": "^1.10.1", diff --git a/spec/beautify-languages-spec.coffee b/spec/beautify-languages-spec.coffee index e3f9f24..c7ebdcc 100644 --- a/spec/beautify-languages-spec.coffee +++ b/spec/beautify-languages-spec.coffee @@ -26,7 +26,7 @@ describe "BeautifyLanguages", -> "mustache", "objective-c", "perl", "php", "python", "ruby", "sass", "sql", "svg", "xml", "csharp", "gfm", "marko", - "go", "html-swig" + "go", "html-swig", "lua" ] # All Atom packages that Atom Beautify is dependent on dependentPackages = [ diff --git a/src/beautifiers/index.coffee b/src/beautifiers/index.coffee index 5c97d32..b469166 100644 --- a/src/beautifiers/index.coffee +++ b/src/beautifiers/index.coffee @@ -49,6 +49,7 @@ module.exports = class Beautifiers extends EventEmitter 'fortran-beautifier' 'js-beautify' 'jscs' + 'lua-beautifier' 'ocp-indent' 'perltidy' 'php-cs-fixer' diff --git a/src/beautifiers/lua-beautifier/beautifier.pl b/src/beautifiers/lua-beautifier/beautifier.pl new file mode 100644 index 0000000..349831a --- /dev/null +++ b/src/beautifiers/lua-beautifier/beautifier.pl @@ -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; diff --git a/src/beautifiers/lua-beautifier/index.coffee b/src/beautifiers/lua-beautifier/index.coffee new file mode 100644 index 0000000..e7ddfa4 --- /dev/null +++ b/src/beautifiers/lua-beautifier/index.coffee @@ -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) + ]) diff --git a/src/languages/index.coffee b/src/languages/index.coffee index 4861a1c..027488b 100644 --- a/src/languages/index.coffee +++ b/src/languages/index.coffee @@ -40,6 +40,7 @@ module.exports = class Languages "jsx" "latex" "less" + "lua" "markdown" 'marko' "mustache" diff --git a/src/languages/lua.coffee b/src/languages/lua.coffee new file mode 100644 index 0000000..3595401 --- /dev/null +++ b/src/languages/lua.coffee @@ -0,0 +1,22 @@ +module.exports = { + + name: "Lua" + namespace: "lua" + + ### + Supported Grammars + ### + grammars: [ + "Lua" + ] + + ### + Supported extensions + ### + extensions: [ + 'lua' + ] + + defaultBeautifier: "lua" + +}