Snapshots are immutable objects that represent the entire state of a document at a specific point in time. Snapshots are created whenever changes are made to the document’s content (for example, when typing in the xamSyntaxEditor’s editing area). The TextDocument’s CurrentSnapshot property contains the most recent snapshot, which returns an object of type TextDocumentSnapshot.
The snapshots are thread-safe, so they can be used for longer running operations, without having to worry about dealing with any changes made to the document during the process.
Terms:
Term
Description
Offset
This is a zero-based index of a character in a document starting from the beginning.
Text Location
This is a structure, which contains zero-based line and character offset that uniquely identify a character location in a document.
Token
Produced during the lexical analysis of the document tokens represent sections of the text (for example, keyword, punctuations characters, whitespace, etc…). By concatenating the text of all of its tokens, you can reconstruct the entire text of a document.
Word
Unlike the token, the Word structure can only represent a word (not punctuation characters, whitespace or delimiters).
Properties summary
The following table lists the available read-only properties for obtaining document information:
You can use the indexer of the TextDocumentSnapshot class to access the characters at a zero-based offset.
Note
Note
You can also use the Find and FindAll methods of the snapshots to find single or multiple occurrences of a text by using search criteria similar to the one explained in the Find / Replace topic.
Snapshot Scanner
Overview
The snapshot’s CreateScanner method creates a TextDocumentSnapshotScanner class targeting a specific instance of a TextDocumentSnaphot. It is useful for efficiently scanning through the snapshot’s lines, tokens and words.
Note
Note
Even though the snapshot that it targets is thread-safe, the scanner is not. Create the scanner on the same thread that uses it. Creating a scanner on one thread and trying to access the properties and methods of a scanner created on another thread raises an InvalidOperationException.
When first creating the scanner its current scan position initializes to zero. The scan position can be explicitly set (via the CurrentOffset property) or manipulated by calling any of the following “seek” methods. There are also several “peek” methods, which are useful in obtain Tokens or Words surrounding the current scan position, without changing the current scan position.
Properties summary
You can use the following properties to obtain different information regarding the current scan position: