@@ -21,8 +21,6 @@ if not db then
21
21
return nil
22
22
end
23
23
24
- local wrap , preview_format , required_db_keys
25
-
26
24
--- Gets the cite format for the filetype
27
25
--- @return string #The cite format for the filetype (or fallback if undefined)
28
26
local function parse_format_string ()
@@ -33,24 +31,49 @@ local function parse_format_string()
33
31
return cite_format
34
32
end
35
33
34
+ local wrap , preview_format , required_db_keys , initial_sort_by_time_added
35
+
36
36
--- Defines the papis.nvim telescope picker
37
37
--- @param opts table #Options for the papis picker
38
38
local function papis_picker (opts )
39
39
opts = opts or {}
40
40
41
41
local results = db .data :get (nil , required_db_keys )
42
42
local format_string = parse_format_string ()
43
+
44
+ -- amend the generic_sorter so that we can change initial sorting
45
+ local generic_sorter = telescope_config .generic_sorter (opts )
46
+ local papis_sorter = {}
47
+ setmetatable (papis_sorter , { __index = generic_sorter })
48
+
49
+ if initial_sort_by_time_added then
50
+ --- @param prompt string
51
+ --- @param line string
52
+ --- @return number score number from 1 to 0. lower the number the better. -1 will filter out the entry though.
53
+ function papis_sorter :scoring_function (prompt , line , entry )
54
+ local score = generic_sorter .scoring_function (self , prompt , line )
55
+ if # prompt == 0 then
56
+ local min_timestamp = 0
57
+ local max_timestamp = os.time ()
58
+ local timestamp = entry [" timestamp" ]
59
+
60
+ score = 1 - (timestamp - min_timestamp ) / (max_timestamp - min_timestamp )
61
+ end
62
+ return score
63
+ end
64
+ end
65
+
43
66
pickers
44
67
.new (opts , {
45
68
prompt_title = " Papis References" ,
46
69
finder = finders .new_table ({
47
70
results = results ,
48
71
entry_maker = function (entry )
49
72
local entry_pre_calc = db [" search" ]:get (entry [" id" ])[1 ]
73
+ local timestamp = entry_pre_calc [" timestamp" ]
50
74
local items = entry_pre_calc [" items" ]
51
75
52
76
local displayer_tbl = entry_pre_calc [" displayer_tbl" ]
53
-
54
77
local displayer = entry_display .create ({
55
78
separator = " " ,
56
79
items = items ,
@@ -65,6 +88,7 @@ local function papis_picker(opts)
65
88
value = search_string ,
66
89
ordinal = search_string ,
67
90
display = make_display ,
91
+ timestamp = timestamp ,
68
92
id = entry ,
69
93
}
70
94
end ,
@@ -91,7 +115,7 @@ local function papis_picker(opts)
91
115
vim .api .nvim_set_option_value (" wrap" , wrap , { win = status .preview_win })
92
116
end ,
93
117
}),
94
- sorter = telescope_config . generic_sorter ( opts ) ,
118
+ sorter = papis_sorter ,
95
119
attach_mappings = function (_ , map )
96
120
actions .select_default :replace (papis_actions .ref_insert (format_string ))
97
121
map (" i" , " <c-o>f" , papis_actions .open_file (), { desc = " Open file" })
112
136
return telescope .register_extension ({
113
137
setup = function (opts )
114
138
wrap = opts [" wrap" ]
139
+ initial_sort_by_time_added = opts [" initial_sort_by_time_added" ]
115
140
preview_format = opts [" preview_format" ]
116
141
local search_keys = opts [" search_keys" ]
117
142
local results_format = opts [" results_format" ]
0 commit comments