A mutable text container with undo/redo support and the ability to annotate logical regions in the text.
You can observe changes in a TextBuffer using methods like ::onDidChange,
::onDidStopChanging, and ::getChangesSinceCheckpoint. These methods report
aggregated buffer updates as arrays of change objects containing the following
fields: oldRange
, newRange
, oldText
, and newText
. The oldText
,
newText
, and newRange
fields are self-explanatory, but the interepretation
of oldRange
is more nuanced:
The reported oldRange
is the range of the replaced text in the original
contents of the buffer irrespective of the spatial impact of any other
reported change. So, for example, if you wanted to apply all the changes made
in a transaction to a clone of the observed buffer, the easiest approach would
be to apply the changes in reverse:
buffer1.onDidChange(([changes](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/changes)) => {
for (const {oldRange, newText} of changes.reverse()) {
buffer2.setTextInRange(oldRange, newText)
}
})
If you needed to apply the changes in the forwards order, you would need to incorporate the impact of preceding changes into the range passed to ::setTextInRange, as follows:
buffer1.onDidChange(([changes](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/changes)) => {
for (const {oldRange, newRange, newText} of changes) {
const rangeToReplace = Range(
newRange.start,
newRange.start.traverse(oldRange.getExtent())
)
buffer2.setTextInRange(rangeToReplace, newText)
}
})
Create a new buffer backed by the given file path.
Argument | Description |
---|---|
|
Either a String path to a local file or (experimentally) a file Object as described by the ::setFile method. |
|
An Object with the following properties: |
|
optional
String The file’s encoding. |
|
optional
A Function that returns a Boolean indicating whether the buffer should be destroyed if its file is deleted. |
Return values |
---|
Returns a Promise that resolves with a TextBuffer instance. |
Create a new buffer backed by the given file path. For better performance, use {TextBuffer.load} instead.
Argument | Description |
---|---|
|
The String file path. |
|
An Object with the following properties: |
|
optional
String The file’s encoding. |
|
optional
A Function that returns a Boolean indicating whether the buffer should be destroyed if its file is deleted. |
Return values |
---|
Returns a TextBuffer instance. |
Restore a TextBuffer based on an earlier state created using the TextBuffer::serialize method.
Argument | Description |
---|---|
|
An Object returned from TextBuffer::serialize |
Return values |
---|
Returns a Promise that resolves with a TextBuffer instance. |
Invoke the given callback synchronously before the content of the buffer changes.
Because observers are invoked synchronously, it’s important not to perform any expensive operations via this method.
Argument | Description |
---|---|
|
Function to be called when the buffer changes. |
Return values |
---|
Returns a Disposable on which |
Invoke the given callback synchronously when a transaction finishes with a list of all the changes in the transaction.
Argument | Description |
---|---|
|
Function to be called when a transaction in which textual changes occurred is completed. |
|
Object with the following keys: |
|
The smallest combined Range containing all of the old text. |
|
The smallest combined Range containing all of the new text. |
|
Array of Objects summarizing the aggregated changes that occurred during the transaction. See Working With Aggregated Changes in the description of the TextBuffer class for details. |
|
The Range of the deleted text in the contents of the buffer as it existed before the batch of changes reported by this event. |
|
The Range of the inserted text in the current contents of the buffer. |
|
A String representing the deleted text. |
|
A String representing the inserted text. |
Return values |
---|
Returns a Disposable on which |
This is now identical to ::onDidChange.
Invoke the given callback asynchronously following one or more changes after ::getStoppedChangingDelay milliseconds elapse without an additional change.
This method can be used to perform potentially expensive operations that don’t need to be performed synchronously. If you need to run your callback synchronously, use ::onDidChange instead.
Argument | Description |
---|---|
|
Function to be called when the buffer stops changing. |
|
Object with the following keys: |
|
An Array containing Objects summarizing the aggregated changes. See Working With Aggregated Changes in the description of the TextBuffer class for details. |
|
The Range of the deleted text in the contents of the buffer as it existed before the batch of changes reported by this event. |
|
The Range of the inserted text in the current contents of the buffer. |
|
A String representing the deleted text. |
|
A String representing the inserted text. |
Return values |
---|
Returns a Disposable on which |
Invoke the given callback when the in-memory contents of the buffer become in conflict with the contents of the file on disk.
Argument | Description |
---|---|
|
Function to be called when the buffer enters conflict. |
Return values |
---|
Returns a Disposable on which |
Invoke the given callback if the value of ::isModified changes.
Argument | Description |
---|---|
|
Function to be called when ::isModified changes. |
|
Boolean indicating whether the buffer is modified. |
Return values |
---|
Returns a Disposable on which |
Invoke the given callback when all marker ::onDidChange
observers have been notified following a change to the buffer.
The order of events following a buffer change is as follows:
::onDidChange
observers
are not notified.TextBuffer::onDidChange
observers are notified.Marker::onDidChange
observers are notified.TextBuffer::onDidUpdateMarkers
observers are notified.Basically, this method gives you a way to take action after both a buffer change and all associated marker changes.
Argument | Description |
---|---|
|
Function to be called after markers are updated. |
Return values |
---|
Returns a Disposable on which |
Invoke the given callback when a marker is created.
Argument | Description |
---|---|
|
Function to be called when a marker is created. |
|
Marker that was created. |
Return values |
---|
Returns a Disposable on which |
Invoke the given callback when the value of ::getPath changes.
Argument | Description |
---|---|
|
Function to be called when the path changes. |
|
String representing the buffer’s current path on disk. |
Return values |
---|
Returns a Disposable on which |
Invoke the given callback when the value of ::getEncoding changes.
Argument | Description |
---|---|
|
Function to be called when the encoding changes. |
|
String character set encoding of the buffer. |
Return values |
---|
Returns a Disposable on which |
Invoke the given callback before the buffer is saved to disk.
Argument | Description |
---|---|
|
Function to be called before the buffer is saved. If this function returns a Promise, then the buffer will not be saved until the promise resolves. |
Return values |
---|
Returns a Disposable on which |
Invoke the given callback after the buffer is saved to disk.
Argument | Description |
---|---|
|
Function to be called after the buffer is saved. |
|
Object with the following keys: |
|
The path to which the buffer was saved. |
Return values |
---|
Returns a Disposable on which |
Invoke the given callback after the file backing the buffer is deleted.
Argument | Description |
---|---|
|
Function to be called after the buffer is deleted. |
Return values |
---|
Returns a Disposable on which |
Invoke the given callback before the buffer is reloaded from the contents of its file on disk.
Argument | Description |
---|---|
|
Function to be called before the buffer is reloaded. |
Return values |
---|
Returns a Disposable on which |
Invoke the given callback after the buffer is reloaded from the contents of its file on disk.
Argument | Description |
---|---|
|
Function to be called after the buffer is reloaded. |
Return values |
---|
Returns a Disposable on which |
Invoke the given callback when the buffer is destroyed.
Argument | Description |
---|---|
|
Function to be called when the buffer is destroyed. |
Return values |
---|
Returns a Disposable on which |
Invoke the given callback when there is an error in watching the file.
Argument | Description |
---|---|
|
Function callback |
|
|
|
Object the error object |
|
Function call this to indicate you have handled the error. The error will not be thrown if this function is called. |
Return values |
---|
Returns a Disposable on which |
Get the number of milliseconds that will elapse without a change before ::onDidStopChanging observers are invoked following a change.
Return values |
---|
Returns a Number. |
Determine if the in-memory contents of the buffer differ from its contents on disk.
If the buffer is unsaved, always returns true
unless the buffer is empty.
Return values |
---|
Returns a Boolean. |
Determine if the in-memory contents of the buffer conflict with the on-disk contents of its associated file.
Return values |
---|
Returns a Boolean. |
Set the path for the buffer’s associated file.
Argument | Description |
---|---|
|
A String representing the new file path |
Sets the character set encoding for this buffer.
Argument | Description |
---|---|
|
The String encoding to use (default: ‘utf8’). |
Get the entire text of the buffer. Avoid using this unless you know that the buffer’s text is reasonably short.
Return values |
---|
Returns a String. |
Get the text of the last line of the buffer, without its line ending.
Return values |
---|
Returns a String. |
Return true if the buffer contains any astral-plane Unicode characters that are encoded as surrogate pairs.
Return values |
---|
Returns a Boolean. |
Replace the current buffer contents by applying a diff based on the given text.
Argument | Description |
---|---|
|
A String containing the new buffer contents. |
Set the text in the given range.
Argument | Description |
---|---|
|
A Range |
|
A String |
|
optional |
|
optional
Boolean (default: true) |
|
optional
Deprecated String ‘skip’ will cause this change to be grouped with the preceding change for the purposes of undo and redo. This property is deprecated. Call groupLastChanges() on the buffer after instead. |
Return values |
---|
Returns the Range of the inserted text. |
Insert text at the given position.
Argument | Description |
---|---|
|
A Point representing the insertion location. The position is clipped before insertion. |
|
A String representing the text to insert. |
|
optional |
|
optional
Boolean (default: true) |
|
optional
Deprecated String ‘skip’ will skip the undo system. This property is deprecated. Call groupLastChanges() on the TextBuffer afterward instead. |
Return values |
---|
Returns the Range of the inserted text. |
Append text to the end of the buffer.
Argument | Description |
---|---|
|
A String representing the text text to append. |
|
optional |
|
optional
Boolean (default: true) |
|
optional
Deprecated String ‘skip’ will skip the undo system. This property is deprecated. Call groupLastChanges() on the TextBuffer afterward instead. |
Return values |
---|
Returns the Range of the inserted text |
Delete the lines associated with the specified 0-indexed row range.
If the row range is out of bounds, it will be clipped. If the startRow
is
greater than the endRow
, they will be reordered.
Argument | Description |
---|---|
|
A Number representing the first row to delete. |
|
A Number representing the last row to delete, inclusive. |
Return values |
---|
Returns the Range of the deleted text. |
Create a layer to contain a set of related markers.
Argument | Description |
---|---|
|
optional
An Object contaning the following keys: |
|
optional
A Boolean indicating whether or not the state of this layer should be restored on undo/redo operations. Defaults to |
|
optional
A Boolean indicating whether or not this marker layer should be serialized and deserialized along with the rest of the buffer. Defaults to |
|
optional
A String indicating role of this marker layer |
Return values |
---|
Returns a MarkerLayer. |
Get a MarkerLayer by id.
Argument | Description |
---|---|
|
The id of the marker layer to retrieve. |
Return values |
---|
Returns a MarkerLayer or |
Get the default MarkerLayer.
All Marker APIs not tied to an explicit layer interact with this default layer.
Return values |
---|
Returns a MarkerLayer. |
Create a Marker with the given range in the default MarkerLayer. This marker will maintain its logical location as the buffer is changed, so if you mark a particular word, the marker will remain over that word even if the word’s location in the buffer changes.
Argument | Description |
---|---|
|
|
|
optional
A hash of key-value pairs to associate with the marker. There are also reserved property names that have marker-specific meaning. |
|
optional
Boolean Creates the marker in a reversed orientation. (default: false) |
|
optional
String Determines the rules by which changes to the buffer invalidate the marker. (default: ‘overlap’) It can be any of the following strategies, in order of fragility:
|
|
optional
Boolean indicating whether insertions at the start or end of the marked range should be interpreted as happening outside the marker. Defaults to |
Return values |
---|
Returns a Marker. |
Create a Marker at the given position with no tail in the default marker layer.
Argument | Description |
---|---|
|
|
|
optional
An Object with the following keys: |
|
optional
String Determines the rules by which changes to the buffer invalidate the marker. (default: ‘overlap’) It can be any of the following strategies, in order of fragility:
|
|
optional
Boolean indicating whether insertions at the start or end of the marked range should be interpreted as happening outside the marker. Defaults to |
Return values |
---|
Returns a Marker. |
Find markers conforming to the given parameters in the default marker layer.
Markers are sorted based on their position in the buffer. If two markers start at the same position, the larger marker comes first.
Argument | Description |
---|---|
|
A hash of key-value pairs constraining the set of returned markers. You can query against custom marker properties by listing the desired key-value pairs here. In addition, the following keys are reserved and have special semantics: |
|
Only include markers that start at the given Point. |
|
Only include markers that end at the given Point. |
|
Only include markers that start inside the given Range. |
|
Only include markers that end inside the given Range. |
|
Only include markers that contain the given Point, inclusive. |
|
Only include markers that contain the given Range, inclusive. |
|
Only include markers that start at the given row Number. |
|
Only include markers that end at the given row Number. |
|
Only include markers that intersect the given row Number. |
Return values |
---|
Batch multiple operations as a single undo/redo step.
Any group of operations that are logically grouped from the perspective of undoing and redoing should be performed in a transaction. If you want to abort the transaction, call ::abortTransaction to terminate the function’s execution and revert any changes performed up to the abortion.
Argument | Description |
---|---|
|
optional |
|
optional
The Number of milliseconds for which this transaction should be considered ‘open for grouping’ after it begins. If a transaction with a positive |
|
optional
When provided, skip taking snapshot for other selections markerLayers except given one. |
|
optional
The Number of milliseconds for which this transaction should be considered ‘open for grouping’ after it begins. If a transaction with a positive |
|
A Function to call inside the transaction. |
Abort the currently running transaction
Only intended to be called within the fn
option to ::transact
Clear the undo stack.
Create a pointer to the current state of the buffer for use with ::revertToCheckpoint and ::groupChangesSinceCheckpoint.
Argument | Description |
---|---|
|
optional |
|
optional
When provided, skip taking snapshot for other selections markerLayers except given one. |
Return values |
---|
Returns a checkpoint id value. |
Revert the buffer to the state it was in when the given checkpoint was created.
The redo stack will be empty following this operation, so changes since the
checkpoint will be lost. If the given checkpoint is no longer present in the
undo history, no changes will be made to the buffer and this method will
return false
.
Argument | Description |
---|---|
|
Number id of the checkpoint to revert to. |
|
optional |
|
optional
Restore snapshot of selections marker layer to given selectionsMarkerLayer. |
Return values |
---|
Returns a Boolean indicating whether the operation succeeded. |
Group all changes since the given checkpoint into a single transaction for purposes of undo/redo.
If the given checkpoint is no longer present in the undo history, no
grouping will be performed and this method will return false
.
Argument | Description |
---|---|
|
Number id of the checkpoint to group changes since. |
|
optional |
|
optional
When provided, skip taking snapshot for other selections markerLayers except given one. |
Return values |
---|
Returns a Boolean indicating whether the operation succeeded. |
Group the last two text changes for purposes of undo/redo.
This operation will only succeed if there are two changes on the undo stack. It will not group past the beginning of an open transaction.
Return values |
---|
Returns a Boolean indicating whether the operation succeeded. |
If the given checkpoint is no longer present in the undo history, this method will return an empty Array.
Argument | Description |
---|---|
|
Number id of the checkpoint to get changes since. |
Return values |
---|
Returns a list of changes since the given checkpoint. |
Returns an Array of Objects with the following fields that summarize the aggregated changes since the checkpoint. See Working With Aggregated Changes in the description of the TextBuffer class for details. |
Scan regular expression matches in the entire buffer, calling the given iterator function on each match.
If you’re programmatically modifying the results, you may want to try ::backwardsScan to avoid tripping over your own changes.
Argument | Description |
---|---|
|
A RegExp to search for. |
|
optional |
|
Number default |
|
Number default |
|
A Function that’s called on each match with an Object containing the following keys: |
|
The current regular expression match. |
|
A String with the text of the match. |
|
The Range of the match. |
|
Call this Function to terminate the scan. |
|
|
|
An Array with |
|
An Array with |
Scan regular expression matches in the entire buffer in reverse order, calling the given iterator function on each match.
Argument | Description |
---|---|
|
A RegExp to search for. |
|
optional |
|
Number default |
|
Number default |
|
A Function that’s called on each match with an Object containing the following keys: |
|
The current regular expression match. |
|
A String with the text of the match. |
|
The Range of the match. |
|
Call this Function to terminate the scan. |
|
|
|
An Array with |
|
An Array with |
Scan regular expression matches in a given range , calling the given iterator function on each match.
Argument | Description |
---|---|
|
A RegExp to search for. |
|
A Range in which to search. |
|
optional |
|
Number default |
|
Number default |
|
A Function that’s called on each match with an Object containing the following keys: |
|
The current regular expression match. |
|
A String with the text of the match. |
|
The Range of the match. |
|
Call this Function to terminate the scan. |
|
|
|
An Array with |
|
An Array with |
Scan regular expression matches in a given range in reverse order, calling the given iterator function on each match.
Argument | Description |
---|---|
|
A RegExp to search for. |
|
A Range in which to search. |
|
optional |
|
Number default |
|
Number default |
|
A Function that’s called on each match with an Object containing the following keys: |
|
The current regular expression match. |
|
A String with the text of the match. |
|
The Range of the match. |
|
Call this Function to terminate the scan. |
|
Get the maximal position in the buffer, where new text would be appended.
Return values |
---|
Returns a Point. |
Get the length of the buffer’s text.
Clip the given range so it starts and ends at valid positions.
For example, the position [1, 100]
is out of bounds if the line at row 1 is
only 10 characters long, and it would be clipped to (1, 10)
.
Argument | Description |
---|---|
|
Return values |
---|
Returns the given Range if it is already in bounds, or a new clipped Range if the given range is out-of-bounds. |
Clip the given point so it is at a valid position in the buffer.
For example, the position (1, 100) is out of bounds if the line at row 1 is only 10 characters long, and it would be clipped to (1, 10)
Argument | Description |
---|---|
|
Return values |
---|
Returns a new Point if the given position is invalid, otherwise returns the given position. |
Save the buffer at a specific path.
Argument | Description |
---|---|
|
The path to save at. |
Return values |
---|
Returns a Promise that resolves when the save has completed. |
Reload the file’s content from disk.
Return values |
---|
Returns a Promise that resolves when the load is complete. |