-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpony.lua
132 lines (114 loc) · 2.31 KB
/
pony.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
VERSION = "0.2.1"
if GetOption("pony-mode") == nil then
AddOption("pony-mode", true)
end
local indent = {
"actor",
"be",
"class",
"do",
"else",
"elseif",
"for",
"fun",
"if",
"in",
"interface",
"new",
"object",
"primitive",
"recover",
"ref",
"repeat",
"struct",
"tag",
"then",
"trait",
"try",
"type",
"until",
"while",
"with",
"=>"
}
function preInsertNewline(v)
if not GetOption("pony-mode") or not (v.Buf:FileType() == "pony") then
return
end
local line = v.Buf:Line(v.Cursor.Y)
local ws = GetLeadingWhitespace(line)
local x = v.Cursor.X
if
(string.sub(line, x+1, x+1) == "}") and
(string.find(string.sub(line, x+1), "{") == nil)
then
v:InsertNewline(false)
v:CursorUp(false)
v:EndOfLine(false)
v:InsertNewline(false)
v:InsertTab(false)
return false
end
for _, key in pairs(indent) do
for word in string.gmatch(string.sub(line, 1, x), "%S+") do
if word == key then
v:InsertNewline(false)
if string.find(string.sub(line, 1, x+1), "end") == nil then
v:InsertTab(false)
end
return false
end
end
end
end
local unindent = {
"then",
"else",
"elseif",
"do",
"until",
"end"
}
--[[
function onRune(r, v)
checkOutdent(v)
end
function onBackspace(v)
checkOutdent(v)
end
-- lineN can change
-- TODO: use indentation level of last line with an indent word
local outdented = {}
function checkOutdent(v)
if not GetOption("pony-mode") or not (v.Buf:FileType() == "pony") then
return
end
local lineN = v.Cursor.Y
local line = v.Buf:Line(lineN)
local trimmed = line:match("(%w+)(.*)")
local count = 0
for _ in pairs(outdented) do count = count + 1 end
isod = tostring(outdented[lineN] == nil)
if trimmed == nil then
messenger:Message("nil " .. isod .. " len=" .. count)
else
messenger:Message(trimmed .. " " .. isod .. " len=" .. count)
end
for _, key in pairs(unindent) do
if trimmed == key then
if outdented[lineN] == nil then
outdented[lineN] = true
v:OutdentLine(false)
end
return
end
end
if outdented[lineN] ~= nil then
outdented[lineN] = nil
v:SelectToStartOfLine(false)
v:IndentSelection(false)
v:CursorRight(false)
v:CursorRight(false)
end
end
]]--