[gnome-builder] python: improve indenting after list comprehensions, tuples, and dicts
- From: Christian Hergert <chergert src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-builder] python: improve indenting after list comprehensions, tuples, and dicts
- Date: Sun, 11 Jan 2015 10:09:10 +0000 (UTC)
commit d65c53d672e6fb3f3154694488049bfedafdc94c
Author: Christian Hergert <christian hergert me>
Date: Sun Jan 11 02:09:04 2015 -0800
python: improve indenting after list comprehensions, tuples, and dicts
src/auto-indent/gb-source-auto-indenter-python.c | 127 ++++++++++++++++++++++
1 files changed, 127 insertions(+), 0 deletions(-)
---
diff --git a/src/auto-indent/gb-source-auto-indenter-python.c
b/src/auto-indent/gb-source-auto-indenter-python.c
index 386e6d3..e578003 100644
--- a/src/auto-indent/gb-source-auto-indenter-python.c
+++ b/src/auto-indent/gb-source-auto-indenter-python.c
@@ -114,6 +114,92 @@ copy_indent (GbSourceAutoIndenterPython *python,
return g_string_free (str, FALSE);
}
+static gboolean
+backtrack_to_open_pair (GtkTextIter *iter)
+{
+ GtkTextIter copy;
+ GtkSourceBuffer *buffer;
+
+ buffer = GTK_SOURCE_BUFFER (gtk_text_iter_get_buffer (iter));
+ copy = *iter;
+
+ do
+ {
+ GtkTextIter match_start;
+ GtkTextIter match_end;
+ gunichar ch;
+
+ if (gtk_source_buffer_iter_has_context_class (buffer, ©, "comment") ||
+ gtk_source_buffer_iter_has_context_class (buffer, ©, "string"))
+ continue;
+
+ ch = gtk_text_iter_get_char (©);
+
+ switch (ch)
+ {
+ case '=':
+ return FALSE;
+
+ case '{':
+ case '(':
+ case '[':
+ *iter = copy;
+ return TRUE;
+
+ case ')':
+ if (!gtk_text_iter_backward_search (©, "(",
+ GTK_TEXT_SEARCH_TEXT_ONLY,
+ &match_start, &match_end,
+ NULL))
+ return FALSE;
+ copy = match_start;
+ break;
+
+ case ']':
+ if (!gtk_text_iter_backward_search (©, "[",
+ GTK_TEXT_SEARCH_TEXT_ONLY,
+ &match_start, &match_end,
+ NULL))
+ return FALSE;
+ copy = match_start;
+ break;
+
+ case '}':
+ if (!gtk_text_iter_backward_search (©, "{",
+ GTK_TEXT_SEARCH_TEXT_ONLY,
+ &match_start, &match_end,
+ NULL))
+ return FALSE;
+ copy = match_start;
+ break;
+
+ case '\'':
+ if (!gtk_text_iter_backward_search (©, "'",
+ GTK_TEXT_SEARCH_TEXT_ONLY,
+ &match_start, &match_end,
+ NULL))
+ return FALSE;
+ copy = match_start;
+ break;
+
+ case '"':
+ if (!gtk_text_iter_backward_search (©, "\"",
+ GTK_TEXT_SEARCH_TEXT_ONLY,
+ &match_start, &match_end,
+ NULL))
+ return FALSE;
+ copy = match_start;
+ break;
+
+ default:
+ break;
+ }
+ }
+ while (gtk_text_iter_backward_char (©));
+
+ return FALSE;
+}
+
static gchar *
copy_indent_minus_tab (GbSourceAutoIndenterPython *python,
GtkTextView *view,
@@ -334,6 +420,7 @@ indent_parens (GbSourceAutoIndenterPython *python,
return g_string_free (str, FALSE);
}
+#if 0
static gchar *
indent_previous_stmt (GbSourceAutoIndenterPython *python,
GtkTextView *text_view,
@@ -372,6 +459,7 @@ indent_previous_stmt (GbSourceAutoIndenterPython *python,
return NULL;
}
+#endif
static gchar *
gb_source_auto_indenter_python_format (GbSourceAutoIndenter *indenter,
@@ -411,8 +499,10 @@ gb_source_auto_indenter_python_format (GbSourceAutoIndenter *indenter,
case '[': /* Or this */
return indent_colon (python, text_view, begin, end, &iter);
+#if 0
case ')':
return indent_previous_stmt (python, text_view, begin, end, &iter);
+#endif
case ',':
return indent_parens (python, text_view, begin, end, &iter);
@@ -427,6 +517,43 @@ gb_source_auto_indenter_python_format (GbSourceAutoIndenter *indenter,
line_starts_with (&iter, "pass"))
return copy_indent_minus_tab (python, text_view, begin, end, &iter);
+ if (backtrack_to_open_pair (&iter))
+ {
+ GString *str;
+ guint offset;
+ guint i;
+
+ offset = gtk_text_iter_get_line_offset (&iter);
+ str = g_string_new (NULL);
+
+ for (i = 0; i <= offset; i++)
+ g_string_append (str, " ");
+
+ return g_string_free (str, FALSE);
+ }
+
+ if (ch == ')' || ch == ']' || ch == '}')
+ {
+ GtkTextIter copy;
+
+ copy = iter;
+
+ gtk_text_iter_backward_char (©);
+
+ if (backtrack_to_open_pair (©))
+ {
+ gtk_text_iter_set_line_offset (©, 0);
+ while (g_unichar_isspace (gtk_text_iter_get_char (©)) &&
+ !gtk_text_iter_ends_line (©) &&
+ gtk_text_iter_forward_char (©))
+ {
+ /* Do nothing */
+ }
+
+ return copy_indent (python, begin, end, ©);
+ }
+ }
+
return copy_indent (python, begin, end, &iter);
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]