diff --git a/lua/telescope/builtin.lua b/lua/telescope/builtin.lua index f6b665e..2c59b14 100644 --- a/lua/telescope/builtin.lua +++ b/lua/telescope/builtin.lua @@ -152,7 +152,7 @@ builtin.oldfiles = function(opts) finder = finders.new_table(vim.tbl_filter(function(val) return 0 ~= vim.fn.filereadable(val) end, vim.v.oldfiles)), - sorter = sorters.get_norcalli_sorter(), + sorter = sorters.get_fuzzy_file(), previewer = previewers.cat, }):find() end @@ -178,4 +178,34 @@ builtin.command_history = function(opts) }):find() end +-- TODO: What the heck should we do for accepting this. +-- vim.fn.setreg("+", "nnoremap $TODO :lua require('telescope.builtin').()") +-- TODO: Can we just do the names instead? +builtin.builtin = function(opts) + local objs = {} + + for k, v in pairs(builtin) do + local debug_info = debug.getinfo(v) + + table.insert(objs, { + vimgrep_str = k, + filename = string.sub(debug_info.source, 2), + lnum = debug_info.linedefined, + col = 0, + + start = debug_info.linedefined, + finish = debug_info.lastlinedefined, + }) + end + + local entries = utils.quickfix_items_to_entries(objs) + + pickers.new(opts, { + prompt = 'Telescope Builtin', + finder = finders.new_table(entries), + previewer = previewers.qflist, + sorter = sorters.get_norcalli_sorter(), + }):find() +end + return builtin diff --git a/lua/telescope/previewers.lua b/lua/telescope/previewers.lua index dec99cc..48b3a1a 100644 --- a/lua/telescope/previewers.lua +++ b/lua/telescope/previewers.lua @@ -193,7 +193,7 @@ previewers.qflist = previewers.new { setup = function() local command_string = "cat %s" if vim.fn.executable("bat") then - command_string = "bat %s --highlight-line %s -r %s:%s" + command_string = "bat %s --highlight-line %s -r %s:%s" .. bat_options end return { @@ -209,9 +209,15 @@ previewers.qflist = previewers.new { local filename = entry.value.filename local lnum = entry.value.lnum - local context = math.floor(height / 2) - local start = math.max(0, lnum - context) - local finish = lnum + context + local start, finish + if entry.start and entry.finish then + start = entry.start + finish = entry.finish + else + local context = math.floor(height / 2) + start = math.max(0, lnum - context) + finish = lnum + context + end vim.api.nvim_win_set_buf(status.preview_win, bufnr) diff --git a/lua/telescope/sorters.lua b/lua/telescope/sorters.lua index 6074f1b..e2a7639 100644 --- a/lua/telescope/sorters.lua +++ b/lua/telescope/sorters.lua @@ -179,15 +179,13 @@ sorters.get_fuzzy_file = function(opts) tail_modifier = 2 end - -- TODO: Copied from ashkan. local denominator = ( (10 * match_count / #prompt_lower_ngrams) -- biases for shorter strings - -- TODO(ashkan): this can bias towards repeated finds of the same - -- subpattern with overlapping_ngrams + 3 * match_count * ngram_len / #line + consecutive_matches + N / (contains_string or (2 * #line)) + -- + 30/(c1 or 2*N) -- TODO: It might be possible that this too strongly correlates, diff --git a/lua/telescope/utils.lua b/lua/telescope/utils.lua index cd1c20c..030d942 100644 --- a/lua/telescope/utils.lua +++ b/lua/telescope/utils.lua @@ -51,9 +51,9 @@ utils.quickfix_items_to_entries = function(locations) local results = {} for _, entry in ipairs(locations) do - local vimgrep_str = string.format( + local vimgrep_str = entry.vimgrep_str or string.format( "%s:%s:%s: %s", - vim.fn.fnamemodify(entry.filename, ":."), + vim.fn.fnamemodify(entry.display_filename or entry.filename, ":."), entry.lnum, entry.col, entry.text @@ -64,6 +64,9 @@ utils.quickfix_items_to_entries = function(locations) value = entry, ordinal = vimgrep_str, display = vimgrep_str, + + start = entry.start, + finish = entry.finish, }) end