Skip to content

Commit c0e0630

Browse files
author
KristofferC
committed
open editor at correct column for edit_input
1 parent 6441311 commit c0e0630

File tree

2 files changed

+24
-8
lines changed

2 files changed

+24
-8
lines changed

stdlib/REPL/src/LineEdit.jl

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1314,7 +1314,7 @@ function guess_current_mode_name(s)
13141314
end
13151315

13161316
# edit current input in editor
1317-
function edit_input(s, f = (filename, line) -> InteractiveUtils.edit(filename, line))
1317+
function edit_input(s, f = (filename, line, column) -> InteractiveUtils.edit(filename, line, column))
13181318
mode_name = guess_current_mode_name(s)
13191319
filename = tempname()
13201320
if mode_name == :julia
@@ -1325,9 +1325,26 @@ function edit_input(s, f = (filename, line) -> InteractiveUtils.edit(filename, l
13251325
buf = buffer(s)
13261326
pos = position(buf)
13271327
str = String(take!(buf))
1328-
line = 1 + count(==(_newline), view(str, 1:pos))
1328+
lines = readlines(IOBuffer(str); keep=true)
1329+
1330+
# Compute line
1331+
line_start_offset = 0
1332+
line = 1
1333+
while line < length(lines) && line_start_offset + sizeof(lines[line]) <= pos
1334+
line_start_offset += sizeof(lines[line])
1335+
line += 1
1336+
end
1337+
1338+
# Compute column
1339+
col = 0
1340+
off = line_start_offset
1341+
while off <= pos
1342+
off = nextind(str, off)
1343+
col += 1
1344+
end
1345+
13291346
write(filename, str)
1330-
f(filename, line)
1347+
f(filename, line, col)
13311348
str_mod = readchomp(filename)
13321349
rm(filename)
13331350
if str != str_mod # something was changed, run the input

stdlib/REPL/test/repl.jl

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1506,11 +1506,10 @@ fake_repl() do stdin_write, stdout_read, repl
15061506
end
15071507
repl.interface = REPL.setup_interface(repl)
15081508
s = LineEdit.init_state(repl.t, repl.interface)
1509-
LineEdit.edit_insert(s, "1234")
1510-
@show buffercontents(LineEdit.buffer(s))
1511-
input_f = function(filename, line)
1512-
write(filename, "123456\n")
1509+
LineEdit.edit_insert(s, "1234αβ")
1510+
input_f = function(filename, line, column)
1511+
write(filename, "1234αβ56γ\n")
15131512
end
15141513
LineEdit.edit_input(s, input_f)
1515-
@test buffercontents(LineEdit.buffer(s)) == "123456"
1514+
@test buffercontents(LineEdit.buffer(s)) == "1234αβ56γ"
15161515
end

0 commit comments

Comments
 (0)