feat: managed codemp window with custom binds

a first implementation, definitely lot to improve!!
This commit is contained in:
əlemi 2024-08-15 03:41:28 +02:00
parent e0a5752d11
commit 1de55250ab
Signed by: alemi
GPG key ID: A4895B84D311642C
4 changed files with 110 additions and 1 deletions

View file

@ -18,7 +18,7 @@ end
local function attach(name, current, content) local function attach(name, current, content)
local buffer = nil local buffer = nil
if current then if current ~= nil then
buffer = vim.api.nvim_get_current_buf() buffer = vim.api.nvim_get_current_buf()
else else
buffer = vim.api.nvim_create_buf(true, true) buffer = vim.api.nvim_create_buf(true, true)

View file

@ -59,6 +59,7 @@ return {
state = require('codemp.state'), state = require('codemp.state'),
buffers = require('codemp.buffers'), buffers = require('codemp.buffers'),
workspace = require('codemp.workspace'), workspace = require('codemp.workspace'),
window = require('codemp.window'),
utils = require('codemp.utils'), utils = require('codemp.utils'),
async = require('codemp.async'), async = require('codemp.async'),
} }

87
src/window.lua Normal file
View file

@ -0,0 +1,87 @@
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")
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)
local line = vim.api.nvim_buf_get_lines(buffer_id, cursor[1]-1, cursor[1], true)
if not vim.startswith(line[1], " |- ") then return end
local path = string.gsub(line[1], " |%- ", "")
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)
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 })
end
local function update_window()
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, _ in ipairs(tree) do
vim.highlight.range(buffer_id, ns, 'InlayHint', {2+n,1}, {2+n, 3})
end
vim.api.nvim_set_option_value('modifiable', false, { buf = buffer_id })
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,
}

View file

@ -4,6 +4,7 @@ local utils = require('codemp.utils')
local buffers = require('codemp.buffers') local buffers = require('codemp.buffers')
local async = require('codemp.async') local async = require('codemp.async')
local state = require('codemp.state') local state = require('codemp.state')
local window = require('codemp.window')
local user_hl = {} local user_hl = {}
local user_buffer = {} local user_buffer = {}
@ -58,6 +59,26 @@ local function join(workspace)
local controller = state.client:join_workspace(workspace) local controller = state.client:join_workspace(workspace)
register_cursor_callback(controller) register_cursor_callback(controller)
register_cursor_handler(controller) register_cursor_handler(controller)
-- TODO nvim docs say that we should stop all threads before exiting nvim
-- but we like to live dangerously (:
local refresher = vim.loop.new_async(function () vim.schedule(function() window.update() end) end)
vim.loop.new_thread({}, function(_id, _ws, _refresher)
local _codemp = require('codemp.loader').load()
local _workspace = _codemp.get_client(_id):get_workspace(_ws)
while true do
local success, res = pcall(_workspace.event, _workspace)
if success then
print("workspace event!")
_refresher:send()
else
print("error waiting for workspace event: " .. res)
break
end
end
end, state.client.id, state.workspace, refresher)
window.update()
end end
local function leave() local function leave()