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

sqls formating removes all whitespaces #149

Open
594212 opened this issue Apr 13, 2024 · 7 comments · May be fixed by #165
Open

sqls formating removes all whitespaces #149

594212 opened this issue Apr 13, 2024 · 7 comments · May be fixed by #165

Comments

@594212
Copy link

594212 commented Apr 13, 2024

Expected Behavior

CREATE TABLE accounts (
  user_id SERIAL PRIMARY KEY, 
  username VARCHAR (50) UNIQUE NOT NULL, 
  password VARCHAR (50) NOT NULL, 
  email VARCHAR (255) UNIQUE NOT NULL, 
  created_at TIMESTAMP NOT NULL, 
  last_login TIMESTAMP
);

Actual Behavior

CREATETABLEaccounts(
    user_id SERIALPRIMARYKEY,
    usernameVARCHAR(
        50
    )UNIQUENOTNULL,
    passwordVARCHAR(
        50
    )NOTNULL,
    emailVARCHAR(
        255
    )UNIQUENOTNULL,
    created_atTIMESTAMPNOTNULL,
    last_loginTIMESTAMP
);

I'm not sure how it started, I was using nvim and setting up for postgres with lsqconfig,
because I have same issue,for work around I added to ~/.config/sqls/config.yml following:

# Set to true to use lowercase keywords instead of uppercase.
lowercaseKeywords: false
connections:
  - alias: psql
    driver: postgresql
    dataSourceName: 'host=127.0.0.1 port=5432 user=postgres password=secret dbname=testdb sslmode=disable'

it's clearly not related to error, but after that in some point, formatter started going crazy. Now I removed all configs from every corner but this bug persists

@jamietanna
Copy link

It looks like this "only" seems to happen on non-insert and non-selects, which includes a lot of queries 🤔

@jamietanna
Copy link

It looks like in

func formatItem(node ast.Node, env *formatEnvironment) ast.Node {
we're not matching i.e. CREATE or VARCHAR.

I can see a diff like so:

diff --git internal/formatter/formatter.go internal/formatter/formatter.go
index 15cbf96..e17d485 100644
--- internal/formatter/formatter.go
+++ internal/formatter/formatter.go
@@ -144,6 +144,8 @@ func formatItem(node ast.Node, env *formatEnvironment) ast.Node {
                        "LIMIT",
                        "WHEN",
                        "ELSE",
+                       "CREATE",
+                       "TABLE",
                },
        }
        if whitespaceAfterMatcher.IsMatch(node) {

Will start to do this correctly, but not sure if we want to allowlist each keyword + combination? (I realise that's how it's currently already done)

@jamietanna jamietanna linked a pull request Aug 15, 2024 that will close this issue
asadmoosvi added a commit to asadmoosvi/nvim that referenced this issue Sep 9, 2024
Disable the lsp formatting for this language server as there's a bug
currently that removes all whitespaces. See sqls-server/sqls#149
@A404M
Copy link

A404M commented Oct 3, 2024

Is the problem solved?

@jamietanna
Copy link

As per the unclosed issue / unmerged PRs, no, it's still present

@dpbrackin
Copy link

It's also removing white spaces for insert statements for me, not sure if this is expected

Unformatted

INSERT INTO users (
  username,
  password
) VALUES ($1, $2);

Formatted

INSERT INTO users(
  username,
  password
)VALUES($1,$2);

@zyriab
Copy link

zyriab commented Oct 30, 2024

If anybody wants to stop sqls from formatting the buffer, you can add this in the on_attach function when setting up the LSP (using lspconfig in my case).

on_attach = function(client, _)
    client.server_capabilities.documentFormattingProvider = false
    client.server_capabilities.documentRangeFormattingProvider = false
end

Here it is in context.

As a bonus, here's my solution for formatting my .sql files (usually Postgres) using sleek.
It needs to be installed on your system, then you can just run the snippet below

-- This is part of a bigger function containing all of my formatters.
-- Pick what you need :)
if filetype == filetypes.sql then
    if vim.fn.executable("sleek") ~= 1 then
        vim.notify("sleek is not installed, skipping formatting", vim.log.levels.ERROR)
        return
    end

    local bufnr = vim.api.nvim_get_current_buf()
    local temp_file_name = vim.fn.tempname()

    vim.api.nvim_buf_call(bufnr, function()
        vim.cmd("silent noa write " .. vim.fn.fnameescape(temp_file_name))
    end)

    vim.cmd("silent !sleek " .. vim.fn.fnameescape(temp_file_name))

    local temp_file = io.open(temp_file_name, "r")

    if temp_file == nil then
        vim.notify_once("Failed to read formatted file", vim.log.levels.ERROR)

        return
    end

    local temp_content = temp_file:read("*all")
    local formatted = vim.split(temp_content, " ")
    formatted = vim.split(temp_content, "\n")

    temp_file:close()

    os.remove(temp_file_name)

    vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, formatted)

    return
end

Here it is in context.

Basically it's copying the current buffer's content to a temporary buffer, formats it, reads it, replaces the current buffer's content with the formatted output.

Maneren added a commit to Maneren/astronvim_config that referenced this issue Nov 7, 2024
@i11010520
Copy link

still exist

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants