-
Notifications
You must be signed in to change notification settings - Fork 0
/
crystal.lua
84 lines (69 loc) · 1.84 KB
/
crystal.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
VERSION = "1.0.0"
if GetOption("crystalfmt") == nil then
AddOption("crystalfmt", true)
end
function onViewOpen(view)
-- Set tabsize by default for crystal files to 2
if CurView().Buf:FileType() == "crystal" then
SetLocalOption("tabsize", "2", view)
end
end
function onSave(view)
if CurView().Buf:FileType() == "crystal" then
if GetOption("crystalfmt") then
format()
end
end
end
function crystal(a)
local ft = CurView().Buf:FileType()
local file = CurView().Buf.Path
if a == "version" or a == "--version" or a == "-v" then
version(args)
elseif a == "format" then
format()
elseif a == "run" then
run()
elseif a == "build" then
build()
elseif a == "eval" then
eval()
end
end
function version(v)
JobSpawn("crystal", {v}, "", "", "crystal.out")
end
function format()
local ft = CurView().Buf:FileType()
local file = CurView().Buf.Path
CurView():Save(false)
JobSpawn("crystal", {"tool", "format", file}, "", "", "crystal.onExit")
end
function run()
local file = CurView().Buf.Path
JobSpawn("crystal", {"run", file}, "", "", "crystal.out")
end
function build()
local file = CurView().Buf.Path
JobSpawn("crystal", {"build", file}, "", "", "crystal.out")
end
function eval()
local ft = CurView().Buf:FileType()
local file = CurView().Buf.Path
local line = CurView().Buf:Line(CurView().Cursor.Y)
JobSpawn("crystal", {"eval", line}, "", "", "crystal.out")
end
function onExit()
CurView().Buf:ReOpen()
end
function out(output)
messenger:Message(output)
end
function string.contains(str, word)
return string.match(' '..str..' ', '%A'..word..'%A') ~= nil
end
function string.starts(str,start)
return string.sub(str, 1, string.len(start)) == start
end
MakeCommand("crystal", "crystal.crystal", 0)
AddRuntimeFile("crystal", "help", "help/crystal-plugin.md")