198
197
return result_string;
200
typedef struct _CongCleanupSourceUpdateLocationData {
203
/* Used in whitespace stripping */
204
const gchar *old_content;
205
const gchar *new_content;
207
/* Used in indentation */
210
} CongCleanupSourceUpdateLocationData;
213
strip_whitespace_update_location_callback (CongDocument *doc,
214
CongLocation *location,
217
CongCleanupSourceUpdateLocationData *location_data = (CongCleanupSourceUpdateLocationData *) user_data;
218
const gchar *old_content, *new_content;
219
gunichar old_char, new_next_char;
220
gboolean valid = TRUE;
222
g_assert (location_data->node);
223
g_assert (cong_node_type (location_data->node)==CONG_NODE_TYPE_TEXT);
225
old_content = location_data->old_content;
226
new_content = location_data->new_content;
228
if (location->node == location_data->node) {
229
if (*new_content != ' ') {
230
new_next_char = g_utf8_get_char (new_content);
231
while (g_utf8_get_char (old_content) != new_next_char && *old_content != 0)
232
old_content = g_utf8_next_char (old_content);
235
while (*old_content != 0 && *new_content != 0 ) {
238
if (old_content - location_data->old_content >= location->byte_offset)
244
if (*new_content == ' ') {
245
new_next_char = g_utf8_get_char (new_content + 1);
246
old_char = g_utf8_get_char (old_content);
248
if (new_next_char != old_char) {
249
old_content = g_utf8_next_char (old_content);
252
new_content = new_content + 1;
255
old_content = g_utf8_next_char (old_content);
256
new_content = g_utf8_next_char (new_content);
260
location->byte_offset = new_content - location_data->new_content;
201
268
static gboolean strip_whitespace_callback(CongDocument *doc, CongNodePtr node, gpointer user_data, guint recursion_level)
203
270
CongSourceCleanupData *cleanup_data = user_data;
271
CongCleanupSourceUpdateLocationData update_location_data;
205
273
if (cong_node_type(node)==CONG_NODE_TYPE_TEXT) {
206
274
if (cong_node_get_whitespace_handling (doc, node)==CONG_WHITESPACE_NORMALIZE) {
224
new_content = cong_util_strip_whitespace_from_string (node->content,
292
new_content = cong_util_strip_whitespace_from_string ((const gchar*)node->content,
225
293
strip_all_initial_whitespace);
295
update_location_data.node = node;
296
update_location_data.old_content = (const gchar*)node->content;
297
update_location_data.new_content = new_content;
299
cong_command_for_each_location (cleanup_data->cmd,
300
strip_whitespace_update_location_callback,
301
&update_location_data);
227
303
cong_command_add_node_set_text (cleanup_data->cmd,
341
add_indentation_update_location_callback (CongDocument *doc,
342
CongLocation *location,
345
CongCleanupSourceUpdateLocationData *location_data = (CongCleanupSourceUpdateLocationData *) user_data;
347
g_assert (location_data->node);
348
g_assert (cong_node_type (location_data->node)==CONG_NODE_TYPE_TEXT);
350
if (location->node == location_data->node) {
351
location->byte_offset += location_data->indent;
264
358
static gboolean add_indentation_callback(CongDocument *doc, CongNodePtr node, gpointer user_data, guint recursion_level)
266
360
CongSourceCleanupData *cleanup_data = user_data;
270
364
g_assert (options);
273
/* g_message("cleanup_source_callback(%s)", cong_node_debug_description(node)); */
275
367
if (node->parent) {
276
368
if (cong_util_is_recursively_inline (doc, node)) {
277
369
if (cong_util_is_recursively_inline (doc, node->parent)) {
325
417
case CONG_NODE_TYPE_TEXT:
326
418
/* Clean up a text node, provided it's not one of the whitespace ones we've added ourselves: */
327
if (!cong_util_is_pure_whitespace (node->content))
419
if (!cong_util_is_pure_whitespace ((const gchar*)node->content))
329
421
if (cong_util_is_recursively_inline (doc, node->parent)) {
330
422
/* Do nothing: */
424
CongCleanupSourceUpdateLocationData update_location_data;
332
426
gchar *indentation = generate_indentation (options, recursion_level);
333
gchar *new_content = g_strdup_printf ("%s%s\n", indentation, node->content);
427
gchar *new_content = g_strconcat (indentation, node->content, NULL);
335
429
cong_command_add_node_set_text (cleanup_data->cmd, node, new_content);
431
update_location_data.node = node;
432
update_location_data.indent = strlen (indentation);
434
cong_command_for_each_location (cleanup_data->cmd,
435
add_indentation_update_location_callback,
436
&update_location_data);
337
437
g_free (indentation);
338
438
g_free (new_content);