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

editor: highlight multiple selections and words on cursor #2281

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

azizk
Copy link
Contributor

@azizk azizk commented Mar 22, 2023

Hi!

I reworked the paint_selection_find function so that:

  • Multiple cursor regions are taken into account.
  • data.doc.selection_find is used directly because a FindProgress isn't really necessary for an implicit search like this which is also limited to the visible lines.
  • The "active" selection under the cursor isn't filled with a color as it's distracting and makes only sense with a Ctrl+F search.
  • The phantom text (to the right) isn't included in the rectangular highlight (also fixed for Ctrl+F).
  • Selecting backwards is working (start and end are swapped).
  • Renamed variables for readability.

Notes/Issues:

  • I'm still learning Rust, so there may be beginner mistakes.
  • The code for getting multiple regions and iterating over them seems correct to me, but it doesn't actually highlight all selections. Only those of the first cursor. Not experienced enough to debug this.
  • I left the curly braces block in there after removing the if clause, so that the diff remains readable.

@azizk azizk force-pushed the editor/multi-highlight branch 2 times, most recently from c1750b8 to 21156dd Compare March 23, 2023 14:40
@azizk
Copy link
Contributor Author

azizk commented Mar 23, 2023

I fixed the issue with the highlighting of multiple selection regions. ✌️ (select_word() wasn't given the correct variable)

I also made the change to match whole words if the SelRegion is a caret. If non-empty text is selected, the search is done without checking word boundaries. Maybe we can add an option in the future for enabling or disabling the case sensitivity of the search.

Edit: I just noticed that it already takes the case sensitivity setting from the Ctrl+F search box. 😆
Edit2: It didn't, but I fixed that later on.

@azizk
Copy link
Contributor Author

azizk commented Mar 25, 2023

More fixes and improvements:

  • Completely removed unneeded update_selection_find function.
  • Call paint_selection_find after highlight_scope_and_brackets so the highlight rectangle is drawn over the vertical scope lines when overlapping.
    imagen
  • No need to call .occurrences().regions_in_range() as the selection_find.update_find() function is already called with the first and last screen line offsets.
  • Remember the search strings to search for and the match_whole_words setting for each string.
  • Actually take the case_matching setting from the finder box widget.
  • No need to for line in &screen_lines.lines since we know the range and can iterate over start_line..=end_line instead.
  • Draw the highlight rectangle with rounded corners.
  • Refactored code into a more functional style.

@azizk azizk force-pushed the editor/multi-highlight branch 2 times, most recently from 8fe7468 to 5b02fdb Compare March 25, 2023 23:46
@azizk
Copy link
Contributor Author

azizk commented Mar 25, 2023

TODO: I saw that the view is painted frequently and the search is run each time, so maybe we should cache the selection search results so that redraws are as fast as possible?

@azizk azizk marked this pull request as ready for review March 25, 2023 23:58
@codecov-commenter
Copy link

codecov-commenter commented Mar 26, 2023

Codecov Report

Merging #2281 (5593960) into master (8ebbbe2) will decrease coverage by 0.01%.
The diff coverage is 0.00%.

📣 This organization is not using Codecov’s GitHub App Integration. We recommend you install it so Codecov can continue to function properly for your repositories. Learn more

@@            Coverage Diff            @@
##           master   #2281      +/-   ##
=========================================
- Coverage    9.06%   9.06%   -0.01%     
=========================================
  Files         137     137              
  Lines       59462   59468       +6     
=========================================
  Hits         5393    5393              
- Misses      54069   54075       +6     
Impacted Files Coverage Δ
lapce-core/src/cursor.rs 5.01% <0.00%> (-0.12%) ⬇️
lapce-data/src/document.rs 0.00% <ø> (ø)
lapce-ui/src/editor.rs 0.00% <0.00%> (ø)

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@azizk azizk force-pushed the editor/multi-highlight branch 2 times, most recently from d11d7ef to 417a8dc Compare March 27, 2023 00:23
@azizk
Copy link
Contributor Author

azizk commented Mar 27, 2023

  • Moved code for getting the selection ranges to as_selection() in impl Cursor.
  • No need to call selection_find.update_find() with include_slop: true. Instead calculate the search range from the longest selected string to search for.

@azizk
Copy link
Contributor Author

azizk commented Mar 27, 2023

I'm not sure but I think I found a bug in Find::update_find(). When you search for an Umlaut with whole_words = true the app crashes:

selection_find.search_string = Some("ä".to_string());
selection_find.whole_words = true;
selection_find.update_find(buffer.text(), start, end, false);

Snippet from logs:

[2023-03-27][02:26:59][panic][ERROR] thread 'main' panicked at 'byte index 904 is not a char boundary; it is inside 'ä' (bytes 903..905) of `...ä...`[...]': /home/aziz/.cargo/registry/src/github.com-1ecc6299db9ec823/lapce-xi-rope-0.3.1/src/rope.rs:663
   0: <backtrace::capture::Backtrace as core::default::Default>::default
             at /home/aziz/.cargo/registry/src/github.com-1ecc6299db9ec823/backtrace-0.3.66/src/capture.rs:410:9

@azizk
Copy link
Contributor Author

azizk commented Mar 29, 2023

I really had to implement these continuous multi-line highlights:

imagen

😁

Cool thing is you'll be able to use the same functions for normal Ctrl+F searches!

I'd love to do one final thing: render the shape with rounded corners. I think it will look more pleasant that way.

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

Successfully merging this pull request may close these issues.

None yet

2 participants