refactor!: do nothing until calling setup() (#80)

* Use Lua function to complete tags

* Use Lua func to define validating command

* Use new API's to define options & mappings

* Add not for usage of `FrecencyValidate`
This commit is contained in:
JINNOUCHI Yasushi 2023-04-11 11:16:52 +09:00 committed by GitHub
parent 38d6743eb2
commit 0a4a521471
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 41 additions and 53 deletions

View File

@ -193,6 +193,13 @@ The following configuration control this behaviour:
The command `FrecencyValidate` can be used to clean the database when `auto_validate` is disabled.
```vim
" clean DB
:FrecencyValidate
" clean DB without prompts to confirm
:FrecencyValidate!
```
### Highlight Groups
```vim

View File

@ -62,6 +62,29 @@ local function get_workspace_tags()
return tags
end
local function complete(findstart, base)
if findstart == 1 then
local line = vim.api.nvim_get_current_line()
local start = line:find ":"
-- don't complete if there's already a completed `:tag:` in line
if not start or line:find(":", start + 1) then
return -3
end
return start
else
if vim.fn.pumvisible() == 1 and #vim.v.completed_item > 0 then
return ""
end
local tags = get_workspace_tags()
local matches = vim.tbl_filter(function(v)
return vim.startswith(v, base)
end, tags)
return #matches > 0 and matches or ""
end
end
local frecency = function(opts)
local has_devicons, devicons = pcall(require, "nvim-web-devicons")
local entry_display = require "telescope.pickers.entry_display"
@ -280,22 +303,11 @@ local frecency = function(opts)
})
state.picker:find()
vim.api.nvim_buf_set_option(state.picker.prompt_bufnr, "filetype", "frecency")
vim.api.nvim_buf_set_option(state.picker.prompt_bufnr, "completefunc", "frecency#FrecencyComplete")
vim.api.nvim_buf_set_keymap(
state.picker.prompt_bufnr,
"i",
"<Tab>",
"pumvisible() ? '<C-n>' : '<C-x><C-u>'",
{ expr = true, noremap = true }
)
vim.api.nvim_buf_set_keymap(
state.picker.prompt_bufnr,
"i",
"<S-Tab>",
"pumvisible() ? '<C-p>' : ''",
{ expr = true, noremap = true }
)
local buffer = state.picker.prompt_bufnr
vim.api.nvim_buf_set_option(buffer, "filetype", "frecency")
vim.api.nvim_buf_set_option(buffer, "completefunc", "v:lua.require'telescope'.extensions.frecency.complete")
vim.keymap.set("i", "<Tab>", "pumvisible() ? '<C-n>' : '<C-x><C-u>'", { buffer = buffer, expr = true })
vim.keymap.set("i", "<S-Tab>", "pumvisible() ? '<C-p>' : ''", { buffer = buffer, expr = true })
end
local function set_config_state(opt_name, value, default)
@ -327,13 +339,15 @@ return telescope.register_extension {
set_config_state("disable_devicons", ext_config.disable_devicons, false)
set_config_state("default_workspace", ext_config.default_workspace, nil)
config = vim.deepcopy(ext_config)
vim.api.nvim_create_user_command("FrecencyValidate", function (cmd_info)
local safe_mode = not cmd_info.bang
require"telescope._extensions.frecency.db_client".validate(safe_mode)
end, { bang = true, desc = "Clean up DB for telescope-frecency" })
end,
exports = {
frecency = frecency,
get_workspace_tags = get_workspace_tags,
validate_db = function(...)
require"telescope._extensions.frecency.db_client".validate(...)
end
complete = complete,
},
health = checkhealth,
}

View File

@ -1,33 +0,0 @@
function! frecency#FrecencyComplete(findstart, base)
if a:findstart
let line = getline('.')
" don't complete if there's already a completed `:tag:` in line
if count(line, ":") >= 2
return -3
endif
" locate the start of the tag
let start = col('.') - 1
while start > 0 && line[start -1] != ':'
let start -= 1
endwhile
return start
else
if pumvisible() && !empty(v:completed_item)
return ''
end
let l:workspace_tags = luaeval("require'telescope'.extensions.frecency.get_workspace_tags()")
let matches = []
for ws_tag in l:workspace_tags
if ":" .. ws_tag =~ '^:' .. a:base
call add(matches, ws_tag)
endif
endfor
return len(matches) != 0 ? matches : ''
end
endfunction
command FrecencyValidate lua require'telescope'.extensions.frecency.validate_db()