Skip to content

Commit 1d8cfdb

Browse files
committed
Execute jsonnet on demand
1 parent 39568b5 commit 1d8cfdb

File tree

1 file changed

+82
-33
lines changed

1 file changed

+82
-33
lines changed

home-manager/modules/dev-jsonnet.nix

Lines changed: 82 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -9,39 +9,88 @@ in
99
enable = mkEnableOption "simonswine jsonnet development config";
1010
};
1111

12-
config = mkIf cfg.enable {
13-
home.packages = with pkgs; [
14-
jsonnet
15-
jsonnet-bundler
16-
tanka
17-
nodePackages.js-yaml
18-
];
19-
simonswine.neovim = {
20-
lspconfig.jsonnet_ls = {
21-
cmd = [
22-
"${pkgs.jsonnet-language-server}/bin/jsonnet-language-server"
23-
];
24-
filetypes = [
25-
"jsonnet"
26-
];
27-
};
28-
plugins = with pkgs.vimPlugins; [ vim-jsonnet ];
29-
extraConfig = ''
30-
" JSONNET
31-
au FileType jsonnet nmap <leader>b :call JsonnetEval()<cr>
32-
function! JsonnetEval()
33-
" check if the file is a tanka file or not
34-
let output = system("tk tool jpath " . shellescape(expand('%')))
35-
if v:shell_error
36-
let output = system("jsonnet " . shellescape(expand('%')))
12+
config = mkIf cfg.enable
13+
{
14+
home.packages = with pkgs; [
15+
jsonnet
16+
jsonnet-bundler
17+
tanka
18+
nodePackages.js-yaml
19+
];
20+
programs.nixvim.extraConfigLua = ''
21+
-- Function to evaluate jsonnet files
22+
local function jsonnet_eval()
23+
local current_file = vim.fn.expand('%')
24+
local escaped_file = vim.fn.shellescape(current_file)
25+
26+
-- Check if it's a tanka file
27+
local jpath_cmd = "tk tool jpath " .. escaped_file
28+
local jpath_output = vim.fn.system(jpath_cmd)
29+
local shell_error = vim.v.shell_error
30+
31+
local output
32+
if shell_error ~= 0 then
33+
-- Not a tanka file, use jsonnet
34+
output = vim.fn.system("jsonnet " .. escaped_file)
3735
else
38-
let output = system("tk eval " . shellescape(expand('%')))
39-
endif
40-
vnew
41-
setlocal nobuflisted buftype=nofile bufhidden=wipe noswapfile ft=json
42-
put! = output
43-
endfunction
44-
'';
36+
-- Is a tanka file, use tk eval
37+
output = vim.fn.system("tk eval " .. escaped_file)
38+
end
39+
40+
-- Create new vertical split
41+
vim.cmd('vnew')
42+
43+
-- Set buffer options
44+
vim.bo.buflisted = false
45+
vim.bo.buftype = 'nofile'
46+
vim.bo.bufhidden = 'wipe'
47+
vim.bo.swapfile = false
48+
vim.bo.filetype = 'json'
49+
50+
-- Insert output at the beginning of buffer
51+
vim.fn.append(0, vim.split(output, '\n'))
52+
53+
-- Remove the empty line at the end
54+
vim.cmd('$delete')
55+
end
56+
57+
-- Create autocommand for jsonnet filetype
58+
vim.api.nvim_create_autocmd("FileType", {
59+
pattern = "jsonnet",
60+
callback = function()
61+
vim.keymap.set('n', '<leader>b', jsonnet_eval, {
62+
buffer = true,
63+
desc = "Evaluate jsonnet file"
64+
})
65+
end,
66+
})'';
67+
68+
simonswine.neovim = {
69+
lspconfig.jsonnet_ls = {
70+
cmd = [
71+
"${pkgs.jsonnet-language-server}/bin/jsonnet-language-server"
72+
];
73+
filetypes = [
74+
"jsonnet"
75+
];
76+
};
77+
plugins = with pkgs.vimPlugins; [ vim-jsonnet ];
78+
extraConfig = ''
79+
" JSONNET
80+
au FileType jsonnet nmap <leader>b :call JsonnetEval()<cr>
81+
function! JsonnetEval()
82+
" check if the file is a tanka file or not
83+
let output = system("tk tool jpath " . shellescape(expand('%')))
84+
if v:shell_error
85+
let output = system("jsonnet " . shellescape(expand('%')))
86+
else
87+
let output = system("tk eval " . shellescape(expand('%')))
88+
endif
89+
vnew
90+
setlocal nobuflisted buftype=nofile bufhidden=wipe noswapfile ft=json
91+
put! = output
92+
endfunction
93+
'';
94+
};
4595
};
46-
};
4796
}

0 commit comments

Comments
 (0)