From bb244e05f9668ba9f920f32f75270f7bceaaa3fd Mon Sep 17 00:00:00 2001 From: Alejandro Baez Date: Sat, 12 Nov 2016 22:08:33 -0500 Subject: [PATCH 1/4] merge of abaez/ta-moonscript/moonscript.lua --- lexers/moonscript.lua | 457 +++++++++++++++--------------------------- 1 file changed, 166 insertions(+), 291 deletions(-) diff --git a/lexers/moonscript.lua b/lexers/moonscript.lua index 94f6360..599ec53 100644 --- a/lexers/moonscript.lua +++ b/lexers/moonscript.lua @@ -1,313 +1,188 @@ +--- Moonscript LPeg lexer. +-- See @{README.md} for details on usage. Used Mitchell's lua and original +-- Leaf's moonscript lexer for reference. +-- @copyright 2016 +-- @license MIT (see LICENSE) +-- @module moonscript + local l = require("lexer") -local token, word_match -token, word_match = l.token, l.word_match -local S, P, R, Cmt -do - local _obj_0 = require("lpeg") - S, P, R, Cmt = _obj_0.S, _obj_0.P, _obj_0.R, _obj_0.Cmt -end +local token, word_match = l.token, l.word_match +local P, S, R = lpeg.P, lpeg.S, lpeg.R + local _NAME = "moonscript" -local ws = token(l.WHITESPACE, l.space ^ 1) -local longstring = Cmt("[" * lpeg.C(P("=") ^ 0) * "[", function(input, index, eq) - local _, e = input:find("]" .. tostring(eq) .. "]", index, true) - return (e or #input) + 1 -end) -local line_comment = "--" * l.nonnewline ^ 0 -local block_comment = "--" * longstring + +-- Whitespace. +local ws = token(l.WHITESPACE, l.space^1) + +local longstring = lpeg.Cmt('[' * lpeg.C(P('=')^0) * '[', + function(input, index, eq) + local _, e = input:find(']'..eq..']', index, true) + return (e or #input) + 1 + end) + +-- Comments. +local line_comment = '--' * l.nonnewline^0 +local block_comment = '--' * longstring local comment = token(l.COMMENT, block_comment + line_comment) -local sq_str = l.delimited_range("'") -local dq_str = l.delimited_range('"') -local string = token(l.STRING, sq_str + dq_str) + token("longstring", longstring) -local literals = token(l.LABEL, word_match({ - "true", - "false", - "nil" -})) -local keyword = token(l.KEYWORD, word_match({ - "with", - "else", - "do", - "class", - "while", - "continue", - "or", - "switch", - "using", - "export", - "extends", - "not", - "and", - "return", - "elseif", - "if", - "break", - "for", - "in", - "import", - "local", - "when", - "then", - "from", - "unless" -})) + +-- Strings. +local sq_str = l.delimited_range("'", false, true) +local dq_str = l.delimited_range('"', false, true) + +local string = token(l.STRING, sq_str + dq_str) + + token('longstring', longstring) + +-- Numbers. +local number = token(l.NUMBER, l.float + l.integer) + +-- Literals. +literals = token(l.LABEL, word_match{ + "true", "false", "nil" +}) + +-- Keywords. +local keyword = token(l.KEYWORD, word_match { + -- lua + 'and', 'break', 'do', 'else', 'elseif', 'for', + 'if', 'in', 'local', 'not', 'or', 'return', 'then', + 'while', + -- moonscript + 'continue', 'class', 'export', 'extends', 'from', 'import', 'super', + 'switch', 'unless', 'using', 'when', 'with' +}) + +-- Invalids. local invalid = token(l.ERROR, word_match({ "end", "function", "repeat" })) -local constant = token(l.CONSTANT, word_match({ - "_G", - "_VERSION", - "_ENV" -})) -local func = token(l.FUNCTION, word_match({ - "assert", - "collectgarbage", - "dofile", - "error", - "getmetatable", - "ipairs", - "load", - "loadfile", - "next", - "pairs", - "pcall", - "print", - "rawequal", - "rawget", - "rawset", - "require", - "select", - "setmetatable", - "tonumber", - "tostring", - "type", - "xpcall", - "rawlen" -})) -local library = token("library", word_match({ - "coroutine", - "coroutine.create", - "coroutine.resume", - "coroutine.running", - "coroutine.status", - "coroutine.wrap", - "coroutine.yield", + +-- Constants. +local constant = token(l.CONSTANT, word_match{ + '_G', '_VERSION', + -- Added in 5.2. + '_ENV' +}) + +-- Functions. +local func = token(l.FUNCTION, word_match{ + 'assert', 'collectgarbage', 'dofile', 'error', 'getmetatable', 'ipairs', + 'load', 'loadfile', 'next', 'pairs', 'pcall', 'print', 'rawequal', 'rawget', + 'rawset', 'require', 'select', 'setmetatable', 'tonumber', 'tostring', 'type', + 'xpcall', + -- Added in 5.2. + 'rawlen' +}) + +-- Libraries. +local library = token('library', word_match({ + -- Coroutine. + "coroutine", "coroutine.create", "coroutine.resume", "coroutine.running", + "coroutine.status", "coroutine.wrap", "coroutine.yield", + -- Coroutine added in 5.3. "coroutine.isyieldable", - "package", - "package.cpath", - "package.loaded", - "package.loadlib", - "package.path", - "package.preload", - "package.config", - "package.searchers", - "package.searchpath", - "utf8", - "utf8.char", - "utf8.charpattern", - "utf8.codepoint", - "utf8.codes", - "utf8.len", - "utf8.offset", - "string", - "string.byte", - "string.char", - "string.dump", - "string.find", - "string.format", - "string.gmatch", - "string.gsub", - "string.len", - "string.lower", - "string.match", - "string.rep", - "string.reverse", - "string.sub", - "string.upper", - "string.pack", - "string.packsize", - "string.unpack", - "table", - "table.concat", - "table.insert", - "table.remove", - "table.sort", - "table.pack", - "table.unpack", + -- Module. + "package", "package.cpath", "package.loaded", "package.loadlib", + "package.path", "package.preload", + -- Module added in 5.2. + "package.config", "package.searchers", "package.searchpath", + -- UTF-8 added in 5.3. + "utf8", "utf8.char", "utf8.charpattern", "utf8.codepoint", "utf8.codes", + "utf8.len", "utf8.offset", + -- String. + "string", "string.byte", "string.char", "string.dump", "string.find", + "string.format", "string.gmatch", "string.gsub", "string.len", "string.lower", + "string.match", "string.rep", "string.reverse", "string.sub", "string.upper", + -- String added in 5.3. + "string.pack", "string.packsize", "string.unpack", + -- Table. + "table", "table.concat", "table.insert", "table.remove", "table.sort", + -- Table added in 5.2. + "table.pack", "table.unpack", + -- Table added in 5.3. "table.move", - "math", - "math.abs", - "math.acos", - "math.asin", - "math.atan", - "math.ceil", - "math.cos", - "math.deg", - "math.exp", - "math.floor", - "math.fmod", - "math.huge", - "math.log", - "math.max", - "math.min", - "math.modf", - "math.pi", - "math.rad", - "math.random", - "math.randomseed", - "math.sin", - "math.sqrt", - "math.tan", - "math.maxinteger", - "math.mininteger", - "math.tointeger", - "math.type", + -- Math. + "math", "math.abs", "math.acos", "math.asin", "math.atan", "math.ceil", + "math.cos", "math.deg", "math.exp", "math.floor", "math.fmod", "math.huge", + "math.log", "math.max", "math.min", "math.modf", "math.pi", "math.rad", + "math.random", "math.randomseed", "math.sin", "math.sqrt", "math.tan", + -- Math added in 5.3. + "math.maxinteger", "math.mininteger", "math.tointeger", "math.type", "math.ult", - "io", - "io.close", - "io.flush", - "io.input", - "io.lines", - "io.open", - "io.output", - "io.popen", - "io.read", - "io.stderr", - "io.stdin", - "io.stdout", - "io.tmpfile", - "io.type", - "io.write", - "os", - "os.clock", - "os.date", - "os.difftime", - "os.execute", - "os.exit", - "os.getenv", - "os.remove", - "os.rename", - "os.setlocale", - "os.time", + -- IO. + "io", "io.close", "io.flush", "io.input", "io.lines", "io.open", "io.output", + "io.popen", "io.read", "io.stderr", "io.stdin", "io.stdout", "io.tmpfile", + "io.type", "io.write", + -- OS. + "os", "os.clock", "os.date", "os.difftime", "os.execute", "os.exit", + "os.getenv", "os.remove", "os.rename", "os.setlocale", "os.time", "os.tmpname", - "debug", - "debug.debug", - "debug.gethook", - "debug.getinfo", - "debug.getlocal", - "debug.getmetatable", - "debug.getregistry", - "debug.getupvalue", - "debug.sethook", - "debug.setlocal", - "debug.setmetatable", - "debug.setupvalue", + -- Debug. + "debug", "debug.debug", "debug.gethook", "debug.getinfo", "debug.getlocal", + "debug.getmetatable", "debug.getregistry", "debug.getupvalue", + "debug.sethook", "debug.setlocal", "debug.setmetatable", "debug.setupvalue", "debug.traceback", - "debug.getuservalue", - "debug.setuservalue", - "debug.upvalueid", + -- Debug added in 5.2. + "debug.getuservalue", "debug.setuservalue", "debug.upvalueid", "debug.upvaluejoin", - "package.loaders", - "package.seeall", + + -- Module deprecated in 5.2. + "package.loaders", "package.seeall", + -- Table deprecated in 5.2. "table.maxn", + -- Math deprecated in 5.2. "math.log10", - "math.atan2", - "math.cosh", - "math.frexp", - "math.ldexp", - "math.pow", - "math.sinh", - "math.tanh", - "bit32", - "bit32.arshift", - "bit32.band", - "bit32.bnot", - "bit32.bor", - "bit32.btest", - "bit32.extract", - "bit32.lrotate", - "bit32.lshift", - "bit32.replace", - "bit32.rrotate", - "bit32.rshift", - "bit32.xor", - "debug.getfenv", - "debug.setfenv" -}, ".")) -local alphanum = R("az", "AZ", "09", "__") -local cls = token(l.CLASS, P("@") * alphanum ^ 0 + (R("AZ") * alphanum ^ 0) + word_match({ - "self", - "super" -})) -local upper_operator = token(l.TYPE, P("->") + P("=>") + S("[]()")) -local operator = token(l.OPERATOR, S("+-*/%^#=<>&|~;:,.{}[]()!\\")) + -- Math deprecated in 5.3. + "math.atan2", "math.cosh", "math.frexp", "math.ldexp", "math.pow", + "math.sinh", "math.tanh", + -- Bit32 deprecated in 5.3. + "bit32", "bit32.arshift", "bit32.band", "bit32.bnot", "bit32.bor", + "bit32.btest", "bit32.extract", "bit32.lrotate", "bit32.lshift", + "bit32.replace", "bit32.rrotate", "bit32.rshift", "bit32.xor", + -- Debug deprecated in 5.2. + "debug.getfenv", "debug.setfenv" +}, '.')) + +-- Identifiers. local identifier = token(l.IDENTIFIER, l.word) -local key = token(l.FUNCTION, P(":") * alphanum ^ 1 + alphanum ^ 1 * P(":")) -local lua_integer = P("-") ^ -1 * (l.hex_num + l.dec_num) -local number = token(l.NUMBER, l.float + lua_integer) -local _tokenstyles = { - longstring = l.STYLE_STRING, - library = l.STYLE_TYPE -} + + +-- Operators. +operator = token(l.OPERATOR, S "+-*/%^#=<>&|~;:,.{}[]()!\\") + +local alphanum = R("az", "AZ", "09", "__") + +local cls = token(l.CLASS, P("@") * alphanum^0 + (R("AZ") * alphanum^0) + word_match {"self", "super"}) +local upper_operator = token(l.TYPE, P"->" + P"=>" + S"[]()") + +local tbl_key = token(l.FUNCTION, P":" * alphanum^1 + alphanum^1 * P":") + + + local _rules = { - { - "whitespace", - ws - }, - { - "function", - func + key - }, - { - "label", - literals - }, - { - "keyword", - keyword - }, - { - "error", - invalid - }, - { - "class", - cls - }, - { - "constant", - constant - }, - { - "library", - library - }, - { - "identifier", - identifier - }, - { - "string", - string - }, - { - "comment", - comment - }, - { - "number", - number - }, - { - "type", - upper_operator - }, - { - "operator", - operator - } + {"whitespace", ws}, + {"function", func + tbl_key}, + {"label", literals}, + {"keyword", keyword}, + {"error", invalid}, + {"class", cls}, + {"constant", constant}, + {"library", library}, + {"identifier", identifier}, + {"string", string}, + {"comment", comment}, + {"number", number}, + {"type", upper_operator}, + {"operator", operator} +} + +local _tokenstyles = { + longstring = l.STYLE_STRING, + library = l.STYLE_TYPE, + error = l.STYLE_ERROR, } + return { _NAME = _NAME, _rules = _rules, From 81f6c5ea09279bbf83fa4876d0b9817ba11d9f7c Mon Sep 17 00:00:00 2001 From: Alejandro Baez Date: Sat, 12 Nov 2016 22:12:12 -0500 Subject: [PATCH 2/4] adding snippets from abaez/ta-moonscript --- snippets.lua | 53 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 snippets.lua diff --git a/snippets.lua b/snippets.lua new file mode 100644 index 0000000..75bd490 --- /dev/null +++ b/snippets.lua @@ -0,0 +1,53 @@ +--- Moonscript snippets. +-- @author [Alejandro Baez](https://twitter.com/a_baez) +-- copyright 2015 +-- @license MIT (see LICENSE) +-- @module snippets + +return { + -- function + fn = '%1(name) = %2((%3(param)) )%4(-)> \n\t%0', + fna = '%1((%2(param)) )%3(-)> %0', + + -- oop + class = 'class %1(Name)\n\t%0', + ce = 'class %1(Name) extends %2(NewName)\n\t%0', + ca = 'class extends %1(Name)\n\t%0', + mn = 'new: %1(param) => \n\t%0', + fnm = '%1(name): %2((%3(param))) => \n\t%0', + + -- conditionals + ['if'] = 'if %1(expr)\n\t%0', + ['ift'] = 'if %1(expr) then %0', + ['ifc'] = 'continue if %1(expr) %0', + ['ife'] = 'if %1(expr)\n\t%2(body)\nelse\n\t%0', + ['else'] = 'else \n\t%0', + ['elseif'] = 'elseif %1(expr)\n\t%0', + ['unless'] = 'unless %1(expr)\n\t%0', + ['when'] = 'when %1(expr)\n\t%0', + ['switch'] = 'switch %1(item)\n\t%2(when)\n\t%3(else)', + + -- keys + ['key'] = '%1(key): %2(value)%0', + ['var'] = ':%1(variable)%0', + + -- comprehensions + lc = '[%0 for %1(value) in ipairs %2(list)]', + li = '[%0 for %1(value) in %2(iterator)%3( when)]', + ln = '[%0 for %1(i) = %2(1), %3(10)]', + tc = '{%0 for %1(k), %2(v) in pairs %3(table)%4( when)}', + + -- loops + ['for'] = 'for %1(i) = %2(1), %3(10)%4(, %5(-1))\n\t%0', + ford = 'for %1(value) in %2(iterator) do %0', + fori = 'for %1(value) in %2(iterator)\n\t%0', + forp = 'for %1(k), %2(v) in %3(i)pairs %4(table)\n\t%0', + ['while'] = 'while %1(expr)\n\t%0', + wd = 'while %1(expr) do %0', + + -- random + ['#'] = '#{%1(expr)}%0', + imp = 'import %1(values) %2(from %3(module))', + r = 'require "%1(module)"%0', + ['with'] = 'with %1(expr)\n\t%0', +} From 3c0670404a37c34aaf82f6aeb79dca830af19f57 Mon Sep 17 00:00:00 2001 From: Alejandro Baez Date: Sat, 12 Nov 2016 22:12:37 -0500 Subject: [PATCH 3/4] replacing init with abaez/ta-moonscript --- init.lua | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 60 insertions(+), 6 deletions(-) diff --git a/init.lua b/init.lua index 5910a98..62e131c 100644 --- a/init.lua +++ b/init.lua @@ -1,8 +1,62 @@ -local file_types = require "textadept.file_types" -file_types.extensions.moon = "moonscript" +--- the Textadept initializer for the Moonscript module. +-- @copyright 2015-2016 +-- @license MIT (see LICENSE) +-- @module init -ui.set_theme(CURSES and "term" or "moon", { - font = "Source Code Pro", - fontsize = 14, -}) +local _snippets = require("moonscript.snippets") +textadept.file_types.extensions.moon = 'moonscript' +textadept.editing.comment_string.moonscript = '--' + +textadept.run.compile_commands.moonscript = 'moonc %f' +textadept.run.run_commands.moonscript = 'moon %f' + +textadept.run.build_commands["Tupfile(%.lua)?"] = "tup" + +events.connect(events.LEXER_LOADED, function (lang) + if lang ~= 'moonscript' then return end + + buffer.tab_width = 2 + buffer.use_tabs = false +end) + +if type(snippets) == 'table' then + snippets.moonscript = _snippets +end + +keys.moonscript = { + [not OSX and not CURSES and 'cl' or 'ml'] = { + -- Open this module for editing: `Alt/⌘-L` `M` + s = function() + io.open_file(_USERHOME..'/modules/moonscript/snippets.lua') + end, + }, + +} + +--- compiles automatically any moonscript file. +-- disable by changing _AUTOLINT to false. +events.connect(events.FILE_AFTER_SAVE, function() + if buffer:get_lexer() ~= 'moonscript' or not _MOONAUTOLINT then return end + buffer:annotation_clear_all() + local err = io.popen("moonc -l " .. buffer.filename .. " 2>&1"):read('*a') + + local line = err:match("^.+(%d+).+>>") + if line and tonumber(line) > 0 then + line = tonumber(line) - 1 + local msg = err:match(">>.+"):gsub('>>%s+','') + -- If the error line is not onscreen, annotate the current line. + if (line < buffer.first_visible_line or + line > buffer.first_visible_line + buffer.lines_on_screen) then + msg = 'line '..(line + 1)..'\n'..msg + line = buffer:line_from_position(buffer.current_pos) + end + buffer.annotation_visible = 2 + buffer.annotation_text[line] = "Error: " .. msg + buffer.annotation_style[line] = 8 -- error style number + buffer:goto_line(line) + end +end) + + +return { } From 3686b15809710d3f6c0f8bf38c7ee764ff7252e4 Mon Sep 17 00:00:00 2001 From: Alejandro Baez Date: Sat, 12 Nov 2016 22:57:46 -0500 Subject: [PATCH 4/4] readme: updated from abaez/ta-moonscript --- README.md | 74 ++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 65 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index f6997d6..04eba79 100644 --- a/README.md +++ b/README.md @@ -1,23 +1,61 @@ # MoonScript Textadept support +[![license][6i]][6l] +[Moonscript][4] module for [Textadept][1]. -This package comes with both a theme and a syntax lexer. +## Information +This module comes with snippets, theme, lexer, and auto lint for Moonscript. +The snippets can be found under the **snippets.lua** file. The theme is located +under **themes/** directory. The lexer is in **lexer/** directory. Auto linting +allows you to have your work checked after save. It is disabled by default, but +you can very easily enabled as described below, under *Usage*. ![screenshot](https://raw.githubusercontent.com/leafo/moonscript-textadept/master/screenshot.png) ## Install +To install, you need to clone the repository into your **_USERHOME/modules** directory: -To install, copy `lexers/moonscript.lua` to a `lexers` directory in your local -textadept directory (usually `.textadept`). +``` +cd ~/.textadept/modules +git clone https://github.com/leafo/moonscript-textadept.git \ + moonscript +``` + +You are done! If you want to use the latest in development version of the lexer, +or any of the other abilities of the module, then continue along in *Usage*. -To install the theme copy `themes/moon.lua` to a `themes` directory in your -local textadept directory. +### Usage + +#### enable auto lint. +You only need to modify your **_USERHOME/init.lua** file with the following: + +``` +-- init.lua -Update your `init.lua`: +_MOONAUTOLINT = true +``` + +#### moonscript lexer +Copy the **lexers/moonscript.lua** file into your **_USERHOME/lexers/** +directory: + +``` +cp ~/.textadept/modules/moonscript/lexers/moonscript.lua \ + ~/.textadept/lexers/moonscript.lua +``` + +#### Theme install +To install the theme, copy **themes/moon.lua** into **themes/** directory in +your local textadept directory. + +``` +cp ~/.textadept/modules/moonscript/themes/moon.lua \ + ~/.textadept/themes/moon.lua +``` + +Then update your **_USERHOME/init.lua** with the following: ```lua --- enable the lexer -local file_types = require "textadept.file_types" -file_types.extensions.moon = "moonscript" +-- init.lua -- enable the theme, with a custom font (optional) ui.set_theme(CURSES and "term" or "moon", { @@ -25,3 +63,21 @@ ui.set_theme(CURSES and "term" or "moon", { fontsize = 14, }) ``` + +## Keybindings + +``` +Keys Action +cl+s opens snippets directory for modifying. +cR runs `moonc` on the current moonscript file. +cr runs `moon` on the current moonscript file. +cB builds the project with tup. +``` + +[1]: http://foicica.com/textadept/ +[2]: https://twitter.com/a_baez +[3]: https://twitter.com/moonscript +[4]: http://moonscript.org/ +[5]: http://gittup.org/tup/ +[6i]: https://img.shields.io/badge/license-MIT-green.svg +[6l]: ./LICENSE