From ca21414cd503ee6ca847458aca9e7d1abfb98550 Mon Sep 17 00:00:00 2001 From: Tyler Hallada Date: Thu, 24 Sep 2026 10:40:09 -0400 Subject: [PATCH] Hack to get dadbod auto-complete in sqmeow scratch buffers --- lua/plugins/sqmeow.lua | 52 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/lua/plugins/sqmeow.lua b/lua/plugins/sqmeow.lua index 4adfec4..24f853f 100644 --- a/lua/plugins/sqmeow.lua +++ b/lua/plugins/sqmeow.lua @@ -1,3 +1,46 @@ +-- sqmeow has no completion in scratchpads, so point vim-dadbod-completion (the blink "dadbod" +-- source from the LazyVim sql extra) at the buffer's sqmeow connection by setting b:db. +local dadbod_schemes = { postgres = true, mysql = true } + +-- The dadbod URL for a sqmeow connection, or nil when dadbod can't use it. +local function dadbod_url(conn) + if conn.state ~= "connected" or conn.ssh or conn.url:find("{{", 1, true) then + return nil + end + local prefix, rest = conn.url:match("^%a[%w+.-]*(://[^/?#]*)(.*)$") + if not prefix or not dadbod_schemes[conn.dialect] then + return nil + end + local path, query = rest:match("^([^?#]*)(.*)$") + -- A database opened from the drawer's database list shares its parent's URL. + if conn.database then + path = "/" .. conn.database + end + return conn.dialect .. prefix .. path .. query +end + +local function sync_dadbod(buf) + -- Don't load sqmeow just for this, and leave b:db alone in buffers sqmeow doesn't own. + if not package.loaded["sqmeow.state"] or vim.bo[buf].filetype ~= "sql" then + return + end + local b = vim.b[buf] + if not (b.sqmeow_editor or b.sqmeow_connection) or (b.db and b.db ~= b.sqmeow_dadbod_url) then + return + end + + local conn = require("sqmeow.api").target(buf) + local url = conn and dadbod_url(conn) + if url == b.sqmeow_dadbod_url then + return + end + b.db = url + b.sqmeow_dadbod_url = url + if url and vim.fn.exists("*vim_dadbod_completion#fetch") == 1 then + vim.fn["vim_dadbod_completion#fetch"](buf) + end +end + return { "2giosangmitom/sqmeow.nvim", dependencies = { "MunifTanjim/nui.nvim" }, @@ -14,4 +57,13 @@ return { { "Da", "Sqmeow add", desc = "Add Connection" }, { "Ds", "Sqmeow scratch", desc = "New Scratchpad" }, }, + init = function() + -- InsertEnter catches connecting, or switching connections with `u`, after the buffer opened. + vim.api.nvim_create_autocmd({ "BufEnter", "InsertEnter" }, { + group = vim.api.nvim_create_augroup("sqmeow_dadbod", { clear = true }), + callback = function(ev) + sync_dadbod(ev.buf) + end, + }) + end, }