~ubuntu-branches/ubuntu/maverick/conglomerate/maverick

« back to all changes in this revision

Viewing changes to src/cong-source-layout.c

  • Committer: Bazaar Package Importer
  • Author(s): Daniel T Chen
  • Date: 2005-11-08 05:07:06 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20051108050706-bcg60nwqf1z3w0d6
Tags: 0.9.1-1ubuntu1
* Resynchronise with Debian (Closes: #4397).
  - Thanks, Jordan Mantha.

Show diffs side-by-side

added added

removed removed

Lines of Context:
43
43
        const CongSourceCleanupOptions *options;
44
44
};
45
45
 
46
 
 
47
46
static gchar*
48
47
generate_indentation(const CongSourceCleanupOptions *options, guint indent_level)
49
48
{
198
197
        return result_string;   
199
198
}
200
199
 
 
200
typedef struct _CongCleanupSourceUpdateLocationData {
 
201
        CongNodePtr node;
 
202
        
 
203
        /* Used in whitespace stripping */
 
204
        const gchar *old_content;
 
205
        const gchar *new_content;
 
206
        
 
207
        /* Used in indentation */
 
208
        gint indent;
 
209
 
 
210
} CongCleanupSourceUpdateLocationData;
 
211
 
 
212
static gboolean
 
213
strip_whitespace_update_location_callback (CongDocument *doc,
 
214
                                           CongLocation *location, 
 
215
                                           gpointer user_data)
 
216
{
 
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;
 
221
 
 
222
        g_assert (location_data->node);
 
223
        g_assert (cong_node_type (location_data->node)==CONG_NODE_TYPE_TEXT);
 
224
        
 
225
        old_content = location_data->old_content;
 
226
        new_content = location_data->new_content;
 
227
 
 
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);
 
233
                }       
 
234
                
 
235
                while (*old_content != 0 && *new_content != 0 ) {
 
236
 
 
237
                    if (valid) {
 
238
                        if (old_content - location_data->old_content >= location->byte_offset) 
 
239
                            break;
 
240
                    } else {
 
241
                        valid = TRUE;    
 
242
                    }
 
243
                
 
244
                    if (*new_content == ' ') {
 
245
                        new_next_char = g_utf8_get_char (new_content + 1);
 
246
                        old_char = g_utf8_get_char (old_content);
 
247
 
 
248
                        if (new_next_char != old_char) {
 
249
                            old_content = g_utf8_next_char (old_content);
 
250
                            valid = FALSE;
 
251
                        } else {
 
252
                            new_content = new_content + 1;
 
253
                        }
 
254
                    } else {
 
255
                            old_content = g_utf8_next_char (old_content);
 
256
                            new_content = g_utf8_next_char (new_content);
 
257
                    }
 
258
                } 
 
259
 
 
260
                location->byte_offset = new_content - location_data->new_content;       
 
261
                
 
262
                return TRUE;
 
263
        }
 
264
 
 
265
        return FALSE;
 
266
}
 
267
 
201
268
static gboolean strip_whitespace_callback(CongDocument *doc, CongNodePtr node, gpointer user_data, guint recursion_level)
202
269
{
203
270
        CongSourceCleanupData *cleanup_data = user_data;
 
271
        CongCleanupSourceUpdateLocationData update_location_data;
204
272
 
205
273
        if (cong_node_type(node)==CONG_NODE_TYPE_TEXT) {
206
274
                if (cong_node_get_whitespace_handling (doc, node)==CONG_WHITESPACE_NORMALIZE) {
221
289
                                }
222
290
                        }
223
291
 
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);
 
294
                        
 
295
                        update_location_data.node = node;
 
296
                        update_location_data.old_content = (const gchar*)node->content;
 
297
                        update_location_data.new_content = new_content;
226
298
 
 
299
                        cong_command_for_each_location (cleanup_data->cmd,
 
300
                                                        strip_whitespace_update_location_callback, 
 
301
                                                        &update_location_data);
 
302
                        
227
303
                        cong_command_add_node_set_text (cleanup_data->cmd,
228
304
                                                        node,
229
305
                                                        new_content);
261
337
        }
262
338
}
263
339
 
 
340
static gboolean
 
341
add_indentation_update_location_callback (CongDocument *doc,
 
342
                                           CongLocation *location, 
 
343
                                             gpointer user_data)
 
344
{
 
345
        CongCleanupSourceUpdateLocationData *location_data = (CongCleanupSourceUpdateLocationData *) user_data;
 
346
 
 
347
        g_assert (location_data->node);
 
348
        g_assert (cong_node_type (location_data->node)==CONG_NODE_TYPE_TEXT);
 
349
        
 
350
        if (location->node == location_data->node) {
 
351
                location->byte_offset += location_data->indent; 
 
352
                return TRUE;
 
353
        }
 
354
 
 
355
        return FALSE;
 
356
}
 
357
 
264
358
static gboolean add_indentation_callback(CongDocument *doc, CongNodePtr node, gpointer user_data, guint recursion_level)
265
359
{
266
360
        CongSourceCleanupData *cleanup_data = user_data;
270
364
        g_assert (options);
271
365
        g_assert (node);
272
366
 
273
 
/*      g_message("cleanup_source_callback(%s)", cong_node_debug_description(node)); */
274
 
 
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)) {
324
416
 
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))
328
420
                {
329
421
                        if (cong_util_is_recursively_inline (doc, node->parent)) {
330
422
                                /* Do nothing: */
331
423
                        } else {
 
424
                                CongCleanupSourceUpdateLocationData update_location_data;
 
425
                                
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);
334
428
                                
335
429
                                cong_command_add_node_set_text (cleanup_data->cmd, node, new_content);
336
430
                                
 
431
                                update_location_data.node = node;
 
432
                                update_location_data.indent = strlen (indentation);
 
433
                                
 
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);
339
439
                        }