Skip to content

Add sessions #55

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
13 changes: 9 additions & 4 deletions lua/acid/connections.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion lua/acid/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}

Expand All @@ -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


Expand Down
51 changes: 51 additions & 0 deletions lua/acid/sessions.lua
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions plugin/acid.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down