Reply::ExpressionEditor
Inherits Reference < Object
The ExpressionEditor allows to edit and display an expression.
Its main task is to provide the display of the prompt and a multiline expression within the term bounds, and ensure the correspondence between the cursor on screen and the cursor on the expression.
Usage example:
# new editor:
@editor = ExpressionEditor.new(
prompt: ->(expr_line_number : Int32) { "prompt>" }
)
# edit some code:
@editor.update do
@editor << %(puts "World")
insert_new_line(indent: 1)
@editor << %(puts "!")
end
# move cursor:
@editor.move_cursor_up
4.times { @editor.move_cursor_left }
# edit:
@editor.update do
@editor << "Hello "
end
@editor.end_editing
@editor.expression # => %(puts "Hello World"\n puts "!")
puts "=> ok"
# clear and restart edition:
@editor.prompt_next
The above displays:
prompt>puts "Hello World"
prompt> puts "!"
=> ok
prompt>
Methods that modify the expression should be placed inside an update so the screen can be refreshed taking in account
the adding or removing of lines, and doesn't boilerplate the display.
Constructors
Class methods
Splits the given line (colorized) into parts delimited by wrapping.
Because line is colorized, it's hard to know when it's wrap based on its size (colors sequence might appear anywhere in the string) Here we does the following:
- Create a
String::Builderfor the first part (part_builder) - Iterate over the line, parsing the color sequence
- Count cursor
xfor each char unless color sequences - If count goes over term width:
reset
xto 0, and create a newString::Builderfor next part.
Instance methods
Should be called inside an update.
If char is \n or \r, inserts a new line with indent 0.
Does nothing if the char is an ascii_control?.
Replaces the word under the cursor by replacement, then moves cursor at the end of replacement.
Should be called inside an update.
Prints the full expression (without view bounds), and eventually replace it by replacement.
Sets a Proc allowing to display a header above the prompt. (used by auto-completion)
io: The IO in which the header should be displayed. previous_height: Previous header height, useful to keep a header size constant. Should returns the exact height printed in the io.
Refresh the screen.
It clears the display of the current expression, then yields for modifications, and displays the new expression.
if force_full_view is true, whole expression is displayed, even if it overflow the term width, otherwise the expression is bound and can be scrolled.
The list of characters delimiting words.
default: \n\t+-*/,;@&%<>"'^\\[](){}|.~:=!?
The list of characters delimiting words.
default: \n\t+-*/,;@&%<>"'^\\[](){}|.~:=!?
Tracks the cursor position relatively to the expression's lines, (y=0 corresponds to the first line and x=0 the first char) This position is independent of text wrapping so its position will not match to real cursor on screen.
| : cursor position
prompt>def very_looo
ooo|ng_name <= wrapping
prompt> bar
prompt>end
For example here the cursor position is x=16, y=0, but real cursor is at x=3,y=1 from the beginning of expression.