Skip to content

Commit

Permalink
feat: use sort_text over label for sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
Saghen committed Nov 27, 2024
1 parent 33f7d8d commit b185925
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
10 changes: 8 additions & 2 deletions lua/blink/cmp/fuzzy/fuzzy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,14 @@ pub fn fuzzy(
}
"label" => {
matches.sort_by(|a, b| {
let label_a = &haystack[a.index_in_haystack].label;
let label_b = &haystack[b.index_in_haystack].label;
let label_a = haystack[a.index_in_haystack]
.sort_text
.as_ref()
.unwrap_or(&haystack[a.index_in_haystack].label);
let label_b = haystack[b.index_in_haystack]
.sort_text
.as_ref()
.unwrap_or(&haystack[b.index_in_haystack].label);

// Put anything with an underscore at the end
match (label_a.starts_with('_'), label_b.starts_with('_')) {
Expand Down
7 changes: 5 additions & 2 deletions lua/blink/cmp/fuzzy/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,11 @@ end
function fuzzy.get_query()
local line = vim.api.nvim_get_current_line()
local keyword = config.completion.keyword
local range =
require('blink.cmp.lib.utils').get_regex_around_cursor(keyword.range, keyword.regex, keyword.exclude_from_prefix_regex)
local range = require('blink.cmp.lib.utils').get_regex_around_cursor(
keyword.range,
keyword.regex,
keyword.exclude_from_prefix_regex
)
return string.sub(line, range.start_col, range.start_col + range.length - 1)
end

Expand Down
3 changes: 3 additions & 0 deletions lua/blink/cmp/fuzzy/lsp_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use mlua::prelude::*;
pub struct LspItem {
pub label: String,
pub filter_text: Option<String>,
pub sort_text: Option<String>,
pub kind: u32,
pub score_offset: i32,
pub source_id: String,
Expand All @@ -14,13 +15,15 @@ impl FromLua for LspItem {
if let Some(tab) = value.as_table() {
let label = tab.get("label").unwrap_or_default();
let filter_text = tab.get("filterText").ok();
let sort_text = tab.get("sortText").ok();
let kind = tab.get("kind").unwrap_or_default();
let score_offset = tab.get("score_offset").unwrap_or(0);
let source_id = tab.get("source_id").unwrap_or_default();

Ok(LspItem {
label,
filter_text,
sort_text,
kind,
score_offset,
source_id,
Expand Down

0 comments on commit b185925

Please sign in to comment.