Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Calculate highlighting of brackets #566

Closed
stevedefazio opened this issue Aug 10, 2017 · 5 comments
Closed

Calculate highlighting of brackets #566

stevedefazio opened this issue Aug 10, 2017 · 5 comments

Comments

@stevedefazio
Copy link

stevedefazio commented Aug 10, 2017

I'm trying to calculate highlighting for brackets. And while I think the algorithm is right, I am not sure I'm using the right method to set the style. What I'm trying to do is say "Set the style for the characters from point A to point B", but codeArea.setStyle doesn't seem to be doing anything.

codeArea.caretPositionProperty()
    .addListener((observable, oldValue, newValue) -> {
        if (codeArea.getText(newValue - 1, newValue).equals("[")) {
            int match = computeHighlightingBrackets(configCode.getText(), newValue - 1);
            System.out.println(match);
            codeArea.setStyle(match-1, match, 
                Arrays.asList("-fx-font-family: consolas","-fx-font-size: 30pt;"));
        }
    });

int computeHighlightingBrackets(String text, int start){
	int openPos = start;
		int closePos = openPos;
		int counter = 1;
		while (counter > 0) {
			char c = text.charAt(++closePos);
			if (c == '[') {
				counter++;
			} else if (c == ']') {
				counter--;
			}
		}
	return closePos;
}
@JordanMartinez
Copy link
Contributor

Your algorithm is right. However, CodeArea's style type is Collection<String>>
In other words, it's expecting you to define a style class in an external CSS file. For example

src/
    main/
        java/
            resources/
                custom.css

where custom.css defines brackets as a style class:

.code-area-bracket {
  -fx-font-family: consolas;
  -fx-font-size: 30pt;
}

then you'd use

codeArea.getStyleSheets().add(YourCustomArea.class.getResource("custom.css").toExternalForm());

// ...
codeArea.setStyleClass(match-1, match, "code-area-bracket");

@stevedefazio
Copy link
Author

facepalm that makes sense! Thank you!

@stevedefazio
Copy link
Author

I had been looking at this example, https://github.com/TomasMikula/RichTextFX/blob/master/richtextfx-demos/src/main/java/org/fxmisc/richtext/demo/FontSizeSwitcher.java

Am I right then that the difference is that setStyle(...) gets passed a hardcoded value while setStyleClass(...) uses css?

@JordanMartinez
Copy link
Contributor

setStyleClass is a convenience method that StyleClassTextArea adds so that one doesn't have to write boilerplate.

@JordanMartinez
Copy link
Contributor

Closing since the original issue has been addressed. Feel free to keep asking questions though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants