Skip to content

Commit 2d9acd1

Browse files
authored
Fix completion dialog position when completed part is wordwrapped (#692)
1 parent 3f27286 commit 2d9acd1

File tree

2 files changed

+38
-6
lines changed

2 files changed

+38
-6
lines changed

lib/reline.rb

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -225,17 +225,20 @@ def get_screen_size
225225
journey_data = completion_journey_data
226226
return unless journey_data
227227

228-
target = journey_data.list[journey_data.pointer]
228+
target = journey_data.list.first
229+
completed = journey_data.list[journey_data.pointer]
229230
result = journey_data.list.drop(1)
230231
pointer = journey_data.pointer - 1
231-
return if target.empty? || (result == [target] && pointer < 0)
232+
return if completed.empty? || (result == [completed] && pointer < 0)
232233

233234
target_width = Reline::Unicode.calculate_width(target)
234-
x = cursor_pos.x - target_width
235-
if x < 0
236-
x = screen_width + x
235+
completed_width = Reline::Unicode.calculate_width(completed)
236+
if cursor_pos.x <= completed_width - target_width
237+
# When target is rendered on the line above cursor position
238+
x = screen_width - completed_width
237239
y = -1
238240
else
241+
x = [cursor_pos.x - completed_width, 0].max
239242
y = 0
240243
end
241244
cursor_pos_to_render = Reline::CursorPos.new(x, y)

test/reline/yamatanooroti/test_rendering.rb

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1185,7 +1185,36 @@ def test_autocomplete_target_is_wrapped
11851185
assert_screen(<<~'EOC')
11861186
Multiline REPL.
11871187
prompt> St
1188-
r String
1188+
r
1189+
String
1190+
Struct
1191+
EOC
1192+
end
1193+
1194+
def test_autocomplete_target_at_end_of_line
1195+
start_terminal(20, 20, %W{ruby -I#{@pwd}/lib #{@pwd}/test/reline/yamatanooroti/multiline_repl --autocomplete}, startup_message: 'Multiline REPL.')
1196+
write(' ')
1197+
write('Str')
1198+
write("\C-i")
1199+
close
1200+
assert_screen(<<~'EOC')
1201+
Multiline REPL.
1202+
prompt> Str
1203+
ing String
1204+
Struct
1205+
EOC
1206+
end
1207+
1208+
def test_autocomplete_completed_input_is_wrapped
1209+
start_terminal(20, 20, %W{ruby -I#{@pwd}/lib #{@pwd}/test/reline/yamatanooroti/multiline_repl --autocomplete}, startup_message: 'Multiline REPL.')
1210+
write(' ')
1211+
write('Str')
1212+
write("\C-i")
1213+
close
1214+
assert_screen(<<~'EOC')
1215+
Multiline REPL.
1216+
prompt> Stri
1217+
ng String
11891218
Struct
11901219
EOC
11911220
end

0 commit comments

Comments
 (0)