-
-
Notifications
You must be signed in to change notification settings - Fork 44
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
Comments at the top of the file are deleted #23
Comments
Unfortunately that's difficult: see libpg_query's #15 for details. As Lukas suggests, with lot of gymnastics we could
As an alternative approach, replace point 3 with
|
I discussed this briefly with Lele over email and would like to add an excerpt from reply here.
Here's the mentioned comment splicer from yapf. From my point of view this approach is very similar to 3 & 4 above and seems like a reasonable way to move forward. Would love to hear your comments on this. |
I think a first step toward the goal is implementing the pre-parser speculated in point 1, able to extract only the comments and keeping them apart, with their absolute position in the original statement. |
Looks like the linked libpg_query issue #15 on preserving comments was closed as implemented:
Does this news make implementing comment preservation easier? If so, what would the steps be to using the new |
I will try to understand the effort needed to expose the |
This is a preliminary implementation of the first step mentioned in commit #23 (comment) of issue #23.
I've implemented a preliminary |
I'm seeing commit 55aed47 on the v3 branch. What would the next steps be? I can contribute test cases if nothing else. |
The plan is: add a boolean option to the |
…tring The C functions exposed by the underlying libpg_query operates on the UTF-8 encoded C string, and thus both the nodes "location" and the possible errors position are relative to that, not to the original Python string: when it contains non-ASCII, the offsets are clearly different. This fixes the issue with the exceptions raised in case of errors and with the tokens info returned by the new scan() function, but more work is needed to adjust the "location" slot of the AST nodes: we need that to make further steps of issue #23.
This implements the basic behaviour requested in issue #23: it isn't neither perfect nor beautiful, but... what is?
A first cut of this feature is present in the just released 3.0dev0 version. |
Initial feedback:
demosecho '-- hello
select 1
' > ./normal.sql;
echo 'CREATE TABLE foo(
bar INT -- an informative comment;
, baz TEXT -- another comment,
);' > ./tricky.sql
echo '
CREATE OR REPLACE FUNCTION my_func(x INT) RETURNS TABLE(x INT) LANGUAGE plpgsql AS $$ BEGIN return query select * from foo; END $$;
' > ./plpg.sql
docker run -it -v $(pwd):/workspace --workdir /worksapce python:3.8-buster bash
pip install pglast==3.0.dev0
function compare_pre_post() {
local file_to_format=${1:?missing required positional argument}; shift;
echo "before ----------------------";
cat $file_to_format | tee /tmp/pre;
echo "after ------------------------";
python -m pglast $@ $file_to_format /dev/stdout | tee /tmp/post;
echo "compared ------------------";
diff -u /tmp/pre /tmp/post;
}
compare_pre_post ./normal.sql --preserve-comments | sed 's/^/# /g'
# before ----------------------
# -- hello
#
# select 1
#
# after ------------------------
# /* hello */
# SELECT 1
# compared ------------------
# --- /tmp/pre 2021-05-04 15:09:26.569431088 +0000
# +++ /tmp/post 2021-05-04 15:09:26.726431096 +0000
# @@ -1,4 +1,2 @@
# --- hello
# -
# -select 1
# -
# +/* hello */
# +SELECT 1
compare_pre_post ./tricky.sql --preserve-comments | sed 's/^/# /g'
# before ----------------------
# CREATE TABLE foo(
# bar INT -- an informative comment;
# , baz TEXT -- another comment,
# );
# after ------------------------
# CREATE TABLE foo (
# bar integer
# , /* an informative comment; */ baz text
# ) /* another comment, */
# compared ------------------
# --- /tmp/pre 2021-05-04 15:10:35.471343512 +0000
# +++ /tmp/post 2021-05-04 15:10:35.639343520 +0000
# @@ -1,4 +1,4 @@
# -CREATE TABLE foo(
# - bar INT -- an informative comment;
# - , baz TEXT -- another comment,
# -);
# +CREATE TABLE foo (
# + bar integer
# + , /* an informative comment; */ baz text
# +) /* another comment, */
compare_pre_post ./malformed.plpg.sql --preserve-comments | sed 's/^/# /g'
# before ----------------------
#
# CREATE OR REPLACE FUNCTION my_func(x INT) RETURNS TABLE(x INT) LANGUAGE plpgsql AS $$ BEGIN return query select * from foo; END $$;
#
# after ------------------------
# CREATE OR REPLACE FUNCTION my_func(x integer)
# RETURNS TABLE (x integer)LANGUAGE plpgsql
# AS $$ BEGIN return query select * from foo; END $$
# compared ------------------
# --- /tmp/pre 2021-05-04 15:38:25.591646027 +0000
# +++ /tmp/post 2021-05-04 15:38:25.747646035 +0000
# @@ -1,3 +1,3 @@
# -
# -CREATE OR REPLACE FUNCTION my_func(x INT) RETURNS TABLE(x INT) LANGUAGE plpgsql AS $$ BEGIN return query select * from foo; END $$;
# -
# +CREATE OR REPLACE FUNCTION my_func(x integer)
# +RETURNS TABLE (x integer)LANGUAGE plpgsql
# +AS $$ BEGIN return query select * from foo; END $$ |
Thank you. The first is doable, at least when one using the prettifying printer, but not when using the compact one: the latter does not emit newlines, so embedded comments obviously must be in the The second is not possible, as not all nodes carry their original position (for example literal values), so pglast cannot determine whether the comment is before of after them. |
The logic is pretty simple: the print_comment() method emits the text, and it gets called by print_node() when the given |
Keep the original comment style, at least when prettifying code. See issue #23.
I improved the printing of comments, maintaining the original style. |
See here for an example. |
Looks good to me! I'll see if I can get a chance to play around with the code further in the near future. |
Present in release v3.0.dev1. |
Running pgpp master on the file above returns
I would prefer that it retain the comments :-)
The text was updated successfully, but these errors were encountered: