diff --git a/ftplugin/javascript.lua b/ftplugin/javascript.lua new file mode 100644 index 0000000..3df3d31 --- /dev/null +++ b/ftplugin/javascript.lua @@ -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, 'lua require("dbeer.core").run()', + { noremap = true, silent = true }) + vim.api.nvim_set_keymap('n', setup.commands.execute, 'lua require("dbeer.core").run()', + { noremap = true, silent = true }) + vim.api.nvim_set_keymap('n', setup.commands.close, 'lua require("dbeer.core").close()', + { 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 diff --git a/ftplugin/sql.lua b/ftplugin/sql.lua new file mode 100644 index 0000000..574154b --- /dev/null +++ b/ftplugin/sql.lua @@ -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, 'lua require("dbeer.core").run()', + { noremap = true, silent = true }) + vim.api.nvim_set_keymap('n', setup.commands.execute, 'lua require("dbeer.core").run()', + { noremap = true, silent = true }) + vim.api.nvim_set_keymap('n', setup.commands.close, 'lua require("dbeer.core").close()', + { 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 diff --git a/lua/dbeer.lua b/lua/dbeer.lua index 0347aed..817ac44 100644 --- a/lua/dbeer.lua +++ b/lua/dbeer.lua @@ -1,7 +1,6 @@ local M = {} local util = require 'dbeer.util' local engines = require 'dbeer.engines' -local logger = util.logger M.SETTINGS = { commands = { @@ -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, 'lua require("dbeer.core").run()', - { noremap = true, silent = true }) - vim.api.nvim_set_keymap('n', M.SETTINGS.commands.execute, 'lua require("dbeer.core").run()', - { noremap = true, silent = true }) - vim.api.nvim_set_keymap('n', M.SETTINGS.commands.close, 'lua require("dbeer.core").close()', - { 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, 'lua require("dbeer.core").run()', - { noremap = true, silent = true }) - vim.api.nvim_set_keymap('n', M.SETTINGS.commands.execute, 'lua require("dbeer.core").run()', - { noremap = true, silent = true }) - vim.api.nvim_set_keymap('n', M.SETTINGS.commands.close, 'lua require("dbeer.core").close()', - { noremap = true, silent = true }) - end - end - end, - }) end return M