diff --git a/API.md b/API.md index dad0256..436ed64 100644 --- a/API.md +++ b/API.md @@ -22,6 +22,15 @@ Dissociates the connection for the given path pwd **(string)**: path (usually project root). +## `acid.connections.peek([pwd])` +Return active connection for the given path + +*pwd* **(string)**: path (usually project root). + + +**(string)** Id of the current connection for the path or nil. + + ## `acid.connections.get(pwd)` Return active connection for the given path diff --git a/lua/acid/connections.lua b/lua/acid/connections.lua index 70c2378..9f8624b 100644 --- a/lua/acid/connections.lua +++ b/lua/acid/connections.lua @@ -59,17 +59,22 @@ end connections.unselect = function(pwd) pwd = pwd_to_key(pwd) - -- TODO Potentially wrong connections.current[pwd] = nil end +--- Return active connection for the given path +-- @tparam[opt] string pwd path (usually project root). +-- @treturn string Id of the current connection for the path or nil. +connections.peek = function(pwd) + pwd = pwd_to_key(pwd or vim.api.nvim_call_function("getcwd", {})) + return connections.current[pwd] +end + --- Return active connection for the given path -- @tparam string pwd path (usually project root). -- @treturn {string,string} Connection tuple with ip and port or nil. connections.get = function(pwd) - pwd = pwd_to_key(pwd) - - local ix = connections.current[pwd] + local ix = connections.peek(pwd) if ix == nil then return nil diff --git a/lua/acid/init.lua b/lua/acid/init.lua index b338934..d850bc9 100644 --- a/lua/acid/init.lua +++ b/lua/acid/init.lua @@ -6,6 +6,7 @@ local core = require("acid.core") local connections = require("acid.connections") local utils = require("acid.utils") +local sessions = require("acid.sessions") local acid = {} @@ -27,7 +28,8 @@ end -- @param cmd A command (op + payload + handler) to be executed. -- @param conn A connection where this command will be run. acid.run = function(cmd, conn) - return core.send(conn, cmd:build()) + local filtered = cmd:update_payload(sessions.filter) + return core.send(conn, filtered:build()) end diff --git a/lua/acid/sessions.lua b/lua/acid/sessions.lua new file mode 100644 index 0000000..a622e27 --- /dev/null +++ b/lua/acid/sessions.lua @@ -0,0 +1,51 @@ +-- luacheck: globals vim +local connections = require("acid.connections") +local ops = require("acid.ops") +local core = require("acid.core") + +local pwd_to_key = function(pwd) + if not utils.ends_with(pwd, "/") then + return pwd .. "/" + end + return pwd +end + +local sessions = {} + +sessions.store = {} + + +sessions.register_session = function(connection_ix, session) + if sessions.store[connection_ix] == nil then + sessions.store[connection_ix] = { + list = {}, + selected = nil + } + end + table.insert(sessions.store[connection_ix].list, session) + sessions.store[connection_ix].selected = session +end + +sessions.filter = function(data, connection_ix) + connection_ix = connection_ix or connections.peek() + local session = sessions.store[connection_ix] + if session ~= nil then + data.session = session.selected + end + + return data +end + +sessions.new_session = function(connection_ix) + connection_ix = connection_ix or connections.peek() + local handler = function(data) + sessions.register_session(connection_ix, data['new-session']) + end + + local conn = connections.store[connection_ix] + local clone = ops.clone{} + core.send(conn, clone.payload(), handler) +end + + +return sessions diff --git a/plugin/acid.vim b/plugin/acid.vim index b65005a..a799e08 100644 --- a/plugin/acid.vim +++ b/plugin/acid.vim @@ -90,6 +90,7 @@ augroup acid autocmd! autocmd BufWritePost *.clj call s:require() + autocmd User AcidConnected lua require("acid.sessions").new_session() autocmd User AcidConnected lua require("acid.features").preload() autocmd User AcidConnected lua require("acid.features").load_all_nss()