public class EditorDocument
- Object
- EditorDocument
The plain text backing store shared by the pure Codename One editors. The document keeps the full text in a single buffer addressed by a linear UTF-16 offset space and derives a cached line index (the start offset of every logical line) on demand for rendering and hit testing.
A single buffer keeps offset arithmetic trivial (offsets are direct buffer indices) which is exactly what the caret, selection, undo stack and the platform text input source all operate on. Line oriented feature layers (syntax highlighting, the gutter) sit on top of the line index.
Constructors
public EditorDocument() | Creates an empty document. |
public EditorDocument(String text) | Creates a document initialized with the supplied text. |
Methods
public static String normalizeText(String text) | Normalizes platform line endings into the document’s canonical LF representation. |
public int length() | Returns the total number of UTF-16 characters in the document. |
public String getText() | Returns the full document text. |
public char charAt(int offset) | Returns the character at the given offset. |
public String substring(int start, int end) | Returns the substring in [start, end). |
public void setText(String text) | Replaces the entire document content. |
public void insert(int offset, String text) | Inserts text at the given offset. |
public void delete(int start, int end) | Deletes the range [start, end). |
public int clamp(int offset) | Clamps an arbitrary offset into the valid range [0, length()]. |
public int getLineCount() | Returns the number of logical lines (always at least one). |
public int getLineStart(int line) | Returns the start offset of the given line. |
public int getLineEnd(int line) | Returns the end offset (exclusive, not counting the trailing newline) of the given line. |
public String getLineText(int line) | Returns the text of the given line without its trailing newline. |
public int lineOfOffset(int offset) | Returns the zero based line index containing the given offset. |
public int columnOfOffset(int offset) | Returns the column (offset within its line) for the given absolute offset. |
Inherited methods
Constructor details
EditorDocument
public EditorDocument()Creates an empty document.
EditorDocument
public EditorDocument(String text)Creates a document initialized with the supplied text.
Parameters
textString- the initial text, or null for an empty document
Method details
normalizeText
public static String normalizeText(String text)Normalizes platform line endings into the document’s canonical LF representation.
length
public int length()Returns the total number of UTF-16 characters in the document.
getText
public String getText()Returns the full document text.
charAt
public char charAt(int offset)Returns the character at the given offset.
Parameters
offsetint- a valid offset in
[0, length())
substring
public String substring(int start, int end)Returns the substring in
[start, end).setText
public void setText(String text)Replaces the entire document content.
Parameters
textString- the new text, or null to clear the document
insert
public void insert(int offset, String text)Inserts text at the given offset.
Parameters
offsetint- the insertion offset in
[0, length()] textString- the text to insert
delete
public void delete(int start, int end)Deletes the range
[start, end).Parameters
startint- inclusive start offset
endint- exclusive end offset
clamp
public int clamp(int offset)Clamps an arbitrary offset into the valid range
[0, length()].getLineCount
public int getLineCount()Returns the number of logical lines (always at least one).
getLineStart
public int getLineStart(int line)Returns the start offset of the given line.
Parameters
lineint- a zero based line index in
[0, getLineCount())
getLineEnd
public int getLineEnd(int line)Returns the end offset (exclusive, not counting the trailing newline) of the given line.
Parameters
lineint- a zero based line index in
[0, getLineCount())
getLineText
public String getLineText(int line)Returns the text of the given line without its trailing newline.
Parameters
lineint- a zero based line index in
[0, getLineCount())
lineOfOffset
public int lineOfOffset(int offset)Returns the zero based line index containing the given offset.
Parameters
offsetint- an offset in
[0, length()]
columnOfOffset
public int columnOfOffset(int offset)Returns the column (offset within its line) for the given absolute offset.
Parameters
offsetint- an offset in
[0, length()]