summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian Hergert <chergert@redhat.com>2022-07-22 15:32:39 -0700
committerChristian Hergert <chergert@redhat.com>2022-07-22 15:32:39 -0700
commita9a7f6b0c8a826c18164db4a5b8894f72e1df3ab (patch)
treeb4d2f21a989ae7f6c178a263d036d98ede7b4087
parentd03d0dee7b26bea3d7d55b01f627f112ab97cacd (diff)
downloadgtksourceview-a9a7f6b0c8a826c18164db4a5b8894f72e1df3ab.tar.gz
snippets: ensure final chunk is included in snippet output
We need to flush out any pending snippet text when parsing from strings or we risk loosing the final chunk. Fixes #252
-rw-r--r--gtksourceview/gtksourcesnippetbundle-parser.c2
-rw-r--r--testsuite/test-snippets.c32
2 files changed, 34 insertions, 0 deletions
diff --git a/gtksourceview/gtksourcesnippetbundle-parser.c b/gtksourceview/gtksourcesnippetbundle-parser.c
index 69ddb06d..651f81c7 100644
--- a/gtksourceview/gtksourcesnippetbundle-parser.c
+++ b/gtksourceview/gtksourcesnippetbundle-parser.c
@@ -342,6 +342,8 @@ _gtk_source_snippet_bundle_parse_text (const gchar *text,
}
}
+ flush_chunk (&parser);
+
goto finish;
handle_error:
diff --git a/testsuite/test-snippets.c b/testsuite/test-snippets.c
index d84cb7bb..f93bda6c 100644
--- a/testsuite/test-snippets.c
+++ b/testsuite/test-snippets.c
@@ -114,9 +114,40 @@ test_snippet_parse (void)
g_assert_cmpstr ("", ==, gtk_source_snippet_chunk_get_text (chunk));
chunk = gtk_source_snippet_get_nth_chunk (snippet, 3);
+ g_assert_nonnull (chunk);
+ g_assert_cmpint (0, ==, gtk_source_snippet_chunk_get_focus_position (chunk));
+ g_assert_cmpstr ("", ==, gtk_source_snippet_chunk_get_spec (chunk));
+
+ g_assert_finalize_object (snippet);
+}
+
+static void
+test_snippet_parse_issue_252 (void)
+{
+ GtkSourceSnippet *snippet;
+ GtkSourceSnippetChunk *chunk;
+ GError *error = NULL;
+
+ snippet = gtk_source_snippet_new_parsed ("a\n$0\nb", &error);
+ g_assert_no_error (error);
+ g_assert_nonnull (snippet);
+
+ g_assert_cmpint (3, ==, gtk_source_snippet_get_n_chunks (snippet));
+
+ chunk = gtk_source_snippet_get_nth_chunk (snippet, 0);
+ g_assert_nonnull (chunk);
+ g_assert_cmpint (-1, ==, gtk_source_snippet_chunk_get_focus_position (chunk));
+ g_assert_cmpstr ("a\n", ==, gtk_source_snippet_chunk_get_spec (chunk));
+
+ chunk = gtk_source_snippet_get_nth_chunk (snippet, 1);
+ g_assert_nonnull (chunk);
g_assert_cmpint (0, ==, gtk_source_snippet_chunk_get_focus_position (chunk));
g_assert_cmpstr ("", ==, gtk_source_snippet_chunk_get_spec (chunk));
+
+ chunk = gtk_source_snippet_get_nth_chunk (snippet, 2);
g_assert_nonnull (chunk);
+ g_assert_cmpint (-1, ==, gtk_source_snippet_chunk_get_focus_position (chunk));
+ g_assert_cmpstr ("\nb", ==, gtk_source_snippet_chunk_get_spec (chunk));
g_assert_finalize_object (snippet);
}
@@ -133,6 +164,7 @@ main (gint argc,
g_test_add_func ("/SourceView/Snippets/parse-bundle", test_simple);
g_test_add_func ("/SourceView/Snippets/new-parsed", test_snippet_parse);
+ g_test_add_func ("/SourceView/Snippets/$0-in-middle", test_snippet_parse_issue_252);
g_test_add_func ("/SourceView/Snippets/snippet-fetching", test_snippet_fetching);
ret = g_test_run ();