forked from Hackerpilot/textadept-d
-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.lua
383 lines (348 loc) · 11.6 KB
/
init.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
local M = {}
local cstyle = require "dmd.cstyle"
local icons = require "dmd.icons"
local dsnippets = require "dmd.snippets"
local snippets = _G.snippets
local keys = _G.keys
-- Used for dscanner/ctags symbol finder
local lineDict = {}
-- Used for DCD call tips
local calltips = {}
local currentCalltip = 1
M.PATH_TO_DCD_SERVER = "dcd-server"
M.PATH_TO_DCD_CLIENT = "dcd-client"
M.PATH_TO_DSCANNER = "dscanner"
M.PATH_TO_DFMT = "dfmt"
textadept.editing.comment_string.dmd = '//'
textadept.run.compile_commands.dmd = 'dmd -c -o- %(filename)'
textadept.run.error_patterns.dmd = {
pattern = '^(.-)%((%d+)%): (.+)$',
filename = 1, line = 2, message = 3
}
M.kindToIconMapping = {}
M.kindToIconMapping['c'] = _SCINTILLA.next_image_type()
M.kindToIconMapping['e'] = _SCINTILLA.next_image_type()
M.kindToIconMapping['f'] = _SCINTILLA.next_image_type()
M.kindToIconMapping['i'] = _SCINTILLA.next_image_type()
M.kindToIconMapping['k'] = _SCINTILLA.next_image_type()
M.kindToIconMapping['l'] = _SCINTILLA.next_image_type()
M.kindToIconMapping['M'] = _SCINTILLA.next_image_type()
M.kindToIconMapping['P'] = _SCINTILLA.next_image_type()
M.kindToIconMapping['s'] = _SCINTILLA.next_image_type()
M.kindToIconMapping['t'] = _SCINTILLA.next_image_type()
M.kindToIconMapping['u'] = _SCINTILLA.next_image_type()
M.kindToIconMapping['v'] = _SCINTILLA.next_image_type()
M.kindToIconMapping['m'] = M.kindToIconMapping['v']
M.kindToIconMapping['g'] = M.kindToIconMapping['e']
M.kindToIconMapping['T'] = M.kindToIconMapping['t']
local function registerImages()
buffer:register_image(M.kindToIconMapping['c'], icons.CLASS)
buffer:register_image(M.kindToIconMapping['e'], icons.ENUM)
buffer:register_image(M.kindToIconMapping['f'], icons.FUNCTION)
buffer:register_image(M.kindToIconMapping['i'], icons.INTERFACE)
buffer:register_image(M.kindToIconMapping['k'], icons.KEYWORD)
buffer:register_image(M.kindToIconMapping['l'], icons.ALIAS)
buffer:register_image(M.kindToIconMapping['M'], icons.MODULE)
buffer:register_image(M.kindToIconMapping['P'], icons.PACKAGE)
buffer:register_image(M.kindToIconMapping['s'], icons.STRUCT)
buffer:register_image(M.kindToIconMapping['t'], icons.TEMPLATE)
buffer:register_image(M.kindToIconMapping['u'], icons.UNION)
buffer:register_image(M.kindToIconMapping['v'], icons.FIELD)
end
local function showCompletionList(r)
buffer.auto_c_choose_single = false;
buffer.auto_c_max_width = 0
local completions = {}
for symbol, kind in r:gmatch("([^%s]+)\t(%a)\r?\n") do
completions[#completions + 1] = symbol .. "?" .. M.kindToIconMapping[kind]
end
table.sort(completions, function(a, b) return string.upper(a) < string.upper(b) end)
local charactersEntered = buffer.current_pos - buffer:word_start_position(buffer.current_pos)
local prevChar = buffer.char_at[buffer.current_pos - 1]
if prevChar == string.byte('.')
or prevChar == string.byte(':')
or prevChar == string.byte(' ')
or prevChar == string.byte('\t')
or prevChar == string.byte('(')
or prevChar == string.byte('[') then
charactersEntered = 0
end
if not buffer:auto_c_active() then registerImages() end
local setting = buffer.auto_c_choose_single
buffer:auto_c_show(charactersEntered, table.concat(completions, " "))
buffer.auto_c_choose_single = setting
end
local function showCurrentCallTip()
local tip = calltips[currentCalltip]
buffer:call_tip_show(buffer:word_start_position(buffer.current_pos),
string.format("%d of %d\1\2\n%s", currentCalltip, #calltips,
calltips[currentCalltip]:gsub("(%f[\\])\\n", "%1\n")
:gsub("\\\\n", "\\n")))
end
local function showCalltips(calltip)
currentCalltip = 1
calltips = {}
for tip in calltip:gmatch("(.-)\r?\n") do
if tip ~= "calltips" then
table.insert(calltips, tip)
end
end
if (#calltips > 0) then
showCurrentCallTip()
end
end
local function cycleCalltips(delta)
if not buffer:call_tip_active() then
return false
end
if delta > 0 then
currentCalltip = math.max(math.min(#calltips, currentCalltip + 1), 1)
else
currentCalltip = math.min(math.max(1, currentCalltip - 1), #calltips)
end
showCurrentCallTip()
end
local function runDCDClient(args)
local command = M.PATH_TO_DCD_CLIENT .. " " .. args .. " -c" .. buffer.current_pos
local p = spawn(command)
p:write(buffer:get_text():sub(1, buffer.length))
p:close()
return p:read("*a") or ""
end
local function showDoc()
local r = runDCDClient("-d")
if r ~= "\n" then
showCalltips(r)
end
end
M.gotoStack = {}
function M.goBack()
if #M.gotoStack == 0 then return end
local top = M.gotoStack[#M.gotoStack]
if top.file ~= nil then
ui.goto_file(top.file, false, _VIEWS[top.view])
else
if top.buffer > _BUFFERS then
table.remove(M.gotoStack)
return
end
view:goto_buffer(top.buffer)
end
buffer:goto_line(top.line)
buffer:vertical_centre_caret()
table.remove(M.gotoStack) -- pop last item
end
function M.gotoDeclaration()
local r = runDCDClient("-l")
if r ~= "Not found\n" then
path, position = r:match("^(.-)\t(%d+)")
if (path ~= nil and position ~= nil) then
table.insert(M.gotoStack, {
line = buffer:line_from_position(buffer.current_pos),
file = buffer.filename,
buffer = _BUFFERS[_G.buffer],
view = _VIEWS[_G.view]
})
if (path ~= "stdin") then
ui.goto_file(path, false, _G.view)
end
buffer:goto_pos(tonumber(position))
buffer:vertical_centre_caret()
buffer:word_right_end_extend()
end
end
end
local function expandContext(meta)
local patterns = {"struct:(%w+)", "class:([%w_]+)", "template:([%w_]+)",
"interface:([%w_]+)", "union:([%w_]+)", "function:([%w_]+)"}
if meta == nil or meta == "" then return "" end
for item in meta:gmatch("%w+:[%w%d_]+") do
for _, pattern in ipairs(patterns) do
local result = item:match(pattern)
if result ~= nil then return result end
end
end
return ""
end
-- Expands ctags type abbreviations to full words
local function expandCtagsType(tagType)
if tagType == "g" then return "enum"
elseif tagType == "e" then return ""
elseif tagType == "v" then return "variable"
elseif tagType == "i" then return "interface"
elseif tagType == "c" then return "class"
elseif tagType == "s" then return "struct"
elseif tagType == "f" then return "function"
elseif tagType == "u" then return "union"
elseif tagType == "T" then return "template"
else return "" end
end
local function onSymbolListSelection(list, item)
list:close()
buffer:goto_line(item[4] - 1)
buffer:vertical_centre_caret()
end
-- Uses dscanner's --ctags option to create a symbol index for the contents of
-- the buffer. Automatically uses Textadept's normal dialogs or textredux lists.
local function symbolIndex()
local fileName = os.tmpname()
local mode = "w"
if _G.WIN32 then mode = "wb" end
local tmpFile = io.open(fileName, mode)
tmpFile:write(buffer:get_text():sub(1, buffer.length))
tmpFile:flush()
tmpFile:close()
local command = M.PATH_TO_DSCANNER .. " --ctags " .. fileName
local p = spawn(command)
local r = p:read("*a")
os.remove(fileName)
local symbolList = {}
local i = 0
for line in r:gmatch("(.-)\r?\n") do
if not line:match("^!") then
local name, file, lineNumber, tagType, meta = line:match(
"([~%w_]+)\t([%w/\\._ ]+)\t(%d+);\"\t(%w)\t?(.*)")
if package.loaded['textredux'] then
table.insert(symbolList, {name, expandCtagsType(tagType), expandContext(meta), lineNumber})
else
table.insert(symbolList, name)
table.insert(symbolList, expandCtagsType(tagType))
table.insert(symbolList, expandContext(meta))
table.insert(symbolList, lineNumber)
end
lineDict[i + 1] = tonumber(lineNumber - 1)
i = i + 1
end
end
local headers = {"Name", "Kind", "Context", "Line"}
if package.loaded['textredux'] then
local reduxlist = require 'textredux.core.list'
local reduxstyle = require 'textredux.core.style'
local list = reduxlist.new('Go to symbol')
list.items = symbolList
list.on_selection = onSymbolListSelection
list.headers = headers
list.column_styles = { reduxstyle.variable, reduxstyle.keyword, reduxstyle.class, reduxstyle.number }
list:show()
else
local button, i = ui.dialogs.filteredlist{
title = "Go to symbol",
columns = headers,
items = symbolList
}
if i ~= nil then
buffer:goto_line(lineDict[i])
buffer:vertical_centre_caret()
end
end
end
local function autocomplete()
if not buffer:auto_c_active() then registerImages() end
local r = runDCDClient("")
if r ~= "\n" and r ~= "\r\n" then
if r:match("^identifiers.*") then
showCompletionList(r)
else
showCalltips(r)
end
end
if not buffer:auto_c_active() then
textadept.editing.autocomplete("word")
end
end
-- Autocomplete handler. Launches DCD on '(', '.', or ':' character insertion
events.connect(events.CHAR_ADDED, function(ch)
if buffer:get_lexer() ~= "dmd" or ch > 255 then return end
if string.char(ch) == '(' or string.char(ch) == '.' or string.char(ch) == ':' then
local setting = buffer.auto_c_choose_single
buffer.auto_c_choose_single = false
autocomplete(ch)
buffer.auto_c_choose_single = setting
end
end)
-- Run dscanner's static analysis after saves and print the warnings and errors
-- reported to the buffer as annotations
events.connect(events.FILE_AFTER_SAVE, function()
if buffer:get_lexer() ~= "dmd" then return end
buffer:annotation_clear_all()
local command = M.PATH_TO_DSCANNER .. " --styleCheck " .. buffer.filename
local p = spawn(command)
local result = p:read("*a") or ""
for line in result:gmatch("(.-)\r?\n") do
lineNumber, column, level, message = string.match(line, "^.-%((%d+):(%d+)%)%[(%w+)%]: (.+)$")
if lineNumber == nil then return end
local l = tonumber(lineNumber) - 1
if l >= 0 then
local c = tonumber(column)
if level == "error" then
buffer.annotation_style[l] = 8
elseif buffer.annotation_style[l] ~= 8 then
buffer.annotation_style[l] = 2
end
local t = buffer.annotation_text[l]
if #t > 0 then
buffer.annotation_text[l] = buffer.annotation_text[l] .. "\n" .. message
else
buffer.annotation_text[l] = message
end
end
end
end)
-- Handler for clicks on the up and down arrow on function call tips
events.connect(events.CALL_TIP_CLICK, function(arrow)
if buffer:get_lexer() ~= "dmd" then return end
if arrow == 1 then
cycleCalltips(-1)
elseif arrow == 2 then
cycleCalltips(1)
end
end)
if not _G.WIN32 then
-- Spawn the dcd-server
M.serverProcess = spawn(M.PATH_TO_DCD_SERVER, nil, function() end, function() end)
-- Set an event handler that shuts down the DCD server, but only if this
-- module successfully started it. Do nothing if somebody else owns the
-- server instance
events.connect(events.QUIT, function()
if (M.serverProcess:status() == "running") then
spawn(M.PATH_TO_DCD_CLIENT .. " --shutdown")
_G.timeout(1, function()
if (M.serverProcess:status() == "running") then
M.serverProcess:kill()
end
end)
else
print('Warning: something other than us killed the DCD server')
end
end)
end
-- Key bindings
keys.dmd = {
['a\n'] = cstyle.newline,
['s\n'] = cstyle.newline_semicolon,
['c;'] = cstyle.endline_semicolon,
['}'] = cstyle.match_brace_indent,
['c{'] = function() return cstyle.openBraceMagic(true) end,
['\n'] = cstyle.enter_key_pressed,
['c\n'] = autocomplete,
['cH'] = showDoc,
['down'] = function() return cycleCalltips(1) end,
['up'] = function() return cycleCalltips(-1) end,
['cG'] = M.gotoDeclaration,
['caG'] = M.goBack,
['cM'] = symbolIndex,
['f7'] = function()
if buffer.use_tabs then
textadept.editing.filter_through(M.PATH_TO_DFMT .. " --indent_style=tab")
else
textadept.editing.filter_through(M.PATH_TO_DFMT .. " --indent_style=space")
end
end
}
-- Snippets
if type(snippets) == 'table' then
snippets.dmd = dsnippets.snippets
end
function M.set_buffer_properties()
end
return M