1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
|
/*-*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
/*
cpp-java-utils.c
Copyright (C) 2000 Naba Kumar <naba@gnome.org>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <libanjuta/interfaces/ianjuta-editor-cell.h>
#include "python-utils.h"
#define LEFT_BRACE(ch) (ch == ')'? '(' : (ch == '}'? '{' : (ch == ']'? '[' : ch)))
gboolean
python_util_jump_to_matching_brace (IAnjutaIterable *iter, gchar brace, gint limit)
{
gchar point_ch = brace;
gint cur_iteration = 0;
gboolean use_limit = (limit > 0);
GString *braces_stack = g_string_new ("");
g_return_val_if_fail (point_ch == ')' || point_ch == ']' ||
point_ch == '}', FALSE);
/* DEBUG_PRINT ("%s", "Matching brace being"); */
/* Push brace */
g_string_prepend_c (braces_stack, point_ch);
while (ianjuta_iterable_previous (iter, NULL))
{
/* Check limit */
cur_iteration++;
if (use_limit && cur_iteration > limit)
break;
/* Skip comments and strings */
IAnjutaEditorAttribute attrib =
ianjuta_editor_cell_get_attribute (IANJUTA_EDITOR_CELL (iter), NULL);
if (attrib == IANJUTA_EDITOR_COMMENT || attrib == IANJUTA_EDITOR_STRING)
continue;
/* DEBUG_PRINT ("%s", "point ch = %c", point_ch); */
point_ch = ianjuta_editor_cell_get_char (IANJUTA_EDITOR_CELL (iter), 0,
NULL);
if (point_ch == ')' || point_ch == ']' || point_ch == '}')
{
/* Push brace */
g_string_prepend_c (braces_stack, point_ch);
continue;
}
if (point_ch == LEFT_BRACE (braces_stack->str[0]))
{
/* Pop brace */
g_string_erase (braces_stack, 0, 1);
}
/* Bail out if there is no more in stack */
if (braces_stack->str[0] == '\0')
{
/* DEBUG_PRINT ("%s", "Matching brace end -- found"); */
return TRUE;
}
}
/* DEBUG_PRINT ("%s", "Matching brace end -- not found"); */
return FALSE;
}
|