diff --git a/lua/mssql/default_opts.lua b/lua/mssql/default_opts.lua index 7aee869..c270da3 100644 --- a/lua/mssql/default_opts.lua +++ b/lua/mssql/default_opts.lua @@ -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", } diff --git a/lua/mssql/display_query_results.lua b/lua/mssql/display_query_results.lua index c8281a8..781866a 100644 --- a/lua/mssql/display_query_results.lua +++ b/lua/mssql/display_query_results.lua @@ -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 @@ -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) @@ -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) diff --git a/lua/mssql/init.lua b/lua/mssql/init.lua index 3ed6dd4..9f6dfc3 100644 --- a/lua/mssql/init.lua +++ b/lua/mssql/init.lua @@ -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. diff --git a/tests/download_spec.lua b/tests/download_spec.lua index 5797663..0a3c037 100644 --- a/tests/download_spec.lua +++ b/tests/download_spec.lua @@ -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)