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

feat: sort source entries before slicing when using max_item_count #1765

Merged
merged 4 commits into from
Dec 10, 2023

Conversation

meeehdi-dev
Copy link
Contributor

@meeehdi-dev meeehdi-dev commented Nov 29, 2023

Hi,

When using the prop max_item_count, there is a known catch (sorting issue) from the doc:

sources[n].max_item_count~
  `number`
  The source-specific maximum item count option
  Note: This is applied before sorting, so items that aren't well-matched may be selected.

This PR makes it so the source entries are sorted before being sliced and fed to the full list of entries and then sorted again.
If the performance hit is too big, we could maybe add an optional boolean option such as sort_before = true along with max_item_count, to make sure the user still wants this sorting to happen even if it is slower.

EDIT:
Just had another thought... We could maybe avoid all this sorting X times per source + 1 time per group index, while still keeping the results accuracy, but it would bring some changes to how the final list is created. I'll try that later and maybe create another PR if it's worth it.

EDIT2:
Sorry for editing this PR once again but the first version felt wrong.
So this time what i did is just moving the whole max_item_count handling.
We fetch all the entries, we sort them, and at the end, depending on the source of the entry, we filter them out if they reached max_item_count.
I don't have that much experience with array manipulation in lua but i feel like the second commit would be way better performance-wise than the first one.

Thanks for your time.

@meeehdi-dev meeehdi-dev changed the title feat: sort source entries beforce slicing when using max_item_count feat: sort source entries before slicing when using max_item_count Nov 29, 2023
lua/cmp/view.lua Outdated
-- source order priority bonus.
local priority = s:get_source_config().priority or ((#source_group - (i - 1)) * config.get().sorting.priority_weight)

for _, e in ipairs(s:get_entries(ctx)) do
e.score = e.score + priority
table.insert(entries, e)
e.source_name = s.name -- store source name for filtering after sort.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need assign e.source_name.
You already can use e.source.name instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't notice the source was available in the entry. Thank you!

@hrsh7th
Copy link
Owner

hrsh7th commented Dec 10, 2023

LGTM.

@hrsh7th hrsh7th merged commit 41d7633 into hrsh7th:main Dec 10, 2023
@mrmeowski
Copy link

Cool feature - thank you! Should the documentation be updated to remove Note: This is applied before sorting, so items that aren't well-matched may be selected.?

@meeehdi-dev meeehdi-dev deleted the sort-entries-before-slice branch December 23, 2023 07:33
williamboman pushed a commit to williamboman/nvim-cmp that referenced this pull request Feb 26, 2024
…rsh7th#1765)

* feat: sort source entries beforce slicing when using max_item_count

* feat: optimize filtering by max_item_count after sort

* fix: useless check if max_item_counts map is initialized

* fix: directly use entry source object when checking max_item_count
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.

3 participants