question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Question: CTRL-Z Behavior with highlighted text

See original GitHub issue

When using InlineCssTextArea, when you style individual parts of the text and highlight them, then try to use CTRL-Z functionality, it’ll undo the highlighting rather than undo the text changes you entered.

A potential fix was mentioned in #761:

// plainUndoManager, or whatever that static method is called
UndoManager<PlainTextChange> um = UndoUtils.plainUndoManager(area);
area.setUndoManager(um);

The problem now is that it doesn’t appear that InlineCssTextArea is compatible with it. I’m assuming there’s an alternative version that is? Or some sort of work around would be appreciated. I’d rather not use StyleClassedTextArea in this case because it’d complicate how my project works a bit, but if I need to, I can probably make the change at some point. I just want to make there’s not an easier workaround first.

Reproducible test software:

import java.time.Duration;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

import org.fxmisc.flowless.VirtualizedScrollPane;
import org.fxmisc.richtext.InlineCssTextArea;
import org.fxmisc.richtext.LineNumberFactory;
import org.reactfx.Subscription;

public class SyntaxTest extends Application {

	private static final String[] KEYWORDS = new String[] { "[[test]]" };

	private static final String sampleCode = "this is a syntax [[test]]";

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

	@Override
	public void start(Stage primaryStage) {
		InlineCssTextArea textArea = new InlineCssTextArea();
		
		 Subscription cleanupWhenNoLongerNeedIt = textArea
	                .multiPlainChanges()
	                .successionEnds(Duration.ofMillis(100))
	                .subscribe(ignore -> computeHighlighting(textArea, KEYWORDS, "coral"));

		textArea.setParagraphGraphicFactory(LineNumberFactory.get(textArea));
		
		textArea.replaceText(0, 0, sampleCode);

		Scene scene = new Scene(new StackPane(new VirtualizedScrollPane<>(textArea)), 600, 400);
		primaryStage.setScene(scene);
		primaryStage.setTitle("Syntax Test");
		primaryStage.show();
		
		primaryStage.setOnCloseRequest(event ->{
			cleanupWhenNoLongerNeedIt.unsubscribe();
		});
	}

	public static void computeHighlighting(InlineCssTextArea textArea, String[] keywords, String colorFillCode) {
		String text = textArea.getText();
		
		textArea.setStyle(0, textArea.getLength(), "");
		
		for (int i = 0; i < keywords.length; i++) {
			String keyword = keywords[i];
			
			if (keyword.trim().isEmpty() || keyword.startsWith("//")) continue;
			
			boolean containsKeywords = true;
			int lastEnd = 0;
			
			while(containsKeywords) {
				int start = text.indexOf(keyword, lastEnd);
				int end = start + keyword.length();
				
				if (start >= 0) {
					textArea.setStyle(start, end, "-fx-fill: " + colorFillCode + ";");
					lastEnd = end;
				}else {
					containsKeywords = false;
				}
			}
		}
	}
}

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
JordanMartinezcommented, Jul 3, 2018

Ah, figured it out. The issue is that the change type should be List<PlainTextChange> because of #695

So, the code is actually:

UndoManager<List<PlainTextChange>> um = UndoUtils.plainTextUndoManager(textArea);
textArea.setUndoManager(um);

After that, it stopped undoing the syntax highlighting.

0reactions
ekartoyevcommented, Jul 19, 2018

Regarding the issue-starter’s comment:

I’d rather not use StyleClassedTextArea

I’d like to point out, I ran into the undo-highlight issue in StyleClassedTextArea too, so switching to that probably wouldn’t have resolve it.

But changing the undo-manager did the job. Thank you for the recipe!

Read more comments on GitHub >

github_iconTop Results From Across the Web

In excel 2010, pushing ctrl+x will delete the highlighted words ...
I updated my question. But I'll post the edit here as well: This only seems to happen when I push Ctrl X, then...
Read more >
Keyboard shortcuts in Word - Microsoft Support
Ctrl +C. Paste the contents of the Clipboard. Ctrl+V. Select all document content. Ctrl+A. Apply bold formatting to text. Ctrl+B.
Read more >
Paste/undo bug in Brave Browser - Desktop Support
No undo feature if text is pasted over. Just highlight text, paste over it, and attempt to restore the text by using Ctrl-Z....
Read more >
Ctrl+Z undoes twice in editor - Meta Stack Exchange
Great question, I had ran into this a few times too. Basically the editor does not recognize cut as an undoable operation.
Read more >
How to use Ctrl+Z and Ctrl+Y with all Text Components?
That would be really weird behavior for the user. · You might get better help sooner if you show some code you did...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found