-- Mode for -*- lua -*- files
local editor = polywell
local lume = require("source/libs/polywell.lume")

local keywords = {
	"[-][-]",

	"(keywords)"---------

		, "break"
		, "do"
		, "else"
		, "elseif"
		, "end"
		, "false"
		, "for"
    	, "function"
    	, "if"
    	, "in"
    	, "local"
     	, "repeat"
    	, "return"
    	, "then"
    	, "true"
    	, "until"
    	, "while"
    	, "="
    	, "+"
    	, "-"
    	, "<"
    	, ">"
    	, "=="
    	, "and"
    	, "or"
    	, "not"
    	, "nil"

    ,"(operators)"---------

    	--, "("
    	--, ")"
    	--, "["
    	--, "]"
    	--, "{"
    	--, "}"

    ,"(special)"---------

    	, "  spcl    "
    	, "game"
    	, "editor"
    	, "controls"
    	, "level"
    	, "entity"
    	, "public"
    	, "self"
    	, "room"
    	, "lume"
    	, "cam"
    	, "effects"
    	, "target"

    ,"(functions)"---------

    	, "keyPressed"
    	, "deepcopy"
		, "trace"
		, "log"
		, "easeTo"
		, "tween"
		, "flashCard"
		, "wobble"
		, "tempTip"

    ,"(methods)"---------

    	, "loadSet"
    	, "findSet"
    	, "setContains"
    	, "loadLevel"
		, "changeRooms"
		, "addRoom"
		, "addRooms"
		, "duplicateRoom"
		, "removeRoom"
		, "removeRooms"
		, "getRooms"
		, "getRoom"
		, "getWorld"
		, "reloadSet"
		, "pause"
		, "unPause"

	    , "load"
	    , "loop"
		, "setPosition"
		, "setOrigin"
		, "setBase"
		, "move"
		, "setVelocity"
		, "accelerate"
		, "changeVelocity"
		, "setMaxVelocity"
		, "setDamping"
		, "damp"
		, "setRotation"
		, "setAnim"
		, "transfer"
		, "setLayerVisible"
 		,"showLayer"
 		,"hideLayer"
 		,"setLayerSolo"
 		,"setLayerByAngle"
 		,"setPlaybackSpeed"
 		,"setFrame"
 		,"setPlaying"
 		,"play"
 		,"stop"


	,"(lua)"---------

		, "math"

		, "x"
	    , "y"
	    , "z"

		, ".round"
		, ".zero"
		, ".angle"
		, ".dist"
		, "sign"
		, "mf"
		, "mc"

		, "clamp"
		, "round"
		, "sign"
		, "lerp"
		, "smooth"
		, "pingpong"
		, "distance"
		, "angle"
		, "vector"
		, "random"
		, "randomchoice"
		, "weightedchoice"
		, "isarray"
		, "push"
		, "remove"
		, "clear"
		, "extend"
		, "shuffle"
		, "sort"
		, "array"
		, "each"
		, "map"
		, "all"
		, "any"
		, "reduce"
		, "set"
		, "filter"
		, "reject"
		, "merge"
		, "concat"
		, "find"
		, "match"
		, "count"
		, "slice"
		, "first"
		, "last"
		, "invert"
		, "pick"
		, "keys"
		, "clone"
		, "fn"
		, "once"
		, "memoize"
		, "combine"
		, "call"
		, "time"
		, "lambda"
		, "serialize"
		, "deserialize"
		, "split"
		, "trim"
		, "wordwrap"
		, "format"
		, "trace"
		, "dostring"
		, "uuid"
		, "hotswap"
		, "ripairs"
		, "color"
		, "rgba"
		, "chain"

		, "abs"
		, "acos"
		, "asin"
		, "atan"
		, "atan2"
		, "ceil"
		, "cos"
		, "cosh"
		, "deg"
		, "exp"
		, "floor"
		, "fmod"
		, "frexp"
		, "huge"
		, "ldexp"
		, "log"
		, "max"
		, "min"
		, "modf"
		, "pi"
		, "pow"
		, "rad"
		, "random"
		, "randomseed"
		, "sin"
		, "sinh"
		, "sqrt"
		, "tan"
		, "tanh"
		, "os"
		, "clock"
		, "date"
		, "difftime"
		, "execute"
		, "exit"
		, "getenv"
		, "remove"
		, "rename"
		, "setlocale"
		, "time"
		, "tmpname"
		, "string"
		, "byte"
		, "char"
		, "dump"
		, "find"
		, "format"
		, "gmatch"
		, "gsub"
		, "len"
		, "lower"
		, "match"
		, "rep"
		, "reverse"
		, "sub"
		, "upper"
		, "table"
		, "concat"
		, "insert"
		, "pack"
		, "remove"
		, "sort"
		, "unpack"
		, "ipairs"
		, "tostring"
}

keywords.names = {
	"keyword",
	"operators",
	"special",
	"functions",
	"methods",
	"lua"
}

for i,key in ipairs(keywords) do 
	--print(key)
	for j,name in ipairs(keywords.names) do
		if key:sub(2,-2) == name then
			keywords[name] = i
			--print(name)
			--table.remove(keywords,i)
		end
	end
end



editor.define_mode("lua", "edit",
                   {on_change = lume.fn(editor.colorize, keywords),
                    activate  = lume.fn(editor.colorize, keywords)})

-- change colors:           red green blue
editor.set_color("lua", {text 			= {210/255, 210/255 ,210/255},
                         keyword 		= {200/255, 100/255, 120/255},
                         operators 	= {255/255, 200/255, 200/255},
                         special 		= {216/255, 180/255, 215/255},
                         functions 	= {128/255, 191/255, 170/255},
                         methods 		= {235/255, 165/255, 190/255},
                         lua 	 			= {120/255, 205/255, 217/255},
                         str 			=		{160/255, 180/255, 130/255},
                         number 	=		{208/255, 135/255, 112/255},
                         comment 	=		{180/255, 170/255, 90/255}})

editor.add_auto_mode(".*lua", "lua")

editor.bind("lua", "ctrl-alt-r", editor.reload)
editor.bind("lua", "tab", editor.complete)
editor.bind("lua", "alt-/", editor.complete)
