-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathnvim-jsonnet.lua
More file actions
99 lines (85 loc) · 3.19 KB
/
nvim-jsonnet.lua
File metadata and controls
99 lines (85 loc) · 3.19 KB
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
local utils = require('jsonnet.utils')
local stringtoboolean = { ['true'] = true, ['false'] = false }
local M = {}
local defaults = {
jsonnet_bin = os.getenv('JSONNET_BIN') or 'jsonnet',
jsonnet_args = { '-J', 'vendor', '-J', 'lib' },
jsonnet_string_bin = os.getenv('JSONNET_BIN') or 'jsonnet',
jsonnet_string_args = { '-S', '-J', 'vendor', '-J', 'lib' },
use_tanka_if_possible = stringtoboolean[os.getenv('NVIM_JSONNET_USE_TANKA') or 'true'],
load_lsp_config = false,
capabilities = vim.lsp.protocol.make_client_capabilities(),
-- default to false to not break existing installs
load_dap_config = false,
jsonnet_debugger_bin = 'jsonnet-debugger',
jsonnet_debugger_args = { '-s', '-d', '-J', 'vendor', '-J', 'lib' },
}
M.setup = function(options)
M.options = vim.tbl_deep_extend('force', {}, defaults, options or {})
vim.api.nvim_create_user_command(
'JsonnetPrintConfig',
function()
print(vim.inspect(M.options))
end, {})
vim.api.nvim_create_user_command(
'JsonnetEval',
function(opts)
local jsonnet_bin = M.options.jsonnet_bin
local jsonnet_args = M.options.jsonnet_args
if M.options.use_tanka_if_possible then
-- Use Tanka if `tk tool jpath` works.
local _ = vim.fn.system('tk tool jpath ' .. vim.fn.shellescape(vim.fn.expand('%')))
if vim.api.nvim_get_vvar('shell_error') == 0 then
jsonnet_bin = 'tk'
jsonnet_args = { 'eval' }
end
end
utils.RunCommand(jsonnet_bin, jsonnet_args, 'json', opts)
end,
{ nargs = '?' })
vim.api.nvim_create_user_command(
'JsonnetEvalString',
function(opts)
utils.RunCommand(M.options.jsonnet_string_bin, M.options.jsonnet_string_args, '', opts)
end,
{ nargs = '?' })
vim.api.nvim_create_autocmd(
'FileType',
{
pattern = { 'jsonnet' },
callback = function()
vim.keymap.set('n', '<leader>j', '<cmd>JsonnetEval<cr>')
vim.keymap.set('n', '<leader>k', '<cmd>JsonnetEvalString<cr>')
vim.keymap.set('n', '<leader>l', '<esc>:<\',\'>!jsonnetfmt -<cr>')
vim.opt_local.foldlevelstart = 1
end,
})
local hasLspconfig, lspconfig = pcall(require, 'lspconfig')
if M.options.load_lsp_config and hasLspconfig then
lspconfig.jsonnet_ls.setup {
capabilities = M.options.capabilities,
settings = {
formatting = {
UseImplicitPlus = stringtoboolean[os.getenv('JSONNET_IMPLICIT_PLUS')] or false
}
}
}
end
local hasDap, dap = pcall(require, 'dap')
if M.options.load_dap_config and hasDap then
dap.adapters.jsonnet = {
type = 'executable',
command = M.options.jsonnet_debugger_bin,
args = M.options.jsonnet_debugger_args,
}
dap.configurations.jsonnet = {
{
type = 'jsonnet',
request = 'launch',
name = 'debug',
program = '${file}',
}
}
end
end
return M