Skip to content

Commit

Permalink
Fix/connection (#46)
Browse files Browse the repository at this point in the history
* #41: Add .nrepl-port from lua if it's found

* Add single log fn

* Clear output before result
  • Loading branch information
hkupty committed Mar 27, 2019
1 parent c02d576 commit 9667705
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 10 deletions.
23 changes: 21 additions & 2 deletions lua/acid/core.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
-- @module acid.core
-- @todo merge with acid and acid.connections
local connections = require("acid.connections")
local utils = require("acid.utils")
local log = require("acid.log")

local core = {
indirection = {}
Expand All @@ -20,8 +22,21 @@ core.send = function(conn, obj, handler)
end

local session = math.random(10000, 99999)

conn = conn or connections:get(vim.api.nvim_call_function("getcwd", {}))
local pwd = vim.api.nvim_call_function("getcwd", {})

conn = conn or connections:get(pwd)
local new_conn

if conn == nil then
local fpath = vim.api.nvim_call_function("findfile", {".nrepl-port"})
if fpath == "" then
log.msg("No active connection to a nrepl session. Aborting")
return
end
local portno = table.concat(vim.api.nvim_call_function("readfile", {fpath}), "")
conn = {"127.0.0.1", utils.trim(portno)}
new_conn = true
end

core.indirection[session] = {
fn = handler,
Expand All @@ -33,6 +48,10 @@ core.send = function(conn, obj, handler)
conn,
"lua"
})

if new_conn then
connections:select(pwd, connections:add(new_conn))
end
end

return core
8 changes: 8 additions & 0 deletions lua/acid/log.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
-- luacheck: globals vim
local log = {}

log.msg = function(msg)
vim.api.nvim_out_write("[Acid] " .. msg .. "\n")
end

return log
4 changes: 2 additions & 2 deletions lua/acid/middlewares/go_to.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
-- luacheck: globals vim
local nvim = vim.api
local log = require("acid.log")
local utils = require("acid.utils")
local go_to = {}

Expand All @@ -13,8 +14,7 @@ go_to.middleware = function(config)
local fpath = nvim.nvim_call_function("AcidFindFileInPath", {data.file, data.resource})

if fpath == nil then
vim.api.nvim_command("echo 'File not found'")
vim.api.nvim_err_writeln("File not found.")
log.msg("File not found (" .. data.file .. ").")
return
end

Expand Down
9 changes: 5 additions & 4 deletions lua/acid/middlewares/print.lua
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
-- luacheck: globals vim
local do_print = {}
local log = require("acid.log")

do_print.middleware = function(config)
return function(middleware)
return function(data)
if data.out ~= nil then
vim.api.nvim_out_write(data.out .. "\n")
log.msg(data.out)
end
if data.value ~= nil then
vim.api.nvim_out_write(data.value .. "\n")
log.msg(data.value)
end
if data.ex ~= nil then
vim.api.nvim_out_write(data.ex .. "\n")
log.msg(data.ex)
end
if data.err ~= nil then
vim.api.nvim_out_write(data.err .. "\n")
log.msg(data.err)
end

return middleware(data)
Expand Down
3 changes: 2 additions & 1 deletion lua/acid/nrepl.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
--- nRepl connectivity
-- @module acid.nrepl
local nvim = vim.api
local log = require("acid.log")
local utils = require("acid.utils")
local connections = require("acid.connections")

Expand Down Expand Up @@ -173,7 +174,7 @@ nrepl.handle = {
local opts = pending[ch]
local port = connections.store[opts.ix][2]
connections:select(opts.pwd, opts.ix)
vim.api.nvim_out_write("Acid connected on port " .. tostring(port) .. "\n")
log.msg("Connected on port" .. tostring(port))
vim.api.nvim_command("doautocmd User AcidConnected")
pending[ch] = nil
end
Expand Down
6 changes: 5 additions & 1 deletion lua/acid/utils.lua
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
-- luacheck: globals table
-- luacheck: globals table vim
local utils = {}

utils.pack = function (...)
return {n=select('#',...); ...}
end

utils.trim = vim.trim or function(str)
return vim.api.nvim_call_function("trim", {str})
end

utils.interleave_first = function(tbl, itn)
local new = {}
for _, v in ipairs(tbl) do
Expand Down
1 change: 1 addition & 0 deletions plugin/acid.vim
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ function! AcidSendEval(handler)
call inputsave()
let code = input(ns."=> ")
call inputrestore()
echo
call luaeval("require('acid.features')." . a:handler ."(_A[1], _A[2])", [code, ns])
endfunction

Expand Down

0 comments on commit 9667705

Please sign in to comment.