Re: [HACKERS] pg_bsd_indent: implement -lps ("leave preprocessor space")

2017-02-09 Thread Antonin Houska
Andres Freund  wrote:

> On 2017-02-07 23:30:44 -0500, Tom Lane wrote:
> > Piotr Stefaniak  writes:
> > > this is a patch that Andres asked me for. It makes pg_bsd_indent leave
> > > preprocessor space alone, as in this example:
> > 
> > > #if 0
> > > #  if 0
> > > # if 0
> > > #   error
> > > # endif
> > > #  endif
> > > #else
> > > #  line 7
> > > #endif
> 
> For context: I'd asked Piotr how dificult it'd be to add this.
> 
> > Um ... but the point of pgindent is to standardize spacing, not to let
> > people invent their own style. If you wanted to have a discussion about
> > whether pgindent should force preprocessor directives to look like the
> > above, we could talk about that.  But I do not want to be reading code that
> > looks like the above in one place and code that does not ten lines away.
> 
> I don't think that's something easily done in an automatic
> manner. Because you'd e.g. obviously not want to indent everything
> within include guards.   I don't really buy the danger of large
> divergances in code nearby - this seems mostly useful when writing a bit
> more complicated macros, and I don't think they'll be that frequently
> added in existing files.
> 
> I do think this makes the nesting for #ifdefs a *lot* more readable, and
> we have plenty of cases where it's currently really hard to discern what
> "branch" one is currently reading.  Allowing to opt-in into the "newer"
> formatting in places where it makes sense, seems reasonable to me.

As an alternative approach, the formatting can be implemented in elisp and
added to src/tools/editors/emacs.samples. In particular I mean one function to
make the code human readable and another one to turn it back to the concise
style. User would only call the function on text selection (region) as opposed
to the whole file.

-- 
Antonin Houska
Cybertec Schönig & Schönig GmbH
Gröhrmühlgasse 26
A-2700 Wiener Neustadt
Web: http://www.postgresql-support.de, http://www.cybertec.at


-- 
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers


Re: [HACKERS] pg_bsd_indent: implement -lps ("leave preprocessor space")

2017-02-08 Thread Andres Freund
On 2017-02-07 23:30:44 -0500, Tom Lane wrote:
> Piotr Stefaniak  writes:
> > this is a patch that Andres asked me for. It makes pg_bsd_indent leave
> > preprocessor space alone, as in this example:
> 
> > #if 0
> > #  if 0
> > #   if 0
> > # error
> > #   endif
> > #  endif
> > #else
> > #  line 7
> > #endif

For context: I'd asked Piotr how dificult it'd be to add this.

> Um ... but the point of pgindent is to standardize spacing, not to let
> people invent their own style. If you wanted to have a discussion about
> whether pgindent should force preprocessor directives to look like the
> above, we could talk about that.  But I do not want to be reading code that
> looks like the above in one place and code that does not ten lines away.

I don't think that's something easily done in an automatic
manner. Because you'd e.g. obviously not want to indent everything
within include guards.   I don't really buy the danger of large
divergances in code nearby - this seems mostly useful when writing a bit
more complicated macros, and I don't think they'll be that frequently
added in existing files.

I do think this makes the nesting for #ifdefs a *lot* more readable, and
we have plenty of cases where it's currently really hard to discern what
"branch" one is currently reading.  Allowing to opt-in into the "newer"
formatting in places where it makes sense, seems reasonable to me.

Regards,

Andres


-- 
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers


Re: [HACKERS] pg_bsd_indent: implement -lps ("leave preprocessor space")

2017-02-07 Thread Tom Lane
Piotr Stefaniak  writes:
> this is a patch that Andres asked me for. It makes pg_bsd_indent leave
> preprocessor space alone, as in this example:

> #if 0
> #  if 0
> # if 0
> #   error
> # endif
> #  endif
> #else
> #  line 7
> #endif

Um ... but the point of pgindent is to standardize spacing, not to let
people invent their own style.  If you wanted to have a discussion about
whether pgindent should force preprocessor directives to look like the
above, we could talk about that.  But I do not want to be reading code that
looks like the above in one place and code that does not ten lines away.

regards, tom lane


-- 
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers


[HACKERS] pg_bsd_indent: implement -lps ("leave preprocessor space")

2017-02-07 Thread Piotr Stefaniak
Hello,

this is a patch that Andres asked me for. It makes pg_bsd_indent leave
preprocessor space alone, as in this example:

#if 0
#  if 0
#   if 0
# error
#   endif
#  endif
#else
#  line 7
#endif
diff -ur pg_bsd_indent/args.c pg_bsd_indent_patched/args.c
--- pg_bsd_indent/args.c	2014-01-31 04:09:31.0 +0100
+++ pg_bsd_indent_patched/args.c	2017-02-08 01:59:01.921080544 +0100
@@ -221,6 +221,9 @@
 		"lc", PRO_INT, 0, 0, &block_comment_max_col
 	},
 	{
+		"lps", PRO_BOOL, false, ON, &leave_preprocessor_space
+	},
+	{
 		"lp", PRO_BOOL, true, ON, &lineup_to_parens
 	},
 	{
@@ -269,6 +272,9 @@
 		"nip", PRO_BOOL, true, OFF, &ps.indent_parameters
 	},
 	{
+		"nlps", PRO_BOOL, false, OFF, &leave_preprocessor_space
+	},
+	{
 		"nlp", PRO_BOOL, true, OFF, &lineup_to_parens
 	},
 	{
diff -ur pg_bsd_indent/indent.c pg_bsd_indent_patched/indent.c
--- pg_bsd_indent/indent.c	2014-01-31 04:06:43.0 +0100
+++ pg_bsd_indent_patched/indent.c	2017-02-08 01:56:59.039931984 +0100
@@ -1091,17 +1091,25 @@
 			(s_code != e_code))
 dump_line();
 			*e_lab++ = '#';	/* move whole line to 'label' buffer */
+			if (leave_preprocessor_space) {
+while (isblank((int)*buf_ptr)) {
+	CHECK_SIZE_LAB;
+	*e_lab++ = *buf_ptr++;
+}
+			}
+			else {
+while (isblank((int)*buf_ptr)) {
+	buf_ptr++;
+}
+			}
+			t_ptr = e_lab;
+
 			{
 int in_comment = 0;
 int com_start = 0;
 charquote = 0;
 int com_end = 0;
 
-while (*buf_ptr == ' ' || *buf_ptr == '\t') {
-	buf_ptr++;
-	if (buf_ptr >= buf_end)
-		fill_buffer();
-}
 while (*buf_ptr != '\n' || in_comment) {
 	CHECK_SIZE_LAB;
 	*e_lab = *buf_ptr++;
@@ -1179,7 +1187,7 @@
 ps.pcase = false;
 			}
 
-			if (strncmp(s_lab, "#if", 3) == 0) {
+			if (t_ptr[0] == 'i' && t_ptr[1] == 'f') {
 if (blanklines_around_conditional_compilation) {
 	int c;
 	prefix_blankline_requested++;
@@ -1192,7 +1200,7 @@
 } else
 	diag(1, "#if stack overflow");
 			} else
-if (strncmp(s_lab, "#else", 5) == 0) {
+if (t_ptr[0] == 'e' && t_ptr[1] == 'l') {
 	if (ifdef_level <= 0)
 		diag(1, "Unmatched #else");
 	else {
@@ -1200,7 +1208,7 @@
 		ps = state_stack[ifdef_level - 1];
 	}
 } else
-	if (strncmp(s_lab, "#endif", 6) == 0) {
+	if (strncmp(t_ptr, "endif", 5) == 0) {
 		if (ifdef_level <= 0)
 			diag(1, "Unmatched #endif");
 		else {
diff -ur pg_bsd_indent/indent_globs.h pg_bsd_indent_patched/indent_globs.h
--- pg_bsd_indent/indent_globs.h	2005-11-15 01:30:24.0 +0100
+++ pg_bsd_indent_patched/indent_globs.h	2017-02-08 01:57:51.003994806 +0100
@@ -222,6 +222,7 @@
 	 * "for(e;e;e)" should be indented an extra
 	 * tab stop so that they don't conflict with
 	 * the code that follows */
+EXTERN int leave_preprocessor_space;
 
 /* -troff font state information */
 

-- 
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers