1 /* ide-language.c
2 *
3 * Copyright 2016-2019 Christian Hergert <chergert@redhat.com>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * SPDX-License-Identifier: GPL-3.0-or-later
19 */
20
21 #define G_LOG_DOMAIN "ide-language"
22
23 #include "config.h"
24
25 #include <libide-io.h>
26 #include <string.h>
27 #include <tmpl-glib.h>
28
29 #include "ide-language.h"
30
31 gchar *
ide_language_format_header(GtkSourceLanguage * self,const gchar * header)32 ide_language_format_header (GtkSourceLanguage *self,
33 const gchar *header)
34 {
35 IdeLineReader reader;
36 const gchar *first_prefix;
37 const gchar *last_prefix;
38 const gchar *line_prefix;
39 const gchar *line;
40 gboolean first = TRUE;
41 GString *outstr;
42 gsize len;
43 guint prefix_len;
44
45 g_return_val_if_fail (GTK_SOURCE_IS_LANGUAGE (self), NULL);
46 g_return_val_if_fail (header != NULL, NULL);
47
48 first_prefix = gtk_source_language_get_metadata (self, "block-comment-start");
49 last_prefix = gtk_source_language_get_metadata (self, "block-comment-end");
50 line_prefix = gtk_source_language_get_metadata (self, "line-comment-start");
51
52 if ((g_strcmp0 (first_prefix, "/*") == 0) &&
53 (g_strcmp0 (last_prefix, "*/") == 0))
54 line_prefix = " *";
55
56 if (first_prefix == NULL || last_prefix == NULL)
57 {
58 first_prefix = line_prefix;
59 last_prefix = line_prefix;
60 }
61
62 prefix_len = strlen (first_prefix);
63
64 outstr = g_string_new (NULL);
65
66 ide_line_reader_init (&reader, (gchar *)header, -1);
67
68 while (NULL != (line = ide_line_reader_next (&reader, &len)))
69 {
70 if (first)
71 {
72 g_string_append (outstr, first_prefix);
73 first = FALSE;
74 }
75 else if (line_prefix == NULL)
76 {
77 for (guint i = 0; i < prefix_len; i++)
78 g_string_append_c (outstr, ' ');
79 }
80 else
81 {
82 g_string_append (outstr, line_prefix);
83 }
84
85 if (len)
86 {
87 g_string_append_c (outstr, ' ');
88 g_string_append_len (outstr, line, len);
89 }
90
91 /* Lines ending in expansion need an extra \n */
92 if (outstr->len > 2 &&
93 outstr->str[outstr->len - 2] == '}' &&
94 outstr->str[outstr->len - 1] == '}')
95 g_string_append_c (outstr, '\n');
96
97 g_string_append_c (outstr, '\n');
98 }
99
100 if (last_prefix && g_strcmp0 (first_prefix, last_prefix) != 0)
101 {
102 if (line_prefix && *line_prefix == ' ')
103 g_string_append_c (outstr, ' ');
104 g_string_append (outstr, last_prefix);
105 g_string_append_c (outstr, '\n');
106 }
107
108 return g_string_free (outstr, FALSE);
109 }
110