예제 #1
0
파일: plugin.c 프로젝트: VujinovM/anjuta
static gchar*
get_text_between (IAnjutaEditor *editor, gchar *prefix, gchar *suffix)
{
	IAnjutaEditorCell *search_start = IANJUTA_EDITOR_CELL (
										ianjuta_editor_get_start_position (editor, NULL));
	IAnjutaEditorCell *search_end = IANJUTA_EDITOR_CELL (
										ianjuta_editor_get_end_position (editor, NULL));

	IAnjutaEditorCell *result_start;
	IAnjutaEditorCell *result_end = NULL;

	IAnjutaEditorCell *prefix_end;
	IAnjutaEditorCell *suffix_start;

	ianjuta_editor_search_forward (IANJUTA_EDITOR_SEARCH (editor),
								   prefix, FALSE,
								   search_start, search_end,
								   &result_start,
								   &result_end, NULL);

	if (!result_end)
		return NULL;

	g_object_unref (result_start);

	prefix_end = result_end;

	ianjuta_editor_search_forward (IANJUTA_EDITOR_SEARCH (editor),
								   suffix, FALSE,
								   result_end, search_end,
								   &result_start,
								   &result_end, NULL);

	suffix_start = result_start;

	if (!result_end)
		return NULL;

	g_object_unref (result_end);

	return ianjuta_editor_get_text (editor, prefix_end, suffix_start, NULL);
}
예제 #2
0
void
search_box_highlight_all (SearchBox *search_box)
{
	if (!search_box->priv->current_editor)
		return;

	ianjuta_indicable_clear(IANJUTA_INDICABLE(search_box->priv->current_editor), NULL);
	if (search_box->priv->start_highlight != NULL) g_object_unref (search_box->priv->start_highlight);
	if (search_box->priv->end_highlight != NULL) g_object_unref (search_box->priv->end_highlight);
	search_box->priv->start_highlight = IANJUTA_EDITOR_CELL (ianjuta_editor_get_start_position (search_box->priv->current_editor, NULL));
	search_box->priv->end_highlight = IANJUTA_EDITOR_CELL (ianjuta_editor_get_end_position (search_box->priv->current_editor, NULL));

	if (search_box->priv->idle_id == 0)
	{
		search_box->priv->idle_id = g_idle_add_full(G_PRIORITY_DEFAULT_IDLE,
		                                            (GSourceFunc)highlight_in_background,
		                                            search_box,
		                                            NULL);
	}
}
예제 #3
0
파일: plugin.c 프로젝트: VujinovM/anjuta
static IAnjutaIterable*
language_support_get_mark_position (IAnjutaEditor* editor, gchar* mark)
{
    IAnjutaEditorCell *search_start = IANJUTA_EDITOR_CELL (
                                        ianjuta_editor_get_start_position (editor, NULL));
    IAnjutaEditorCell *search_end = IANJUTA_EDITOR_CELL (
                                        ianjuta_editor_get_end_position (editor, NULL));
    IAnjutaEditorCell *result_start = NULL;
    IAnjutaEditorCell *result_end = NULL;

    ianjuta_editor_search_forward (IANJUTA_EDITOR_SEARCH (editor),
                                   mark, FALSE,
                                   search_start, search_end,
                                   &result_start,
                                   &result_end, NULL);

    if (result_start)
        g_object_unref (result_start);

    return result_end ? IANJUTA_ITERABLE (result_end) : NULL;
}
예제 #4
0
static gboolean
line_is_preprocessor (IAnjutaEditor *editor, IAnjutaIterable *iter)
{
	gboolean is_preprocessor = FALSE;
	IAnjutaIterable *new_iter = ianjuta_iterable_clone (iter, NULL);

	if (skip_iter_to_previous_logical_line (editor, new_iter))
	{
		/* Forward the newline char and point to line begin of next line */
		gchar ch;
		ch = ianjuta_editor_cell_get_char (IANJUTA_EDITOR_CELL (new_iter),
										   0, NULL);
		skip_iter_to_newline_tail (new_iter, ch);
		ianjuta_iterable_next (new_iter, NULL);
	}
	/* else, line is already pointed at first char of the line */

	do
	{
		gchar ch;
		ch = ianjuta_editor_cell_get_char (IANJUTA_EDITOR_CELL (new_iter),
										   0, NULL);
		if (ch == '#')
		{
			is_preprocessor = TRUE;
			break;
		}
		if (iter_is_newline (new_iter, ch) || !isspace (ch))
			break;
	}
	while (ianjuta_iterable_next (new_iter, NULL));

	g_object_unref (new_iter);

	return is_preprocessor;
}
예제 #5
0
/* Sets the iter to line end of previous line and TRUE is returned.
 * If there is no previous line, iter is set to first character in the
 * buffer and FALSE is returned.
 */
static gboolean
skip_iter_to_previous_line (IAnjutaEditor *editor, IAnjutaIterable *iter)
{
	gboolean found = FALSE;
	gchar ch;

	while (ianjuta_iterable_previous (iter, NULL))
	{
		ch = ianjuta_editor_cell_get_char (IANJUTA_EDITOR_CELL (iter), 0, NULL);
		if (iter_is_newline (iter, ch))
		{
			skip_iter_to_newline_head (iter, ch);
			found = TRUE;
			break;
		}
	}
	return found;
}
예제 #6
0
/* Returns TRUE if iter was moved */
static gboolean
skip_iter_to_newline_tail (IAnjutaIterable *iter, gchar ch)
{
	gboolean ret_val = FALSE;

	if (ch == '\r')
	{
		/* Possibly at head */
		if (ianjuta_iterable_previous (iter, NULL))
		{
			ch = ianjuta_editor_cell_get_char (IANJUTA_EDITOR_CELL (iter),
											0, NULL);
			if (ch != '\n')
				/* Already at tail, undo iter */
				ianjuta_iterable_next (iter, NULL);
			else
				/* Correctly at tail */
				ret_val = TRUE;
		}
	}
	return ret_val;
}
예제 #7
0
/**
 * parser_cxx_assist_parse_expression:
 * @assist: self,
 * @iter: current cursor position
 * @start_iter: return location for the start of the completion
 * 
 * Returns: An iter of a list of IAnjutaSymbols or NULL
 */
static IAnjutaIterable*
parser_cxx_assist_parse_expression (ParserCxxAssist* assist, IAnjutaIterable* iter, IAnjutaIterable** start_iter)
{
	IAnjutaEditor* editor = IANJUTA_EDITOR (assist->priv->iassist);
	IAnjutaIterable* res = NULL;
	IAnjutaIterable* cur_pos = ianjuta_iterable_clone (iter, NULL);
	gboolean op_start = FALSE;
	gboolean ref_start = FALSE;
	gchar* stmt = NULL;
	
	/* Cursor points after the current characters, move back */
	ianjuta_iterable_previous (cur_pos, NULL);
	
	/* Search for a operator in the current line */
	do 
	{
		gchar ch = ianjuta_editor_cell_get_char (IANJUTA_EDITOR_CELL(cur_pos),
		                                         0, NULL);
		
		if (parser_cxx_assist_is_expression_separator(ch, FALSE, iter)) {
			break;
		}

		if (ch == '.' || (op_start && ch == '-') || (ref_start && ch == ':'))
		{
			/* Found an operator, get the statement and the pre_word */
			IAnjutaIterable* pre_word_start = ianjuta_iterable_clone (cur_pos,
			                                                          NULL);
			IAnjutaIterable* pre_word_end = ianjuta_iterable_clone (iter, NULL);
			IAnjutaIterable* stmt_end = ianjuta_iterable_clone (pre_word_start,
			                                                    NULL);

			
			/* we need to pass to the parser all the statement included the last
			 * operator, being it "." or "->" or "::"
			 * Increase the end bound of the statement.
			 */
			ianjuta_iterable_next (stmt_end, NULL);
			if (op_start == TRUE || ref_start == TRUE)
				ianjuta_iterable_next (stmt_end, NULL);
				
			
			/* Move one character forward so we have the start of the pre_word
			 * and not the last operator */
			ianjuta_iterable_next (pre_word_start, NULL);
			/* If this is a two character operator, skip the second character */
			if (op_start)
			{
				ianjuta_iterable_next (pre_word_start, NULL);
			}
			
			parser_cxx_assist_update_pre_word (assist, ianjuta_editor_get_text (
			                                                   editor,
			                                                   pre_word_start,
			                                                   pre_word_end,
			                                                   NULL));

			/* Try to get the name of the variable */
			while (ianjuta_iterable_previous (cur_pos, NULL))
			{
				gchar word_ch = ianjuta_editor_cell_get_char (
				                        IANJUTA_EDITOR_CELL(cur_pos), 0, NULL);
				
				if (parser_cxx_assist_is_expression_separator(word_ch, FALSE,
				                                              cur_pos)) 
					break;				
			}
			ianjuta_iterable_next (cur_pos, NULL);
			stmt = ianjuta_editor_get_text (editor,
			                                cur_pos, stmt_end, NULL);
			*start_iter = pre_word_start;
			g_object_unref (stmt_end);
			g_object_unref (pre_word_end);
			break;
		}
		else if (ch == '>')
			op_start = TRUE;
		else if (ch == ':')
			ref_start = TRUE;
		else
		{
			op_start = FALSE;
			ref_start = FALSE;
		}
	}
	while (ianjuta_iterable_previous (cur_pos, NULL));

	if (stmt)
	{
		gint lineno;
		gchar *above_text;
		IAnjutaIterable* start;
		
		if (!assist->priv->editor_filename)
		{
			g_free (stmt);
			return NULL;
		}
		
		start = ianjuta_editor_get_start_position (editor, NULL);
		above_text = ianjuta_editor_get_text (editor, start, iter, NULL);
		g_object_unref (start);
		
		lineno = ianjuta_editor_get_lineno (editor, NULL);

		/* the parser works even for the "Gtk::" like expressions, so it 
		 * shouldn't be created a specific case to handle this.
		 */
		res = engine_parser_process_expression (stmt,
		                                        above_text,
		                                        assist->priv->editor_filename,
		                                        lineno);
		g_free (stmt);
	}
	g_object_unref (cur_pos);
	return res;
}
예제 #8
0
gboolean
search_box_incremental_search (SearchBox* search_box,
                               gboolean search_forward,
                               gboolean search_next,
                               gboolean wrap)
{
	IAnjutaIterable* real_start;
	IAnjutaEditorCell* search_start;
	IAnjutaEditorCell* search_end;
	IAnjutaEditorCell* result_start;
	IAnjutaEditorCell* result_end;
	IAnjutaEditorSelection* selection;

	const gchar* search_text = gtk_entry_get_text (GTK_ENTRY (search_box->priv->search_entry));

	gboolean found = FALSE;

	if (!search_box->priv->current_editor || !search_text || !strlen (search_text))
		return FALSE;

	selection = IANJUTA_EDITOR_SELECTION (search_box->priv->current_editor);

	if (ianjuta_editor_selection_has_selection (selection, NULL))
	{
		search_start =
			IANJUTA_EDITOR_CELL (ianjuta_editor_selection_get_start (selection, NULL));
	}
	else
	{
		search_start =
			IANJUTA_EDITOR_CELL (ianjuta_editor_get_position (search_box->priv->current_editor,
			                                                  NULL));
	}

	real_start =
		ianjuta_iterable_clone (IANJUTA_ITERABLE (search_start), NULL);

	/* If forward, set search start and end to current position of
	 * cursor and editor end, respectively, or if backward to editor
	 * start and current position of cursor, respectively. Current
	 * position of cursor is selection start if have selection. */
	if (search_forward)
	{
		search_end = IANJUTA_EDITOR_CELL (ianjuta_editor_get_position (search_box->priv->current_editor,
	                                                                   NULL));
		ianjuta_iterable_last (IANJUTA_ITERABLE (search_end), NULL);
	}
	else
	{
		search_end = search_start;
		search_start = IANJUTA_EDITOR_CELL (ianjuta_editor_get_position (search_box->priv->current_editor,
	                                                                     NULL));
		ianjuta_iterable_first (IANJUTA_ITERABLE (search_start), NULL);
	}

	/* When there's a selection, if forward, set search start and end
     * to match end and editor end, respectively, or if backward to
     * editor start and match start, respectively. If forward and
     * selection starts with a match, look for next match.
	 */
	if (ianjuta_editor_selection_has_selection (selection,
	                                            NULL) && search_next)
	{
		gchar* selected_text =
			ianjuta_editor_selection_get (selection, NULL);

		gint start_pos, end_pos;
		gboolean selected_have_search_text = FALSE;

		selected_have_search_text = search_box->priv->regex_mode ?
			search_regex_in_text (search_text, selected_text, TRUE, &start_pos, &end_pos) :
			search_str_in_text (search_text, selected_text, search_box->priv->case_sensitive, &start_pos, &end_pos);

		if (selected_have_search_text)
		{
			IAnjutaIterable* selection_start =
				ianjuta_editor_selection_get_start (selection, NULL);

			if (search_forward && start_pos == 0)
			{
				end_pos += ianjuta_iterable_get_position(IANJUTA_ITERABLE (selection_start), NULL);
				ianjuta_iterable_set_position (IANJUTA_ITERABLE(search_start), end_pos, NULL);
				ianjuta_iterable_last (IANJUTA_ITERABLE (search_end), NULL);
			}
			else if (!search_forward)
			{
				start_pos += ianjuta_iterable_get_position(IANJUTA_ITERABLE (selection_start), NULL);
				ianjuta_iterable_set_position (IANJUTA_ITERABLE(search_end), start_pos, NULL);
				ianjuta_iterable_first (IANJUTA_ITERABLE (search_start), NULL);
			}
			g_object_unref (selection_start);
		}

		g_free (selected_text);
	}

	/* Try searching in current position */
	found = editor_search (search_box->priv->current_editor,
	                       search_text,
	                       search_box->priv->case_sensitive,
	                       search_forward,
	                       search_box->priv->regex_mode,
	                       search_start,
	                       search_end,
	                       &result_start,
	                       &result_end);

	if (found)
	{
		anjuta_status_pop (ANJUTA_STATUS (search_box->priv->status));
	}
	else if (wrap)
	{
		/* Try to wrap if not found */
		ianjuta_iterable_first (IANJUTA_ITERABLE (search_start), NULL);
		ianjuta_iterable_last (IANJUTA_ITERABLE (search_end), NULL);

		/* Try to search again */
		found = editor_search (search_box->priv->current_editor,
		                       search_text,
		                       search_box->priv->case_sensitive,
		                       search_forward,
		                       search_box->priv->regex_mode,
		                       search_start,
		                       search_end,
		                       &result_start,
		                       &result_end);

		/* Check if successful */
		if (found)
		{
			if (ianjuta_iterable_compare (IANJUTA_ITERABLE (result_start),
			                              real_start, NULL) != 0)
			{
				anjuta_status_pop (search_box->priv->status);
				if (search_forward)
				{
					anjuta_status_push (search_box->priv->status,
					                    _("Search for \"%s\" reached the end and was continued at the top."), search_text);
				}
				else
				{
					anjuta_status_push (search_box->priv->status,
					                    _("Search for \"%s\" reached top and was continued at the bottom."), search_text);
				}
			}
			else if (ianjuta_editor_selection_has_selection (selection, NULL))
			{
				found = FALSE;
				anjuta_status_pop (search_box->priv->status);
				if (search_forward)
				{
					anjuta_status_push (search_box->priv->status,
						                _("Search for \"%s\" reached the end and was continued at the top but no new match was found."), search_text);
				}
				else
				{
					anjuta_status_push (search_box->priv->status,
						                _("Search for \"%s\" reached top and was continued at the bottom but no new match was found."), search_text);
				}
			}
			else
			{
				found = FALSE;
			}
		}
	}

	if (found)
	{
		ianjuta_editor_selection_set (selection,
		                              IANJUTA_ITERABLE (result_start),
		                              IANJUTA_ITERABLE (result_end), TRUE, NULL);
		g_object_unref (result_start);
		g_object_unref (result_end);
	}

	search_box_set_entry_color (search_box, found);	
	g_object_unref (real_start);
	g_object_unref (search_start);
	g_object_unref (search_end);

	return found;
}
예제 #9
0
/* Search string in editor, return TRUE and matching part as start and
 * end editor cell */
static gboolean
editor_search (IAnjutaEditor *editor,
               const gchar *search_text, 
               gboolean case_sensitive,
               gboolean search_forward,
               gboolean regex_mode,
               IAnjutaEditorCell* search_start,
               IAnjutaEditorCell* search_end,
               IAnjutaEditorCell** result_start,
               IAnjutaEditorCell** result_end)
{
	gboolean found;

	if (regex_mode)
	{
		gint start_pos;
		gint end_pos;
		gchar *text_to_search;

		text_to_search = ianjuta_editor_get_text (editor,
		                                          IANJUTA_ITERABLE (search_start),
		                                          IANJUTA_ITERABLE (search_end), NULL);

		found = search_regex_in_text (search_text, text_to_search, search_forward, &start_pos, &end_pos);

		start_pos += ianjuta_iterable_get_position(IANJUTA_ITERABLE (search_start), NULL);
		end_pos += ianjuta_iterable_get_position(IANJUTA_ITERABLE (search_start), NULL);

		if (found && start_pos >= 0)
		{
			*result_start = IANJUTA_EDITOR_CELL (ianjuta_editor_get_start_position (editor,
			                                                                        NULL));
			*result_end = IANJUTA_EDITOR_CELL (ianjuta_editor_get_start_position (editor,
			                                                                      NULL));

			if (!ianjuta_iterable_set_position(IANJUTA_ITERABLE(*result_start), start_pos, NULL) ||
				!ianjuta_iterable_set_position(IANJUTA_ITERABLE(*result_end), end_pos, NULL))
			{
				g_object_unref(*result_start);
				g_object_unref(*result_end);
				found = FALSE;
			}
		}

		g_free(text_to_search);
	}
	else
	{
		if (search_forward)
		{
			found = ianjuta_editor_search_forward (IANJUTA_EDITOR_SEARCH (editor),
			                                       search_text, case_sensitive,
			                                       search_start, search_end,
			                                       result_start,
			                                       result_end, NULL);
		}
		else
		{
			found = ianjuta_editor_search_backward (IANJUTA_EDITOR_SEARCH (editor),
			                                        search_text, case_sensitive,
			                                        search_end, search_start,
			                                        result_start,
			                                        result_end, NULL);
		}
	}

	return found;
}
예제 #10
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;
}
예제 #11
0
/* Skips to the end-of-line of previous non-preprocessor line. Any multiple
 * preprocessor lines are skipped. If current
 * line is not preprocessor line, nothing happens. If there is no previous
 * non-preprocessor line (we are at first line of the document which happens
 * to be preprocessor line), iter is set to the first character in the
 * document. It returns TRUE if the line is preprocessor line, otherwise
 * FALSE.
 */
static gboolean
skip_preprocessor_lines (IAnjutaEditor *editor, IAnjutaIterable *iter)
{
	gboolean line_found = FALSE;
	gboolean preprocessor_found = FALSE;
	IAnjutaIterable *new_iter = ianjuta_iterable_clone (iter, NULL);

	do
	{
		gboolean is_preprocessor = FALSE;
		if (skip_iter_to_previous_logical_line (editor, new_iter))
		{
			gchar ch;
			ch = ianjuta_editor_cell_get_char (IANJUTA_EDITOR_CELL (new_iter),
											   0, NULL);
			skip_iter_to_newline_tail (new_iter, ch);
			ianjuta_iterable_next (new_iter, NULL);
		}
		do
		{
			gchar ch = ianjuta_editor_cell_get_char (IANJUTA_EDITOR_CELL (new_iter),
													 0, NULL);
			if (ch == '#')
			{
				is_preprocessor = TRUE;
				/*
				DEBUG_PRINT ("Line %d is preprocessor line .. Skipping",
							 ianjuta_editor_get_line_from_position
							 (editor, new_iter, NULL));
				*/
				break;
			}
			if (iter_is_newline (new_iter, ch) || !isspace (ch))
			{
				skip_iter_to_newline_tail (new_iter, ch);
				break;
			}
		}
		while (ianjuta_iterable_next (new_iter, NULL));

		if (is_preprocessor)
		{
			line_found = skip_iter_to_previous_line (editor, new_iter);
			ianjuta_iterable_assign (iter, new_iter, NULL);
			preprocessor_found = TRUE;
		}
		else
		{
			/*
			DEBUG_PRINT ("Line %d is *not* preprocessor line .. Breaking",
						 ianjuta_editor_get_line_from_position
							(editor, new_iter, NULL));
			*/
			break;
		}
	}
	while (line_found);

	g_object_unref (new_iter);
	return preprocessor_found;
}
예제 #12
0
static gint
get_line_indentation (IAnjutaEditor *editor, gint line_num)
{
	IAnjutaIterable *line_begin, *line_end;
	gchar *line_string, *idx;
	gint line_indent = 0, left_braces = 0, right_braces = 0;
	gchar ch;

	line_end = ianjuta_editor_get_line_end_position (editor, line_num, NULL);

	/* Find first right brace going backwards from end of current line that is before a closing bracket */
	while (ianjuta_iterable_previous (line_end, NULL))
	{
		ch = ianjuta_editor_cell_get_char (IANJUTA_EDITOR_CELL (line_end), 0, NULL);
		if (ch == ')')
		{
			right_braces++;
			break;
		}
		else if (ch =='}')
			break;
	}

	/* Find the line which contains the left brace matching the right brace we found */
	if (right_braces > 0)
	{
		while (ianjuta_iterable_previous (line_end, NULL) && right_braces > left_braces)
		{
			ch = ianjuta_editor_cell_get_char (IANJUTA_EDITOR_CELL (line_end), 0, NULL);
			if (ch == ')')
				right_braces++;
			else if (ch == '(')
				left_braces++;
		}

		line_num = ianjuta_editor_get_line_from_position (editor, line_end, NULL);
	}

	g_object_unref (line_end);
	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 ("%s: line begin = %d, line end = %d", __FUNCTION__,
				 line_begin, line_end);
	*/
	if (ianjuta_iterable_compare (line_begin, line_end, NULL) == 0)
	{
		g_object_unref (line_begin);
		g_object_unref (line_end);
		return 0;
	}

	line_string = ianjuta_editor_get_text (editor, line_begin, line_end,
												NULL);
	g_object_unref (line_begin);
	g_object_unref (line_end);

	/* DEBUG_PRINT ("line_string = '%s'", line_string); */

	if (!line_string)
		return 0;

	idx = line_string;

	/* Find first non-white space */
	while (*idx != '\0' && isspace (*idx))
	{
		if (*idx == '\t')
			line_indent += TAB_SIZE;
		else
			line_indent++;
		idx++; /* Since we are looking for first non-space char, simple
				* increment of the utf8 chars would do */
	}
	g_free (line_string);
	return line_indent;
}
예제 #13
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);
}
예제 #14
0
static gint
get_line_auto_indentation (IndentCPlugin *plugin, IAnjutaEditor *editor,
						   gint line, gint *parenthesis_indentation)
{
	IAnjutaIterable *iter;
	IAnjutaIterable *end_iter;
	gint line_indent = 0;
	gint incomplete_statement = -1;
	gboolean colon_indent = FALSE;

	g_return_val_if_fail (line > 0, 0);

	/* be sure to set a default if we're in the first line otherwise
	 * the pointer'll be left hanging with no value.
	 */
	*parenthesis_indentation = 0;

	if (line == 1) /* First line */
	{
		return 0;
	}
	else
	{
		IAnjutaIterable* begin = ianjuta_editor_get_line_begin_position (editor, line -1 , NULL);
		IAnjutaIterable* end = ianjuta_editor_get_line_end_position (editor, line -1 , NULL);

		if (spaces_only (editor, begin, end))
		{
			set_line_indentation (plugin, editor, line -1, 0, 0);
		}
		g_object_unref (begin);
		g_object_unref (end);
	}

	iter = ianjuta_editor_get_line_begin_position (editor, line, NULL);

	if (is_iter_inside_string (iter))
	{
		line_indent = get_line_indentation (editor, line - 1);
	}
	else
	{
		line_indent = get_line_indentation_base (plugin, editor, line,
												 &incomplete_statement,
												 parenthesis_indentation,
												 &colon_indent);
	}

	if (colon_indent)
	{
		/* If the last non-whitespace character in the line is ":" then
		 * we remove the extra colon_indent
		 */
		end_iter = ianjuta_editor_get_line_end_position (editor, line, NULL);
		gchar ch;
		while (ianjuta_iterable_previous (end_iter, NULL))
		{
			ch = ianjuta_editor_cell_get_char (IANJUTA_EDITOR_CELL (end_iter),
			                                   0, NULL);
			if (ch == ':')
			{
				line_indent -= INDENT_SIZE;
				break;
			}
			if (!isspace(ch) || iter_is_newline (end_iter, ch))
				break;
		}
		g_object_unref (end_iter);
	}

	/* Determine what the first non-white char in the line is */
	do
	{
		gchar ch;
		/* Check if we are *inside* comment or string. Begining of comment
		 * or string does not count as inside. If inside, just align with
		 * previous indentation.
		 */
		if (is_iter_inside_string (iter))
		{
			line_indent = get_line_indentation (editor, line - 1);
			break;
		}
		ch = ianjuta_editor_cell_get_char (IANJUTA_EDITOR_CELL (iter),
										   0, NULL);
		if (iter_is_newline (iter, ch))
		{
			skip_iter_to_newline_tail (iter, ch);

			/* First levels are excused from incomplete statement indent */
			if (incomplete_statement == 1 && line_indent > 0)
				line_indent += INDENT_SIZE;
			break;
		}

		if (ch == '{')
		{
			if (line_indent > 0)
			{
				/* The first level braces are excused from brace indentation */
				/*
				DEBUG_PRINT ("Increasing indent level from %d to %d",
							 line_indent,
							 line_indent + BRACE_INDENT);
				*/
				line_indent += BRACE_INDENT;
				/* It looks ugly to add extra indent after case: so remove that */
				if (colon_indent)
					line_indent -= INDENT_SIZE;
			}
			break;
		}
		else if (ch == '}')
		{
			ianjuta_iterable_previous (iter, NULL);
			if (anjuta_util_jump_to_matching_brace (iter, ch, -1))
			{
				gint line = ianjuta_editor_get_line_from_position (editor,
																   iter,
																   NULL);
				line_indent = get_line_indentation (editor, line);
			}
			break;
		}
		else if (ch == '#')
		{
			line_indent = 0;
			*parenthesis_indentation = 0;
		}
		else if (!isspace (ch))
		{
			/* First levels are excused from incomplete statement indent */
			if (incomplete_statement == 1 && line_indent > 0)
				line_indent += INDENT_SIZE;
			break;
		}
	}
	while (ianjuta_iterable_next (iter, NULL));
	g_object_unref (iter);

	return line_indent;
}