TextArea
elva.widgets.textarea
Textual
Widgets for realtime text-editing
Classes:
-
YDocument
–The inner document holding the realtime synchronized content.
-
YTextArea
–Widget for displaying and manipulating text synchronized in realtime.
Functions:
-
ends_with_newline
–Check whether a given text ends with a newline character, i.e.
\n
or\r
. -
get_lines
–Split a text into its lines.
-
get_index_from_binary_index
–Convert the index in UTF-8 encoding to character index.
-
get_binary_index_from_index
–Convert the character index to index in UTF-8 encoding.
-
get_location_from_index
–Return the 2D row and column coordinates in
text
at positionindex
. -
get_location_from_binary_index
–Return the 2D row and column coordinates in the UTF-8 decoded form of
btext
at positionbindex
. -
get_index_from_location
–Convert 2D row and column coordinates to the index position in
text
. -
get_binary_index_from_location
–Convert 2D row and column coordinates to the binary index position in
btext
. -
get_text_range
–Get the slice in
text
between astart
and anend
location. -
get_binary_text_range
–Get the slice in the UTF-8 encoded
btext
between astart
and anend
location. -
update_location
–Update a location index with respect to edit metrics.
-
get_binary_location_from_binary_index
–Get the 2D row and column coordinates in UTF-8 encoded text at a given position.
Attributes:
-
NEWLINE_CHARS
–String of newline characters.
NEWLINE_CHARS = '\n\r'
module-attribute
String of newline characters.
YDocument(ytext, language)
Bases: DocumentBase
The inner document holding the realtime synchronized content.
It supports indexing and implements the asynchronous iterator protocol.
This class is intended for use in YTextArea
.
Examples:
Indexing:
text = r"Hello,\nWorld!"
ytext = Text(text)
doc = YDocument(ytext, "python")
assert doc[0] == "Hello,"
Asynchronous iterator protocol:
Parameters:
-
ytext
(Text
) –Y text data type holding the document content.
-
language
(str
) –the language the document content is written in.
Methods:
-
get_line
–Get the line's content at a specified row.
-
__getitem__
–Get the line's content at a specified row.
-
get_binary_index_from_index
–Convert the character index to index in UTF-8 encoding.
-
get_index_from_location
–Convert 2D row and column coordinates to index position.
-
get_binary_index_from_location
–Convert location to binary index.
-
get_index_from_binary_index
–Convert binary index to index.
-
get_location_from_index
–Convert index to location.
-
get_location_from_binary_index
–Convert binary index to location.
-
get_text_range
–Get the text within a certain range.
-
get_size
–Get the size of the document's text.
-
replace_range
–Apply an edit.
-
update_tree
–Update the the syntax tree if syntax is enabled.
-
get_btext_slice
–Get a slice of the document's UTF-8 encoded text.
-
query_syntax_tree
–Get captures for a query on the syntax tree.
-
parse
–Parse the contents of a document's Y text data type event.
-
callback
–Hook called on a document's Y text data type event.
-
on_retain
–Hook called on a retain actio in a document's Y text data type event.
-
on_insert
–Hook called on an insert action in a document's Y text data type event.
-
on_delete
–Hook called on an insert action in a document's Y text data type event.
-
__aiter__
–Implement the asynchronous iterator protocol.
-
__anext__
–Return an edit on the next asynchronous iterator step.
Attributes:
-
ytext
(Text
) –Y text data type holding the document content.
-
edits
(Queue
) –Queue of applied edits.
-
language
(Language
) –Instance of the tree-sitter language used for queries.
-
parser
(Parser
) –Instance of the tree-sitter parser.
-
tree
(Tree
) –Instance of the tree-sitter tree the parser updates incrementally.
-
syntax_enabled
(bool
) –Flag whether to apply tree-sitter queries.
-
text
(str
) –Text representation of the Y text data type content.
-
btext
(bytes
) –UTF-8 encoded text representation of the Y text data type content.
-
newline
(str
) –The newline character used in this document.
-
lines
(list[str]
) –The document's content formatted as a list of lines.
-
line_count
(int
) –The number of lines.
-
start
(tuple[int, int]
) –Start location of the document's text.
-
end
(tuple[int, int]
) –End location of the document's text.
Source code in src/elva/widgets/textarea.py
ytext = ytext
instance-attribute
Y text data type holding the document content.
edits = Queue()
instance-attribute
Queue of applied edits.
language = get_language(language)
instance-attribute
Instance of the tree-sitter language used for queries.
parser = get_parser(language)
instance-attribute
Instance of the tree-sitter parser.
tree = self.parser.parse(self.get_btext_slice)
instance-attribute
Instance of the tree-sitter tree the parser updates incrementally.
syntax_enabled = True
instance-attribute
Flag whether to apply tree-sitter queries.
text
property
Text representation of the Y text data type content.
btext
property
UTF-8 encoded text representation of the Y text data type content.
newline
property
The newline character used in this document.
lines
property
The document's content formatted as a list of lines.
line_count
property
The number of lines.
start
property
Start location of the document's text.
end
property
End location of the document's text.
get_line(row)
__getitem__(row)
get_binary_index_from_index(index)
get_index_from_location(location)
Convert 2D row and column coordinates to index position.
Parameters:
Returns:
-
int
–the index in the document's text.
Source code in src/elva/widgets/textarea.py
get_binary_index_from_location(location)
Convert location to binary index.
Parameters:
Returns:
-
int
–the index in the document's UTF-8 encoded text.
Source code in src/elva/widgets/textarea.py
get_index_from_binary_index(index)
get_location_from_index(index)
Convert index to location.
Parameters:
-
index
(int
) –index in the document's text.
Returns:
Source code in src/elva/widgets/textarea.py
get_location_from_binary_index(index)
Convert binary index to location.
Parameters:
-
index
(int
) –index in the document's UTF-8 encoded text.
Returns:
Source code in src/elva/widgets/textarea.py
get_text_range(start, end)
Get the text within a certain range.
Parameters:
-
start
(tuple[int, int]
) –location where the text range starts.
-
end
(tuple[int, int]
) –location where the text range ends.
Returns:
-
str
–the slice of text within
start
andend
location.
Source code in src/elva/widgets/textarea.py
get_size(indent_width)
Get the size of the document's text.
Parameters:
-
indent_width
(int
) –number of spaces to replace with tabs.
Returns:
-
Size
–object holding the number of columns and rows.
Source code in src/elva/widgets/textarea.py
replace_range(start, end, text)
Apply an edit.
First, the range from start
to end
gets deleted, then the given text
gets inserted at start
.
This covers all kinds of text manipulation:
- inserting: start == end
, text
is not empty
- deleting: start != end
, text
is empty
- replacing: start != end
, text
is not empty
Parameters:
-
start
(tuple[int, int]
) –location where the edit begins
-
end
(tuple[int, int]
) –location where the edit ends
-
text
(str
) –the text to insert at
start
Source code in src/elva/widgets/textarea.py
update_tree(istart, iend_old, iend, start, end_old, end)
Update the the syntax tree if syntax is enabled.
This method is called by the class internally.
Parameters:
-
istart
(int
) –binary start index
-
iend_old
(int
) –binary end index before the edit
-
iend
(int
) –binary end index after the edit
-
start
(tuple[int, int]
) –binary start location
-
end_old
(tuple[int, int]
) –binary end location before the edit.
-
end
(tuple[int, int]
) –binary end location after the edit.
Source code in src/elva/widgets/textarea.py
get_btext_slice(byte_offset, position)
Get a slice of the document's UTF-8 encoded text.
This method is called by the class internally.
Parameters:
-
byte_offset
(int
) –binary start index of the slice.
-
position
(Point
) –binary start location of the slice.
Returns:
-
bytes
–line chunk starting at
byte_offset
or an empty bytes literal if at the end of document's UTF-8 encoded text.
Source code in src/elva/widgets/textarea.py
query_syntax_tree(query, start=None, end=None)
Get captures for a query on the syntax tree.
Parameters:
-
query
(Query
) –instance of a tree-sitter query
-
start
(Point
, default:None
) –start point of the query
-
end
(Point
, default:None
) –end point of the query
Returns:
-
dict[str, list[Node]]
–a dict where the keys are the names of the captures and the values are lists of the captured nodes.
Source code in src/elva/widgets/textarea.py
parse(event)
Parse the contents of a document's Y text data type event.
This method is called by the class internally.
Parameters:
-
event
(TextEvent
) –event emitted by a change in the document's Y text data type.
Source code in src/elva/widgets/textarea.py
callback(event)
Hook called on a document's Y text data type event.
Parameters:
-
event
(TextEvent
) –event emitted by a change in the document's Y text data type.
on_retain(range_offset)
Hook called on a retain actio in a document's Y text data type event.
No further actions are taken.
Parameters:
-
range_offset
(int
) –start index of the retain action.
on_insert(range_offset, insert_value)
Hook called on an insert action in a document's Y text data type event.
It puts edit metrics as tuples into the internal edits queue and updates the syntax tree.
Parameters:
Source code in src/elva/widgets/textarea.py
on_delete(range_offset, range_length)
Hook called on an insert action in a document's Y text data type event.
It puts edit metrics as tuples into the internal edits queue and updates the syntax tree.
Parameters:
-
range_offset
(int
) –start index of deletion.
-
range_length
(int
) –length of the text slice to delete.
Source code in src/elva/widgets/textarea.py
__aiter__()
YTextArea(ytext, *args, language=None, **kwargs)
Bases: TextArea
Widget for displaying and manipulating text synchronized in realtime.
Parameters:
-
ytext
(Text
) –Y text data type holding the text.
-
language
(None | str
, default:None
) –syntax language the text is written in.
-
args
(tuple
, default:()
) –positional arguments passed to
TextArea
. -
kwargs
(dict
, default:{}
) –keyword arguments passed to
TextArea
.
Methods:
-
on_mount
–Hook called on mounting.
-
update_btext
–Reference the UTF-8 encoded text in an own class attribute.
-
perform_edits
–Task waiting for edits and perform further steps.
-
edit
–Update the widget's visual appearance from an edit's metrics.
-
replace
–Replace a specified range in the text with another text.
-
delete
–Delete a specified range in the text.
-
insert
–Insert text at a specified location.
-
clear
–Clear the document, i.e. delete all text.
-
render_line
–Render a line of content after updating the wrapped text.
Attributes:
-
btext
(bytes
) –UTF-8 encoded text.
-
document
(YDocument
) –Instance of the document object.
-
wrapped_document
(WrappedDocument
) –Instance of a wrapper class providing wrapping functionality.
-
navigator
(DocumentNavigator
) –Instance of a navigator object responsible for proper cursor placement.
Source code in src/elva/widgets/textarea.py
btext
instance-attribute
UTF-8 encoded text.
document = YDocument(ytext, language)
instance-attribute
Instance of the document object.
wrapped_document = WrappedDocument(self.document)
instance-attribute
Instance of a wrapper class providing wrapping functionality.
navigator = DocumentNavigator(self.wrapped_document)
instance-attribute
Instance of a navigator object responsible for proper cursor placement.
on_mount()
Hook called on mounting.
This starts a tasks waiting for edits and updating the widget's visual appearance.
update_btext()
Reference the UTF-8 encoded text in an own class attribute.
This makes it possible to calculate metrics before and after a new edit.
perform_edits()
async
edit(itop, ibot, btext)
Update the widget's visual appearance from an edit's metrics.
Parameters:
-
itop
(int
) –start index of the edit.
-
ibot
(int
) –end index of the edit.
-
btext
(bytes
) –inserted UTF-8 encoded text.
Source code in src/elva/widgets/textarea.py
_replace_via_keyboard(insert, start, end)
Perform a replacement only when the text area is not read-only.
Parameters:
-
insert
(str
) –the text to be inserted at
start
. -
start
(tuple[int, int]
) –a tuple of row and column coordinates where the replaced text starts.
-
end
(tuple[int, int]
) –a tuple of row and column coordinates where the replaced text ends.
Source code in src/elva/widgets/textarea.py
_delete_via_keyboard(start, end)
Perform a deletion when the text area is not read-only.
Parameters:
-
start
(tuple[int, int]
) –a tuple of row and column coordinates where the deleted text starts.
-
end
(tuple[int, int]
) –a tuple of row and column coordinates where the deleted text ends.
Source code in src/elva/widgets/textarea.py
replace(start, end, text)
Replace a specified range in the text with another text.
Parameters:
-
start
(tuple[int, int]
) –a tuple of row and column coordinates where the replaced text starts.
-
end
(tuple[int, int]
) –a tuple of row and column coordinates where the replaced text ends.
-
text
(str
) –the text to be inserted at
start
.
Source code in src/elva/widgets/textarea.py
delete(start, end)
Delete a specified range in the text.
Parameters:
-
start
(tuple[int, int]
) –a tuple of row and column coordinates where the to be deleted text starts.
-
end
(tuple[int, int]
) –a tuple of row and column coordinates where the to be deleted text ends.
Source code in src/elva/widgets/textarea.py
insert(text, location=None)
Insert text at a specified location.
Parameters:
-
text
(str
) –the text to be inserted.
-
location
(tuple[int, int]
, default:None
) –a tuple of row and column coordinates where the insertion starts.
Source code in src/elva/widgets/textarea.py
clear()
render_line(y)
Render a line of content after updating the wrapped text.
Parameters:
-
y
(int
) –row index of the line to be rendered.
Returns:
-
Strip
–the rendered line.
Source code in src/elva/widgets/textarea.py
ends_with_newline(text)
get_lines(text, keepends=False)
Split a text into its lines.
If text
is empty or ends with a newline character, an empty line is appended to the list of lines.
Parameters:
-
text
(str
) –the text to split into lines.
-
keepends
(bool
, default:False
) –flag whether to keep the newline characters in the line strings.
Returns:
Source code in src/elva/widgets/textarea.py
get_index_from_binary_index(btext, bindex)
Convert the index in UTF-8 encoding to character index.
Parameters:
Returns:
-
int
–index in the UTF-8 decoded form of
btext
.
Source code in src/elva/widgets/textarea.py
get_binary_index_from_index(text, index)
Convert the character index to index in UTF-8 encoding.
Parameters:
Returns:
-
int
–index in the UTF-8 encoded form of
text
.
Source code in src/elva/widgets/textarea.py
get_location_from_index(text, index)
Return the 2D row and column coordinates in text
at position index
.
Parameters:
-
text
(str
) –string where to find the coordinates in.
-
index
(int
) –position in
text
to convert to coordinates.
Returns:
Source code in src/elva/widgets/textarea.py
get_location_from_binary_index(btext, bindex)
Return the 2D row and column coordinates in the UTF-8 decoded form of btext
at position bindex
.
Parameters:
-
btext
(bytes
) –UTF-8 encoded string where to find the coordinates in.
-
bindex
(int
) –position in
btext
to convert to coordinates.
Returns:
Source code in src/elva/widgets/textarea.py
get_index_from_location(text, location)
Convert 2D row and column coordinates to the index position in text
.
Parameters:
-
text
(str
) –the text in which to find the index.
-
location
(tuple[int, int]
) –a tuple of row and column coordinates.
Returns:
-
int
–the index position in
text
at the givenlocation
.
Source code in src/elva/widgets/textarea.py
get_binary_index_from_location(btext, location)
Convert 2D row and column coordinates to the binary index position in btext
.
Parameters:
-
btext
(bytes
) –the UTF-8 encoded text in which to find the binary index.
-
location
(tuple[int, int]
) –a tuple of row and column coordinates.
Returns:
-
int
–the binary index position in
btext
at the givenlocation
.
Source code in src/elva/widgets/textarea.py
get_text_range(text, start, end)
Get the slice in text
between a start
and an end
location.
Parameters:
-
text
(str
) –the text to slice.
-
start
(tuple[int, int]
) –a tuple of row and column coordinates to start the slice at.
-
end
(tuple[int, int]
) –a tuple of row and column coordinates the end the slice at.
Returns:
-
str
–the slice in
text
betweenstart
andend
location.
Source code in src/elva/widgets/textarea.py
get_binary_text_range(btext, start, end)
Get the slice in the UTF-8 encoded btext
between a start
and an end
location.
Parameters:
-
btext
(bytes
) –the UTF-8 encoded text to slice.
-
start
(tuple[int, int]
) –a tuple of row and column coordinates to start the slice at.
-
end
(tuple[int, int]
) –a tuple of row and column coordinates the end the slice at.
Returns:
-
bytes
–the UTF-8 encoded slice in
btext
betweenstart
andend
location.
Source code in src/elva/widgets/textarea.py
update_location(iloc, itop, ibot, iend_edit)
Update a location index with respect to edit metrics.
Parameters:
-
iloc
(int
) –index of the location to be updated.
-
itop
(int
) –index of the start of the edited range.
-
ibot
(int
) –index of the end of the edited range.
-
iend_edit
(int
) –index of the end of the edit's content.
Returns:
-
int
–updated location index.
Source code in src/elva/widgets/textarea.py
get_binary_location_from_binary_index(btext, bindex)
Get the 2D row and column coordinates in UTF-8 encoded text at a given position.
Parameters:
-
btext
(bytes
) –UTF-8 encoded text to find the location in.
-
bindex
(int
) –index position to convert to a location.
Returns: