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

Custom EditableStyledDocument #277

Merged
merged 12 commits into from
Apr 1, 2016
Merged
Changes from 1 commit
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 @@ -19,7 +19,13 @@ public interface EditableStyledDocument<PS, S> extends StyledDocument<PS, S> {

ReadOnlyStyledDocument<PS, S> snapshot();

EventStream<PlainTextChange> plainChanges();
default EventStream<PlainTextChange> plainChanges() {
return richChanges()
// map is used to prevent code repetition: StyledDocument#getText()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Out of context of our previous discussion, this comment does not make much sense, does it? ;)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose not... should it be removed entirely?

.map(c -> new PlainTextChange(c.position, c.removed.getText(), c.inserted.getText()))
// filter out rich changes where the style was changed but text wasn't added/removed
.filter(pc -> !pc.removed.equals(pc.inserted));
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If there is a rich-text change, then insertedLength or removedLength is non-zero already. You probably want to first map and then filter out plain-text changes where removed.equals(inserted).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't it be more efficient to filter it first? Otherwise, we're creating unneeded PlainTextChangees:

default EventStream<PlainTextChange> plainChanges() {
    return richChanges()
            // exclude style changes
            .filter(c -> !c.inserted.getText().equals(c.removed.getText()))
            .map(c -> new PlainTextChange(c.position, c.removed.getText(), c.inserted.getText()));
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, slightly. On the other hand there is a slight repetition of code in calling getText().

And if you want to avoid an intermediate EventStream, use filterMap.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, slightly. On the other hand there is a slight repetition of code in calling getText().

Yeah... I noticed that, too. One way to get around it 😜

    default EventStream<PlainTextChange> plainChanges() {
        return richChanges()
                // exclude style changes
                .map(c -> {
                    String inserted = c.inserted.getText();
                    String removed = c.removed.getText();
                    if (!inserted.equals(removed)) {
                        return new PlainTextChange(c.position, removed, inserted);
                    } else {
                        return null;
                    }
                })
                .filter(c -> c != null);
    }

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't care so much, do what you think is right ;)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wouldn't care too much about optimizing one object allocation, since there are already other much higher costs paid anyway.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the most recent approach used, does that lead to higher costs due to the EventStreams?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An extra EventStream adds a few stack frames to the call stack when propagating the event. But even by the time an edit reaches the plainChanges() stream, so many objects have been allocated and dismissed that one more or less doesn't make any difference. Therefore, I would go for code clarity.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So you do care 😉

Ok. I didn't factor that into consideration. Code clarity it is.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess I do care, when I see how much noise 4e6ce6b adds in the name of saving one object allocation per change. Also, code comments can go out of sync with code very easily, so self-documenting code is better :)


EventStream<RichTextChange<PS, S>> richChanges();

Expand Down