Advanced usage patterns and architectural considerations for TextAPI.
| Navigation: Overview | Core API | Examples | Diff |
The document is backed by a piece table data structure that maintains O(log n) insert/delete performance:
// Access the piece table directly for performance-critical scenarios
var pieceTable = doc.PieceTable;
// Search directly on the piece table without materializing
var searcher = new TextSearcher(pieceTable);
var matches = searcher.FindAll("pattern");
Implement your own syntax tokenizer for custom languages:
public class MyTokenizer : ITokenizer
{
public string LanguageId => "mylang";
public IReadOnlyList<Token> Tokenize(string text, int? maxTokens = null)
{
// Implement tokenization
return tokens;
}
}
var doc = new TextDocument(new MyTokenizer());
For very large documents, consider:
doc.Compact() after many edits to consolidate pieces// Reduce piece tree depth after heavy editing
doc.Compact();
// Check memory usage
var stats = doc.GetStats();
Console.WriteLine($"Pieces: {stats.PieceCount}");
Compare documents efficiently:
var oldDoc = new TextDocument();
oldDoc.Load(File.ReadAllText("v1.txt"));
var newDoc = new TextDocument();
newDoc.Load(File.ReadAllText("v2.txt"));
var diffResult = TextDiff.Diff(oldDoc, newDoc);
Console.WriteLine(diffResult.ToUnifiedDiff());
Layer multiple decoration types for rich editing experiences:
// Syntax highlighting
doc.AddDecoration(start, end, DecorationType.SyntaxHighlight, tag: "keyword");
// Error diagnostics
doc.AddDecoration(errStart, errEnd, DecorationType.ErrorSquiggle, tag: "CS0101");
// Search results
doc.AddDecoration(matchStart, matchEnd, DecorationType.SearchMatch);
Monitor what changed:
// Enable change tracking
doc.IsModified = false; // reset flag
doc.Insert(0, "text");
Console.WriteLine(doc.IsModified); // true
doc.Undo();
Console.WriteLine(doc.IsModified); // false (back to saved state)