TextDocument

A TextDocument represents an open text document in the application. Text document objects are not directly mutable, requiring a TextEditor object to make modifications. Text documents are not one-to-one to text editors, since multiple editors may be open for a single document.

Properties

uri

Returns the document’s unique URI, as a String. The URI is guaranteed to exist for all documents, but may change if the document is moved. Unsaved documents have a URI with an unsaved:// scheme.

This property is readonly.

path

Returns the document’s path, as a String. If the document is remote, this will be the path on the relevant server. If the document is unsaved, this may be null or undefined.

This property is readonly.

isRemote

Returns true if the document is a remote document.

This property is readonly.

isDirty

Returns true if the document has modifications that are not yet saved to disk.

This property is readonly.

isEmpty

Returns true if the document contains no text.

This property is readonly.

isUntitled

Returns true if the document has not yet been saved to disk, and does not have a path.

This property is readonly.

isClosed

Returns true if the document has been closed. Closed documents are no longer interact-able.

This property is readonly.

eol

Returns, as a String, the default line ending string used by the document.

This property is readonly.

length

Returns the length of the document in characters.

This property is readonly.

syntax

Returns the identifier for the document’s language syntax. If the document is plain text, this may be null or undefined.

This property is readonly.

Methods

getTextInRange(range)

Returns a section of the document’s text indicated by a provided Range as a String. If the range exceeds the receiver’s bounds, an Error will be thrown.

getLineRangeForRange(range)

Given a Range, this method will return the range of all lines encompassing that range. If the range exceeds the receiver’s bounds, an Error will be thrown.

onDidChangePath(callback)

Adds an event listener that invokes the provided callback when the document’s path changes. The callback will receive as an argument the document object and the new path (or null if the document does not have a path).

onDidChangeSyntax(callback)

Adds an event listener that invokes the provided callback when the document’s syntax (language) changes. The callback will receive as an argument the document object and the new syntax name (or null if the document does not have a syntax / is plain text).