Ejemplo n.º 1
0
static void insert_member_decl_and_init (IAnjutaEditor* editor, gchar* widget_name,
                                         gchar* ui_filename, CppJavaPlugin* lang_plugin)
{
       AnjutaStatus* status;
       gchar* member_decl = generate_widget_member_decl_str(widget_name);
       gchar* member_init = generate_widget_member_init_str(widget_name);

       gchar* member_decl_marker = generate_widget_member_decl_marker (ui_filename);
       gchar* member_init_marker = generate_widget_member_init_marker (ui_filename);

       status = anjuta_shell_get_status (ANJUTA_PLUGIN (lang_plugin)->shell, NULL);

       if (!glade_widget_already_in_scope (editor, widget_name, member_decl_marker, lang_plugin))
       {

              ianjuta_document_begin_undo_action (IANJUTA_DOCUMENT(editor), NULL);
              if (insert_after_mark (editor, member_decl_marker, member_decl, lang_plugin))
              {
                     insert_after_mark (editor, member_init_marker, member_init, lang_plugin);
                     g_signal_emit_by_name (G_OBJECT (editor), "code-changed", NULL, NULL);
                     anjuta_status_set (status, _("Code added for widget."));
              }
              ianjuta_document_end_undo_action (IANJUTA_DOCUMENT(editor), NULL);
       }

       g_free (member_decl);
       g_free (member_init);

       g_free (member_decl_marker);
       g_free (member_init_marker);
}
Ejemplo n.º 2
0
static void
on_toggle_comment (GtkAction *action, gpointer data)
{
    gint line;
    gboolean has_selection;

    CppJavaPlugin *lang_plugin;
    IAnjutaEditor *editor;
    lang_plugin = ANJUTA_PLUGIN_CPP_JAVA (data);
    editor = IANJUTA_EDITOR (lang_plugin->current_editor);

    ianjuta_document_begin_undo_action (IANJUTA_DOCUMENT(editor), NULL);

    has_selection = ianjuta_editor_selection_has_selection
                        (IANJUTA_EDITOR_SELECTION (editor), NULL);
    if (has_selection)
    {
        IAnjutaIterable *sel_start, *sel_end;
        sel_start = ianjuta_editor_selection_get_start (IANJUTA_EDITOR_SELECTION (editor),
                                                        NULL);
        sel_end = ianjuta_editor_selection_get_end (IANJUTA_EDITOR_SELECTION (editor),
                                                    NULL);
        toggle_comment_multiline (editor, sel_start, sel_end);
        g_object_unref (sel_start);
        g_object_unref (sel_end);
    }
    else
    {
        line = ianjuta_editor_get_lineno (IANJUTA_EDITOR(editor), NULL);
        toggle_comment_singleline (lang_plugin, editor, line);
    }
    ianjuta_document_end_undo_action (IANJUTA_DOCUMENT(editor), NULL);
}
Ejemplo n.º 3
0
static void
on_replace_all_activated (GtkWidget* widget, SearchBox* search_box)
{
	IAnjutaIterable* cursor;

	if (!search_box->priv->current_editor)
		return;

	/* Cache current position and search from begin */
	cursor = ianjuta_editor_get_position (IANJUTA_EDITOR (search_box->priv->current_editor),
	                                      NULL);
	ianjuta_editor_goto_start (IANJUTA_EDITOR (search_box->priv->current_editor), NULL);
	
	/* Replace all instances of search_entry with replace_entry text */
	ianjuta_document_begin_undo_action (IANJUTA_DOCUMENT (search_box->priv->current_editor), NULL);
	while (search_box_incremental_search (search_box, TRUE, TRUE, FALSE))
	{
		search_box_replace (search_box, widget, FALSE);
	}
	ianjuta_document_end_undo_action (IANJUTA_DOCUMENT (search_box->priv->current_editor), NULL);

	/* Back to cached position */
	ianjuta_editor_selection_set (IANJUTA_EDITOR_SELECTION (search_box->priv->current_editor),
	                              cursor, cursor, TRUE, NULL);
	g_object_unref (cursor);
}
Ejemplo n.º 4
0
void
cpp_auto_indentation (IAnjutaEditor *editor,
                      IndentCPlugin *lang_plugin,
                      IAnjutaIterable *start,
                      IAnjutaIterable *end)
{
	gint line_start, line_end;
	gint insert_line;
	gint line_indent;
	gboolean has_selection;

	has_selection = ianjuta_editor_selection_has_selection
		(IANJUTA_EDITOR_SELECTION (editor), NULL);
	if (start == NULL || end == NULL)
	{
		if (has_selection)
		{
			IAnjutaIterable *sel_start, *sel_end;
			sel_start = ianjuta_editor_selection_get_start (IANJUTA_EDITOR_SELECTION (editor),
				                                            NULL);
			sel_end = ianjuta_editor_selection_get_end (IANJUTA_EDITOR_SELECTION (editor),
				                                        NULL);
			line_start = ianjuta_editor_get_line_from_position (editor, sel_start, NULL);
			line_end = ianjuta_editor_get_line_from_position (editor, sel_end, NULL);
			g_object_unref (sel_start);
			g_object_unref (sel_end);
		}
		else
		{
			line_start = ianjuta_editor_get_lineno (IANJUTA_EDITOR(editor), NULL);
			line_end = line_start;
		}
	}
	else
	{
			line_start = ianjuta_editor_get_line_from_position (editor, start, NULL);
			line_end = ianjuta_editor_get_line_from_position (editor, end, NULL);
	}
	ianjuta_document_begin_undo_action (IANJUTA_DOCUMENT(editor), NULL);

	for (insert_line = line_start; insert_line <= line_end; insert_line++)
	{
		gint parenthesis_indentation = 0;
		line_indent = get_line_auto_indentation (lang_plugin, editor,
		                                         insert_line,
		                                         &parenthesis_indentation);
		/* DEBUG_PRINT ("Line indent for line %d = %d", insert_line, line_indent); */
		set_line_indentation (lang_plugin, editor, insert_line, line_indent, parenthesis_indentation);
	}
	ianjuta_document_end_undo_action (IANJUTA_DOCUMENT(editor), NULL);
}
Ejemplo n.º 5
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);
}
Ejemplo n.º 6
0
static gboolean
search_box_replace (SearchBox * search_box, GtkWidget * widget,
                    gboolean undo /* treat as undo action */)
{

	IAnjutaEditorSelection* selection;
	gchar * selection_text;
	gboolean replace_successful = FALSE;

	const gchar* replace_text = gtk_entry_get_text (GTK_ENTRY (search_box->priv->replace_entry));
	const gchar* search_text = gtk_entry_get_text (GTK_ENTRY (search_box->priv->search_entry));
		
	selection = IANJUTA_EDITOR_SELECTION (search_box->priv->current_editor);
	selection_text = ianjuta_editor_selection_get (selection, NULL);

	if (ianjuta_editor_selection_has_selection (selection, NULL))
	{
		if (search_box->priv->regex_mode)
		{
			GRegex * regex;
			gchar * replacement_text;
			gint start_pos, end_pos;
			GError * err = NULL;
			gboolean result = search_regex_in_text (search_text, selection_text, TRUE, &start_pos, &end_pos);
				
			if (result)
			{
				regex = g_regex_new (search_text, 0, 0, NULL);
				replacement_text = g_regex_replace(regex, selection_text, strlen(selection_text), 0, 
													replace_text, 0, &err);
				if (err)
				{
					g_message ("%s",err->message);
					g_error_free (err);
					g_regex_unref(regex);
				}
                else
				{
					if (undo)
						ianjuta_document_begin_undo_action (IANJUTA_DOCUMENT (selection), NULL);
					ianjuta_editor_selection_replace (selection, replacement_text, strlen(replacement_text), NULL);
					if (undo)
						ianjuta_document_end_undo_action (IANJUTA_DOCUMENT (selection), NULL);
 
					replace_successful = TRUE;
				}

				if (regex)
					g_regex_unref(regex);
				if (replacement_text)			
					g_free(replacement_text);
			}
		}
		else if ((search_box->priv->case_sensitive && g_str_equal (selection_text, search_text)) ||
		         (!search_box->priv->case_sensitive && strcasecmp (selection_text, search_text) == 0))
		{
			if (undo)
				ianjuta_document_begin_undo_action (IANJUTA_DOCUMENT (selection), NULL);
			ianjuta_editor_selection_replace (selection, replace_text, strlen(replace_text), NULL);
			if (undo)
				ianjuta_document_end_undo_action (IANJUTA_DOCUMENT (selection), NULL);

			replace_successful = TRUE;
		}
	
		g_free(selection_text);
	}
		
	return replace_successful;

}
Ejemplo n.º 7
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;
}
Ejemplo n.º 8
0
void
cpp_java_indentation_char_added (IAnjutaEditor *editor,
                                 IAnjutaIterable *insert_pos,
                                 gchar ch,
                                 IndentCPlugin *plugin)
{
	IAnjutaEditorAttribute attrib;
	IAnjutaIterable *iter;
	gboolean should_auto_indent = FALSE;

	iter = ianjuta_iterable_clone (insert_pos, NULL);

	/* If autoindent is enabled*/
	if (plugin->smart_indentation)
	{

		/* DEBUG_PRINT ("Char added at position %d: '%c'", insert_pos, ch); */

		if (iter_is_newline (iter, ch))
		{
			skip_iter_to_newline_head (iter, ch);
			/* All newline entries means enable indenting */
			should_auto_indent = TRUE;
		}
		else if (ch == '{' || ch == '}' || ch == '#')
		{
			/* Indent only when it's the first non-white space char in the line */

			/* Don't bother if we are inside string */
			attrib = ianjuta_editor_cell_get_attribute (IANJUTA_EDITOR_CELL (iter),
			                                            NULL);
			if (attrib != IANJUTA_EDITOR_STRING)
			{
				/* Iterate backwards till the begining of the line and disable
				 * indenting if any non-white space char is encountered
				 */

				/* Begin by assuming it should be indented */
				should_auto_indent = TRUE;

				while (ianjuta_iterable_previous (iter, NULL))
				{
					ch = ianjuta_editor_cell_get_char (IANJUTA_EDITOR_CELL (iter),
					                                   0, NULL);

					//DEBUG_PRINT ("Looking at char '%c'", ch);

					/* Break on begining of line (== end of previous line) */
					if (iter_is_newline (iter, ch))
					{
						skip_iter_to_newline_head (iter, ch);
						break;
					}
					/* If a non-white space char is encountered, disabled indenting */
					if (!isspace (ch))
					{
						should_auto_indent = FALSE;
						break;
					}
				}
			}
		}
		if (should_auto_indent)
		{
			gint insert_line;
			gint line_indent;
			gint parenthesis_indentation;

			ianjuta_document_begin_undo_action (IANJUTA_DOCUMENT(editor), NULL);

			insert_line = ianjuta_editor_get_lineno (editor, NULL);
			line_indent = get_line_auto_indentation (plugin, editor, insert_line, &parenthesis_indentation);
			set_line_indentation (plugin, editor, insert_line, line_indent, parenthesis_indentation);
			ianjuta_document_end_undo_action (IANJUTA_DOCUMENT(editor), NULL);
		}
	}

	if (g_settings_get_boolean (plugin->settings, PREF_BRACE_AUTOCOMPLETION))
	{
		if (ch == '[' || ch == '(')
		            {
						gchar *prev_char, *next_char;
						IAnjutaIterable *previous, *next, *next_end;

						previous = ianjuta_iterable_clone (iter, NULL);
						ianjuta_iterable_previous (previous, NULL);
						prev_char = ianjuta_editor_get_text (editor, previous, iter, NULL);

						next = ianjuta_iterable_clone (iter, NULL);
						ianjuta_iterable_next (next, NULL);
						next_end = ianjuta_iterable_clone (next, NULL);
						ianjuta_iterable_next (next_end, NULL);
						next_char = ianjuta_editor_get_text (editor, next, next_end, NULL);

						/* If the previous char is a ' we don't have to autocomplete,
						   also we only autocomplete if the next character is white-space
						   a closing bracket, "," or ";" */
						if (*prev_char != '\'' &&
						    (g_ascii_isspace(*next_char) || is_closing_bracket (*next_char) ||
							 *next_char == ',' || *next_char == ';'|| *next_char == '\0'))
						{
							ianjuta_document_begin_undo_action (IANJUTA_DOCUMENT (editor), NULL);
							ianjuta_iterable_next (iter, NULL);
							switch (ch)
							{
								case '[':
									       insert_editor_blocked (editor, iter,
									                              "]", plugin);
									       break;
								case '(':
									       insert_editor_blocked (editor, iter,
									                       ")", plugin);
									       break;
								default:
									       break;
							}
							ianjuta_editor_goto_position (editor, iter, NULL);
							ianjuta_document_end_undo_action (IANJUTA_DOCUMENT (editor), NULL);
						}
						g_object_unref (previous);
					}
		            else if (ch == '"' || ch == '\'')
		            {
						gchar *prev_char;
						IAnjutaIterable *previous;

						previous = ianjuta_iterable_clone (iter, NULL);
						ianjuta_iterable_previous (previous, NULL);
						prev_char = ianjuta_editor_get_text (editor, previous, iter, NULL);

						/* First iter*/
						ianjuta_iterable_next (iter, NULL);

						/*
						 * If the character is " we have to decide if we need insert
						 * another " or we have to skip the character
						 */
						if (ch == '"' || ch == '\'')
						{
							/*
							 * Now we have to detect if we want to manage " as a char
							 */
							if (*prev_char != '\'' && *prev_char != '\\')
							{
								gchar *c;

								if (ch == '"')
									c = g_strdup ("\"");
								else c = g_strdup ("'");

								ianjuta_document_begin_undo_action (IANJUTA_DOCUMENT (editor), NULL);
								insert_editor_blocked (editor, iter, c, plugin);
								ianjuta_editor_goto_position (editor, iter, NULL);
								ianjuta_document_end_undo_action (IANJUTA_DOCUMENT (editor), NULL);

								g_free (c);
							}
							g_object_unref (previous);
							g_object_unref (iter);
							return;
						}
						g_object_unref (previous);
					}
	            }
	g_object_unref (iter);
}