This document describes the C# wrapper in platform/CSharp/SweetLine.
The WinForms/.NET wrapper is a P/Invoke binding over the SweetLine C API:
- Native binding and library resolver:
SweetLineNative.cs - Managed API (
HighlightEngine,TextAnalyzer,DocumentAnalyzer,Document):SweetLine.cs - Data models (
TokenSpan,DocumentHighlight,IndentGuideResult, ...):Models.cs
dotnet add package SweetLine --version 1.2.4Or in .csproj:
<ItemGroup>
<PackageReference Include="SweetLine" Version="1.2.4" />
</ItemGroup>HighlightConfig: engine options (ShowIndex,InlineStyle)HighlightEngine: compile syntax, create analyzers, load documentsTextAnalyzer: full-text/single-line analysisDocumentAnalyzer: full and incremental document analysisDocument: managed document handle used for incremental updatesSyntaxCompileError: thrown when syntax compile fails
using SweetLine;
var config = new HighlightConfig(showIndex: true, inlineStyle: false);
using var engine = new HighlightEngine(config);
engine.RegisterStyleName("keyword", 1);
engine.RegisterStyleName("string", 2);
engine.CompileSyntaxFromFile("syntaxes/csharp.json");
using var textAnalyzer = engine.CreateAnalyzerByFileName("Example.cs");
DocumentHighlight preview = textAnalyzer!.AnalyzeText("public class Demo {}");
using var document = new Document("file:///example.cs", "public class Demo {}");
using var analyzer = engine.LoadDocument(document);
if (analyzer != null)
{
DocumentHighlight result = analyzer.Analyze();
IndentGuideResult guides = analyzer.AnalyzeIndentGuides();
}AnalyzeLineRange(...) analyzes enough lines from the current document state to satisfy the requested visible range.
AnalyzeIncrementalInLineRange(...) applies a patch and returns the requested slice immediately.
GetHighlightSlice(...) reads a visible slice from the latest cached result after Analyze() or AnalyzeIncremental(...).
var changeRange = new TextRange(
new TextPosition(line: 2, column: 4),
new TextPosition(line: 2, column: 8));
DocumentHighlight updated = analyzer!.AnalyzeIncremental(changeRange, "modified");
DocumentHighlightSlice analyzed = analyzer.AnalyzeLineRange(
new LineRange(startLine: 0, lineCount: 120));
DocumentHighlightSlice visibleFromCache = analyzer.GetHighlightSlice(
new LineRange(startLine: 0, lineCount: 120));
DocumentHighlightSlice visible = analyzer.AnalyzeIncrementalInLineRange(
changeRange,
"modified",
new LineRange(startLine: 0, lineCount: 120));- Call
Dispose()/usingforHighlightEngineandDocument. TextAnalyzerandDocumentAnalyzerhandles are managed by the engine; they still implementIDisposablefor lifecycle consistency.- Native library lookup:
SWEETLINE_LIB_PATH(file path or directory)- app directory / runtime native directories
- system default native resolution