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

Question: replaceText/insertText without calling richChanges #684

Closed
iamgio opened this issue Feb 11, 2018 · 3 comments
Closed

Question: replaceText/insertText without calling richChanges #684

iamgio opened this issue Feb 11, 2018 · 3 comments

Comments

@iamgio
Copy link
Contributor

iamgio commented Feb 11, 2018

(Hope this is the last issue I'll open here, ahaha)

Basically, I need to do this:

  • If I type, for example, '[', it generates '[ ]' and puts caret between them
  • If I delete '[', it deletes ']' too

It works using '[' and ']', the problem is doing this with characters like quotes and double quotes, because they are equals.

So, when I type one of those characters, each replaceText calls a new richChanges and throws a StackOverflowError.

So, is there a way to call replaceText/insertText without calling richChanges?

The code below is written in Kotlin, but I think it's pretty readable.
(The onChange method is called inside the richChanges().filter(...).subscribe method.)

fun onChange(change: RichTextChange<Collection<String>, StyledText<Collection<String>>, Collection<String>>, area: CodeArea) {
        if(change.inserted.text == char1.toString()) {
            area.insertText(change.position + 1, c2.toString()) //If I type '[', it generates ']'
        } else if(change.removed.text == char1.toString() && area.text.toCharArray()[change.position] == c2) {
            area.replaceText(change.position, change.position + 1, "") //If I remove '[', it removes ']' too
        }
}
@iamgio iamgio changed the title Question: replaceText without calling richChanges Question: replaceText/insertText without calling richChanges Feb 11, 2018
@JordanMartinez
Copy link
Contributor

Ah.... that is a problem...

There's two ways you could potentially solve this.

  • The first is probably the easier of the two. You could override the area's InputMap for KeyTyped events to first check whether the inputted character is a " and change it to the actual opening quotes and ending quotes characters: . Then you'll have to check for that character and add/delete the corresponding when you insert/delete one of those characters. The InputMap would continue from there with the default behavior.
  • The second is using your own custom UndoManager that handle identity changes differently. If you're not familiar with that FP concept, 0 is the identity change for any number, x, so that x + 0 = x. Your custom UM could ignore all identity changes except when the quotes change occurs.

@iamgio
Copy link
Contributor Author

iamgio commented Feb 12, 2018

Thanks for the reply, I'm going to try. Anyways, do you think these methods might be implemented?

@JordanMartinez
Copy link
Contributor

Probably not since RTFX is meant to be a base upon which others (like yourself) build.

@iamgio iamgio closed this as completed Feb 13, 2018
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