[geany/geany] d152c5: Properly indent even if the indenting character isn't the last one

Colomban Wendling git-noreply at xxxxx
Tue Aug 7 15:50:17 UTC 2012


Branch:      refs/heads/master
Author:      Colomban Wendling <ban at herbesfolles.org>
Committer:   Colomban Wendling <ban at herbesfolles.org>
Date:        Tue, 07 Aug 2012 15:50:17
Commit:      d152c5ce2235c0bd34cc76613e3045100f95a4b9
             https://github.com/geany/geany/commit/d152c5ce2235c0bd34cc76613e3045100f95a4b9

Log Message:
-----------
Properly indent even if the indenting character isn't the last one

For all languages, this change allows comments at the end of the
checked line, e.g.:

	if 42:  # magic number
		print("I'm indented!")

For languages with braces, it also properly indent if there is code
on the same line as the opening brace, e.g.:

	if (42) { printf("some code here...");
		printf("...but I'm properly indented");
	} else { /* comment! */
		printf("normal block is fine too, of course");
	}

Although this is uncommon (and quite ugly) it's valid and should be
handled properly.


Modified Paths:
--------------
    src/editor.c

Modified: src/editor.c
61 files changed, 34 insertions(+), 27 deletions(-)
===================================================================
@@ -1292,45 +1292,52 @@ static void read_indent(GeanyEditor *editor, gint pos)
 
 static gint get_brace_indent(ScintillaObject *sci, gint line)
 {
-	guint i, len;
-	gint ret = 0;
-	gchar *linebuf;
-
-	len = sci_get_line_length(sci, line);
-	linebuf = sci_get_line(sci, line);
+	gint start = sci_get_position_from_line(sci, line);
+	gint end = sci_get_line_end_position(sci, line) - 1;
+	gint lexer = sci_get_lexer(sci);
+	gint count = 0;
+	gint pos;
 
-	for (i = 0; i < len; i++)
+	for (pos = end; pos >= start && count < 1; pos--)
 	{
-		/* i == (len - 1) prevents wrong indentation after lines like
-		 * "	{ return bless({}, shift); }" (Perl) */
-		if (linebuf[i] == '{' && i == (len - 1))
+		if (highlighting_is_code_style(lexer, sci_get_style_at(sci, pos)))
 		{
-			ret++;
-			break;
+			gchar c = sci_get_char_at(sci, pos);
+
+			if (c == '{')
+				count ++;
+			else if (c == '}')
+				count --;
 		}
-		else
-		{
-			gint k = len - 1;
+	}
 
-			while (k > 0 && isspace(linebuf[k])) k--;
+	return count > 0 ? 1 : 0;
+}
 
-			/* if last non-whitespace character is a { increase indentation by a tab
-			 * e.g. for (...) { */
-			if (linebuf[k] == '{')
-			{
-				ret++;
-			}
+
+/* gets the last code position on a line
+ * warning: if there is no code position on the line, returns the start position */
+static gint get_sci_line_code_end_position(ScintillaObject *sci, gint line)
+{
+	gint start = sci_get_position_from_line(sci, line);
+	gint lexer = sci_get_lexer(sci);
+	gint pos;
+
+	for (pos = sci_get_line_end_position(sci, line) - 1; pos > start; pos--)
+	{
+		gint style = sci_get_style_at(sci, pos);
+
+		if (highlighting_is_code_style(lexer, style) && ! isspace(sci_get_char_at(sci, pos)))
 			break;
-		}
 	}
-	g_free(linebuf);
-	return ret;
+
+	return pos;
 }
 
 
 static gint get_python_indent(ScintillaObject *sci, gint line)
 {
-	gint last_char = sci_get_line_end_position(sci, line) - 1;
+	gint last_char = get_sci_line_code_end_position(sci, line);
 
 	/* add extra indentation for Python after colon */
 	if (sci_get_char_at(sci, last_char) == ':' &&
@@ -1345,7 +1352,7 @@ static gint get_python_indent(ScintillaObject *sci, gint line)
 static gint get_xml_indent(ScintillaObject *sci, gint line)
 {
 	gboolean need_close = FALSE;
-	gint end = sci_get_line_end_position(sci, line) - 1;
+	gint end = get_sci_line_code_end_position(sci, line);
 	gint pos;
 
 	/* don't indent if there's a closing tag to the right of the cursor */


@@ Diff output truncated at 100000 characters. @@


--------------
This E-Mail was brought to you by github_commit_mail.py (Source: TBD).



More information about the Commits mailing list