lsp_workspace filter

This commit is contained in:
Senghan Bright 2021-01-16 21:56:15 +01:00
parent e547c1cd32
commit 2f7636ba0f
3 changed files with 28 additions and 9 deletions

View File

@ -6,7 +6,6 @@ if not has_telescope then
end
-- start the database client
-- print("start")
local db_client = require("telescope._extensions.frecency.db_client")
-- vim.defer_fn(db_client.init, 100) -- TODO: this is a crappy attempt to lessen loadtime impact, use VimEnter?
db_client.init()
@ -21,7 +20,7 @@ local pickers = require "telescope.pickers"
local previewers = require "telescope.previewers"
-- local sorters = require "telescope.sorters"
local sorters = require "telescope._extensions.frecency.sorter"
local conf = require('telescope.config').values
-- local conf = require('telescope.config').values
local path = require('telescope.path')
local utils = require('telescope.utils')
@ -29,7 +28,9 @@ local frecency = function(opts)
opts = opts or {}
local cwd = vim.fn.expand(opts.cwd or vim.fn.getcwd())
local results = db_client.get_file_scores()
-- opts.lsp_workspace_filter = true
-- TODO: decide on how to handle cwd or lsp_workspace for pathname shorten?
local results = db_client.get_file_scores(opts) -- TODO: pass `filter_workspace` option
local displayer = entry_display.create {
separator = "",

View File

@ -18,7 +18,6 @@ local sql_wrapper = nil
local function import_oldfiles()
local oldfiles = vim.api.nvim_get_vvar("oldfiles")
for _, filepath in pairs(oldfiles) do
-- TODO: don't touch existing entries
sql_wrapper:update(filepath)
end
print(("Telescope-Frecency: Imported %d entries from oldfiles."):format(#oldfiles))
@ -66,7 +65,17 @@ local function filter_timestamps(timestamps, file_id)
return res
end
local function get_file_scores()
local function filter_workspace(filelist, workspace_path)
local res = {}
for _, entry in pairs(filelist) do
if util.string_starts(entry.path, workspace_path) then
table.insert(res, entry)
end
end
return res
end
local function get_file_scores(opts)
if not sql_wrapper then return {} end
local queries = sql_wrapper.queries
@ -78,6 +87,14 @@ local function get_file_scores()
-- print(vim.inspect(timestamp_ages))
if vim.tbl_isempty(files) then return scores end
-- filter to LSP workspace directory
local buf_workspaces = opts.lsp_workspace_filter and vim.lsp.buf.list_workspace_folders() or {}
if not vim.tbl_isempty(buf_workspaces) then
for _, ws_path in pairs(buf_workspaces) do
files = filter_workspace(files, ws_path)
end
end
for _, file_entry in ipairs(files) do
table.insert(scores, {
filename = file_entry.path,
@ -86,10 +103,7 @@ local function get_file_scores()
end
-- sort the table
local function compare(a, b)
return a.score > b.score
end
table.sort(scores, compare)
table.sort(scores, function(a, b) return a.score > b.score end)
return scores
end

View File

@ -6,6 +6,10 @@ util.string_isempty = function(s)
return s == nil or s == ''
end
util.string_starts = function(str, start)
return string.sub(str, 1, str.len(start)) == start
end
util.split = function(s, delimiter)
local result = {}
for match in (s .. delimiter):gmatch("(.-)" .. delimiter) do