Skip to content

Commit 1de5525

Browse files
committed
feat: managed codemp window with custom binds
a first implementation, definitely lot to improve!!
1 parent e0a5752 commit 1de5525

4 files changed

Lines changed: 110 additions & 1 deletion

File tree

src/buffers.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ end
1818

1919
local function attach(name, current, content)
2020
local buffer = nil
21-
if current then
21+
if current ~= nil then
2222
buffer = vim.api.nvim_get_current_buf()
2323
else
2424
buffer = vim.api.nvim_create_buf(true, true)

src/init.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ return {
5959
state = require('codemp.state'),
6060
buffers = require('codemp.buffers'),
6161
workspace = require('codemp.workspace'),
62+
window = require('codemp.window'),
6263
utils = require('codemp.utils'),
6364
async = require('codemp.async'),
6465
}

src/window.lua

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
local state = require('codemp.state')
2+
local utils = require('codemp.utils')
3+
local buffers = require('codemp.buffers')
4+
5+
local prev_window = nil
6+
local window_id = nil
7+
local buffer_id = nil
8+
local ns = vim.api.nvim_create_namespace("codemp-window")
9+
10+
local function open_buffer_under_cursor()
11+
if window_id == nil then return end
12+
if buffer_id == nil then return end
13+
local cursor = vim.api.nvim_win_get_cursor(window_id)
14+
local line = vim.api.nvim_buf_get_lines(buffer_id, cursor[1]-1, cursor[1], true)
15+
if not vim.startswith(line[1], " |- ") then return end
16+
local path = string.gsub(line[1], " |%- ", "")
17+
if prev_window ~= nil then
18+
vim.api.nvim_set_current_win(prev_window)
19+
end
20+
if buffers.map_rev[path] ~= nil then
21+
vim.api.nvim_set_current_buf(buffers.map_rev[path])
22+
else
23+
buffers.attach(path)
24+
end
25+
end
26+
27+
local function init_window()
28+
buffer_id = vim.api.nvim_create_buf(false, true)
29+
vim.api.nvim_buf_set_name(buffer_id, "codemp::window")
30+
vim.api.nvim_set_option_value('buftype', 'nofile', { buf = buffer_id })
31+
utils.buffer.set_content(buffer_id, "> codemp")
32+
vim.api.nvim_set_option_value('modifiable', false, { buf = buffer_id })
33+
vim.highlight.range(buffer_id, ns, 'InlayHint', {0,0}, {0, 1})
34+
vim.highlight.range(buffer_id, ns, 'Title', {0,3}, {0, 9})
35+
vim.keymap.set('n', '<CR>', function () open_buffer_under_cursor() end, { buffer = buffer_id })
36+
vim.keymap.set('n', 'a', function () buffers.create(vim.fn.input("path > ", "")) end, { buffer = buffer_id })
37+
end
38+
39+
local function update_window()
40+
local tree = state.client:get_workspace(state.workspace).filetree
41+
vim.api.nvim_set_option_value('modifiable', true, { buf = buffer_id })
42+
utils.buffer.set_content(
43+
buffer_id,
44+
">| codemp\n |: " .. state.workspace .. "\n |\n |- "
45+
.. vim.fn.join(tree, "\n |- ")
46+
)
47+
vim.highlight.range(buffer_id, ns, 'InlayHint', {0,0}, {0, 2})
48+
vim.highlight.range(buffer_id, ns, 'Title', {0,3}, {0, 9})
49+
vim.highlight.range(buffer_id, ns, 'InlayHint', {1,1}, {1, 3})
50+
vim.highlight.range(buffer_id, ns, 'Directory', {1,4}, {1, 128})
51+
vim.highlight.range(buffer_id, ns, 'InlayHint', {2,1}, {2, 3})
52+
for n, _ in ipairs(tree) do
53+
vim.highlight.range(buffer_id, ns, 'InlayHint', {2+n,1}, {2+n, 3})
54+
end
55+
vim.api.nvim_set_option_value('modifiable', false, { buf = buffer_id })
56+
end
57+
58+
local function open_window()
59+
window_id = vim.api.nvim_open_win(buffer_id, true, {
60+
win = 0,
61+
split = 'left',
62+
width = 20,
63+
})
64+
vim.api.nvim_set_option_value('relativenumber', false, {})
65+
vim.api.nvim_set_option_value('number', false, {})
66+
vim.api.nvim_set_option_value('cursorlineopt', 'line', {})
67+
end
68+
69+
local function toggle_window()
70+
if window_id ~= nil then
71+
vim.api.nvim_win_close(window_id, true)
72+
window_id = nil
73+
else
74+
prev_window = vim.api.nvim_get_current_win()
75+
open_window()
76+
end
77+
end
78+
79+
init_window()
80+
81+
return {
82+
init = init_window,
83+
open = open_window,
84+
update = update_window,
85+
toggle = toggle_window,
86+
buffer = buffer_id,
87+
}

src/workspace.lua

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ local utils = require('codemp.utils')
44
local buffers = require('codemp.buffers')
55
local async = require('codemp.async')
66
local state = require('codemp.state')
7+
local window = require('codemp.window')
78

89
local user_hl = {}
910
local user_buffer = {}
@@ -58,6 +59,26 @@ local function join(workspace)
5859
local controller = state.client:join_workspace(workspace)
5960
register_cursor_callback(controller)
6061
register_cursor_handler(controller)
62+
63+
-- TODO nvim docs say that we should stop all threads before exiting nvim
64+
-- but we like to live dangerously (:
65+
local refresher = vim.loop.new_async(function () vim.schedule(function() window.update() end) end)
66+
vim.loop.new_thread({}, function(_id, _ws, _refresher)
67+
local _codemp = require('codemp.loader').load()
68+
local _workspace = _codemp.get_client(_id):get_workspace(_ws)
69+
while true do
70+
local success, res = pcall(_workspace.event, _workspace)
71+
if success then
72+
print("workspace event!")
73+
_refresher:send()
74+
else
75+
print("error waiting for workspace event: " .. res)
76+
break
77+
end
78+
end
79+
end, state.client.id, state.workspace, refresher)
80+
81+
window.update()
6182
end
6283

6384
local function leave()

0 commit comments

Comments
 (0)