public class EditorDocument

  1. Object
  2. 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

text String
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

offset int
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

text String
the new text, or null to clear the document

insert

public void insert(int offset, String text)
Inserts text at the given offset.

Parameters

offset int
the insertion offset in [0, length()]
text String
the text to insert

delete

public void delete(int start, int end)
Deletes the range [start, end).

Parameters

start int
inclusive start offset
end int
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

line int
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

line int
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

line int
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

offset int
an offset in [0, length()]

columnOfOffset

public int columnOfOffset(int offset)
Returns the column (offset within its line) for the given absolute offset.

Parameters

offset int
an offset in [0, length()]