Merge pull request #4 from barrettruth/refactor/vim-g-config
refactor: use vim.g config and lazy loading
This commit is contained in:
commit
6ff6a63598
2 changed files with 59 additions and 33 deletions
|
|
@ -1,49 +1,67 @@
|
||||||
local M = {}
|
local M = {}
|
||||||
|
|
||||||
local open_url = require('http-codes.os').get_open_url()
|
local initialized = false
|
||||||
|
|
||||||
M.config = {
|
local defaults = {
|
||||||
use = nil,
|
use = nil,
|
||||||
open_url = open_url,
|
open_url = nil,
|
||||||
}
|
}
|
||||||
|
|
||||||
M.http_codes = function()
|
local config = vim.deepcopy(defaults)
|
||||||
if M.config.use == 'telescope' then
|
|
||||||
require('telescope').extensions.http.list(M.config.open_url)
|
|
||||||
elseif M.config.use == 'fzf-lua' then
|
|
||||||
require('http-codes.fzf-lua').pick(M.config.open_url)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
M.setup = function(user_config)
|
local function init()
|
||||||
|
if initialized then
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
|
||||||
|
local user_config = vim.g.http_codes or {}
|
||||||
|
config = vim.tbl_deep_extend('force', defaults, user_config)
|
||||||
|
|
||||||
|
if not config.open_url then
|
||||||
|
config.open_url = require('http-codes.os').get_open_url()
|
||||||
|
end
|
||||||
|
|
||||||
|
if not config.use then
|
||||||
if pcall(require, 'fzf-lua') then
|
if pcall(require, 'fzf-lua') then
|
||||||
M.config.use = 'fzf-lua'
|
config.use = 'fzf-lua'
|
||||||
elseif pcall(require, 'telescope') then
|
elseif pcall(require, 'telescope') then
|
||||||
M.config.use = 'telescope'
|
config.use = 'telescope'
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
if M.config.use == nil then
|
if not config.use then
|
||||||
vim.notify_once 'http-codes.nvim: install fzf-lua or telescope.nvim'
|
vim.notify_once('http-codes.nvim: install fzf-lua or telescope.nvim', vim.log.levels.ERROR)
|
||||||
return
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
if user_config.use then M.config.use = user_config.use end
|
if not vim.tbl_contains({ 'fzf-lua', 'telescope' }, config.use) then
|
||||||
|
vim.notify_once(
|
||||||
|
"http-codes.nvim: 'use' must be 'fzf-lua' or 'telescope'",
|
||||||
|
vim.log.levels.ERROR
|
||||||
|
)
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
|
||||||
if not vim.tbl_contains({ 'fzf-lua', 'telescope' }, M.config.use) then
|
if config.use == 'telescope' then
|
||||||
vim.notify_once(
|
require('http-codes.telescope').setup()
|
||||||
'http-codes.nvim: must specify `use = {fzf-lua,telescope}` in setup.',
|
end
|
||||||
vim.log.levels.ERROR
|
|
||||||
)
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
M.config = vim.tbl_deep_extend('force', M.config, user_config)
|
initialized = true
|
||||||
|
return true
|
||||||
if M.config.use == 'telescope' then
|
|
||||||
require('http-codes.telescope').setup()
|
|
||||||
end
|
|
||||||
|
|
||||||
vim.api.nvim_create_user_command('HTTPCodes', M.http_codes, {})
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function M.pick()
|
||||||
|
if not init() then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
if config.use == 'telescope' then
|
||||||
|
require('telescope').extensions.http.list(config.open_url)
|
||||||
|
elseif config.use == 'fzf-lua' then
|
||||||
|
require('http-codes.fzf-lua').pick(config.open_url)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
M.http_codes = M.pick
|
||||||
|
|
||||||
return M
|
return M
|
||||||
|
|
|
||||||
8
plugin/http-codes.lua
Normal file
8
plugin/http-codes.lua
Normal file
|
|
@ -0,0 +1,8 @@
|
||||||
|
if vim.g.loaded_http_codes then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
vim.g.loaded_http_codes = 1
|
||||||
|
|
||||||
|
vim.api.nvim_create_user_command('HTTPCodes', function()
|
||||||
|
require('http-codes').pick()
|
||||||
|
end, {})
|
||||||
Loading…
Add table
Add a link
Reference in a new issue