Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

import java.util.Collection;
import java.util.Collections;
import java.util.function.IntFunction;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -85,8 +86,9 @@ public static void main(String[] args) {
public void start(Stage primaryStage) {
CodeArea codeArea = new CodeArea();
String stylesheet = JavaKeywords.class.getResource("java-keywords.css").toExternalForm();
IntFunction<String> format = (digits -> " %" + digits + "d ");

codeArea.setParagraphGraphicFactory(LineNumberFactory.get(codeArea, stylesheet));
codeArea.setParagraphGraphicFactory(LineNumberFactory.get(codeArea, format, stylesheet));
codeArea.textProperty().addListener((obs, oldText, newText) -> {
codeArea.setStyleSpans(0, computeHighlighting(newText));
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.fxmisc.richtext;

import java.util.function.IntFunction;
import java.util.function.Supplier;

import javafx.scene.Node;
import javafx.scene.control.Label;
Expand All @@ -20,11 +19,24 @@ public static IntFunction<Node> get(StyledTextArea<?> area) {
return new LineNumberFactory(area, STYLESHEET);
}

public static IntFunction<Node> get(StyledTextArea<?> area,IntFunction<String> format) {
return new LineNumberFactory(area,format,STYLESHEET);
}
public static IntFunction<Node> get(StyledTextArea<?> area,IntFunction<String> format,String customStylesheet) {
return new LineNumberFactory(area,format,customStylesheet);
}
private final EventStream<Integer> nParagraphs;
private final String stylesheet;
private final IntFunction<String> format;

private LineNumberFactory(StyledTextArea<?> area, String stylesheet) {
nParagraphs = EventStreams.sizeOf(area.getParagraphs());
this.format = (digits -> "%0" + digits + "d");
this.stylesheet = stylesheet;
}
private LineNumberFactory(StyledTextArea<?> area, IntFunction<String> format, String stylesheet) {
nParagraphs = EventStreams.sizeOf(area.getParagraphs());
this.format = format;
this.stylesheet = stylesheet;
}

Expand All @@ -47,6 +59,6 @@ public Node apply(int idx) {

private String format(int x, int max) {
int digits = (int) Math.floor(Math.log10(max)) + 1;
return String.format("%0" + digits + "d", x);
return String.format(format.apply(digits), x);
}
}