-
Notifications
You must be signed in to change notification settings - Fork 7
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
Implement FormattedString method #26
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is very cool stuff, I've long been wanting this but never took the time. Could we land both replace
and replaceAll
in the same pull request? :)
const index = this.text.indexOf(searchValue); | ||
|
||
if (index !== -1 && index < newEntity.offset + newEntity.length) { | ||
newEntity.offset = newEntity.offset + replaceValue.length - searchValue.length; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is an increased complexity in replacing FormattedString as there are now 2 dimensions to consider. We can list down the following cases and discuss what the expected behavior should be. EasyEntity covers a full or partial word, occurs before replacement string, and does not intersect with replacement string.Example: We love the grammY framework very much Entity covers a full or partial word, occurs after replacement string, and does not intersect with replacement string.Example: We love the grammY framework very much Entity covers a full word and completely intersects with replacement stringExample: We love the grammY framework very much MediumEntity covers a partial word, intersects with replacement string, and the replacement string length is greater than the entity lengthExample: We love the grammY framework very much Entity covers a partial word, intersects with replacement string, and the replacement string length is equal to the entity lengthExample: We love the grammY framework very much Entity covers a partial word, intersects with replacement string, and the replacement string length is less than the entity lengthExample: We love the grammY framework very much HardWe won't go into much detail on what the hard cases are for the time being. In general, consider cases where phrases are formatted instead (either as a collection of full words only, or full and partial words). |
I feel like it would be more intuitive to drop all formatting from the replaced strings. I don't see how we can assume that the old lengths or offsets make a lot of sense for newly inserted strings. We love the grammY library a lot We love the grammY library a lot and so on. We can accept new formatted strings as replacements instead. We love the grammY library a lot Note that this suggestion means that we have to split and merge entities as necessary. |
This PR adds a simplified implementation of the basic replace method from the JavaScript String class that preserves the original formatting after replacing text.
Problem:
Solution after changes: