This document describes the Java 22 FFM wrapper in platform/Java22/sweetline.
- Package:
com.qiplat.sweetline - Binding style: Java 22 Foreign Function & Memory (FFM) over SweetLine C API
- Demo project:
platform/Java22/demo(Swing)
<dependency>
<groupId>com.qiplat</groupId>
<artifactId>sweetline-ffm</artifactId>
<version>1.2.4</version>
</dependency>// build.gradle
implementation 'com.qiplat:sweetline-ffm:1.2.4'- JDK 22
- Runtime flags:
--enable-preview--enable-native-access=ALL-UNNAMED
- Native library (
sweetline) must be loadable (see Native Loading)
cd platform/Java22
./gradlew :sweetline:build
./gradlew :demo:runHighlightConfig(boolean showIndex, boolean inlineStyle)HighlightEngineDocument(AutoCloseable)TextAnalyzer(AutoCloseable)DocumentAnalyzer(AutoCloseable)TextPosition,TextRange,TextLineInfoTokenSpan,LineHighlight,DocumentHighlightLineRange,DocumentHighlightSliceIndentGuideLine,IndentGuideResult,LineScopeStateSyntaxCompileError
Note:
HighlightConfigin Java 22 currently includesshowIndexandinlineStyle.
public class HighlightEngine implements AutoCloseable {
public HighlightEngine(HighlightConfig config);
public HighlightEngine();
public void registerStyleName(String styleName, int styleId);
public String getStyleName(int styleId);
public void defineMacro(String macroName);
public void undefineMacro(String macroName);
public void compileSyntaxFromJson(String syntaxJson) throws SyntaxCompileError;
public void compileSyntaxFromFile(String path) throws SyntaxCompileError;
public TextAnalyzer createAnalyzerBySyntaxName(String syntaxName);
public TextAnalyzer createAnalyzerByFileName(String fileName);
public DocumentAnalyzer loadDocument(Document document);
public void close();
}public class TextAnalyzer implements AutoCloseable {
public DocumentHighlight analyzeText(String text);
public LineAnalyzeResult analyzeLine(String text, TextLineInfo info);
public IndentGuideResult analyzeIndentGuides(String text);
public void close();
}public class DocumentAnalyzer implements AutoCloseable {
public DocumentHighlight analyze();
public DocumentHighlight analyzeIncremental(TextRange range, String newText);
public DocumentHighlightSlice analyzeLineRange(LineRange visibleRange);
public DocumentHighlightSlice analyzeIncrementalInLineRange(
TextRange range, String newText, LineRange visibleRange);
public DocumentHighlightSlice getHighlightSlice(LineRange visibleRange);
public IndentGuideResult analyzeIndentGuides();
public void close();
}analyzeLineRange(...) analyzes enough lines from the current managed document state to satisfy the requested visible range.
analyzeIncrementalInLineRange(...) applies a patch and immediately returns the requested slice.
getHighlightSlice(...) reads a visible slice from the latest cached result after analyze() or analyzeIncremental(...).
SweetLineNative resolves native library in this order:
- System property
sweetline.lib.path - Common source/build output directories
- JAR resource extraction fallback (
NativeLibraryExtractor.extractToDefaultDir()) java.library.path/System.loadLibrary
You can explicitly set:
-Dsweetline.lib.path=/path/to/native/lib/dirJAR packaging scenario:
import com.qiplat.sweetline.NativeLibraryExtractor;
import java.nio.file.Path;
Path libPath = NativeLibraryExtractor.extractToDefaultDir();import com.qiplat.sweetline.*;
String sourceCode = "public class Demo {}";
try (HighlightEngine engine = new HighlightEngine(new HighlightConfig(true, false))) {
engine.registerStyleName("keyword", 1);
engine.registerStyleName("string", 2);
engine.compileSyntaxFromFile("syntaxes/java.json");
try (TextAnalyzer textAnalyzer = engine.createAnalyzerBySyntaxName("java")) {
if (textAnalyzer != null) {
DocumentHighlight full = textAnalyzer.analyzeText(sourceCode);
}
}
try (Document document = new Document("file:///Demo.java", sourceCode)) {
DocumentAnalyzer analyzer = engine.loadDocument(document);
if (analyzer != null) {
try (analyzer) {
DocumentHighlight initial = analyzer.analyze();
TextRange change = new TextRange(
new TextPosition(0, 13),
new TextPosition(0, 17));
DocumentHighlight updated = analyzer.analyzeIncremental(change, "Sample");
DocumentHighlightSlice cachedVisible = analyzer.getHighlightSlice(
new LineRange(0, 80));
DocumentHighlightSlice visible = analyzer.analyzeIncrementalInLineRange(
change,
"Sample",
new LineRange(0, 80));
IndentGuideResult guides = analyzer.analyzeIndentGuides();
}
}
}
}