2024-08-15 03:41:28 +02:00
|
|
|
local state = require('codemp.state')
|
|
|
|
local utils = require('codemp.utils')
|
|
|
|
local buffers = require('codemp.buffers')
|
|
|
|
|
|
|
|
local prev_window = nil
|
|
|
|
local window_id = nil
|
|
|
|
local buffer_id = nil
|
|
|
|
local ns = vim.api.nvim_create_namespace("codemp-window")
|
|
|
|
|
2024-08-17 04:27:08 +02:00
|
|
|
vim.api.nvim_create_autocmd({"WinLeave"}, {
|
|
|
|
callback = function (ev)
|
|
|
|
if ev.id ~= window_id then
|
|
|
|
prev_window = vim.api.nvim_get_current_win()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
})
|
|
|
|
|
|
|
|
local row_to_buffer = {}
|
|
|
|
|
|
|
|
local function update_window()
|
|
|
|
if buffer_id == nil then error("cannot update window while codemp buffer is unset") end
|
|
|
|
row_to_buffer = {}
|
|
|
|
local buffer_to_row = {}
|
|
|
|
local off = {}
|
|
|
|
local tree = state.client:get_workspace(state.workspace).filetree
|
|
|
|
vim.api.nvim_set_option_value('modifiable', true, { buf = buffer_id })
|
|
|
|
utils.buffer.set_content(
|
|
|
|
buffer_id,
|
|
|
|
">| codemp\n |: " .. state.workspace .. "\n |\n |- "
|
|
|
|
.. vim.fn.join(tree, "\n |- ")
|
|
|
|
)
|
|
|
|
vim.highlight.range(buffer_id, ns, 'InlayHint', {0,0}, {0, 2})
|
|
|
|
vim.highlight.range(buffer_id, ns, 'Title', {0,3}, {0, 9})
|
|
|
|
vim.highlight.range(buffer_id, ns, 'InlayHint', {1,1}, {1, 3})
|
|
|
|
vim.highlight.range(buffer_id, ns, 'Directory', {1,4}, {1, 128})
|
|
|
|
vim.highlight.range(buffer_id, ns, 'InlayHint', {2,1}, {2, 3})
|
|
|
|
for n, name in ipairs(tree) do
|
|
|
|
buffer_to_row[name] = n+3
|
|
|
|
row_to_buffer[n+3] = name
|
|
|
|
vim.highlight.range(buffer_id, ns, 'InlayHint', {2+n,1}, {2+n, 3})
|
|
|
|
if buffers.map_rev[name] ~= nil then
|
|
|
|
vim.highlight.range(buffer_id, ns, 'Underlined', {2+n,4}, {2+n, 128})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
for user, buffer in pairs(buffers.users) do
|
|
|
|
local row = buffer_to_row[buffer]
|
|
|
|
if off[row] == nil then
|
|
|
|
off[row] = 0
|
|
|
|
end
|
|
|
|
vim.highlight.range(buffer_id, ns, utils.color(user), {row,4+off[row]}, {row, 5+off[row]})
|
|
|
|
off[row] = off[row] + 1
|
|
|
|
end
|
|
|
|
vim.api.nvim_set_option_value('modifiable', false, { buf = buffer_id })
|
|
|
|
end
|
|
|
|
|
2024-08-15 03:41:28 +02:00
|
|
|
local function open_buffer_under_cursor()
|
|
|
|
if window_id == nil then return end
|
|
|
|
if buffer_id == nil then return end
|
|
|
|
local cursor = vim.api.nvim_win_get_cursor(window_id)
|
2024-08-17 04:27:08 +02:00
|
|
|
local path = row_to_buffer[cursor[1]]
|
2024-08-15 03:41:28 +02:00
|
|
|
if prev_window ~= nil then
|
|
|
|
vim.api.nvim_set_current_win(prev_window)
|
|
|
|
end
|
|
|
|
if buffers.map_rev[path] ~= nil then
|
|
|
|
vim.api.nvim_set_current_buf(buffers.map_rev[path])
|
|
|
|
else
|
|
|
|
buffers.attach(path)
|
2024-08-17 04:27:08 +02:00
|
|
|
update_window()
|
2024-08-15 03:41:28 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local function init_window()
|
|
|
|
buffer_id = vim.api.nvim_create_buf(false, true)
|
|
|
|
vim.api.nvim_buf_set_name(buffer_id, "codemp::window")
|
|
|
|
vim.api.nvim_set_option_value('buftype', 'nofile', { buf = buffer_id })
|
|
|
|
utils.buffer.set_content(buffer_id, "> codemp")
|
|
|
|
vim.api.nvim_set_option_value('modifiable', false, { buf = buffer_id })
|
|
|
|
vim.highlight.range(buffer_id, ns, 'InlayHint', {0,0}, {0, 1})
|
|
|
|
vim.highlight.range(buffer_id, ns, 'Title', {0,3}, {0, 9})
|
|
|
|
vim.keymap.set('n', '<CR>', function () open_buffer_under_cursor() end, { buffer = buffer_id })
|
|
|
|
vim.keymap.set('n', 'a', function () buffers.create(vim.fn.input("path > ", "")) end, { buffer = buffer_id })
|
2024-08-17 04:27:08 +02:00
|
|
|
vim.api.nvim_create_autocmd({"WinClosed"}, {
|
|
|
|
callback = function (ev)
|
|
|
|
if tonumber(ev.match) == window_id then
|
|
|
|
window_id = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
})
|
2024-08-15 03:41:28 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
local function open_window()
|
|
|
|
window_id = vim.api.nvim_open_win(buffer_id, true, {
|
|
|
|
win = 0,
|
|
|
|
split = 'left',
|
|
|
|
width = 20,
|
|
|
|
})
|
|
|
|
vim.api.nvim_set_option_value('relativenumber', false, {})
|
|
|
|
vim.api.nvim_set_option_value('number', false, {})
|
|
|
|
vim.api.nvim_set_option_value('cursorlineopt', 'line', {})
|
|
|
|
end
|
|
|
|
|
|
|
|
local function toggle_window()
|
|
|
|
if window_id ~= nil then
|
|
|
|
vim.api.nvim_win_close(window_id, true)
|
|
|
|
window_id = nil
|
|
|
|
else
|
|
|
|
prev_window = vim.api.nvim_get_current_win()
|
|
|
|
open_window()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
init_window()
|
|
|
|
|
|
|
|
return {
|
|
|
|
init = init_window,
|
|
|
|
open = open_window,
|
|
|
|
update = update_window,
|
|
|
|
toggle = toggle_window,
|
|
|
|
buffer = buffer_id,
|
2024-08-17 04:27:08 +02:00
|
|
|
id = window_id,
|
2024-08-15 03:41:28 +02:00
|
|
|
}
|