[gtksourceview/wip/chergert/vim] improve paragraph text objects
- From: Christian Hergert <chergert src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gtksourceview/wip/chergert/vim] improve paragraph text objects
- Date: Fri, 5 Nov 2021 20:07:39 +0000 (UTC)
commit 9c53ba0c78ebbd82efd5fe140d5e570b6102e362
Author: Christian Hergert <chergert redhat com>
Date: Fri Nov 5 13:07:35 2021 -0700
improve paragraph text objects
gtksourceview/vim/gtk-source-vim-text-object.c | 86 +++++++++++++++++++++++++-
testsuite/test-vim-text-object.c | 19 ++++++
2 files changed, 103 insertions(+), 2 deletions(-)
---
diff --git a/gtksourceview/vim/gtk-source-vim-text-object.c b/gtksourceview/vim/gtk-source-vim-text-object.c
index a91daecd..e80ed4a9 100644
--- a/gtksourceview/vim/gtk-source-vim-text-object.c
+++ b/gtksourceview/vim/gtk-source-vim-text-object.c
@@ -169,6 +169,46 @@ gtk_source_vim_iter__forward_sentence_end (GtkTextIter *iter)
return FALSE;
}
+static gboolean
+gtk_source_vim_iter_is_paragraph_break (const GtkTextIter *iter)
+{
+ return gtk_text_iter_is_end (iter) || is_empty_line (iter);
+}
+
+static gboolean
+gtk_source_vim_iter__backward_paragraph_start (GtkTextIter *iter)
+{
+ while (!is_empty_line (iter))
+ {
+ if (gtk_text_iter_is_start (iter))
+ return TRUE;
+ gtk_text_iter_backward_line (iter);
+ }
+
+ return TRUE;
+}
+
+static gboolean
+gtk_source_vim_iter__forward_paragraph_end (GtkTextIter *iter)
+{
+ while (!is_empty_line (iter))
+ {
+ if (gtk_text_iter_is_end (iter))
+ return TRUE;
+
+ gtk_text_iter_forward_line (iter);
+
+ if (is_empty_line (iter))
+ {
+ /* Place at the end of the previous non-empty line */
+ gtk_text_iter_backward_char (iter);
+ return TRUE;
+ }
+ }
+
+ return TRUE;
+}
+
static void
backward_to_first_space (GtkTextIter *iter)
{
@@ -246,6 +286,48 @@ text_object_extend_paragraph (const GtkTextIter *origin,
GtkTextIter *a_begin,
GtkTextIter *a_end)
{
+ GtkTextIter next;
+
+ if (is_empty_line (a_begin))
+ {
+ GtkTextIter prev = *a_begin;
+
+ while (gtk_text_iter_backward_line (&prev) ||
+ gtk_text_iter_is_start (&prev))
+ {
+ if (!is_empty_line (&prev))
+ {
+ gtk_text_iter_forward_to_line_end (&prev);
+ *a_begin = prev;
+ break;
+ }
+ else if (gtk_text_iter_is_start (&prev))
+ {
+ *a_begin = prev;
+ break;
+ }
+ }
+ }
+
+ next = *a_end;
+
+ while (gtk_text_iter_forward_line (&next) ||
+ gtk_text_iter_is_end (&next))
+ {
+ if (!is_empty_line (&next))
+ break;
+
+ *a_end = next;
+
+ if (gtk_text_iter_is_end (&next))
+ break;
+ }
+
+ if (is_empty_line (inner_begin))
+ *inner_begin = *a_begin;
+
+ if (is_empty_line (inner_end))
+ *inner_end = *a_end;
}
static void
@@ -313,7 +395,7 @@ gtk_source_vim_text_object_new_##name (void)
TEXT_OBJECT_CTOR (inner_word, ends_word, starts_word, forward_word_end, backward_word_start, word, INNER);
TEXT_OBJECT_CTOR (inner_WORD, always_false, starts_WORD, forward_WORD_end, backward_WORD_start, word, INNER);
TEXT_OBJECT_CTOR (inner_sentence, ends_sentence, always_false, _forward_sentence_end,
backward_sentence_start, sentence, INNER);
-TEXT_OBJECT_CTOR (inner_paragraph, always_false, always_false, forward_paragraph_end,
backward_paragraph_start, paragraph, INNER);
+TEXT_OBJECT_CTOR (inner_paragraph, is_paragraph_break, is_paragraph_break, _forward_paragraph_end,
_backward_paragraph_start, paragraph, INNER);
TEXT_OBJECT_CTOR (inner_block_paren, ends_paren, starts_paren, forward_block_paren_end,
backward_block_paren_start, one, INNER);
TEXT_OBJECT_CTOR (inner_block_brace, ends_brace, starts_brace, forward_block_brace_end,
backward_block_brace_start, one, INNER);
TEXT_OBJECT_CTOR (inner_block_bracket, ends_bracket, starts_bracket, forward_block_bracket_end,
backward_block_bracket_start, one, INNER);
@@ -325,7 +407,7 @@ TEXT_OBJECT_CTOR (inner_quote_grave, ends_quote_grave, always_false, forward_quo
TEXT_OBJECT_CTOR (a_word, ends_word, starts_word, forward_word_end, backward_word_start, word, A);
TEXT_OBJECT_CTOR (a_WORD, ends_WORD, starts_WORD, forward_WORD_end, backward_WORD_start, word, A);
TEXT_OBJECT_CTOR (a_sentence, ends_sentence, always_false, _forward_sentence_end, backward_sentence_start,
sentence, A);
-TEXT_OBJECT_CTOR (a_paragraph, always_false, always_false, forward_paragraph_end, backward_paragraph_start,
paragraph, A);
+TEXT_OBJECT_CTOR (a_paragraph, is_paragraph_break, is_paragraph_break, _forward_paragraph_end,
_backward_paragraph_start, paragraph, A);
TEXT_OBJECT_CTOR (a_block_paren, ends_paren, starts_paren, forward_block_paren_end,
backward_block_paren_start, one, A);
TEXT_OBJECT_CTOR (a_block_brace, ends_brace, starts_brace, forward_block_brace_end,
backward_block_brace_start, one, A);
TEXT_OBJECT_CTOR (a_block_bracket, ends_bracket, starts_bracket, forward_block_bracket_end,
backward_block_bracket_start, one, A);
diff --git a/testsuite/test-vim-text-object.c b/testsuite/test-vim-text-object.c
index b958123e..8dcfc9b7 100644
--- a/testsuite/test-vim-text-object.c
+++ b/testsuite/test-vim-text-object.c
@@ -180,6 +180,24 @@ test_sentence (void)
run_test (gtk_source_vim_text_object_new_a_sentence (), "\n a. b! c?", 2, "a. ");
}
+static void
+test_paragraph (void)
+{
+ run_test (gtk_source_vim_text_object_new_inner_paragraph (), "testing this.\n\n\n", 0, "testing
this.");
+ run_test (gtk_source_vim_text_object_new_inner_paragraph (), "testing this.\n", 5, "testing this.");
+ run_test (gtk_source_vim_text_object_new_inner_paragraph (), "\n\n", 0, "\n\n");
+ run_test (gtk_source_vim_text_object_new_inner_paragraph (), "\n\n", 1, "\n\n");
+ run_test (gtk_source_vim_text_object_new_inner_paragraph (), "\n\n\n", 1, "\n\n\n");
+ run_test (gtk_source_vim_text_object_new_inner_paragraph (), "what\nwill\n we\n\nfind\nhere.", 1,
"what\nwill\n we");
+
+ run_test (gtk_source_vim_text_object_new_a_paragraph (), "testing this.\n\n\n", 0, "testing
this.\n\n\n");
+ run_test (gtk_source_vim_text_object_new_a_paragraph (), "testing this.\n", 5, "testing this.\n");
+ run_test (gtk_source_vim_text_object_new_a_paragraph (), "\n\n", 0, "\n\n");
+ run_test (gtk_source_vim_text_object_new_a_paragraph (), "\n\n", 1, "\n\n");
+ run_test (gtk_source_vim_text_object_new_a_paragraph (), "\n\n\n", 1, "\n\n\n");
+ run_test (gtk_source_vim_text_object_new_a_paragraph (), "what\nwill\n we\n\nfind\nhere.", 1,
"what\nwill\n we\n");
+}
+
int
main (int argc,
char *argv[])
@@ -194,6 +212,7 @@ main (int argc,
g_test_add_func ("/GtkSourceView/vim-text-object/block", test_block);
g_test_add_func ("/GtkSourceView/vim-text-object/quote", test_quote);
g_test_add_func ("/GtkSourceView/vim-text-object/sentence", test_sentence);
+ g_test_add_func ("/GtkSourceView/vim-text-object/paragraph", test_paragraph);
ret = g_test_run ();
gtk_source_finalize ();
return ret;
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]