aboutsummaryrefslogtreecommitdiffstats
path: root/lib/reline/line_editor.rb
diff options
context:
space:
mode:
authoraycabta <aycabta@gmail.com>2021-03-23 21:43:39 +0900
committeraycabta <aycabta@gmail.com>2021-03-24 15:43:27 +0900
commit758f2b35f93478c481902d9d3f8876216a5dcc4d (patch)
tree6d6515fbe13f246f503c1c5147d8eba4dce2f30b /lib/reline/line_editor.rb
parent4b33d860e84f0a5efeefbf8a68324801a0215a08 (diff)
downloadruby-758f2b35f93478c481902d9d3f8876216a5dcc4d.tar.gz
[ruby/reline] Support preposing and postposing for Reline.completion_proc
https://github.com/ruby/reline/commit/1f469de90c
Diffstat (limited to 'lib/reline/line_editor.rb')
-rw-r--r--lib/reline/line_editor.rb34
1 files changed, 32 insertions, 2 deletions
diff --git a/lib/reline/line_editor.rb b/lib/reline/line_editor.rb
index 870b89b550..4e5860ceb9 100644
--- a/lib/reline/line_editor.rb
+++ b/lib/reline/line_editor.rb
@@ -1158,8 +1158,25 @@ class Reline::LineEditor
def call_completion_proc
result = retrieve_completion_block(true)
- slice = result[1]
- result = @completion_proc.(slice) if @completion_proc and slice
+ preposing, target, postposing = result
+ if @completion_proc and target
+ argnum = @completion_proc.parameters.inject(0) { |result, item|
+ case item.first
+ when :req, :opt
+ result + 1
+ when :rest
+ break 3
+ end
+ }
+ case argnum
+ when 1
+ result = @completion_proc.(target)
+ when 2
+ result = @completion_proc.(target, preposing)
+ when 3..Float::INFINITY
+ result = @completion_proc.(target, preposing, postposing)
+ end
+ end
Reline.core.instance_variable_set(:@completion_quote_character, nil)
result
end
@@ -1264,6 +1281,19 @@ class Reline::LineEditor
end
target = before
end
+ if @is_multiline
+ if @previous_line_index
+ lines = whole_lines(index: @previous_line_index, line: @line)
+ else
+ lines = whole_lines
+ end
+ if @line_index > 0
+ preposing = lines[0..(@line_index - 1)].join("\n") + "\n" + preposing
+ end
+ if (lines.size - 1) > @line_index
+ postposing = postposing + "\n" + lines[(@line_index + 1)..-1].join("\n")
+ end
+ end
[preposing.encode(@encoding), target.encode(@encoding), postposing.encode(@encoding)]
end