Skip to content

Commit 9667705

Browse files
authored
Fix/connection (#46)
* #41: Add .nrepl-port from lua if it's found * Add single log fn * Clear output before result
1 parent c02d576 commit 9667705

File tree

7 files changed

+44
-10
lines changed

7 files changed

+44
-10
lines changed

lua/acid/core.lua

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
-- @module acid.core
55
-- @todo merge with acid and acid.connections
66
local connections = require("acid.connections")
7+
local utils = require("acid.utils")
8+
local log = require("acid.log")
79

810
local core = {
911
indirection = {}
@@ -20,8 +22,21 @@ core.send = function(conn, obj, handler)
2022
end
2123

2224
local session = math.random(10000, 99999)
23-
24-
conn = conn or connections:get(vim.api.nvim_call_function("getcwd", {}))
25+
local pwd = vim.api.nvim_call_function("getcwd", {})
26+
27+
conn = conn or connections:get(pwd)
28+
local new_conn
29+
30+
if conn == nil then
31+
local fpath = vim.api.nvim_call_function("findfile", {".nrepl-port"})
32+
if fpath == "" then
33+
log.msg("No active connection to a nrepl session. Aborting")
34+
return
35+
end
36+
local portno = table.concat(vim.api.nvim_call_function("readfile", {fpath}), "")
37+
conn = {"127.0.0.1", utils.trim(portno)}
38+
new_conn = true
39+
end
2540

2641
core.indirection[session] = {
2742
fn = handler,
@@ -33,6 +48,10 @@ core.send = function(conn, obj, handler)
3348
conn,
3449
"lua"
3550
})
51+
52+
if new_conn then
53+
connections:select(pwd, connections:add(new_conn))
54+
end
3655
end
3756

3857
return core

lua/acid/log.lua

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
-- luacheck: globals vim
2+
local log = {}
3+
4+
log.msg = function(msg)
5+
vim.api.nvim_out_write("[Acid] " .. msg .. "\n")
6+
end
7+
8+
return log

lua/acid/middlewares/go_to.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
-- luacheck: globals vim
22
local nvim = vim.api
3+
local log = require("acid.log")
34
local utils = require("acid.utils")
45
local go_to = {}
56

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

1516
if fpath == nil then
16-
vim.api.nvim_command("echo 'File not found'")
17-
vim.api.nvim_err_writeln("File not found.")
17+
log.msg("File not found (" .. data.file .. ").")
1818
return
1919
end
2020

lua/acid/middlewares/print.lua

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
-- luacheck: globals vim
22
local do_print = {}
3+
local log = require("acid.log")
34

45
do_print.middleware = function(config)
56
return function(middleware)
67
return function(data)
78
if data.out ~= nil then
8-
vim.api.nvim_out_write(data.out .. "\n")
9+
log.msg(data.out)
910
end
1011
if data.value ~= nil then
11-
vim.api.nvim_out_write(data.value .. "\n")
12+
log.msg(data.value)
1213
end
1314
if data.ex ~= nil then
14-
vim.api.nvim_out_write(data.ex .. "\n")
15+
log.msg(data.ex)
1516
end
1617
if data.err ~= nil then
17-
vim.api.nvim_out_write(data.err .. "\n")
18+
log.msg(data.err)
1819
end
1920

2021
return middleware(data)

lua/acid/nrepl.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
--- nRepl connectivity
44
-- @module acid.nrepl
55
local nvim = vim.api
6+
local log = require("acid.log")
67
local utils = require("acid.utils")
78
local connections = require("acid.connections")
89

@@ -173,7 +174,7 @@ nrepl.handle = {
173174
local opts = pending[ch]
174175
local port = connections.store[opts.ix][2]
175176
connections:select(opts.pwd, opts.ix)
176-
vim.api.nvim_out_write("Acid connected on port " .. tostring(port) .. "\n")
177+
log.msg("Connected on port" .. tostring(port))
177178
vim.api.nvim_command("doautocmd User AcidConnected")
178179
pending[ch] = nil
179180
end

lua/acid/utils.lua

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1-
-- luacheck: globals table
1+
-- luacheck: globals table vim
22
local utils = {}
33

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

8+
utils.trim = vim.trim or function(str)
9+
return vim.api.nvim_call_function("trim", {str})
10+
end
11+
812
utils.interleave_first = function(tbl, itn)
913
local new = {}
1014
for _, v in ipairs(tbl) do

plugin/acid.vim

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ function! AcidSendEval(handler)
4242
call inputsave()
4343
let code = input(ns."=> ")
4444
call inputrestore()
45+
echo
4546
call luaeval("require('acid.features')." . a:handler ."(_A[1], _A[2])", [code, ns])
4647
endfunction
4748

0 commit comments

Comments
 (0)