Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit43b0d15

Browse files
authored
Print evaluation output to buffer (#79)
* Simplify work around cleanupIt's simper if the nrepl keeps track of the connection internal id, which is something it might want to do anyways.* Fix duplicated call to accessor fn* Rename local table and local varsFor better expressing their (new) intent.* Allow supplying connection id or ip-port tuple* Allow for reverse lookup on connections* Allow reverse lookup for sessions* Print input and output text to output windowOnly if enabled, will pass through if buffer doesn't exist.
1 parentf260aec commit43b0d15

File tree

8 files changed

+180
-46
lines changed

8 files changed

+180
-46
lines changed

‎lua/acid/connections.lua‎

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -24,33 +24,15 @@ connections.add = function(addr)
2424
returnulid
2525
end
2626

27-
connections.remove=function(pwd,addr)
28-
-- If removed address is current to a pwd
29-
localkey=connections.current[pwd]
30-
localconn=connections.store[key]
31-
32-
-- Double-check if address is correct
33-
ifkey~=nilandconn~=nilandconn[2]==addr[2]andconn[1]==addr[1]then
34-
-- Then remove it from current pwd
35-
connections.current[pwd]=nil
36-
-- Remove it's definition
37-
connections.store[key]=nil
38-
39-
-- And remove all other addresses that point to it
40-
forix,vinpairs(connections.current)do
41-
ifv==keythen
42-
connections.current[ix]=nil
43-
end
44-
end
45-
else
46-
-- Else, remove it from the connections if no address points to it
47-
fori,vinpairs(connections.store)do
48-
ifv[2]==addr[2]andv[1]==addr[1]then
49-
connections.store[i]=nil
50-
end
27+
connections.remove=function(key)
28+
-- Remove all addresses that point to id
29+
forix,vinpairs(connections.current)do
30+
ifv==keythen
31+
connections.current[ix]=nil
5132
end
5233
end
5334

35+
connections.store[key]=nil
5436
end
5537

5638
--- Elects selected connection as primary (thus default) for a certain address
@@ -103,6 +85,16 @@ connections.search = function(pwd)
10385
returnnil
10486
end
10587

88+
connections.reverse_lookup=function(conn)
89+
fork,vinpairs(connections.store)do
90+
ifv[2]==conn[2]andv[1]==conn[1]then
91+
returnk
92+
end
93+
end
94+
95+
returnnil
96+
end
97+
10698
connections.attempt_get=function(pwd)
10799
localconn=connections.get(pwd)
108100
ifconn==nilthen

‎lua/acid/core.lua‎

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
--- low-level connection handler.
44
-- @module acid.core
55
-- @todo merge with acid and acid.connections
6+
localoutput=require("acid.output")
67
localconnections=require("acid.connections")
78
localutils=require("acid.utils")
89
locallog=require("acid.log")
@@ -12,24 +13,39 @@ local core = {
1213
}
1314

1415
--- Forward messages to the nrepl and registers the handler.
15-
-- @tparam[opt] {string,string}conn Ip and Port tuple. Will try to get one if nil.
16+
-- @tparam[opt] {string,string}connection Ip and Port tuple. Will try to get one if nil.
1617
-- @tparam table obj Payload to be sent to the nrepl.
1718
-- @tparam function handler Handler function to deal with the response.
18-
core.send=function(conn,obj,handler)
19+
core.send=function(connection,obj,handler)
20+
localconn,conn_id
1921
ifhandler==nilthen
2022
vim.api.nvim_err_writeln("Please provide a handler for that operation.")
2123
return
2224
end
2325

2426
localpwd=vim.api.nvim_call_function("getcwd", {})
2527

26-
conn=connorconnections.attempt_get(pwd)
28+
localctp=type(connection)
29+
ifctp=="string"then
30+
conn_id=connection
31+
conn=connections.store[connection]
32+
elseifctp=="table"then
33+
conn=connection
34+
conn_id=connections.reverse_lookup(connection)
35+
elseifconnection==nilthen
36+
conn_id=connections.peek()
37+
conn=connections.store[conn_id]
38+
end
2739

2840
ifconn==nilthen
2941
log.msg("No active connection to a nrepl session. Aborting")
3042
return
3143
end
3244

45+
ifobj.code~=nilthen
46+
output.draw(conn_id, {(obj.nsor"").."=>"..obj.code})
47+
end
48+
3349
ifobj.id==nilthen
3450
obj.id=utils.ulid()
3551
end

‎lua/acid/features.lua‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ features.eval_expr = function(mode, replace, ns)
9898
end}))
9999
else
100100
midlws=middlewares
101+
.output{}
101102
.print{}
102103
.clipboard{}
103104
.virtualtext(coord)

‎lua/acid/middlewares/output.lua‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
-- luacheck: globals vim
2+
localdo_print= {}
3+
localoutput=require("acid.output")
4+
localsessions=require("acid.sessions")
5+
localutils=require("acid.utils")
6+
locallog=require("acid.log")
7+
8+
output.middleware=function(config)
9+
returnfunction(middleware)
10+
returnfunction(data)
11+
localsession=data.session
12+
localconn_id=sessions.reverse_lookup(session)
13+
14+
ifdata.out~=nilthen
15+
localout= {}
16+
data.out:gsub("[^\n]+",function(dt)table.insert(out,dt)end)
17+
output.draw(conn_id,out)
18+
end
19+
ifdata.value~=nilthen
20+
output.draw(conn_id, {"=>"..data.value})
21+
end
22+
ifdata.ex~=nilthen
23+
output.draw(conn_id, {"!!"..data.ex})
24+
end
25+
ifdata.err~=nilthen
26+
localout= {}
27+
data.err:gsub("[^\n]+",function(dt)table.insert(out,"!!"..dt)end)
28+
output.draw(conn_id,out)
29+
end
30+
31+
ifconfig.accessor~=nilthen
32+
localmsg=config.accessor(data)
33+
ifmsg~=nilandmsg~=""then
34+
output.draw(conn_id, {"<>"..msg})
35+
end
36+
end
37+
38+
returnmiddleware(data)
39+
end
40+
end
41+
end
42+
43+
returnoutput

‎lua/acid/middlewares/print.lua‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ do_print.middleware = function(config)
2222
ifconfig.accessor~=nilthen
2323
localmsg=config.accessor(data)
2424
ifmsg~=nilandmsg~=""then
25-
log.msg(config.accessor(data))
25+
log.msg(msg)
2626
end
2727
end
2828

‎lua/acid/nrepl.lua‎

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ local nvim = vim.api
66
locallog=require("acid.log")
77
localutils=require("acid.utils")
88
localconnections=require("acid.connections")
9+
localoutput=require("acid.output")
910

10-
localpending= {}
11+
localjob_mapping= {}
1112
localnrepl= {}
1213

1314
localdeps= {
@@ -104,6 +105,7 @@ nrepl.default_middlewares = {'nrepl/nrepl', 'cider/cider-nrepl', 'refactor-nrepl
104105
-- @tparam[opt] string obj.connect -c parameter for the nrepl process
105106
-- @tparam[opt] string obj.bind -b parameter for the nrepl process
106107
-- @tparam[opt] boolean obj.skip_autocmd don't fire an autocmd after starting this repl
108+
-- @tparam[opt] boolean obj.disable_output_capture disables output capturing.
107109
-- @treturn boolean Whether it was possible to spawn a nrepl process
108110
nrepl.start=function(obj)
109111
localpwd=obj.pwdorvim.api.nvim_call_function("getcwd", {})
@@ -148,25 +150,23 @@ nrepl.start = function(obj)
148150
addr=conn
149151
}
150152

151-
localix=connections.add(conn)
153+
localconn_id=connections.add(conn)
152154

153-
pending[ret]= {pwd=pwd,ix=ix}
155+
nrepl.cache[pwd].id=conn_id
156+
157+
job_mapping[ret]= {pwd=pwd,conn=conn_id,init=false}
158+
ifobj.disable_output_capture==falsethen
159+
output.buffer(conn_id)
160+
end
154161
returntrue
155162
end
156163

157164
--- Stops a nrepl process managed by acid
158165
-- @tparam table obj Configuration for the nrepl process to be stopped
159166
-- @tparam string obj.pwd Path where the nrepl process was started
160167
nrepl.stop=function(obj)
161-
localpwd=obj.pwd
162-
163-
ifnotutils.ends_with(pwd,"/")then
164-
pwd=pwd.."/"
165-
end
166-
167-
nvim.nvim_call_function("jobstop", {nrepl.cache[pwd].job})
168-
connections.unselect(pwd)
169-
connections.remove(pwd,nrepl.cache[pwd].addr)
168+
nvim.nvim_call_function("jobstop", {nrepl.cache[obj.pwd].job})
169+
connections.remove(nrepl.cache[obj.pwd].id)
170170
nrepl.cache[obj.pwd]=nil
171171
end
172172

@@ -185,21 +185,25 @@ nrepl.handle = {
185185
_store= {},
186186
stdout=function(dt,ch)
187187
nrepl.handle._store[ch]=nrepl.handle._store[ch]or {}
188-
ifpending[ch]~=nilthen
188+
localjob=job_mapping[ch]
189+
190+
ifnotjob.initthen
189191
for_,lninipairs(dt)do
190192
ifstring.sub(ln,1,20)=="nREPL server started"then
191-
localopts=pending[ch]
192193
localport=ln:match("%d+")
193-
connections.store[opts.ix][2]=port
194-
connections.select(opts.pwd,opts.ix)
195-
pending[ch]=nil
196-
ifnotnrepl.cache[opts.pwd].skip_autocmdthen
194+
connections.store[job.conn][2]=port
195+
connections.select(job.pwd,job.conn)
196+
job_mapping[ch].init=true
197+
ifnotnrepl.cache[job.pwd].skip_autocmdthen
197198
log.msg("Connected on port",tostring(port))
198199
vim.api.nvim_command("doautocmd User AcidConnected")
199200
end
201+
break
200202
end
201203
end
202204
end
205+
206+
output.draw(job.conn,dt)
203207
table.insert(nrepl.handle._store[ch],dt)
204208
end,
205209
stderr=function(dt,ch)

‎lua/acid/output.lua‎

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
-- luacheck: globals vim
2+
3+
localoutput= {}
4+
5+
locallocation=function(opts)
6+
localwidth=vim.api.nvim_get_option("columns")
7+
localheight=vim.api.nvim_get_option("lines")
8+
return {
9+
relative="editor",
10+
width=opts.widthor60,
11+
height=height,
12+
row=0,
13+
focusable=false,
14+
col=width- (opts.widthor60)
15+
}
16+
end
17+
18+
output.conn_to_buf= {}
19+
20+
output.buffer=function(conn)
21+
localbuf=output.conn_to_buf[conn]
22+
ifbuf==nilthen
23+
buf=vim.api.nvim_create_buf(false,true)
24+
vim.api.nvim_buf_set_option(buf,"bufhidden","wipe")
25+
output.conn_to_buf[conn]=buf
26+
end
27+
28+
returnbuf
29+
end
30+
31+
output.window=function(conn,opts)
32+
localbuf=output.buffer(conn)
33+
localwinnr=vim.fn.bufwinnr(buf)
34+
localwinid
35+
36+
ifwinnr==-1then
37+
winid=vim.api.nvim_open_win(buf,true,location(optsor {}))
38+
vim.api.nvim_win_set_option(winid,"breakindent",true)
39+
vim.api.nvim_win_set_option(winid,"number",false)
40+
vim.api.nvim_win_set_option(winid,"relativenumber",false)
41+
vim.api.nvim_win_set_option(winid,"fillchars","eob:")
42+
else
43+
winid=vim.fn.win_getid()
44+
end
45+
vim.api.nvim_set_current_win(winid)
46+
end
47+
48+
output.close_window=function(conn,opts)
49+
localbuf=output.buffer(conn)
50+
localwinnr=vim.fn.bufwinnr(buf)
51+
52+
ifwinnr~=-1then
53+
localwinid=vim.fn.win_getid()
54+
vim.api.nvim_win_close(winid,true)
55+
end
56+
end
57+
58+
59+
output.draw=function(conn,lines)
60+
localbuf=output.conn_to_buf[conn]
61+
ifbuf==nilthen
62+
return
63+
end
64+
65+
vim.api.nvim_buf_set_lines(buf,-1,-1,false,lines)
66+
end
67+
68+
returnoutput

‎lua/acid/sessions.lua‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,15 @@ sessions.new_session = function(connection_ix)
4747
core.send(conn,clone.payload(),handler)
4848
end
4949

50+
sessions.reverse_lookup=function(session_id)
51+
fork,vinpairs(sessions.store)do
52+
for_,v2inipairs(v.list)do
53+
ifv2==session_idthen
54+
returnk
55+
end
56+
end
57+
end
58+
end
59+
5060

5161
returnsessions

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp