Skip to content

Commit

Permalink
ftplugin
Browse files Browse the repository at this point in the history
  • Loading branch information
Javier Orfo committed Oct 30, 2024
1 parent 4a91816 commit e0886e6
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 43 deletions.
22 changes: 22 additions & 0 deletions ftplugin/javascript.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
local logger = require 'dbeer.util'.logger
local setup = require 'dbeer'.SETTINGS
local db = setup.db

if db.connections then
local connection = db.connections[require 'dbeer'.default_db]
if connection.name and connection.dbname and connection.engine and require 'dbeer.engines'.db[connection.engine] and connection.engine == "mongo" then
logger:info(string.format("Database set to [%s]", connection.name))
vim.api.nvim_set_keymap('v', setup.commands.execute, '<cmd>lua require("dbeer.core").run()<CR>',
{ noremap = true, silent = true })
vim.api.nvim_set_keymap('n', setup.commands.execute, '<cmd>lua require("dbeer.core").run()<CR>',
{ noremap = true, silent = true })
vim.api.nvim_set_keymap('n', setup.commands.close, '<cmd>lua require("dbeer.core").close()<CR>',
{ noremap = true, silent = true })
else
pcall(vim.keymap.del, 'v', setup.commands.execute)
pcall(vim.keymap.del, 'n', setup.commands.execute)
pcall(vim.keymap.del, 'n', setup.commands.close)
end
else
logger:info("No database configured.")
end
22 changes: 22 additions & 0 deletions ftplugin/sql.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
local logger = require 'dbeer.util'.logger
local setup = require 'dbeer'.SETTINGS
local db = setup.db

if db.connections then
local connection = db.connections[require 'dbeer'.default_db]
if connection.name and connection.dbname and connection.engine and require 'dbeer.engines'.db[connection.engine] and connection.engine ~= "mongo" then
logger:info(string.format("Database set to [%s]", connection.name))
vim.api.nvim_set_keymap('v', setup.commands.execute, '<cmd>lua require("dbeer.core").run()<CR>',
{ noremap = true, silent = true })
vim.api.nvim_set_keymap('n', setup.commands.execute, '<cmd>lua require("dbeer.core").run()<CR>',
{ noremap = true, silent = true })
vim.api.nvim_set_keymap('n', setup.commands.close, '<cmd>lua require("dbeer.core").close()<CR>',
{ noremap = true, silent = true })
else
pcall(vim.keymap.del, 'v', setup.commands.execute)
pcall(vim.keymap.del, 'n', setup.commands.execute)
pcall(vim.keymap.del, 'n', setup.commands.close)
end
else
logger:info("No database configured.")
end
43 changes: 0 additions & 43 deletions lua/dbeer.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
local M = {}
local util = require 'dbeer.util'
local engines = require 'dbeer.engines'
local logger = util.logger

M.SETTINGS = {
commands = {
Expand Down Expand Up @@ -123,48 +122,6 @@ function M.setup(opts)
end

util.logger:debug("Configuration: " .. vim.inspect(M.SETTINGS))

vim.api.nvim_create_autocmd("BufEnter", {
pattern = "*.js",
callback = function()
local db = M.SETTINGS.db

if db.connections then
local connection = db.connections[require 'dbeer'.default_db]
if connection.name and connection.dbname and connection.engine and require 'dbeer.engines'.db[connection.engine] and connection.engine == "mongo" then
logger:info(string.format("Database set to [%s]", connection.name))

vim.api.nvim_set_keymap('v', M.SETTINGS.commands.execute, '<cmd>lua require("dbeer.core").run()<CR>',
{ noremap = true, silent = true })
vim.api.nvim_set_keymap('n', M.SETTINGS.commands.execute, '<cmd>lua require("dbeer.core").run()<CR>',
{ noremap = true, silent = true })
vim.api.nvim_set_keymap('n', M.SETTINGS.commands.close, '<cmd>lua require("dbeer.core").close()<CR>',
{ noremap = true, silent = true })
end
end
end,
})

vim.api.nvim_create_autocmd("BufEnter", {
pattern = "*.sql",
callback = function()
local db = M.SETTINGS.db

if db.connections then
local connection = db.connections[require 'dbeer'.default_db]
if connection.name and connection.dbname and connection.engine and require 'dbeer.engines'.db[connection.engine] and connection.engine == "mongo" then
logger:info(string.format("Database set to [%s]", connection.name))

vim.api.nvim_set_keymap('v', M.SETTINGS.commands.execute, '<cmd>lua require("dbeer.core").run()<CR>',
{ noremap = true, silent = true })
vim.api.nvim_set_keymap('n', M.SETTINGS.commands.execute, '<cmd>lua require("dbeer.core").run()<CR>',
{ noremap = true, silent = true })
vim.api.nvim_set_keymap('n', M.SETTINGS.commands.close, '<cmd>lua require("dbeer.core").close()<CR>',
{ noremap = true, silent = true })
end
end
end,
})
end

return M

0 comments on commit e0886e6

Please sign in to comment.