1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
4 Copyright (C) 2008 Sébastien Granjoux
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any laterdversion.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 * Group all objects handling variables (watch, local) and take care of general
23 * feature like variable tips.
24 *---------------------------------------------------------------------------*/
31 #include "utilities.h"
36 #include <libanjuta/anjuta-debug.h>
37 #include <libanjuta/interfaces/ianjuta-editor-hover.h>
38 #include <libanjuta/interfaces/ianjuta-editor-selection.h>
39 #include <libanjuta/interfaces/ianjuta-document-manager.h>
42 *---------------------------------------------------------------------------*/
44 #define DEFAULT_VARIABLE_NAME 64
47 *---------------------------------------------------------------------------*/
49 struct _DmaVariableDBase
56 guint editor_watch
; /* Editor watch */
57 IAnjutaEditor
* editor
; /* Current editor */
61 *---------------------------------------------------------------------------*/
66 return g_ascii_isalnum (c
) || (c
== '_');
70 get_hovered_word (IAnjutaEditor
* editor
, IAnjutaIterable
* iter
)
73 IAnjutaIterable
*start
;
76 if (iter
== NULL
) return NULL
;
78 /* Get selected characters if possible */
79 if (IANJUTA_IS_EDITOR_SELECTION (editor
))
81 /* Check if hover on selection */
82 start
= ianjuta_editor_selection_get_start (IANJUTA_EDITOR_SELECTION (editor
), NULL
);
83 if (start
&& (ianjuta_iterable_compare (start
, iter
, NULL
) <= 0))
85 end
= ianjuta_editor_selection_get_end (IANJUTA_EDITOR_SELECTION (editor
), NULL
);
86 if (end
&& (ianjuta_iterable_compare (end
, iter
, NULL
) > 0))
88 /* Hover on selection, get selected characters */
90 g_object_unref (start
);
91 return ianjuta_editor_selection_get (IANJUTA_EDITOR_SELECTION (editor
),
94 if (end
) g_object_unref (end
);
96 if (start
) g_object_unref (start
);
99 /* Find word below cursor */
100 DEBUG_PRINT("current char %c", ianjuta_editor_cell_get_char (IANJUTA_EDITOR_CELL (iter
), 0, NULL
));
101 if (!is_name (ianjuta_editor_cell_get_char (IANJUTA_EDITOR_CELL (iter
), 0, NULL
)))
103 /* Not hover on a name */
107 /* Look for the beginning of the name */
108 for (start
= ianjuta_iterable_clone (iter
, NULL
); ianjuta_iterable_previous (start
, NULL
);)
110 if (!is_name (ianjuta_editor_cell_get_char (IANJUTA_EDITOR_CELL (start
), 0, NULL
)))
112 ianjuta_iterable_next (start
, NULL
);
117 /* Look for the end of the name */
118 for (end
= ianjuta_iterable_clone (iter
, NULL
); ianjuta_iterable_next (end
, NULL
);)
120 if (!is_name (ianjuta_editor_cell_get_char (IANJUTA_EDITOR_CELL (end
), 0, NULL
)))
124 buf
= ianjuta_editor_get_text (editor
, start
, end
, NULL
);
125 DEBUG_PRINT("get name %s", buf
== NULL
? "(null)" : buf
);
126 g_object_unref (start
);
127 g_object_unref (end
);
133 *---------------------------------------------------------------------------*/
135 /* Callback functions
136 *---------------------------------------------------------------------------*/
139 on_hover_over (DmaVariableDBase
*self
, IAnjutaIterable
* pos
, IAnjutaEditorHover
* editor
)
143 DEBUG_PRINT("Hover on editor %p at %d", editor
,
144 pos
== NULL
? -1 : ianjuta_iterable_get_position (pos
, NULL
));
146 name
= get_hovered_word (IANJUTA_EDITOR (editor
), pos
);
151 /* Search for variable in local */
152 value
= locals_find_variable_value (self
->locals
, name
);
155 /* Not found search in watch */
156 value
= expr_watch_find_variable_value (self
->watch
, name
);
167 display
= g_strconcat(name
, " = ", value
, NULL
);
168 ianjuta_editor_hover_display (editor
, pos
, display
, NULL
);
176 on_added_current_editor (AnjutaPlugin
*plugin
, const char *name
,
177 const GValue
*value
, gpointer user_data
)
179 DmaVariableDBase
*self
= (DmaVariableDBase
*)user_data
;
182 editor
= g_value_get_object (value
);
184 /* Connect to hover interface */
185 if (IANJUTA_IS_EDITOR_HOVER (editor
))
187 g_signal_connect_swapped (editor
, "hover-over", G_CALLBACK (on_hover_over
), self
);
188 self
->editor
= IANJUTA_EDITOR (editor
);
193 on_removed_current_editor (AnjutaPlugin
*plugin
,
194 const char *name
, gpointer user_data
)
196 DmaVariableDBase
*self
= (DmaVariableDBase
*)user_data
;
198 /* Disconnect to previous hover interface */
199 if (self
->editor
!= NULL
)
201 g_signal_handlers_disconnect_matched (self
->editor
, G_SIGNAL_MATCH_DATA
, 0, 0, NULL
, NULL
, self
);
207 on_program_running (DmaVariableDBase
*self
)
209 if (self
->editor_watch
!= -1)
211 anjuta_plugin_remove_watch (ANJUTA_PLUGIN(self
->plugin
), self
->editor_watch
, TRUE
);
212 self
->editor_watch
= -1;
217 on_program_stopped (DmaVariableDBase
*self
)
219 if (self
->editor_watch
== -1)
221 /* set editor watch */
222 self
->editor_watch
= anjuta_plugin_add_watch (ANJUTA_PLUGIN(self
->plugin
), IANJUTA_DOCUMENT_MANAGER_CURRENT_DOCUMENT
,
223 on_added_current_editor
, on_removed_current_editor
, self
);
228 on_program_exited (DmaVariableDBase
*self
)
230 if (self
->editor_watch
!= -1)
232 anjuta_plugin_remove_watch (ANJUTA_PLUGIN(self
->plugin
), self
->editor_watch
, TRUE
);
233 self
->editor_watch
= -1;
236 /* Disconnect from other debugger signal */
237 g_signal_handlers_disconnect_by_func (self
->plugin
, G_CALLBACK (on_program_exited
), self
);
238 g_signal_handlers_disconnect_by_func (self
->plugin
, G_CALLBACK (on_program_stopped
), self
);
239 g_signal_handlers_disconnect_by_func (self
->plugin
, G_CALLBACK (on_program_running
), self
);
243 on_program_started (DmaVariableDBase
*self
)
245 /* Connect to other debugger signal */
246 g_signal_connect_swapped (self
->plugin
, "program-stopped", G_CALLBACK (on_program_stopped
), self
);
247 g_signal_connect_swapped (self
->plugin
, "program-exited", G_CALLBACK (on_program_exited
), self
);
248 g_signal_connect_swapped (self
->plugin
, "program-running", G_CALLBACK (on_program_running
), self
);
251 /* Constructor & Destructor
252 *---------------------------------------------------------------------------*/
255 dma_variable_dbase_new (DebugManagerPlugin
*plugin
)
257 DmaVariableDBase
*self
= g_new0 (DmaVariableDBase
, 1);
259 self
->plugin
= ANJUTA_PLUGIN (plugin
);
260 self
->editor_watch
= -1;
262 self
->watch
= expr_watch_new (ANJUTA_PLUGIN (plugin
));
263 self
->locals
= locals_new (plugin
);
265 g_signal_connect_swapped (self
->plugin
, "program-started", G_CALLBACK (on_program_started
), self
);
271 dma_variable_dbase_free (DmaVariableDBase
*self
)
273 g_return_if_fail (self
!= NULL
);
275 g_signal_handlers_disconnect_matched (self
->plugin
, G_SIGNAL_MATCH_DATA
, 0, 0, NULL
, NULL
, self
);
277 locals_free (self
->locals
);
278 expr_watch_destroy (self
->watch
);