Beispiel #1
0
static void
toggle_comment_multiline (IAnjutaEditor *editor,
                          IAnjutaIterable *start,
                          IAnjutaIterable *end)
{
    IAnjutaIterable *start_copy, *end_copy;
    gchar *text;
    gboolean is_commented;

    start_copy = ianjuta_iterable_clone (start, NULL);
    end_copy = ianjuta_iterable_clone (end, NULL);
    is_commented = is_commented_multiline (editor, start_copy, end_copy);
    text = ianjuta_editor_get_text (editor, start_copy, end_copy, NULL);

    if (is_commented)
    {
        ianjuta_editor_erase (editor, start_copy, end_copy, NULL);
        ianjuta_editor_insert (editor, start_copy, text + 2,
                               (strlen (text) - 4), NULL);
    }
    else
    {
        ianjuta_editor_insert (editor, end, "*/", -1, NULL);
        ianjuta_editor_insert (editor, start, "/*", -1, NULL);
    }

    g_object_unref (start_copy);
    g_object_unref (end_copy);
    g_free (text);
}
Beispiel #2
0
static gboolean
match_keyword (MacroPlugin * plugin, GtkTreeIter * iter, const gchar *keyword)
{
	gchar *name;
	gint offset = 0;
	
	gtk_tree_model_get(macro_db_get_model(plugin->macro_db), iter,
		MACRO_NAME, &name, -1);
	if ( name && strcmp(keyword, name) == 0)
	{
		gchar* text = macro_db_get_macro(plugin, plugin->macro_db, iter, &offset);
		if (plugin->current_editor != NULL && text != NULL)
		{
			gint i;
			IAnjutaIterable *pos =
				ianjuta_editor_get_position (IANJUTA_EDITOR(plugin->current_editor),
											 NULL);
			ianjuta_editor_insert (IANJUTA_EDITOR (plugin->current_editor),
			                       pos, text, -1, NULL);
			for (i = 0; i < offset; i++)
				ianjuta_iterable_next (pos, NULL);
			ianjuta_editor_goto_position (IANJUTA_EDITOR(plugin->current_editor), 
			                              pos, NULL);
			g_free(text);
			g_object_unref (pos);
		}
		return TRUE;
	}
	return FALSE;
}
Beispiel #3
0
static void
insert_editor_blocked (IAnjutaEditor* editor,
                       IAnjutaIterable* iter,
                       gchar* text,
                       IndentCPlugin* plugin)
{
	g_signal_handlers_block_by_func (editor, cpp_java_indentation_char_added, plugin);
	ianjuta_editor_insert (editor, iter, text, -1, NULL);
	g_signal_handlers_unblock_by_func (editor, cpp_java_indentation_char_added, plugin);
}
Beispiel #4
0
static void
iprovider_activate (IAnjutaProvider *obj, IAnjutaIterable* iter,  gpointer data, GError **err)
{
	DEBUG_PRINT("activate");

	JSLang *plugin = (JSLang*)obj;
	gchar *str = (gchar*)data;

	g_assert (plugin->current_editor);
	g_assert (str);

	gint a = ianjuta_iterable_diff (plugin->last, iter, NULL);

	ianjuta_editor_insert (IANJUTA_EDITOR (plugin->current_editor), iter, str + a, -1, NULL);
			
	gchar *sym = code_completion_get_str (IANJUTA_EDITOR (plugin->current_editor), FALSE);
	g_assert (sym != NULL);

	if (sym && code_completion_is_symbol_func (plugin, sym))
	{
		IAnjutaIterable *position = ianjuta_editor_get_position (IANJUTA_EDITOR (plugin->current_editor), NULL);

		if (g_settings_get_boolean (plugin->prefs, ADD_BRACE_AFTER_FUNCCALL))
		{
			ianjuta_editor_insert (IANJUTA_EDITOR (plugin->current_editor), position, " (", -1, NULL);
		}
		if (g_settings_get_boolean (plugin->prefs, SHOW_CALLTIPS))
		{
/*			GList *t = NULL;
			gchar *args = code_completion_get_func_tooltip (plugin, sym);
			t = g_list_append (t, args);
			if (args)
			{
				ianjuta_editor_tip_show (IANJUTA_EDITOR_TIP(plugin->current_editor), t,
							 position, NULL);

				g_free (args);
			}*/
		}
	}
	g_free (sym);
}
Beispiel #5
0
static gboolean insert_after_mark (IAnjutaEditor* editor, gchar* mark,
                                   gchar* code_to_add, CppJavaPlugin* lang_plugin)
{
       IAnjutaIterable* mark_position;
       mark_position = language_support_get_mark_position (editor, mark);
       if (!mark_position)
        return FALSE;

       ianjuta_editor_insert (editor, mark_position, code_to_add, -1, NULL);

       /* Emit code-added signal, so symbols will be updated *
       g_signal_emit_by_name (G_OBJECT (editor), "code-changed", mark_position, code_to_add);*/

       g_object_unref (mark_position);

       return TRUE;
}
Beispiel #6
0
static void
language_support_add_c_callback (CppJavaPlugin* lang_plugin,
                                 IAnjutaEditor* editor,
                                 IAnjutaIterable* position,
                                 GStrv split_signal_data,
                                 CppFileType filetype)
{
    GSignalQuery query;

    gchar* separator;
    gchar* body;
    gint offset;

    const gchar* widget = split_signal_data[0];
    const gchar* signal = split_signal_data[1];
    const gchar* handler = split_signal_data[2];
    const gchar* user_data = split_signal_data[3];

    gboolean swapped = g_str_equal (split_signal_data[4], "1");

    GType type = g_type_from_name (widget);
    guint id = g_signal_lookup (signal, type);

    g_signal_query (id, &query);


    if (!language_support_get_callback_strings (&separator, &body, &offset, user_data, editor, filetype))
        return;

    GString* str = language_support_generate_c_signature (separator, widget,
                                                          query, swapped, handler);

    g_string_append (str, body);

    ianjuta_document_begin_undo_action (IANJUTA_DOCUMENT(editor), NULL);
    ianjuta_editor_insert (editor, position,
                           str->str, -1, NULL);
	ianjuta_document_end_undo_action (IANJUTA_DOCUMENT(editor), NULL);

    /* Code was inserted, we'll now check if we should add a prototype to the header */
    if (filetype == LS_FILE_C)
    {
        IAnjutaEditor* header_editor;
        IAnjutaIterable* mark_position;
        mark_position = language_support_get_header_editor_and_mark (lang_plugin,
                                                                     editor,
                                                                     "/* Callbacks */",
                                                                     &header_editor);
        if (mark_position)
        {
            /* Check if there's a the prototype to the header */
            IAnjutaIterable* symbol;
            symbol = language_support_find_symbol (lang_plugin, header_editor, handler);

            if (symbol)
            {
                g_object_unref (symbol);
            } else {
                /* Add prototype to the header */
                language_support_add_c_callback (lang_plugin, header_editor, mark_position,
                                                 split_signal_data, LS_FILE_CHDR);
                g_signal_emit_by_name (G_OBJECT (header_editor), "code-changed", NULL, NULL);
            }

            g_object_unref (mark_position);
        }
    }

    gchar *string = g_string_free (str, FALSE);

    /* Emit code-added signal, so symbols will be updated */
    g_signal_emit_by_name (G_OBJECT (editor), "code-changed", position, string);

    if (string) g_free (string);

    /* Body is a bit different form other strings and must be freed */
    if (body) g_free (body);

    /* Will now set the caret position offset */
    ianjuta_editor_goto_line (editor,
                              ianjuta_editor_get_line_from_position (
                                            editor, position, NULL) + offset, NULL);
}
Beispiel #7
0
static void
toggle_comment_singleline (CppJavaPlugin *plugin, IAnjutaEditor *editor,
                           gint line)
{
    IAnjutaIterable *begin, *end, *begin_copy, *end_copy;
    gchar *text, *text_stripped, **text_diff = NULL;

    begin = ianjuta_editor_get_line_begin_position (editor, line, NULL);
    end = ianjuta_editor_get_line_end_position (editor, line, NULL);
    begin_copy = ianjuta_iterable_clone (begin, NULL);
    end_copy = ianjuta_iterable_clone (end, NULL);

    if (is_commented_multiline (editor, begin_copy, end_copy))
    {
        toggle_comment_multiline (editor, begin_copy, end_copy);
        g_object_unref (begin);
        g_object_unref (end);
        g_object_unref (begin_copy);
        g_object_unref (end_copy);
        return;
    }
    g_object_unref (begin_copy);
    g_object_unref (end_copy);

    text = ianjuta_editor_get_text (editor, begin, end, NULL);
    text_stripped = g_strstrip (g_strdup (text));
    text_diff = g_strsplit (text, text_stripped, 2);

    if (plugin->current_language &&
        (g_str_equal (plugin->current_language, "C")))
    {
        if (g_str_has_prefix (text_stripped, "/*") &&
            g_str_has_suffix (text_stripped, "*/"))
        {
            ianjuta_editor_erase (editor, begin, end, NULL);
            ianjuta_editor_insert (editor, begin, text_stripped + 2,
                                   (strlen (text_stripped) - 4), NULL);
            if (text_diff != NULL)
                ianjuta_editor_insert (editor, begin, *text_diff, -1, NULL);
        }
        else
        {
            ianjuta_editor_insert (editor, end, "*/", -1, NULL);
            ianjuta_editor_insert (editor, begin, "/*", -1, NULL);
        }
    }
    else
    {
        if (g_str_has_prefix (text_stripped, "//"))
        {
            ianjuta_editor_erase (editor, begin, end, NULL);
            ianjuta_editor_insert (editor, begin, text_stripped + 2, -1, NULL);
            if (text_diff != NULL)
                ianjuta_editor_insert (editor, begin, *text_diff, -1, NULL);
        }
        else
        {
            ianjuta_editor_insert (editor, begin, "//", -1, NULL);
        }
    }

    g_object_unref (begin);
    g_object_unref (end);
    g_free (text);
    g_free (text_stripped);
    g_strfreev (text_diff);
}
Beispiel #8
0
/*  incomplete_statement:
 *  1 == COMPLETE STATEMENT
 *  0 == INCOMPLETE STATEMENT
 * -1 == UNKNOWN
 */
static gint
get_line_indentation_base (IndentCPlugin *plugin,
						   IAnjutaEditor *editor,
						   gint line_num,
						   gint *incomplete_statement,
						   gint *parenthesis_indentation,
						   gboolean *colon_indent)
{
	IAnjutaIterable *iter;
	gchar point_ch;
	gint line_indent = 0;
	gint extra_indent = 0;
	gboolean looking_at_just_next_line = TRUE;
	gboolean current_line_is_preprocessor = FALSE;
	gboolean current_line_is_continuation = FALSE;
	gboolean line_checked_for_comment = FALSE;

    /* Determine whether or not to add multi-line comment asterisks */
	const gchar *comment_continued = " * ";
	IAnjutaIterable *line_begin = ianjuta_editor_get_line_begin_position (editor, line_num, NULL);
	IAnjutaIterable  *line_end = ianjuta_editor_get_line_end_position (editor, line_num, NULL);

	*incomplete_statement = -1;
	*parenthesis_indentation = 0;

	if (line_num <= 1)
		return 0;

	/* DEBUG_PRINT ("In %s()", __FUNCTION__); */

	iter = ianjuta_editor_get_line_begin_position (editor, line_num, NULL);

	current_line_is_preprocessor = line_is_preprocessor (editor, iter);
	current_line_is_continuation =
		line_is_continuation (editor, iter);
	/*
	DEBUG_PRINT ("Current line is preprocessor = %d",
				 current_line_is_preprocessor);
	DEBUG_PRINT ("Current line is continuation = %d",
				 current_line_is_continuation);
	*/
	/* line_indent = get_line_indentation (editor, line_num - 1); */

	if (current_line_is_preprocessor && current_line_is_continuation)
	{
		/* Continuation of preprocessor line -- just maintain indentation */
		g_object_unref (iter);
		return get_line_indentation (editor, line_num - 1);
	}
	else if (current_line_is_preprocessor)
	{
		/* Preprocessor line -- indentation should be 0 */
		g_object_unref (iter);
		return 0;
	}

	while (ianjuta_iterable_previous (iter, NULL))
	{
		/* Skip strings */
		IAnjutaEditorAttribute attrib =
			ianjuta_editor_cell_get_attribute (IANJUTA_EDITOR_CELL (iter), NULL);
		if (attrib == IANJUTA_EDITOR_STRING)
			continue;

		point_ch = ianjuta_editor_cell_get_char (IANJUTA_EDITOR_CELL (iter), 0,
												 NULL);

		/* DEBUG_PRINT("point_ch = %c", point_ch); */

		/* Check for line comment comment */
		if (!line_checked_for_comment && !isspace(point_ch))
		{
			gboolean comment = FALSE;
			IAnjutaIterable* new_iter = ianjuta_iterable_clone (iter, NULL);
			do
			{
				gchar c;

				/* Skip strings */
				if (ianjuta_editor_cell_get_attribute (IANJUTA_EDITOR_CELL (new_iter), NULL) == IANJUTA_EDITOR_STRING)
					continue;

				c = ianjuta_editor_cell_get_char (IANJUTA_EDITOR_CELL (new_iter), 0,
												  NULL);

				if (iter_is_newline (new_iter, c))
				{
					line_checked_for_comment = TRUE;
					break;
				}
				if (c == '/')
				{
					IAnjutaIterable* tmp_iter = ianjuta_iterable_clone (new_iter, NULL);
					if (!ianjuta_iterable_previous (tmp_iter, NULL))
					{
						g_object_unref (tmp_iter);
						break;
					}
					c = ianjuta_editor_cell_get_char (IANJUTA_EDITOR_CELL (tmp_iter), 0,
													  NULL);
					if (c == '/')
					{
						/* is a line comment, skip until begin of comment */
						comment = TRUE;
						g_object_unref (tmp_iter);
						break;
					}
					g_object_unref (tmp_iter);
				}
			} while (ianjuta_iterable_previous (new_iter, NULL));
			if (comment)
			{
				ianjuta_iterable_assign (iter, new_iter, NULL);
				ianjuta_iterable_previous (iter, NULL);
				g_object_unref (new_iter);
				continue;
			}
			g_object_unref (new_iter);
		}
		/* Check if we are inside a comment */
		if (point_ch == '/' || point_ch == '*')
		{
			gboolean comment = FALSE;
			gboolean comment_end = FALSE;
			IAnjutaIterable* new_iter = ianjuta_iterable_clone (iter, NULL);
			do
			{
				gchar c = ianjuta_editor_cell_get_char (IANJUTA_EDITOR_CELL(new_iter),
												  0, NULL);
				if (!comment_end && iter_is_newline (new_iter, c))
				{
					break;
				}
				if (c == '*')
				{
					IAnjutaIterable* prev = ianjuta_iterable_clone (new_iter, NULL);
					IAnjutaIterable* next = ianjuta_iterable_clone (new_iter, NULL);
					ianjuta_iterable_previous (prev, NULL);
					ianjuta_iterable_next (next, NULL);
					gchar prev_c = ianjuta_editor_cell_get_char (IANJUTA_EDITOR_CELL (prev), 0,
													  NULL);
					gchar next_c = ianjuta_editor_cell_get_char (IANJUTA_EDITOR_CELL (next), 0,
													  NULL);
					if (prev_c == '/')
					{
						/* starts comment */
						comment = TRUE;
						if (!comment_end)
						{
							extra_indent++;

							/* If a multiline comment is continuing, check the next line and insert " * "
							 * only if it does not already exist there. The purpose of this fix is to avoid
							 * extra " * " on auto-indent. */

							if ((g_settings_get_boolean (plugin->settings, PREF_COMMENT_LEADING_ASTERISK)) &&
								(ianjuta_iterable_compare (line_end, line_begin, NULL)) == 0)
							{
								ianjuta_editor_insert (editor, line_begin, comment_continued, -1, NULL);
							}

							/* In the middle of a comment we can't know
						     * if the statement is incomplete
							 */
							*incomplete_statement = -1;

							/* ":" have to be ignored inside comments */
							if (*colon_indent)
							{
								*colon_indent = FALSE;
								extra_indent -= INDENT_SIZE;
							}
						}
						g_object_unref (prev);
						g_object_unref (next);
						break;

					}
					else if (next_c == '/')
					{
						/* ends comment: */
						comment_end = TRUE;
						g_object_unref (prev);
						g_object_unref (next);
						continue;
					}
					/* Possibly continued comment */
					else if (isspace(prev_c))
					{
						gboolean possible_comment = FALSE;
						while (ianjuta_iterable_previous (prev, NULL))
						{
							prev_c = ianjuta_editor_cell_get_char (IANJUTA_EDITOR_CELL (prev), 0,
															  	   NULL);
							if (!isspace(prev_c))
								break;
							if (iter_is_newline (prev, prev_c))
							{
								possible_comment = TRUE;
								break;
							}
						}
						if (possible_comment)
						{
							ianjuta_iterable_assign (new_iter, prev, NULL);
							g_object_unref (prev);
							g_object_unref (next);
							continue;
						}
					}
					g_object_unref (prev);
					g_object_unref (next);
				}
			} while (ianjuta_iterable_previous (new_iter, NULL));
			if (comment)
			{
				ianjuta_iterable_assign (iter, new_iter, NULL);
				ianjuta_iterable_previous (iter, NULL);
				g_object_unref (new_iter);
				continue;
			}
			g_object_unref (new_iter);
		}
		if (point_ch == ')' || point_ch == ']' || point_ch == '}')
		{
			gint line_saved;

			line_saved = ianjuta_editor_get_line_from_position (editor, iter,
																NULL);

			/* If we encounter a block-end before anything else, the
			 * statement could hardly be incomplte.
			 */
			if (point_ch == '}' && *incomplete_statement == -1)
				*incomplete_statement = 0;

			/* If at level 0 indentation, encoutered a
			 * block end, don't bother going further
			 */
			if (point_ch == '}' && get_line_indentation (editor, line_saved) <= 0)
			{
				line_indent = 0;
				line_indent += extra_indent;
				break;
			}

			/* Find matching brace and continue */
			if (!anjuta_util_jump_to_matching_brace (iter, point_ch, -1))
			{
				line_indent = get_line_indentation (editor, line_saved);
				line_indent += extra_indent;
				break;
			}
		}
		else if (point_ch == '{')
		{
			gint line_for_indent =
				ianjuta_editor_get_line_from_position (editor, iter, NULL);
			line_indent = get_line_indentation (editor, line_for_indent);
			/* Increase line indentation */
			line_indent += INDENT_SIZE;
			line_indent += extra_indent;

			/* If we encounter a block-start before anything else, the
			 * statement could hardly be incomplte.
			 */
			if (point_ch == '{' && *incomplete_statement == -1)
				*incomplete_statement = 0;

			break;
		}
		else if (point_ch == '(' || point_ch == '[')
		{
			line_indent = 0;
			if (g_settings_get_boolean (plugin->settings,
			                            PREF_INDENT_PARENTHESIS_LINEUP))
			{
				while (ianjuta_iterable_previous (iter, NULL))
				{
					gchar dummy_ch = ianjuta_editor_cell_get_char (IANJUTA_EDITOR_CELL (iter), 0,
					                                               NULL);
					if (iter_is_newline (iter, dummy_ch))
					{
						skip_iter_to_newline_head (iter, dummy_ch);
						break;
					}
					if (dummy_ch == '\t')
						line_indent += TAB_SIZE;
					else
						(*parenthesis_indentation)++;
				}
				(*parenthesis_indentation)++;
				line_indent += extra_indent;
			}
			else
			{
				gint line_for_indent =
					ianjuta_editor_get_line_from_position (editor, iter, NULL);
				line_indent = get_line_indentation (editor, line_for_indent);
				line_indent += extra_indent;

				(*parenthesis_indentation) += g_settings_get_int (plugin->settings,
				                                             PREF_INDENT_PARENTHESIS_SIZE);
			}

			/* Although statement is incomplete at this point, we don't
			 * set it to incomplete and just leave it to unknown to avaoid
			 * increating indentation for it, because incomplete braces,
			 * overrides any existing indentation
			 */
			*incomplete_statement = -1;
			break;
		}
		else if (point_ch == ';' || point_ch == ',')
		{
			/* If we encounter statement-end before any non-whitespace
			 * char, the statement is complete.
			 */
			if (*incomplete_statement == -1)
				*incomplete_statement = 0;
		}
		else if (point_ch == ':' && *colon_indent == FALSE)
		{
			/* This is a forward reference, all lines below should have
			 * increased indentation until the next statement has
			 * a ':'
			 * If current line indentation is zero, that we don't indent
			 */
			IAnjutaIterable* new_iter = ianjuta_iterable_clone (iter, NULL);
			IAnjutaIterable* line_begin;
			gboolean indent = FALSE;
			gchar c;

			/* Is the last non-whitespace in line */
			while (ianjuta_iterable_next (new_iter, NULL))
			{
				c = ianjuta_editor_cell_get_char (IANJUTA_EDITOR_CELL (new_iter),
													   0, NULL);
				if (!isspace(c))
					break;
				if (iter_is_newline (new_iter, c))
				{
					indent = TRUE;
					break;
				}
			}
			line_begin = ianjuta_editor_get_line_begin_position(editor,
																ianjuta_editor_get_line_from_position(editor, iter, NULL),
																NULL);
			c = ianjuta_editor_cell_get_char (IANJUTA_EDITOR_CELL (line_begin),
														0, NULL);
			if (indent)
			{
				*colon_indent = TRUE;
				if (*incomplete_statement == -1)
					*incomplete_statement = 0;
			}
			if (indent && isspace(c))
			{
				extra_indent += INDENT_SIZE;
			}
			g_object_unref (new_iter);
			g_object_unref (line_begin);
		}
		else if (iter_is_newline (iter, point_ch))
		{
			skip_iter_to_newline_head (iter, point_ch);

			/* We just crossed a line boundary. Skip any preprocessor lines,
			 * and ensure that line_indent is updated with correct real
			 * previous non-preprocessor line.
			 */
			if (skip_preprocessor_lines (editor, iter) &&
				looking_at_just_next_line)
			{
				/*
				gint line = ianjuta_editor_get_line_from_position (editor, iter, NULL);
				line_indent = get_line_indentation (editor, line);
				*/
			}
			looking_at_just_next_line = FALSE;
			line_checked_for_comment = FALSE;
		}
		else if (!isspace (point_ch))
		{
			/* If we encounter any non-whitespace char before any of the
			 * statement-complete indicators, the statement is basically
			 * incomplete
			 */
			if (*incomplete_statement == -1)
				*incomplete_statement = 1;
		}
	}
	if (!line_indent && extra_indent)
	{
		line_indent += extra_indent;
	}
	g_object_unref (iter);

	return line_indent;
}
Beispiel #9
0
static gint
set_line_indentation (IndentCPlugin *plugin, IAnjutaEditor *editor, gint line_num, gint indentation, gint parenthesis_indentation)
{
	IAnjutaIterable *line_begin, *line_end, *indent_position;
	IAnjutaIterable *current_pos;
	gint carat_offset, nchars = 0;
	gchar *old_indent_string = NULL, *indent_string = NULL;

	/* DEBUG_PRINT ("In %s()", __FUNCTION__); */
	line_begin = ianjuta_editor_get_line_begin_position (editor, line_num, NULL);
	line_end = ianjuta_editor_get_line_end_position (editor, line_num, NULL);

	/*
	DEBUG_PRINT ("line begin = %d, line end = %d, current_pos = %d",
				 line_begin, line_end, current_pos);
	*/
	indent_position = ianjuta_iterable_clone (line_begin, NULL);

	if (ianjuta_iterable_compare (line_end, line_begin, NULL) > 0)
	{
		gchar *idx;
		gchar *line_string = ianjuta_editor_get_text (editor, line_begin,
														   line_end, NULL);

		//DEBUG_PRINT ("line_string = '%s'", line_string);
		if (line_string)
		{
			idx = line_string;

			/* Find first non-white space */
			while (*idx != '\0' && isspace (*idx))
			{
				idx = g_utf8_find_next_char (idx, NULL);
				ianjuta_iterable_next (indent_position, NULL);
			}
			g_free (line_string);
		}
	}
	/* Indent iter defined at this point, Identify how much is current
	 * position is beyound this point. We need to restore it later after
	 * indentation
	*/
	current_pos = ianjuta_editor_get_position (editor, NULL);
	carat_offset = ianjuta_iterable_diff (indent_position, current_pos, NULL);
	//DEBUG_PRINT ("carat offset is = %d", carat_offset);

	/* Set new indentation */
	if ((indentation + parenthesis_indentation) > 0)
	{
		indent_string = get_line_indentation_string (plugin, editor, indentation, parenthesis_indentation);
		nchars = indent_string ? g_utf8_strlen (indent_string, -1) : 0;

		/* Only indent if there is something to indent with */
		if (indent_string)
		{
			/* Get existing indentation */
			if (ianjuta_iterable_compare (indent_position, line_begin, NULL) > 0)
			{
				old_indent_string =
					ianjuta_editor_get_text (editor, line_begin,
												  indent_position, NULL);

				//DEBUG_PRINT ("old_indent_string = '%s'", old_indent_string);
			}

			/* Only indent if there was no indentation before or old
			 * indentation string was different from the new indent string
			 */
			if (old_indent_string == NULL ||
				strcmp (old_indent_string, indent_string) != 0)
			{
				/* Remove the old indentation string, if there is any */
				if (old_indent_string)
					ianjuta_editor_erase (editor, line_begin,
										  indent_position, NULL);

				/* Insert the new indentation string */
				ianjuta_editor_insert (editor, line_begin,
									   indent_string, -1, NULL);
			}
		}
	}

	/* If indentation == 0, we really didn't enter the previous code block,
	 * but we may need to clear existing indentation.
	 */
	if ((indentation + parenthesis_indentation) == 0)
	{
		/* Get existing indentation */
		if (ianjuta_iterable_compare (indent_position, line_begin, NULL) > 0)
		{
			old_indent_string =
				ianjuta_editor_get_text (editor, line_begin,
											  indent_position, NULL);
		}
		if (old_indent_string)
			ianjuta_editor_erase (editor, line_begin, indent_position, NULL);
	}

	/* Restore current position */
	if (carat_offset >= 0)
	{
		/* If the cursor was not before the first non-space character in
		 * the line, restore it's position after indentation.
		 */
		gint i;
		IAnjutaIterable *pos = ianjuta_editor_get_line_begin_position (editor, line_num, NULL);
		for (i = 0; i < nchars + carat_offset; i++)
			ianjuta_iterable_next (pos, NULL);
		ianjuta_document_begin_undo_action (IANJUTA_DOCUMENT(editor), NULL);
		ianjuta_editor_goto_position (editor, pos, NULL);
		ianjuta_document_end_undo_action (IANJUTA_DOCUMENT(editor), NULL);
		g_object_unref (pos);
	}
	else /* cursor_offset < 0 */
	{
		/* If the cursor was somewhere in the old indentation spaces,
		 * home the cursor to first non-space character in the line (or
		 * end of line if there is no non-space characters in the line.
		 */
		gint i;
		IAnjutaIterable *pos = ianjuta_editor_get_line_begin_position (editor, line_num, NULL);
		for (i = 0; i < nchars; i++)
			ianjuta_iterable_next (pos, NULL);
		ianjuta_document_begin_undo_action (IANJUTA_DOCUMENT(editor), NULL);
		ianjuta_editor_goto_position (editor, pos, NULL);
		ianjuta_document_end_undo_action (IANJUTA_DOCUMENT(editor), NULL);
		g_object_unref (pos);
	}

	g_object_unref (current_pos);
	g_object_unref (indent_position);
	g_object_unref (line_begin);
	g_object_unref (line_end);

	g_free (old_indent_string);
	g_free (indent_string);
	return nchars;
}
Beispiel #10
0
static void
on_glade_drop (IAnjutaEditor* editor,
               IAnjutaIterable* iterator,
               const gchar* signal_data,
               PythonPlugin* lang_plugin)
{
	GSignalQuery query;
	GType type;
	guint id;
	
	const gchar* widget;
	const gchar* signal;
	const gchar* handler;
	const gchar* user_data;
	gboolean swapped;
	GList* names = NULL;
	GString* str = g_string_new (NULL);
	int i;
	IAnjutaIterable* start, * end;
	
	GStrv data = g_strsplit(signal_data, ":", 5);
	
	widget = data[0];
	signal = data[1];
	handler = data[2];
	user_data = data[3];
	swapped = g_str_equal (data[4], "1");
	
	type = g_type_from_name (widget);
	id = g_signal_lookup (signal, type);

	g_signal_query (id, &query);

	g_string_append_printf (str, "\ndef %s (self", handler);
	for (i = 0; i < query.n_params; i++)
	{
		const gchar* type_name = g_type_name (query.param_types[i]);
		const gchar* param_name = language_support_get_signal_parameter (type_name,
		                                                                 &names);

		g_string_append_printf (str, ", %s", param_name);
	}
	g_string_append (str, "):\n");

	ianjuta_editor_insert (editor, iterator,
	                       str->str, -1, NULL);

	/* Indent code correctly */
	start = iterator;
	end = ianjuta_iterable_clone (iterator, NULL);
	ianjuta_iterable_set_position (end, 
	                               ianjuta_iterable_get_position (iterator, NULL)
	                           		+ g_utf8_strlen (str->str, -1),
	                               NULL);
	ianjuta_indenter_indent (IANJUTA_INDENTER (lang_plugin),
	                         start, end, NULL);
	g_object_unref (end);

	g_string_free (str, TRUE);
	anjuta_util_glist_strings_free (names);
	
	g_strfreev (data);
}