159 lines
4.8 KiB
Lua
159 lines
4.8 KiB
Lua
local ok_conform, conform = pcall(require, 'conform')
|
|
if ok_conform then
|
|
conform.formatters = conform.formatters or {}
|
|
conform.formatters_by_ft = conform.formatters_by_ft or {}
|
|
|
|
conform.formatters_by_ft.julia = { 'juliaformatter' }
|
|
|
|
vim.api.nvim_create_autocmd('BufWritePre', {
|
|
buffer = 0,
|
|
callback = function()
|
|
if vim.api.nvim_buf_line_count(0) > 2000 then
|
|
return
|
|
end
|
|
conform.format({ timeout_ms = 2000, lsp_fallback = false })
|
|
end,
|
|
})
|
|
end
|
|
|
|
local ok_toggleterm, toggleterm_terminal = pcall(require, 'toggleterm.terminal')
|
|
local Terminal = ok_toggleterm and toggleterm_terminal.Terminal or nil
|
|
|
|
if not vim.g.julia_repl_setup_done and Terminal then
|
|
vim.g.julia_repl_setup_done = true
|
|
|
|
local function get_startup_commands()
|
|
return [[
|
|
ENV["JULIA_NOVERSIONS"] = "yes"
|
|
try
|
|
using Revise
|
|
catch
|
|
end
|
|
println("\033[2J\033[H")
|
|
]]
|
|
end
|
|
|
|
local function new_julia_terminal()
|
|
return Terminal:new({
|
|
cmd = 'julia --banner=no',
|
|
direction = 'vertical',
|
|
size = 80,
|
|
hidden = true,
|
|
on_open = function(term)
|
|
vim.defer_fn(function()
|
|
term:send(get_startup_commands())
|
|
end, 100)
|
|
|
|
vim.api.nvim_buf_set_keymap(term.bufnr, 'n', 'q', '<cmd>close<CR>', { noremap = true, silent = true })
|
|
vim.api.nvim_buf_set_keymap(term.bufnr, 't', '<Esc>', '<C-\\><C-n>', { noremap = true, silent = true })
|
|
|
|
local win = term.window or vim.api.nvim_get_current_win()
|
|
vim.api.nvim_set_option_value('signcolumn', 'no', { win = win })
|
|
vim.api.nvim_set_option_value('number', false, { win = win })
|
|
vim.api.nvim_set_option_value('relativenumber', false, { win = win })
|
|
end,
|
|
on_exit = function(_)
|
|
vim.cmd('stopinsert')
|
|
end,
|
|
})
|
|
end
|
|
|
|
vim.__julia_repl_terminal = new_julia_terminal()
|
|
|
|
vim.api.nvim_create_user_command('JuliaREPL', function()
|
|
local julia = vim.__julia_repl_terminal
|
|
if not julia then
|
|
vim.__julia_repl_terminal = new_julia_terminal()
|
|
julia = vim.__julia_repl_terminal
|
|
end
|
|
|
|
if julia:is_open() then
|
|
julia:close()
|
|
else
|
|
julia:open()
|
|
vim.cmd('wincmd p')
|
|
end
|
|
end, {})
|
|
|
|
vim.keymap.set('n', '<leader>Rr', '<cmd>JuliaREPL<CR>', { noremap = true, silent = true, desc = 'Run/REPL: Toggle' })
|
|
end
|
|
|
|
local function send_to_julia(code)
|
|
local julia_terminal = vim.__julia_repl_terminal
|
|
if not julia_terminal then
|
|
return
|
|
end
|
|
|
|
if not julia_terminal:is_open() then
|
|
julia_terminal:open()
|
|
vim.defer_fn(function()
|
|
julia_terminal:send(code)
|
|
vim.cmd('wincmd p')
|
|
end, 200)
|
|
else
|
|
julia_terminal:send(code)
|
|
vim.cmd('wincmd p')
|
|
end
|
|
end
|
|
|
|
local function get_cell_content()
|
|
local cur_line = vim.api.nvim_win_get_cursor(0)[1]
|
|
local lines = vim.api.nvim_buf_get_lines(0, 0, -1, false)
|
|
local start_line, end_line = cur_line, cur_line
|
|
|
|
while start_line > 1 and not lines[start_line - 1]:match('^#%%') and not lines[start_line - 1]:match('^##') do
|
|
start_line = start_line - 1
|
|
end
|
|
|
|
while end_line < #lines and not lines[end_line + 1]:match('^#%%') and not lines[end_line + 1]:match('^##') do
|
|
end_line = end_line + 1
|
|
end
|
|
|
|
return table.concat(vim.api.nvim_buf_get_lines(0, start_line - 1, end_line, false), '\n')
|
|
end
|
|
|
|
local opts = { buffer = true, silent = true }
|
|
|
|
vim.keymap.set('n', '<leader>Rf', function()
|
|
send_to_julia('using Revise\nincludet("' .. vim.fn.expand('%:p') .. '")\n')
|
|
end, vim.tbl_extend('force', opts, { desc = 'Run/REPL: Run file' }))
|
|
|
|
vim.keymap.set('n', '<leader>Rl', function()
|
|
send_to_julia(vim.api.nvim_get_current_line() .. '\n')
|
|
end, vim.tbl_extend('force', opts, { desc = 'Run/REPL: Send line' }))
|
|
|
|
vim.keymap.set('n', '<leader>Rc', function()
|
|
send_to_julia(get_cell_content() .. '\n')
|
|
end, vim.tbl_extend('force', opts, { desc = 'Run/REPL: Send cell' }))
|
|
|
|
vim.keymap.set('v', '<leader>Rs', function()
|
|
local start_pos = vim.fn.getpos("'<")[2]
|
|
local end_pos = vim.fn.getpos("'>")[2]
|
|
local code = table.concat(vim.api.nvim_buf_get_lines(0, start_pos - 1, end_pos, false), '\n')
|
|
send_to_julia(code .. '\n')
|
|
end, vim.tbl_extend('force', opts, { desc = 'Run/REPL: Send selection' }))
|
|
|
|
vim.keymap.set('n', '<leader>Ri', function()
|
|
send_to_julia('\x03')
|
|
end, vim.tbl_extend('force', opts, { desc = 'Run/REPL: Interrupt' }))
|
|
|
|
vim.keymap.set('n', '<leader>jb', function()
|
|
send_to_julia('using BenchmarkTools\n@btime ' .. vim.api.nvim_get_current_line() .. '\n')
|
|
end, vim.tbl_extend('force', opts, { desc = 'Julia: Benchmark line' }))
|
|
|
|
vim.keymap.set('n', '<leader>Rx', function()
|
|
local julia = vim.__julia_repl_terminal
|
|
if julia then
|
|
pcall(function()
|
|
julia:shutdown()
|
|
end)
|
|
end
|
|
vim.__julia_repl_terminal = Terminal:new({
|
|
cmd = 'julia --banner=no',
|
|
direction = 'vertical',
|
|
size = 80,
|
|
hidden = true,
|
|
})
|
|
end, vim.tbl_extend('force', opts, { desc = 'Run/REPL: Restart' }))
|
|
|
|
vim.g.julia_ftplugin_migrated = true
|