Skip to content

Latest commit

 

History

History
95 lines (67 loc) · 2.47 KB

README.md

File metadata and controls

95 lines (67 loc) · 2.47 KB

SweetLine for Java 22

SweetLine for Java 22 is an FFM binding over the native SweetLine C API. The package bundles native libraries for supported desktop targets and includes a Swing demo.

Installation

Maven

<dependency>
  <groupId>com.qiplat</groupId>
  <artifactId>sweetline-ffm</artifactId>
  <version>1.3.2</version>
</dependency>

Gradle

implementation "com.qiplat:sweetline-ffm:1.3.2"

Requirements

  • JDK 22
  • --enable-native-access=ALL-UNNAMED at runtime

Quick Start

import com.qiplat.sweetline.*;

try (HighlightEngine engine = new HighlightEngine(new HighlightConfig(true, false, 4))) {
    engine.compileSyntaxFromFile("syntaxes/java.json");

    try (TextAnalyzer analyzer = engine.createAnalyzerByFileName("Demo.java")) {
        if (analyzer != null) {
            DocumentHighlight result = analyzer.analyzeText("public class Demo {}");
        }
    }
}

Managed Documents

Use HighlightEngine.removeDocument(uri) after closing the document analyzer to remove the document and its cached analyzer from the engine.

String uri = "file:///Demo.java";

try (HighlightEngine engine = new HighlightEngine();
     Document document = new Document(uri, sourceCode)) {
    engine.compileSyntaxFromFile("syntaxes/java.json");

    DocumentAnalyzer loaded = engine.loadDocument(document);
    if (loaded != null) {
        try (DocumentAnalyzer analyzer = loaded) {
            DocumentHighlight initial = analyzer.analyze();
            DocumentHighlightSlice visible = analyzer.getHighlightSlice(
                    new LineRange(0, 80));
        }
    }

    engine.removeDocument(uri);
}

Resource Management

HighlightEngine, Document, TextAnalyzer, and DocumentAnalyzer implement AutoCloseable. Prefer try-with-resources. Each class also provides a finalize() fallback for objects that were not closed explicitly, but its execution timing is not guaranteed.

For managed documents, release resources in this order:

  • close DocumentAnalyzer
  • call HighlightEngine.removeDocument(uri)
  • close Document
  • close HighlightEngine

Native Loading

Native loading checks the sweetline.lib.path system property, common build output directories, bundled JAR resources, and java.library.path.

-Dsweetline.lib.path=/path/to/native/lib/dir

Build and Run

cd platform/Java22
.\gradlew.bat :sweetline:build
.\gradlew.bat :demo:run

See CHANGELOG.md for release notes.