Skip to content

Instantly share code, notes, and snippets.

@Velrok
Created September 18, 2025 10:52
Show Gist options
  • Save Velrok/56b1e32a160dd4dc64f884ec4c6471a5 to your computer and use it in GitHub Desktop.
Save Velrok/56b1e32a160dd4dc64f884ec4c6471a5 to your computer and use it in GitHub Desktop.
-- Written with Claude
-- dbt ref() jump to definition
local function extract_ref_model_name()
local line = vim.api.nvim_get_current_line()
local ref_pattern = "ref%(%s*['\"]([^'\"]+)['\"]%s*%)"
return line:match(ref_pattern)
end
local function find_model_files(model_name)
local cmd = string.format("fd -t f '%s.sql'", model_name)
local handle = io.popen(cmd)
if not handle then
return nil, "Failed to execute fd command"
end
local result = handle:read("*a")
handle:close()
if not result then
return nil, "Failed to read fd output"
end
result = result:gsub("%s+$", "")
if result == "" then
return {}, nil
end
local files = {}
for file in result:gmatch("[^\n]+") do
table.insert(files, file)
end
return files, nil
end
local function open_model_file(files, model_name)
if #files == 0 then
vim.notify(string.format("No file found for model: %s", model_name), vim.log.levels.WARN)
elseif #files == 1 then
vim.cmd("edit " .. files[1])
else
vim.ui.select(files, {
prompt = "Multiple files found, select one:",
}, function(choice)
if choice then
vim.cmd("edit " .. choice)
end
end)
end
end
local function jump_to_dbt_ref()
local model_name = extract_ref_model_name()
if not model_name then
vim.notify("No dbt ref() found on current line", vim.log.levels.WARN)
return
end
local files, err = find_model_files(model_name)
if err then
vim.notify(err, vim.log.levels.ERROR)
return
end
open_model_file(files, model_name)
end
vim.keymap.set("n", "gd", jump_to_dbt_ref, { buffer = true, desc = "Jump to dbt ref model" })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment