在RichTextFx CodeArea中的行号处显示断点

问题描述 投票:7回答:1

我正在使用RichTextFx的CodeArea来突出自定义迷你语言代码。

现在,在执行此代码时,我想在当前执行的行前面显示一个小箭头。我知道具体的行号,但行号标签不能发生任何事情。

由于github项目声称显示行号或断点切换作为一个功能,这可能不是很困难。但无法得到任何工作......

提前致谢

javafx breakpoints line-numbers richtextfx
1个回答
13
投票

要显示该行前面的任何图形,您需要设置CodeArea的“段落图形工厂”。这个图形工厂只是一个函数int -> Node:给定行号,它返回一个将显示在行前面的Node

这是一个图形工厂,生成一个指向该线的绿色三角形。只有当行等于给定的整数属性shownLine时才会显示它。

class ArrowFactory implements IntFunction<Node> {
    private final ObservableValue<Integer> shownLine;

    ArrowFactory(ObservableValue<Integer> shownLine) {
        this.shownLine = shownLine;
    }

    @Override
    public Node apply(int lineNumber) {
        Polygon triangle = new Polygon(0.0, 0.0, 10.0, 5.0, 0.0, 10.0);
        triangle.setFill(Color.GREEN);

        ObservableValue<Boolean> visible = Val.map(
                shownLine,
                sl -> sl == lineNumber);

        triangle.visibleProperty().bind(visible.conditionOnShowing(t‌​riangle));

        return triangle;
    }
}

您创建的每个图形(即小绿色三角形)将观察给定的shownLine属性以确定它是否应该可见。由于线条以及线条图形的来去,重要的是在不再使用图形时删除shownLine的监听器。 visible.conditionOnShowing(t‌​riangle)是一个新属性,它将停止观察visible属性(并且由于ReactFX的惰性绑定语义而自动也是shownLine属性)并且每当三角形不是显示窗口的一部分时重置为常量false。因此,我们不会因未清理的侦听器而导致内存或CPU泄漏。

这是一个完整的可运行演示,它使用这个ArrowFactory与RichTextFX提供的LineNumberFactory结合显示行号和一个小三角形。此演示使用CodeArea的当前行作为shownLine属性。您将希望将其替换为包含当前执行行的属性。

import java.util.function.IntFunction;

import javafx.application.Application;
import javafx.beans.value.ObservableValue;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Polygon;
import javafx.stage.Stage;

import org.fxmisc.richtext.CodeArea;
import org.fxmisc.richtext.LineNumberFactory;
import org.reactfx.value.Val;

public class CodeAreaWithLineIndicator extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {
        CodeArea codeArea = new CodeArea();

        IntFunction<Node> numberFactory = LineNumberFactory.get(codeArea);
        IntFunction<Node> arrowFactory = new ArrowFactory(codeArea.currentParagraphProperty());
        IntFunction<Node> graphicFactory = line -> {
            HBox hbox = new HBox(
                numberFactory.apply(line),
                arrowFactory.apply(line));
            hbox.setAlignment(Pos.CENTER_LEFT);
            return hbox;
        };
        codeArea.setParagraphGraphicFactory(graphicFactory);

        primaryStage.setScene(new Scene(new StackPane(codeArea), 600, 400));
        primaryStage.show();
    }
}

class ArrowFactory implements IntFunction<Node> {
    private final ObservableValue<Integer> shownLine;

    ArrowFactory(ObservableValue<Integer> shownLine) {
        this.shownLine = shownLine;
    }

    @Override
    public Node apply(int lineNumber) {
        Polygon triangle = new Polygon(0.0, 0.0, 10.0, 5.0, 0.0, 10.0);
        triangle.setFill(Color.GREEN);

        ObservableValue<Boolean> visible = Val.map(
                shownLine,
                sl -> sl == lineNumber);

        triangle.visibleProperty().bind(visible.conditionOnShowing(t‌​riangle));

        return triangle;
    }
}

这就是结果:

© www.soinside.com 2019 - 2024. All rights reserved.