File: variable.c

package info (click to toggle)
anjuta 2%3A2.32.0.0-5
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 61,524 kB
  • ctags: 29,402
  • sloc: ansic: 207,516; xml: 31,169; cpp: 11,403; sh: 10,749; perl: 7,107; makefile: 3,373; yacc: 1,018; lex: 126; sql: 97; python: 91; java: 6
file content (281 lines) | stat: -rw-r--r-- 8,030 bytes parent folder | download | duplicates (7)
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
/*
    variable.c
    Copyright (C) 2008  Sébastien Granjoux

    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 laterdversion.

    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
*/

/*
 * Group all objects handling variables (watch, local) and take care of general
 * feature like variable tips.
 *---------------------------------------------------------------------------*/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include "variable.h"
#include "utilities.h"
#include "locals.h"
#include "watch.h"

/*#define DEBUG*/
#include <libanjuta/anjuta-debug.h>
#include <libanjuta/interfaces/ianjuta-editor-hover.h>
#include <libanjuta/interfaces/ianjuta-editor-selection.h>
#include <libanjuta/interfaces/ianjuta-document-manager.h>

/* Constants
 *---------------------------------------------------------------------------*/

#define DEFAULT_VARIABLE_NAME	64

/* Types
 *---------------------------------------------------------------------------*/

struct _DmaVariableDBase
{
	AnjutaPlugin *plugin;

	Locals *locals;
	ExprWatch *watch;
	
	guint editor_watch;			/* Editor watch */
	IAnjutaEditor* editor;		/* Current editor */
};

/* Helper functions
 *---------------------------------------------------------------------------*/

static gboolean
is_name (gchar c)
{
	return g_ascii_isalnum (c) || (c == '_');
}

static gchar*
get_hovered_word (IAnjutaEditor* editor, IAnjutaIterable* iter)
{
	gchar *buf;
	IAnjutaIterable *start;
	IAnjutaIterable	*end;
	
	if (iter == NULL) return NULL;
	
	/* Get selected characters if possible */
	if (IANJUTA_IS_EDITOR_SELECTION (editor))
	{
		/* Check if hover on selection */
		start = ianjuta_editor_selection_get_start (IANJUTA_EDITOR_SELECTION (editor), NULL);
		if (start && (ianjuta_iterable_compare (start, iter, NULL) <= 0))
		{
			end = ianjuta_editor_selection_get_end (IANJUTA_EDITOR_SELECTION (editor), NULL);
			if (end && (ianjuta_iterable_compare (end, iter, NULL) > 0))
			{
				/* Hover on selection, get selected characters */
			    g_object_unref (end);
				g_object_unref (start);
				return ianjuta_editor_selection_get (IANJUTA_EDITOR_SELECTION (editor),
													 NULL);
			}
			if (end) g_object_unref (end);
		}
		if (start) g_object_unref (start);
	}
	
	/* Find word below cursor */
	DEBUG_PRINT("current char %c", ianjuta_editor_cell_get_char (IANJUTA_EDITOR_CELL (iter), 0, NULL));
	if (!is_name (ianjuta_editor_cell_get_char (IANJUTA_EDITOR_CELL (iter), 0, NULL)))
	{
		/* Not hover on a name */
		return NULL;
	}
	
	/* Look for the beginning of the name */
	for (start = ianjuta_iterable_clone (iter, NULL); ianjuta_iterable_previous (start, NULL);)
	{
		if (!is_name (ianjuta_editor_cell_get_char (IANJUTA_EDITOR_CELL (start), 0, NULL)))
		{
			ianjuta_iterable_next (start, NULL);
			break;
		}
	}

	/* Look for the end of the name */
	for (end = ianjuta_iterable_clone (iter, NULL); ianjuta_iterable_next (end, NULL);)
	{
		if (!is_name (ianjuta_editor_cell_get_char (IANJUTA_EDITOR_CELL (end), 0, NULL)))
			break;
	}

	buf = ianjuta_editor_get_text (editor, start, end, NULL);
	DEBUG_PRINT("get name %s", buf == NULL ? "(null)" : buf);
	g_object_unref (start);
	g_object_unref (end);
	
	return buf;
}
	
/* Private functions
 *---------------------------------------------------------------------------*/

/* Callback functions
 *---------------------------------------------------------------------------*/

static void
on_hover_over (DmaVariableDBase *self, IAnjutaIterable* pos, IAnjutaEditorHover* editor)
{
	gchar *name;
	
	DEBUG_PRINT("Hover on editor %p at %d", editor, 
				pos == NULL ? -1 : ianjuta_iterable_get_position (pos, NULL));
	
	name = get_hovered_word (IANJUTA_EDITOR (editor), pos);
	if (name != NULL)
	{
		gchar *value;
		
		/* Search for variable in local */
		value = locals_find_variable_value (self->locals, name);
		if (value == NULL)
		{
			/* Not found search in watch */
			value = expr_watch_find_variable_value (self->watch, name);
			if (value == NULL)
			{
				/* Does not exist */
				g_free (name);
				return;
			}
		}
		
		gchar *display;
		
		display = g_strconcat(name, " = ", value, NULL);
		ianjuta_editor_hover_display (editor, pos, display, NULL);
		g_free (display);
		g_free (value);
		g_free (name);
	}
}

static void
on_added_current_editor (AnjutaPlugin *plugin, const char *name,
							const GValue *value, gpointer user_data)
{
	DmaVariableDBase *self = (DmaVariableDBase *)user_data;
	GObject *editor;
		
	editor = g_value_get_object (value);
	
    /* Connect to hover interface */
	if (IANJUTA_IS_EDITOR_HOVER (editor))
	{
		g_signal_connect_swapped (editor, "hover-over", G_CALLBACK (on_hover_over), self);
		self->editor = IANJUTA_EDITOR (editor);
	}
}

static void
on_removed_current_editor (AnjutaPlugin *plugin,
							  const char *name, gpointer user_data)
{
	DmaVariableDBase *self = (DmaVariableDBase *)user_data;

	/* Disconnect to previous hover interface */
	if (self->editor != NULL)
	{
		g_signal_handlers_disconnect_matched (self->editor, G_SIGNAL_MATCH_DATA, 0, 0, NULL, NULL, self);
		self->editor = NULL;
	}
}

static void
on_program_running (DmaVariableDBase *self)
{
	if (self->editor_watch != -1)
	{
		anjuta_plugin_remove_watch (ANJUTA_PLUGIN(self->plugin), self->editor_watch, TRUE);
		self->editor_watch = -1;
	}
}

static void
on_program_stopped (DmaVariableDBase *self)
{
	if (self->editor_watch == -1)
	{
		/* set editor watch */
		self->editor_watch = anjuta_plugin_add_watch (ANJUTA_PLUGIN(self->plugin), IANJUTA_DOCUMENT_MANAGER_CURRENT_DOCUMENT,
								 on_added_current_editor, on_removed_current_editor, self);
	}
}

static void
on_program_exited (DmaVariableDBase *self)
{
	if (self->editor_watch != -1)
	{
		anjuta_plugin_remove_watch (ANJUTA_PLUGIN(self->plugin), self->editor_watch, TRUE);
		self->editor_watch = -1;
	}

	/* Disconnect from other debugger signal */
	g_signal_handlers_disconnect_by_func (self->plugin, G_CALLBACK (on_program_exited), self);
	g_signal_handlers_disconnect_by_func (self->plugin, G_CALLBACK (on_program_stopped), self);
	g_signal_handlers_disconnect_by_func (self->plugin, G_CALLBACK (on_program_running), self);
}	

static void
on_program_started (DmaVariableDBase *self)
{
	/* Connect to other debugger signal */
	g_signal_connect_swapped (self->plugin, "program-stopped", G_CALLBACK (on_program_stopped), self);
	g_signal_connect_swapped (self->plugin, "program-exited", G_CALLBACK (on_program_exited), self);
	g_signal_connect_swapped (self->plugin, "program-running", G_CALLBACK (on_program_running), self);
}

/* Constructor & Destructor
 *---------------------------------------------------------------------------*/

DmaVariableDBase *
dma_variable_dbase_new (DebugManagerPlugin *plugin)
{
	DmaVariableDBase *self = g_new0 (DmaVariableDBase, 1);

	self->plugin = ANJUTA_PLUGIN (plugin);
	self->editor_watch = -1;
	self->editor = NULL;
	self->watch = expr_watch_new (ANJUTA_PLUGIN (plugin));
	self->locals = locals_new (plugin);
	
	g_signal_connect_swapped (self->plugin, "program-started", G_CALLBACK (on_program_started), self);
	
	return self;
}

void
dma_variable_dbase_free (DmaVariableDBase *self)
{
	g_return_if_fail (self != NULL);
	
	g_signal_handlers_disconnect_matched (self->plugin, G_SIGNAL_MATCH_DATA, 0, 0, NULL, NULL, self);

	locals_free (self->locals);
    expr_watch_destroy (self->watch);
	
	g_free (self);
}