angular-cli icon indicating copy to clipboard operation
angular-cli copied to clipboard

insertLeft/Right beyond boundary flips and binds to end?

Open alexeagle opened this issue 5 years ago • 2 comments

From @dansanderson on June 14, 2017 18:23

In the text update recorder, insertLeft(length + 1, ...) returns an index error. It would be helpful and intuitive for some algorithms for this specific case to be interpreted as insertRight(length, ...). For example, if I'm scanning a string for a position to do an insertLeft and do not find a matching position prior to the end, I want to append to the end. As is, I have to make a special case and call insertRight directly.

The symmetric operation would be insertRight(-1, ...) -> insertLeft(0, ...).

I don't know if it needs to allow all indexes past the ends or just one past the end. And there may be reasons I'm not thinking of to not do this at all.

Copied from original issue: angular/devkit#9

alexeagle avatar Sep 05 '18 00:09 alexeagle

From @hansl on August 22, 2017 22:33

Unfortunately, insertRight(i, ...) and insertLeft(i + 1, ...) have different semantics. It basically comes down to when deleting parts of the string, the inserted text is linked to the index itself. So the following code has undefined behaviour if we allow for your use case:

const buffer = new UpdateBuffer(new Buffer('012345'));
buffer.insertRight(-1, 'ABC');  // The buffer is ABC12345
buffer.insertLeft(7, 'DEF');  // The buffer is ABC12345DEF
buffer.remove(0, 6);  // Is the buffer '' or 'ABC' or 'DEF' or 'ABCDEF'?

Conversely, insertLeft does not have this ambiguity.

There is a case for adding other methods, prepend and append, which would insert before and after the content itself and would not be attached to it. But in the context of refactoring code, if you delete the start of a source file you rarely want to keep the content that was added before it.

Does that make sense?

alexeagle avatar Sep 05 '18 00:09 alexeagle

From @hansl on August 22, 2017 22:50

Please note that I just added a test with that exact case. Right now the last line results in ABC.

insertLeft is basically "append to the right of the string before it".

insertRight is "prepend to the left of the string after it".

I'm not sure I like these semantics, and might change them soon and rename the functions.

alexeagle avatar Sep 05 '18 00:09 alexeagle

Thanks for reporting this issue. This issue is now obsolete due to changes in the recent releases. Please update to the most recent Angular CLI version.

If the problem persists after upgrading, please open a new issue, provide a simple repository reproducing the problem, and describe the difference between the expected and current behavior.

alan-agius4 avatar May 30 '23 11:05 alan-agius4

This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.