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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
|
local api = vim.api
local fn = vim.fn
local luv = vim.loop
local utils = require'nvim-treesitter.utils'
local parsers = require'nvim-treesitter.parsers'
local info = require'nvim-treesitter.info'
local configs = require'nvim-treesitter.configs'
local M = {}
local lockfile = {}
M.compilers = { vim.fn.getenv('CC'), "cc", "gcc", "clang" }
local started_commands = 0
local finished_commands = 0
local failed_commands = 0
local function reset_progress_counter()
if started_commands ~= finished_commands then
return
end
started_commands = 0
finished_commands = 0
failed_commands = 0
end
local function get_job_status()
return "["..finished_commands.."/"..started_commands
..(failed_commands > 0 and ", failed: "..failed_commands or "").."]"
end
local function get_revision(lang)
if #lockfile == 0 then
lockfile = vim.fn.json_decode(vim.fn.readfile(utils.join_paths(utils.get_package_path(), 'lockfile.json')))
end
return (lockfile[lang] and lockfile[lang].revision)
end
local function select_rm_file_cmd(file, info_msg)
if fn.has('win32') == 1 then
return {
cmd = 'cmd',
opts = {
args = { '/C', 'if', 'exist', file, 'del', file },
},
info = info_msg,
err = "Could not delete "..file,
}
else
return {
cmd = 'rm',
opts = {
args = { file },
},
info = info_msg,
err = "Could not delete "..file,
}
end
end
function M.iter_cmd(cmd_list, i, lang, success_message)
if i == 1 then
started_commands = started_commands + 1
end
if i == #cmd_list + 1 then
finished_commands = finished_commands + 1
return print(get_job_status().." "..success_message)
end
local attr = cmd_list[i]
if attr.info then print(get_job_status().." "..attr.info) end
local handle
handle = luv.spawn(attr.cmd, attr.opts, vim.schedule_wrap(function(code)
handle:close()
if code ~= 0 then
failed_commands = failed_commands + 1
finished_commands = finished_commands + 1
return api.nvim_err_writeln(attr.err or ("Failed to execute the following command:\n"..vim.inspect(attr)))
end
M.iter_cmd(cmd_list, i + 1, lang, success_message)
end))
end
local function get_command(cmd)
local ret = ''
if cmd.opts and cmd.opts.cwd then
ret = string.format('cd %s;\n', cmd.opts.cwd)
end
ret = string.format('%s%s ', ret, cmd.cmd)
local options = ""
if cmd.opts and cmd.opts.args then
for _, opt in ipairs(cmd.opts.args) do
options = string.format("%s %s", options, opt)
end
end
return string.format('%s%s', ret, options)
end
local function iter_cmd_sync(cmd_list)
for _, cmd in ipairs(cmd_list) do
if cmd.info then
print(cmd.info)
end
local ret = vim.fn.system(get_command(cmd))
if vim.v.shell_error ~= 0 then
print(ret)
api.nvim_err_writeln((cmd.err and cmd.err..'\n' or '')
.."Failed to execute the following command:\n"
..vim.inspect(cmd))
return false
end
end
return true
end
local function select_executable(executables)
return vim.tbl_filter(function(c) return fn.executable(c) == 1 end, executables)[1]
end
local function select_args(repo)
local args = {
'-o',
'parser.so',
'-I./src',
repo.files,
'-shared',
'-Os',
'-lstdc++',
}
if fn.has('win32') == 0 then
table.insert(args, '-fPIC')
end
return args
end
local function select_install_rm_cmd(cache_folder, project_name)
if fn.has('win32') == 1 then
local dir = cache_folder ..'\\'.. project_name
return {
cmd = 'cmd',
opts = {
args = { '/C', 'if', 'exist', dir, 'rmdir', '/s', '/q', dir },
}
}
else
return {
cmd = 'rm',
opts = {
args = { '-rf', cache_folder..'/'..project_name },
}
}
end
end
local function select_mv_cmd(from, to, cwd)
if fn.has('win32') == 1 then
return {
cmd = 'cmd',
opts = {
args = { '/C', 'move', '/Y', from, to },
cwd = cwd,
}
}
else
return {
cmd = 'mv',
opts = {
args = { from, to },
cwd = cwd,
},
}
end
end
local function select_download_commands(repo, project_name, cache_folder, revision)
if vim.fn.executable('unzip') == 1 and vim.fn.executable('curl') == 1 and repo.url:find("github.com", 1, true) then
revision = revision or repo.branch or "master"
local path_sep = utils.get_path_sep()
return {
select_install_rm_cmd(cache_folder, project_name..'-tmp'),
{
cmd = 'curl',
info = 'Downloading...',
err = 'Error during download, please verify your internet connection',
opts = {
args = {
'-L', -- follow redirects
repo.url.."/archive/"..revision..".zip",
'--output',
project_name..".zip"
},
cwd = cache_folder,
},
},
{
cmd = 'unzip',
info = 'Extracting...',
err = 'Error during unzipping',
opts = {
args = {
'-o',
project_name..".zip",
'-d',
project_name..'-tmp',
},
cwd = cache_folder,
},
},
select_rm_file_cmd(cache_folder..path_sep..project_name..".zip"),
select_mv_cmd(utils.join_paths(project_name..'-tmp', repo.url:match('[^/]-$')..'-'..revision),
project_name,
cache_folder),
select_install_rm_cmd(cache_folder, project_name..'-tmp')
}
else
return {
cmd = 'git',
info = 'Downloading...',
err = 'Error during download, please verify your internet connection',
opts = {
args = {
'clone',
'--single-branch',
'--branch', repo.branch or 'master',
'--depth', '1',
repo.url,
project_name
},
cwd = cache_folder,
},
}
end
end
local function run_install(cache_folder, install_folder, lang, repo, with_sync)
parsers.reset_cache()
local path_sep = utils.get_path_sep()
local project_name = 'tree-sitter-'..lang
-- compile_location only needed for typescript installs.
local compile_location = cache_folder..path_sep..(repo.location or project_name)
local parser_lib_name = install_folder..path_sep..lang..".so"
local cc = select_executable(M.compilers)
if not cc then
api.nvim_err_writeln('No C compiler found! "'
..table.concat(vim.tbl_filter(function(c) return type(c) == 'string' end, M.compilers), '", "')
..'" are not executable.')
return
end
local revision = configs.get_update_strategy() == 'lockfile' and get_revision(lang)
local command_list = {}
vim.list_extend(command_list, { select_install_rm_cmd(cache_folder, project_name) })
vim.list_extend(command_list, select_download_commands(repo, project_name, cache_folder, revision))
vim.list_extend(command_list, {
{
cmd = cc,
info = 'Compiling...',
err = 'Error during compilation',
opts = {
args = vim.tbl_flatten(select_args(repo)),
cwd = compile_location
}
},
select_mv_cmd('parser.so', parser_lib_name, compile_location),
select_install_rm_cmd(cache_folder, project_name)
})
if with_sync then
if iter_cmd_sync(command_list) == true then
print('Treesitter parser for '..lang..' has been installed')
end
else
M.iter_cmd(command_list, 1, lang, 'Treesitter parser for '..lang..' has been installed')
end
end
local function install_lang(lang, ask_reinstall, cache_folder, install_folder, with_sync)
if #api.nvim_get_runtime_file('parser/'..lang..'.so', false) > 0 then
if ask_reinstall ~= 'force' then
if not ask_reinstall then return end
local yesno = fn.input(lang .. ' parser already available: would you like to reinstall ? y/n: ')
print('\n ') -- mandatory to avoid messing up command line
if not string.match(yesno, '^y.*') then return end
end
end
local parser_config = parsers.get_parser_configs()[lang]
if not parser_config then
return api.nvim_err_writeln('Parser not available for language '..lang)
end
local install_info = parser_config.install_info
vim.validate {
url={ install_info.url, 'string' },
files={ install_info.files, 'table' }
}
run_install(cache_folder, install_folder, lang, install_info, with_sync)
end
local function install(with_sync, ask_reinstall)
return function (...)
if fn.executable('git') == 0 then
return api.nvim_err_writeln('Git is required on your system to run this command')
end
local cache_folder, err = utils.get_cache_dir()
if err then return api.nvim_err_writeln(err) end
local install_folder, err = utils.get_parser_install_dir()
if err then return api.nvim_err_writeln(err) end
local languages
local ask
if ... == 'all' then
languages = parsers.available_parsers()
ask = false
else
languages = vim.tbl_flatten({...})
ask = ask_reinstall
end
if #languages > 1 then
reset_progress_counter()
end
for _, lang in ipairs(languages) do
install_lang(lang, ask, cache_folder, install_folder, with_sync)
end
end
end
function M.update(lang)
reset_progress_counter()
if lang then
install(false, 'force')(lang)
else
local installed = info.installed_parsers()
for _, lang in pairs(installed) do
install(false, 'force')(lang)
end
end
end
function M.uninstall(lang)
local path_sep = '/'
if fn.has('win32') == 1 then
path_sep = '\\'
end
if lang == 'all' then
reset_progress_counter()
local installed = info.installed_parsers()
for _, lang in pairs(installed) do
M.uninstall(lang)
end
elseif lang then
local install_dir, err = utils.get_parser_install_dir()
if err then return api.nvim_err_writeln(err) end
local parser_lib = install_dir..path_sep..lang..".so"
local command_list = {
select_rm_file_cmd(parser_lib, "Uninstalling parser for "..lang)
}
M.iter_cmd(command_list, 1, lang, 'Treesitter parser for '..lang..' has been uninstalled')
end
end
function M.write_lockfile(verbose)
local sorted_parsers = {}
for k, v in pairs(parsers.get_parser_configs()) do
table.insert(sorted_parsers, {name = k, parser = v})
end
table.sort(sorted_parsers, function(a, b) return a.name < b.name end)
for _, v in ipairs(sorted_parsers) do
-- I'm sure this can be done in aync way with iter_cmd
local sha = vim.split(vim.fn.systemlist('git ls-remote '..v.parser.install_info.url)[1], '\t')[1]
lockfile[v.name] = { revision = sha }
if verbose then
print(v.name..': '..sha)
end
end
if verbose then
print(vim.inspect(lockfile))
end
vim.fn.writefile(vim.fn.split(vim.fn.json_encode(lockfile), '\n'),
utils.join_paths(utils.get_package_path(), "lockfile.json"))
end
M.ensure_installed = install(false, false)
M.commands = {
TSInstall = {
run = install(false, true),
args = {
"-nargs=+",
"-complete=custom,nvim_treesitter#installable_parsers",
},
},
TSInstallSync = {
run = install(true, true),
args = {
"-nargs=+",
"-complete=custom,nvim_treesitter#installable_parsers",
},
},
TSUpdate = {
run = M.update,
args = {
"-nargs=*",
"-complete=custom,nvim_treesitter#installed_parsers",
},
},
TSUninstall = {
run = M.uninstall,
args = {
"-nargs=+",
"-complete=custom,nvim_treesitter#installed_parsers",
},
},
}
return M
|