-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Add trim_selections command #1092
Conversation
helix-term/src/commands.rs
Outdated
static START_REGEX: Lazy<Regex> = Lazy::new(|| Regex::new(r"^\s+\S").unwrap()); | ||
static END_REGEX: Lazy<Regex> = Lazy::new(|| Regex::new(r"\S\s+$").unwrap()); |
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.
I think using regex is overkill. You should be able to fetch the range.fragment()
then use skip_while
ch.is_whitespace()
: https://github.com/helix-editor/helix/blob/master/helix-core/src/movement.rs#L150-L181
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.
thats what i originally did, then changed to regex cause of the ending whitespaces, good thing i made backup
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.
Nevermind, i didnt know about the backward search, seems like trim_selections will be the first one to use it 🕺
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.
For some reason backwards_skip_while is starting from pos+1, is there a reason for that or should i change it?
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.
I think that was so that the current pos is included once you reverse the direction. chars().rev() would skip pos and instead start with pos-1
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.
Though maybe that's not the case and you can remove that. I see other code simply reversing https://github.com/helix-editor/helix/blob/b824e091a948c076a428fb981cd5be2929378533/helix-core/src/textobject.rs#L19-21
0586770
to
d6a2080
Compare
helix-term/src/commands.rs
Outdated
+ 1; | ||
} | ||
|
||
if start > end { |
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.
This happens only when the whole range is whitespace right? Those ranges should be removed altogether, you can use ranges = filter_map+collect instead of transform to construct a new selection. (You'll need to check !ranges.is_empty?() and fail because a selection needs at least one range)
helix-term/src/commands.rs
Outdated
let mut end = range.to(); | ||
|
||
start = movement::skip_while(text, start, |x| x.is_whitespace()).unwrap_or(start); | ||
if end > 0 { |
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.
Rather than this check for > 0 then -1 I think you have to use prev_grapheme_boundary
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.
If backwards_skip_while returns none then you also end up increasing end by 1 here (end = end+1)
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.
Actually, I think you want to use range.cursor
helix/helix-core/src/selection.rs
Line 264 in b824e09
pub fn cursor(self, text: RopeSlice) -> usize { |
This way you have an inclusive range between range.anchor and range.cursor()(you have to check which one is start and which one is end based on orientation though)
19f4914
to
4c810ec
Compare
What is the use case for this? |
@pickfire this is a feature kakoune includes. One use case is when you have a selection spanning many lines of code at different indentation levels. You use split on newlines It's a bit situational but I have used it organically (meaning I didn't reach for it just to practice it's usage but because it was the most natural thing that came to mind to enable what I was doing) in the past. |
4c810ec
to
e30d9bd
Compare
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.
Tested locally, thanks! 🎉
No description provided.