Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix issues with PgFmt when using lsp #2

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
66 changes: 59 additions & 7 deletions lua/psql.lua
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,41 @@ local function psql_run_visual()
psql_run_file(tmp_sql_file)
end

local function select_sql_statement()
local current_pos = vim.fn.getpos('.')
local start_pos = vim.fn.searchpos([[\v(SELECT|INSERT|UPDATE|DELETE|CREATE|DROP|ALTER|WITH)\_s+]], 'cnbW')

-- If no matching positions found, reset cursor and return
if start_pos[1] == 0 then
vim.fn.setpos('.', current_pos)
return false
end

-- if cursor starts between two queries avoids selecting both
vim.fn.setpos('.', {0, start_pos[1], start_pos[2], 0})
vim.cmd('normal! v')

local end_pos = vim.fn.searchpos(';\\s*$', 'cW')

-- If no matching positions found, reset cursor and return
if end_pos[1] == 0 then
vim.fn.setpos('.', current_pos)
return false
end

vim.fn.setpos('.', {0, end_pos[1], end_pos[2], 0})
return true
end

local function psql_run_close_to_cursor()
select_sql_statement()
if select_sql_statement() then
psql_run_visual()
end
vim.cmd('normal! v\\<Esc>')
end


local function psql_run_curr_buf()
local current_buf_name = vim.api.nvim_buf_get_name(0)
if not is_sql_file(current_buf_name) then
Expand All @@ -152,13 +187,29 @@ local function psql_run_curr_buf()
end

local function psql_format()
local current_buf_name = vim.api.nvim_buf_get_name(0)
if not is_sql_file(current_buf_name) then
vim.notify("Not a SQL file!", vim.log.levels.ERROR)
return
end
os.execute(string.format("pg_format -i %s", current_buf_name))
vim.api.nvim_command("edit")
local current_buf = vim.api.nvim_get_current_buf()
local current_buf_name = vim.api.nvim_buf_get_name(0)

if not is_sql_file(current_buf_name) then
vim.notify("Not a SQL file!", vim.log.levels.ERROR)
return
end

-- Detach LSP clients before making changes
for _, client in pairs(vim.lsp.get_clients()) do
vim.lsp.buf_detach_client(current_buf, client.id)
end

-- Execute pg_format command on the file
os.execute(string.format("pg_format -i %s", current_buf_name))

-- Reload the buffer
vim.api.nvim_command("edit")

-- Reattach LSP clients after reloading the buffer
for _, client in pairs(vim.lsp.get_clients()) do
vim.lsp.buf_attach_client(current_buf, client.id)
end
end

local function psql_temp()
Expand All @@ -171,6 +222,7 @@ end
return {
psql_run_curr_buf = psql_run_curr_buf,
psql_run_visual = psql_run_visual,
psql_run_close_to_cursor = psql_run_close_to_cursor,
psql_cancel = psql_cancel,
psql_run_file = psql_run_file,
psql_temp = psql_temp,
Expand Down
7 changes: 7 additions & 0 deletions plugin/psql.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ vim.api.nvim_create_user_command(
psql.psql_run_curr_buf,
{}
)

vim.api.nvim_create_user_command(
"PgRunCursor",
psql.psql_run_close_to_cursor,
{}
)

vim.api.nvim_create_user_command(
"PgCancel",
psql.psql_cancel,
Expand Down