From 4402d72358b3d0d1ec97ffd998ec50b6e0989f76 Mon Sep 17 00:00:00 2001 From: Javier Orfo Date: Wed, 30 Oct 2024 14:15:10 -0300 Subject: [PATCH] bug mongo and sql maps --- ftplugin/javascript.lua | 12 +----------- ftplugin/sql.lua | 12 +----------- lua/dbeer.lua | 37 +++++++++++++++++++++++++++++++++++++ 3 files changed, 39 insertions(+), 22 deletions(-) diff --git a/ftplugin/javascript.lua b/ftplugin/javascript.lua index 3df3d31..00fb09b 100644 --- a/ftplugin/javascript.lua +++ b/ftplugin/javascript.lua @@ -4,18 +4,8 @@ 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 + if connection.name and connection.dbname and connection.engine and require 'dbeer.engines'.db[connection.engine] 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.") diff --git a/ftplugin/sql.lua b/ftplugin/sql.lua index 574154b..00fb09b 100644 --- a/ftplugin/sql.lua +++ b/ftplugin/sql.lua @@ -4,18 +4,8 @@ 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 + if connection.name and connection.dbname and connection.engine and require 'dbeer.engines'.db[connection.engine] 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.") diff --git a/lua/dbeer.lua b/lua/dbeer.lua index 817ac44..935bd74 100644 --- a/lua/dbeer.lua +++ b/lua/dbeer.lua @@ -122,6 +122,43 @@ function M.setup(opts) end util.logger:debug("Configuration: " .. vim.inspect(M.SETTINGS)) + + local function map_or_unmap(connection, is_mongo) + if connection.name and connection.dbname and connection.engine and require 'dbeer.engines'.db[connection.engine] and is_mongo then + 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 }) + else + pcall(vim.keymap.del, 'v', M.SETTINGS.commands.execute) + pcall(vim.keymap.del, 'n', M.SETTINGS.commands.execute) + pcall(vim.keymap.del, 'n', M.SETTINGS.commands.close) + end + end + + 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] + map_or_unmap(connection, connection.engine == "mongo") + 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] + map_or_unmap(connection, connection.engine ~= "mongo") + end + end, + }) end return M