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

[Feature request] Reverse line order #9837

Closed
debiedowner opened this issue May 7, 2021 · 3 comments
Closed

[Feature request] Reverse line order #9837

debiedowner opened this issue May 7, 2021 · 3 comments
Assignees
Labels

Comments

@debiedowner
Copy link
Contributor

Notepad++ can sort lines in various ways, including the reverse of each sort option. But there is no way to reverse the existing order of lines. This Super User question from 10 years ago, with 117 upvotes, has a suggested workaround of inserting line numbers, sorting in descending order, and deleting line numbers, which is a quite convoluted and inconvenient way of achieving this result.

I am hoping that the usefulness of this feature would be evident to most that I don't need to list many reasons; at any rate I am sure that 77 thousand people who viewed that question had many more use cases than I can think of and list. In my case, I needed this feature because Notepad++ Regex replace didn't support variable-length lookbehind. I wanted to replace

# title
- item
- item
# title2
- item
...

with

title - item
title - item
title2 - item

which required variable-length lookbehind to get the previous title for every line. My simple workaround was reversing lines, doing a variable-length lookahead, and reversing the lines again. But I had to use an external solution to reverse line orders (which also meant that it wasn't macro-able), it would be much simpler with a Notepad++ command. This is a fairly technical use case, but I would assume that more casual users desire this feature, as they simply want to invert their lists for whatever reason. I also have some lists that being able to easily reverse them occasionally would come in handy.

Finally, I would use @sasumner's reason in [Feature Request] Sort lines randomly (i.e., shuffle lines) (#8682) (whose pull request is what I used as a model to code mine): "We have ranDOm CasE, why not have Sort lines randomly?" -> "We have iNVERT cASE, why note have Reverse line order?".

For consistency with the Convert Case menu, I placed the proposed "Reverse line order" item just above the "Sort lines randomly" item.

@debiedowner
Copy link
Contributor Author

Sorry about duplicate pull requests.

While trying to implement this, I came across SCI_LINEREVERSE command on Scintilla Documentation; apparently a similar feature was already available in Scintilla, but wasn't available in the Notepad++ Shortcut Mapper or anywhere in the menus.

I considered just using that function, adding it to Shortcut Mapper and Line Operations menu directly; but SCI_LINEREVERSE works only on selected lines, whereas other sort commands are able to sort all lines if nothing is selected. I didn't want to do a half-job, so I implemented it similar to other sorting functions in Sorters.h.

I don't know if there is still any benefit to adding SCI_LINEREVERSE to the Shortcut Mapper separately. Probably not, as I don't think it serves any further purpose. I also noticed that, for example, SCI_UPPERCASE isn't available in the Shortcut Mapper (which would be duplicating Notepad++ UPPERCASE); so I assumed not adding it was the way to go.

@sasumner
Copy link
Contributor

sasumner commented May 7, 2021

I needed this feature because Notepad++ Regex replace didn't support variable-length lookbehind

The Boost regex engine is what doesn't support variable-length lookbehind.

The workaround for that is to use the \K syntax.

Example:
Instead of attempting to do (?<=foo.*?bar)fubar, one would do foo.*?bar\Kfubar

@debiedowner
Copy link
Contributor Author

The workaround for that is to use the \K syntax.

I considered that, after seeing it mentioned here, but in my case it wasn't of much help. It only helps match the first items; for the next items, since it doesn't do a lookbehind it is not able to match, so it just moves to the next title and its first item. It of course then matches the second items if I click "Replace" again, but I didn't want to click "Replace" hundreds of times! Reversing line orders and then doing a lookahead solved the issue nicely.

In any case, that's probably a rather niche use case for this feature, and normally it would be used for actually reversing the line order, rather than as a temporary step for a workaround. I just mentioned it as it was specifically my use case this week, which is what urged me to write the code for this pull request. But I guess you mentioned it to be helpful in case I need to do it again, so thank you! I would of course prefer to do it in a single regex even if the feature is added to Notepad++, but I couldn't figure out a way.

(Actually, I did figure out one way: Since in this case my titles were of fixed length (just the items weren't), I added the titles to the start and end of every item, and thus on every next item I was able make a lookbehind, so I could replace everything on one go; and then remove titles from the ends of items in a second regex. But that's an even uglier and more convoluted workaround than reversing lines, and wouldn't work if my titles weren't of fixed length.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants