[geany/geany] fe0280: Indentation width detection: try not to get fooled by comments

Colomban Wendling git-noreply at xxxxx
Sun Jun 2 20:42:47 UTC 2013

Branch:      refs/heads/master
Author:      Colomban Wendling <ban at herbesfolles.org>
Committer:   Colomban Wendling <ban at herbesfolles.org>
Date:        Sun, 02 Jun 2013 20:42:47 UTC
Commit:      fe0280037d9b964e56c405b1efedbcea5cec762e

Log Message:
Indentation width detection: try not to get fooled by comments

C-style multiline comments, used among others in C, C++ and Java, are
often continued on next lines with an additional space followed by an

  1.    /* first comment line
  2.     * continuation line (asterisk is aligned with previous line)
  3.     * last line */

This fools the indentation with detection because lines 2 and 3 from
the above example have an extra space in what is considered being the
line indentation.  In this example, the algorithm would detect an
indentation width of 5 rather than 4, because here most lines have an
indent of 5 -- although they actually have an indent of 4 plus a space
for alignment.  This is not a problem in most situations because there
generally are fewer comment continuation lines than actual code lines
which have a indent multiple of the actual indent width, but with some
code with a lot of comments (e.g. short functions with verbose
documentation comments) this might start to fool the algorithm and
give wrong, annoying, results.

So, try to detect these continuation lines and avoid taking them into

Modified Paths:

Modified: src/document.c
10 files changed, 10 insertions(+), 0 deletions(-)
@@ -976,6 +976,16 @@ static gboolean detect_indent_width(GeanyEditor *editor, GeanyIndentType type, g
 	line_count = sci_get_line_count(sci);
 	for (line = 0; line < line_count; line++)
+		gint pos = sci_get_line_indent_position(sci, line);
+		/* We probably don't have style info yet, because we're generally called just after
+		 * the document got created, so we can't use highlighting_is_code_style().
+		 * That's not good, but the assumption below that concerning lines start with an
+		 * asterisk (common continuation character for C/C++/Java/...) should do the trick
+		 * without removing too much legitimate lines. */
+		if (sci_get_char_at(sci, pos) == '*')
+			continue;
 		width = sci_get_line_indentation(sci, line);
 		/* most code will have indent total <= 24, otherwise it's more likely to be
 		 * alignment than indentation */

This E-Mail was brought to you by github_commit_mail.py (Source: https://github.com/geany/infrastructure).

More information about the Commits mailing list