Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions lua/mssql/default_opts.lua
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,14 @@ return {

-- When choosing a table/view in the finder, immediately execute the generated SELECT statement
execute_generated_select_statements = true,

--[[ How to open a buffer containing sql results.
Valid options are:
"split" - Open results in a horizontal split
"current_window" - Open results in the current window
function (bufnr) ... end - Function which takes the buffer number of the results buffer to open
(called for each results buffer if there are multiple). Use this
to open the buffer in a custom way
--]]
open_results_in = "split",
}
4 changes: 2 additions & 2 deletions lua/mssql/display_query_results.lua
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ end
local result_buffers = {}

local function create_buffer(name, filetype)
local bufnr = vim.api.nvim_create_buf(true, false)
local bufnr = vim.api.nvim_create_buf(false, false)
table.insert(result_buffers, bufnr)
vim.api.nvim_buf_set_name(bufnr, name)
if filetype and filetype ~= "" then
Expand All @@ -103,7 +103,6 @@ local function display_markdown(lines, bufnr)
vim.api.nvim_set_option_value("swapfile", false, { buf = bufnr })
vim.api.nvim_set_option_value("readonly", true, { buf = bufnr })
vim.api.nvim_set_option_value("modifiable", false, { buf = bufnr })
vim.api.nvim_set_current_buf(bufnr)
end

local function show_result_set_async(column_info, subset_params, opts)
Expand All @@ -127,6 +126,7 @@ local function show_result_set_async(column_info, subset_params, opts)
)
vim.b[buf].query_result_info = { subset_params = subset_params }
display_markdown(lines, buf)
opts.open_results_in(buf)
end

local function display_query_results(opts, result)
Expand Down
38 changes: 38 additions & 0 deletions lua/mssql/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -179,10 +179,48 @@ end

local plugin_opts

local mssql_window
local show_results_buffer_options = {
current_window = function(bufnr)
vim.api.nvim_set_option_value("buflisted", true, { buf = bufnr })
vim.api.nvim_set_current_buf(bufnr)
end,
split = function(bufnr)
local original_window = vim.api.nvim_get_current_win()

-- open a split if we haven't done already
if not (mssql_window and vim.api.nvim_win_is_valid(mssql_window)) then
vim.cmd("split")
mssql_window = vim.api.nvim_get_current_win()
end

vim.api.nvim_set_option_value("buflisted", true, { buf = bufnr })
vim.api.nvim_win_set_buf(mssql_window, bufnr)
vim.api.nvim_set_current_win(original_window)
end,
}

-- If the open_results_in is a string, sets it to the appropriate function
local function set_show_results_option(opts)
if type(opts.open_results_in) == "string" and show_results_buffer_options[opts.open_results_in] then
opts.open_results_in = show_results_buffer_options[opts.open_results_in]
elseif type(opts.open_results_in) == "function" then
return
else
utils.log_error(
vim.inspect(opts.open_results_in)
.. " is not a valid option for open_results_in. Must be one of: "
.. table.concat(vim.tbl_keys(show_results_buffer_options), ", ")
.. ", or a function"
)
end
end

local function setup_async(opts)
opts = opts or {}
opts = vim.tbl_deep_extend("keep", opts or {}, default_opts)
opts.connections_file = opts.connections_file or joinpath(opts.data_dir, "connections.json")
set_show_results_option(opts)
make_directory(opts.data_dir)

-- if the opts specify a tools file path, don't download.
Expand Down
4 changes: 3 additions & 1 deletion tests/download_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ end

local function setup_async()
local co = coroutine.running()
mssql.setup({}, function()
mssql.setup({
open_results_in = "current_window",
}, function()
vim.schedule(function()
coroutine.resume(co)
end)
Expand Down