public interface Document extends UserDataHolder
\n
characters,
and are converted to proper format when the document is saved.
Please see IntelliJ Platform Architectural Overview
for high-level overview.Modifier and Type | Field and Description |
---|---|
static Document[] |
EMPTY_ARRAY |
static java.lang.String |
PROP_WRITABLE |
Modifier and Type | Method and Description |
---|---|
default void |
addDocumentListener(DocumentListener listener)
Adds a listener for receiving notifications about changes in the document content.
|
default void |
addDocumentListener(DocumentListener listener,
Disposable parentDisposable) |
default void |
addPropertyChangeListener(java.beans.PropertyChangeListener listener)
Adds a listener for receiving notifications about changes in the properties of the document
(for example, its read-only state).
|
RangeMarker |
createGuardedBlock(int startOffset,
int endOffset)
Marks a range of text in the document as read-only (attempts to modify text in the
range cause
ReadOnlyFragmentModificationException to be thrown). |
default RangeMarker |
createRangeMarker(int startOffset,
int endOffset)
Creates a range marker which points to the specified range of text in the document and
is automatically adjusted when the document text is changed.
|
RangeMarker |
createRangeMarker(int startOffset,
int endOffset,
boolean surviveOnExternalChange)
Creates a range marker which points to the specified range of text in the document and
is automatically adjusted when the document text is changed.
|
default RangeMarker |
createRangeMarker(TextRange textRange) |
void |
deleteString(int startOffset,
int endOffset)
Deletes the specified range of text from the document.
|
default void |
fireReadOnlyModificationAttempt()
Fires a notification that the user would like to remove the read-only state
from the document (the read-only state can be removed by checking the file out
from the version control system, or by clearing the read-only attribute on the file).
|
default char [] |
getChars()
Deprecated.
Use
getCharsSequence() or getText() instead. |
default java.lang.CharSequence |
getCharsSequence()
Use this method instead of
getText() if you do not need to create a copy of the content. |
java.lang.CharSequence |
getImmutableCharSequence() |
int |
getLineCount()
Returns the number of lines in the document.
|
int |
getLineEndOffset(int line)
Returns the end offset for the line with the specified number.
|
int |
getLineNumber(int offset)
Returns the line number (0-based) corresponding to the specified offset in the document.
|
default int |
getLineSeparatorLength(int line) |
int |
getLineStartOffset(int line)
Returns the start offset for the line with the specified number.
|
long |
getModificationStamp()
Gets the modification stamp value.
|
default RangeMarker |
getOffsetGuard(int offset)
Returns the read-only marker covering the specified offset in the document.
|
default RangeMarker |
getRangeGuard(int start,
int end)
Returns the read-only marker covering the specified range in the document.
|
default java.lang.String |
getText()
Retrieves a copy of the document content.
|
default java.lang.String |
getText(TextRange range) |
default int |
getTextLength()
Returns the length of the document text.
|
void |
insertString(int offset,
java.lang.CharSequence s)
Inserts the specified text at the specified offset in the document.
|
default boolean |
isInBulkUpdate() |
default boolean |
isLineModified(int line) |
boolean |
isWritable()
Checks if the document text is read-only.
|
default void |
removeDocumentListener(DocumentListener listener)
Removes a listener for receiving notifications about changes in the document content, previously added via
addDocumentListener(DocumentListener) . |
default void |
removeGuardedBlock(RangeMarker block)
Removes a marker marking a range of text in the document as read-only.
|
default void |
removePropertyChangeListener(java.beans.PropertyChangeListener listener)
Removes a listener for receiving notifications about changes in the properties of the document
(for example, its read-only state).
|
void |
replaceString(int startOffset,
int endOffset,
java.lang.CharSequence s)
Replaces the specified range of text in the document with the specified string.
|
default void |
setCyclicBufferSize(int bufferSize)
Sets the maximum size of the cyclic buffer used for the document.
|
default void |
setInBulkUpdate(boolean value)
Enters or exits 'bulk' mode for processing of document changes.
|
default void |
setReadOnly(boolean isReadOnly)
Marks the document as read-only or read/write.
|
void |
setText(java.lang.CharSequence text) |
default void |
startGuardedBlockChecking()
Enables checking for read-only markers when the document is modified.
|
default void |
stopGuardedBlockChecking()
Disables checking for read-only markers when the document is modified.
|
getUserData, putUserData
static final Document[] EMPTY_ARRAY
static final java.lang.String PROP_WRITABLE
default java.lang.String getText()
getCharsSequence()
whenever it's possible.default java.lang.String getText(TextRange range)
default java.lang.CharSequence getCharsSequence()
getText()
if you do not need to create a copy of the content.
Content represented by returned CharSequence is subject to change whenever document is modified via delete/replace/insertString method
calls. It is necessary to obtain Application.runWriteAction() to modify content of the document though so threading issues won't
arise.getTextLength()
java.lang.CharSequence getImmutableCharSequence()
ImmutableCharSequence
@Deprecated default char [] getChars()
getCharsSequence()
or getText()
instead.default int getTextLength()
getCharsSequence()
int getLineCount()
int getLineNumber(int offset)
offset
- the offset to get the line number for (must be in the range from 0 to
getTextLength()-1)int getLineStartOffset(int line)
line
- the line number (from 0 to getLineCount()-1)int getLineEndOffset(int line)
line
- the line number (from 0 to getLineCount()-1)default boolean isLineModified(int line)
void insertString(int offset, java.lang.CharSequence s)
offset
- the offset to insert the text at.s
- the text to insert.ReadOnlyModificationException
- if the document is read-only.ReadOnlyFragmentModificationException
- if the fragment to be modified is covered by a guarded block.void deleteString(int startOffset, int endOffset)
startOffset
- the start offset of the range to delete.endOffset
- the end offset of the range to delete.ReadOnlyModificationException
- if the document is read-only.ReadOnlyFragmentModificationException
- if the fragment to be modified is covered by a guarded block.void replaceString(int startOffset, int endOffset, java.lang.CharSequence s)
startOffset
- the start offset of the range to replace.endOffset
- the end offset of the range to replace.s
- the text to replace with.ReadOnlyModificationException
- if the document is read-only.ReadOnlyFragmentModificationException
- if the fragment to be modified is covered by a guarded block.boolean isWritable()
true
if the document text is writable, false
if it is read-only.fireReadOnlyModificationAttempt()
long getModificationStamp()
PsiFile.getModificationStamp()
,
VirtualFile.getModificationStamp()
default void fireReadOnlyModificationAttempt()
default void addDocumentListener(DocumentListener listener)
listener
- the listener instance.default void addDocumentListener(DocumentListener listener, Disposable parentDisposable)
default void removeDocumentListener(DocumentListener listener)
addDocumentListener(DocumentListener)
.
Don't call this method for listeners added via addDocumentListener(DocumentListener, Disposable)
, as that might cause memory leaks.listener
- the listener instance.default RangeMarker createRangeMarker(int startOffset, int endOffset)
startOffset
- the start offset for the range of text covered by the marker.endOffset
- the end offset for the range of text covered by the marker.RangeMarker createRangeMarker(int startOffset, int endOffset, boolean surviveOnExternalChange)
startOffset
- the start offset for the range of text covered by the marker.endOffset
- the end offset for the range of text covered by the marker.surviveOnExternalChange
- if true, the marker is not invalidated by external changes.default void addPropertyChangeListener(java.beans.PropertyChangeListener listener)
listener
- the listener instance.default void removePropertyChangeListener(java.beans.PropertyChangeListener listener)
listener
- the listener instance.default void setReadOnly(boolean isReadOnly)
isReadOnly
- the new value of the read-only flag.isWritable()
,
fireReadOnlyModificationAttempt()
RangeMarker createGuardedBlock(int startOffset, int endOffset)
ReadOnlyFragmentModificationException
to be thrown).startOffset
- the start offset of the text range to mark as read-only.endOffset
- the end offset of the text range to mark as read-only.removeGuardedBlock(RangeMarker)
,
startGuardedBlockChecking()
,
EditorActionManager.setReadonlyFragmentModificationHandler(com.intellij.openapi.editor.actionSystem.ReadonlyFragmentModificationHandler)
default void removeGuardedBlock(RangeMarker block)
block
- the marker to remove.createGuardedBlock(int, int)
default RangeMarker getOffsetGuard(int offset)
offset
- the offset for which the marker is requested.null
if the specified offset is not covered by a read-only marker.default RangeMarker getRangeGuard(int start, int end)
start
- the start offset of the range for which the marker is requested.end
- the end offset of the range for which the marker is requested.null
if the specified range is not covered by a read-only marker.default void startGuardedBlockChecking()
default void stopGuardedBlockChecking()
default void setCyclicBufferSize(int bufferSize)
bufferSize
- the cyclic buffer size, or 0 if the document should not use a cyclic buffer.void setText(java.lang.CharSequence text)
default RangeMarker createRangeMarker(TextRange textRange)
default int getLineSeparatorLength(int line)
default boolean isInBulkUpdate()
setInBulkUpdate(boolean)
default void setInBulkUpdate(boolean value)
As the reconciliation after exiting bulk mode implies some additional overhead, bulk mode shouldn't be used if the number of document changes to be performed is relatively small. The number of changes which justifies switching to bulk mode is usually determined empirically, but typically it's around hundred(s) of changes.
In bulk mode editor(s) associated with the document will stop updating internal caches on each document change. As a result, certain operations with editor can return invalid results or lead to exception, if they are preformed in bulk mode. They include: querying or updating folding or soft wrap data, editor position recalculation functions (offset to logical position, logical to visual position, etc), querying or updating caret position or selection state.
Bulk mode shouldn't span more than one thread or EDT event. Typically it should turned on/off in a try/finally statement.