Skip to content

Commit 0980b96

Browse files
committed
Make history a local variable and added clear_history() function.
This fixes a failing test.
1 parent 286d2d1 commit 0980b96

File tree

2 files changed

+16
-11
lines changed

2 files changed

+16
-11
lines changed

init.lua

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ local env = setmetatable({
4949

5050
--- Lua command history.
5151
-- It has a numeric `pos` field that indicates where in the history the user currently is.
52-
M.history = {pos = 0}
52+
local history = {pos = 0}
5353

5454
--- Evaluates as Lua code the current line or the text on the currently selected lines.
5555
-- If the current line has a syntax error, it is ignored and treated as a line continuation.
@@ -91,8 +91,8 @@ function M.evaluate_repl()
9191
buffer:add_text(tostring(result):gsub('(\r?\n)', '%1--> '))
9292
buffer:new_line()
9393
end
94-
M.history[#M.history + 1] = code
95-
M.history.pos = #M.history + 1
94+
history[#history + 1] = code
95+
history.pos = #history + 1
9696
buffer:set_save_point()
9797
end
9898

@@ -125,29 +125,32 @@ end
125125
--- Cycle backward through command history, taking into account commands with multiple lines.
126126
function M.cycle_history_prev()
127127
if buffer:auto_c_active() then return false end -- propagate
128-
if M.history.pos <= 1 then return end
129-
for _ in (M.history[M.history.pos] or ''):gmatch('\n') do
128+
if history.pos <= 1 then return end
129+
for _ in (history[history.pos] or ''):gmatch('\n') do
130130
buffer:line_delete()
131131
buffer:delete_back()
132132
end
133133
buffer:line_delete()
134-
M.history.pos = math.max(M.history.pos - 1, 1)
135-
buffer:add_text(M.history[M.history.pos])
134+
history.pos = math.max(history.pos - 1, 1)
135+
buffer:add_text(history[history.pos])
136136
end
137137

138138
--- Cycle forward through command history, taking into account commands with multiple lines.
139139
function M.cycle_history_next()
140140
if buffer:auto_c_active() then return false end -- propagate
141-
if M.history.pos >= #M.history then return end
142-
for _ in (M.history[M.history.pos] or ''):gmatch('\n') do
141+
if history.pos >= #history then return end
142+
for _ in (history[history.pos] or ''):gmatch('\n') do
143143
buffer:line_delete()
144144
buffer:delete_back()
145145
end
146146
buffer:line_delete()
147-
M.history.pos = math.min(M.history.pos + 1, #M.history)
148-
buffer:add_text(M.history[M.history.pos])
147+
history.pos = math.min(history.pos + 1, #history)
148+
buffer:add_text(history[history.pos])
149149
end
150150

151+
--- Clears the command history.
152+
function M.clear_history() history = {pos = 0} end
153+
151154
--- Table of key bindings for the REPL.
152155
-- @field keys
153156

init_test.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
local lua_repl = require('lua_repl')
44

5+
teardown(lua_repl.clear_history)
6+
57
test('lua_repl.open should open a REPL', function()
68
lua_repl.open()
79

0 commit comments

Comments
 (0)