Reenabled completion

master
Yevgen Muntyan 2007-12-06 10:23:29 -06:00
parent 9c69efdb16
commit 5df2716943
5 changed files with 130 additions and 85 deletions

View File

@ -54,6 +54,7 @@ completion_sources = \
mooedit_sources = \ mooedit_sources = \
$(mooedit_include_headers) \ $(mooedit_include_headers) \
$(moocommand_stuff) \ $(moocommand_stuff) \
$(completion_sources) \
moocmdview.c \ moocmdview.c \
moocommand.c \ moocommand.c \
moocommanddisplay.c \ moocommanddisplay.c \
@ -207,7 +208,6 @@ BUILT_SOURCES += mooedit-ui.h medit-ui.h
CLEANFILES += mooedit-ui.h medit-ui.h CLEANFILES += mooedit-ui.h medit-ui.h
EXTRA_DIST += \ EXTRA_DIST += \
$(completion_sources) \
$(mooedit_tools_DATA) \ $(mooedit_tools_DATA) \
$(mooedit_lua_DATA) \ $(mooedit_lua_DATA) \
medit-ui.xml \ medit-ui.xml \

View File

@ -12,10 +12,8 @@
#include "mooedit/moocompletionsimple.h" #include "mooedit/moocompletionsimple.h"
#include "mooedit/mootextpopup.h" #include "mooedit/mootextpopup.h"
#include "mooedit/mooedit-script.h"
#include "mooutils/moomarshals.h" #include "mooutils/moomarshals.h"
#include "mooutils/eggregex.h" #include <glib/gregex.h>
#include "mooscript/mooscript-parser.h"
#include <gtk/gtk.h> #include <gtk/gtk.h>
#include <gdk/gdkkeysyms.h> #include <gdk/gdkkeysyms.h>
#include <string.h> #include <string.h>
@ -50,14 +48,14 @@ struct _MooCompletionSimplePrivate {
struct _MooCompletionGroup { struct _MooCompletionGroup {
char *name; char *name;
EggRegex *regex; GRegex *regex;
guint *parens; guint *parens;
guint n_parens; guint n_parens;
GCompletion *cmpl; GCompletion *cmpl;
GList *data; GList *data;
char *suffix; char *suffix;
MSNode *script; gpointer script;
MooCompletionFreeFunc free_func; MooCompletionFreeFunc free_func;
}; };
@ -230,36 +228,34 @@ list_sort_func (GtkTreeModel *model,
static void static void
moo_completion_simple_exec_script (MooCompletionSimple *cmpl, moo_completion_simple_exec_script (G_GNUC_UNUSED MooCompletionSimple *cmpl,
MooCompletionGroup *group, G_GNUC_UNUSED MooCompletionGroup *group,
GtkTextIter *start, G_GNUC_UNUSED GtkTextIter *start,
GtkTextIter *end, G_GNUC_UNUSED GtkTextIter *end,
const char *completion) G_GNUC_UNUSED const char *completion)
{ {
MSContext *ctx; // char *match;
char *match; // GtkTextView *doc;
MSValue *result; //
GtkTextView *doc; // doc = moo_text_completion_get_doc (MOO_TEXT_COMPLETION (cmpl));
// ctx = moo_edit_script_context_new (doc, NULL);
doc = moo_text_completion_get_doc (MOO_TEXT_COMPLETION (cmpl)); // match = gtk_text_iter_get_slice (start, end);
ctx = moo_edit_script_context_new (doc, NULL); //
match = gtk_text_iter_get_slice (start, end); // ms_context_assign_string (ctx, MOO_COMPLETION_VAR_MATCH, match);
// ms_context_assign_string (ctx, MOO_COMPLETION_VAR_COMPLETION, completion);
ms_context_assign_string (ctx, MOO_COMPLETION_VAR_MATCH, match); //
ms_context_assign_string (ctx, MOO_COMPLETION_VAR_COMPLETION, completion); // gtk_text_buffer_delete (get_buffer (cmpl), start, end);
// gtk_text_buffer_place_cursor (get_buffer (cmpl), start);
gtk_text_buffer_delete (get_buffer (cmpl), start, end); // result = ms_top_node_eval (group->script, ctx);
gtk_text_buffer_place_cursor (get_buffer (cmpl), start); //
result = ms_top_node_eval (group->script, ctx); // if (result)
// ms_value_unref (result);
if (result) // else
ms_value_unref (result); // g_warning ("%s: %s", G_STRLOC,
else // ms_context_get_error_msg (ctx));
g_warning ("%s: %s", G_STRLOC, //
ms_context_get_error_msg (ctx)); // g_free (match);
// g_object_unref (ctx);
g_free (match);
g_object_unref (ctx);
} }
@ -286,18 +282,16 @@ moo_completion_simple_complete (MooTextCompletion *text_cmpl,
moo_text_completion_get_region (MOO_TEXT_COMPLETION (cmpl), &start, &end); moo_text_completion_get_region (MOO_TEXT_COMPLETION (cmpl), &start, &end);
old_text = gtk_text_iter_get_slice (&start, &end); old_text = gtk_text_iter_get_slice (&start, &end);
gtk_text_buffer_begin_user_action (get_buffer (cmpl));
if (group->script) if (group->script)
{ {
moo_completion_simple_exec_script (cmpl, group, &start, &end, text); moo_completion_simple_exec_script (cmpl, group, &start, &end, text);
} }
else else
{ {
if (strcmp (text, old_text)) if (strcmp (text, old_text) != 0)
{ {
gtk_text_buffer_delete (get_buffer (cmpl), &start, &end); _moo_text_completion_replace_text (MOO_TEXT_COMPLETION (cmpl),
gtk_text_buffer_insert (get_buffer (cmpl), &start, text, -1); &start, &end, text);
set_cursor = TRUE; set_cursor = TRUE;
} }
@ -324,7 +318,7 @@ moo_completion_simple_complete (MooTextCompletion *text_cmpl,
if (do_insert) if (do_insert)
{ {
gtk_text_buffer_insert (get_buffer (cmpl), &start, group->suffix, -1); _moo_text_completion_replace_text (text_cmpl, &start, &start, group->suffix);
set_cursor = TRUE; set_cursor = TRUE;
} }
} }
@ -333,10 +327,6 @@ moo_completion_simple_complete (MooTextCompletion *text_cmpl,
if (set_cursor) if (set_cursor)
gtk_text_buffer_place_cursor (get_buffer (cmpl), &start); gtk_text_buffer_place_cursor (get_buffer (cmpl), &start);
gtk_text_buffer_end_user_action (get_buffer (cmpl));
moo_text_completion_hide (MOO_TEXT_COMPLETION (cmpl));
g_free (old_text); g_free (old_text);
} }
@ -534,6 +524,19 @@ moo_completion_group_add_data (MooCompletionGroup *group,
} }
void
moo_completion_group_remove_data (MooCompletionGroup *group,
GList *data)
{
g_return_if_fail (group != NULL);
if (!data)
return;
g_completion_remove_items (group->cmpl, data);
}
static gboolean static gboolean
moo_completion_group_find (MooCompletionGroup *group, moo_completion_group_find (MooCompletionGroup *group,
const char *line, const char *line,
@ -554,8 +557,8 @@ moo_completion_group_find (MooCompletionGroup *group,
{ {
int start_pos = -1, end_pos = -1; int start_pos = -1, end_pos = -1;
g_regex_fetch_pos (match_info, group->parens[i], g_match_info_fetch_pos (match_info, group->parens[i],
&start_pos, &end_pos); &start_pos, &end_pos);
if (start_pos >= 0 && end_pos >= 0) if (start_pos >= 0 && end_pos >= 0)
{ {
@ -599,7 +602,7 @@ moo_completion_group_set_pattern (MooCompletionGroup *group,
const guint *parens, const guint *parens,
guint n_parens) guint n_parens)
{ {
EggRegex *regex; GRegex *regex;
GError *error = NULL; GError *error = NULL;
char *real_pattern; char *real_pattern;
@ -617,7 +620,8 @@ moo_completion_group_set_pattern (MooCompletionGroup *group,
goto err; goto err;
} }
g_regex_free (group->regex); if (group->regex)
g_regex_unref (group->regex);
group->regex = regex; group->regex = regex;
g_free (group->parens); g_free (group->parens);
@ -639,8 +643,9 @@ moo_completion_group_set_pattern (MooCompletionGroup *group,
err: err:
if (error) if (error)
g_error_free (error); g_error_free (error);
if (regex)
g_regex_unref (regex);
g_free (real_pattern); g_free (real_pattern);
g_regex_free (regex);
} }
@ -659,19 +664,19 @@ moo_completion_group_set_suffix (MooCompletionGroup *group,
void void
moo_completion_group_set_script (MooCompletionGroup *group, moo_completion_group_set_script (G_GNUC_UNUSED MooCompletionGroup *group,
const char *script) G_GNUC_UNUSED const char *script)
{ {
g_return_if_fail (group != NULL); // g_return_if_fail (group != NULL);
//
if (group->script) // if (group->script)
{ // {
ms_node_unref (group->script); // ms_node_unref (group->script);
group->script = NULL; // group->script = NULL;
} // }
//
if (script) // if (script)
group->script = ms_script_parse (script); // group->script = ms_script_parse (script);
} }
@ -680,14 +685,15 @@ moo_completion_group_free (MooCompletionGroup *group)
{ {
g_return_if_fail (group != NULL); g_return_if_fail (group != NULL);
g_regex_free (group->regex); if (group->regex)
g_regex_unref (group->regex);
g_free (group->parens); g_free (group->parens);
g_completion_free (group->cmpl); g_completion_free (group->cmpl);
g_free (group->suffix); g_free (group->suffix);
g_free (group->name); g_free (group->name);
if (group->script) // if (group->script)
ms_node_unref (group->script); // ms_node_unref (group->script);
if (group->free_func) if (group->free_func)
g_list_foreach (group->data, (GFunc) group->free_func, NULL); g_list_foreach (group->data, (GFunc) group->free_func, NULL);

View File

@ -30,7 +30,7 @@ G_BEGIN_DECLS
typedef struct _MooCompletionSimple MooCompletionSimple; typedef struct _MooCompletionSimple MooCompletionSimple;
typedef struct _MooCompletionSimplePrivate MooCompletionSimplePrivate; typedef struct _MooCompletionSimplePrivate MooCompletionSimplePrivate;
typedef struct _MooCompletionSimpleClass MooCompletionSimpleClass; typedef struct _MooCompletionSimpleClass MooCompletionSimpleClass;
typedef struct _MooCompletionGroup MooCompletionGroup; typedef struct _MooCompletionGroup MooCompletionGroup;
struct _MooCompletionSimple struct _MooCompletionSimple
{ {
@ -49,6 +49,8 @@ GType moo_completion_simple_get_type (void) G_GNUC_CONST;
/* steals data */ /* steals data */
void moo_completion_group_add_data (MooCompletionGroup *group, void moo_completion_group_add_data (MooCompletionGroup *group,
GList *data); GList *data);
void moo_completion_group_remove_data (MooCompletionGroup *group,
GList *data);
void moo_completion_group_set_pattern (MooCompletionGroup *group, void moo_completion_group_set_pattern (MooCompletionGroup *group,
const char *pattern, const char *pattern,

View File

@ -48,11 +48,12 @@ static void moo_text_completion_populate (MooTextCompletion *cmp
const char *text); const char *text);
static void moo_text_completion_complete (MooTextCompletion *cmpl, static void moo_text_completion_complete (MooTextCompletion *cmpl,
GtkTreeIter *iter); GtkTreeIter *iter);
static char *moo_text_completion_get_text (MooTextCompletion *cmpl,
GtkTreeModel *model,
GtkTreeIter *iter);
static void moo_text_completion_update (MooTextCompletion *cmpl); static void moo_text_completion_update (MooTextCompletion *cmpl);
static void moo_text_completion_update_real (MooTextCompletion *cmpl); static void moo_text_completion_update_real (MooTextCompletion *cmpl);
static void moo_text_completion_replace_text_real (MooTextCompletion *cmpl,
GtkTextIter *start,
GtkTextIter *end,
const char *text);
static gboolean moo_text_completion_empty (MooTextCompletion *cmpl); static gboolean moo_text_completion_empty (MooTextCompletion *cmpl);
static gboolean moo_text_completion_unique (MooTextCompletion *cmpl, static gboolean moo_text_completion_unique (MooTextCompletion *cmpl,
@ -93,6 +94,7 @@ moo_text_completion_class_init (MooTextCompletionClass *klass)
klass->try_complete = moo_text_completion_try_complete_real; klass->try_complete = moo_text_completion_try_complete_real;
klass->update = moo_text_completion_update_real; klass->update = moo_text_completion_update_real;
klass->complete = moo_text_completion_complete_real; klass->complete = moo_text_completion_complete_real;
klass->replace_text = moo_text_completion_replace_text_real;
signals[FINISH] = g_signal_new ("finish", signals[FINISH] = g_signal_new ("finish",
G_TYPE_FROM_CLASS (klass), G_TYPE_FROM_CLASS (klass),
@ -243,12 +245,7 @@ moo_text_completion_try_complete_real (MooTextCompletion *cmpl,
prefix = find_common_prefix (cmpl, text); prefix = find_common_prefix (cmpl, text);
if (prefix && strcmp (text, prefix) != 0) if (prefix && strcmp (text, prefix) != 0)
{ _moo_text_completion_replace_text (cmpl, &start, &end, prefix);
gtk_text_buffer_begin_user_action (cmpl->priv->buffer);
gtk_text_buffer_delete (cmpl->priv->buffer, &start, &end);
gtk_text_buffer_insert (cmpl->priv->buffer, &start, prefix, -1);
gtk_text_buffer_end_user_action (cmpl->priv->buffer);
}
} }
moo_text_completion_show (cmpl); moo_text_completion_show (cmpl);
@ -266,13 +263,42 @@ finish:
return; return;
} }
void
_moo_text_completion_replace_text (MooTextCompletion *cmpl,
GtkTextIter *start,
GtkTextIter *end,
const char *text)
{
g_return_if_fail (MOO_IS_TEXT_COMPLETION (cmpl));
g_return_if_fail (start != NULL && end != NULL);
gtk_text_buffer_begin_user_action (cmpl->priv->buffer);
MOO_TEXT_COMPLETION_GET_CLASS (cmpl)->replace_text (cmpl, start, end, text);
gtk_text_buffer_end_user_action (cmpl->priv->buffer);
}
static void
moo_text_completion_replace_text_real (MooTextCompletion *cmpl,
GtkTextIter *start,
GtkTextIter *end,
const char *text)
{
if (!gtk_text_iter_equal (start, end))
gtk_text_buffer_delete (cmpl->priv->buffer, start, end);
if (text && text[0])
gtk_text_buffer_insert (cmpl->priv->buffer, start, text, -1);
}
static void static void
moo_text_completion_complete (MooTextCompletion *cmpl, moo_text_completion_complete (MooTextCompletion *cmpl,
GtkTreeIter *iter) GtkTreeIter *iter)
{ {
g_return_if_fail (MOO_TEXT_COMPLETION_GET_CLASS(cmpl)->complete != NULL); g_return_if_fail (MOO_TEXT_COMPLETION_GET_CLASS(cmpl)->complete != NULL);
gtk_text_buffer_begin_user_action (cmpl->priv->buffer);
MOO_TEXT_COMPLETION_GET_CLASS(cmpl)->complete (cmpl, cmpl->priv->model, iter); MOO_TEXT_COMPLETION_GET_CLASS(cmpl)->complete (cmpl, cmpl->priv->model, iter);
gtk_text_buffer_end_user_action (cmpl->priv->buffer);
moo_text_completion_finish (cmpl); moo_text_completion_finish (cmpl);
} }
@ -292,31 +318,28 @@ moo_text_completion_complete_real (MooTextCompletion *cmpl,
old_text = gtk_text_buffer_get_slice (cmpl->priv->buffer, old_text = gtk_text_buffer_get_slice (cmpl->priv->buffer,
&start, &end, TRUE); &start, &end, TRUE);
gtk_text_buffer_begin_user_action (cmpl->priv->buffer); if (strcmp (text, old_text) != 0)
if (strcmp (text, old_text))
{ {
gtk_text_buffer_delete (cmpl->priv->buffer, &start, &end); _moo_text_completion_replace_text (cmpl, &start, &end, text);
gtk_text_buffer_insert (cmpl->priv->buffer, &start, text, -1);
set_cursor = TRUE; set_cursor = TRUE;
} }
if (set_cursor) if (set_cursor)
gtk_text_buffer_place_cursor (cmpl->priv->buffer, &start); gtk_text_buffer_place_cursor (cmpl->priv->buffer, &start);
gtk_text_buffer_end_user_action (cmpl->priv->buffer);
moo_text_completion_hide (cmpl);
g_free (old_text); g_free (old_text);
g_free (text);
} }
static char * char *
moo_text_completion_get_text (MooTextCompletion *cmpl, moo_text_completion_get_text (MooTextCompletion *cmpl,
GtkTreeModel *model, GtkTreeModel *model,
GtkTreeIter *iter) GtkTreeIter *iter)
{ {
g_return_val_if_fail (MOO_IS_TEXT_COMPLETION (cmpl), NULL);
g_return_val_if_fail (model == cmpl->priv->model, NULL);
g_return_val_if_fail (iter != NULL, NULL);
g_return_val_if_fail (cmpl->priv->text_func != NULL, NULL); g_return_val_if_fail (cmpl->priv->text_func != NULL, NULL);
return cmpl->priv->text_func (model, iter, cmpl->priv->text_func_data); return cmpl->priv->text_func (model, iter, cmpl->priv->text_func_data);
} }

View File

@ -53,6 +53,11 @@ struct _MooTextCompletionClass
GtkTreeModel *model, GtkTreeModel *model,
GtkTextIter *cursor, GtkTextIter *cursor,
const char *text); const char *text);
void (*replace_text) (MooTextCompletion *cmpl,
GtkTextIter *start,
GtkTextIter *end,
const char *text);
}; };
typedef char *(*MooTextCompletionTextFunc) (GtkTreeModel *model, typedef char *(*MooTextCompletionTextFunc) (GtkTreeModel *model,
@ -89,8 +94,17 @@ void moo_text_completion_set_text_func (MooTextCompletion *cmpl,
gpointer data, gpointer data,
GDestroyNotify notify); GDestroyNotify notify);
char *moo_text_completion_get_text (MooTextCompletion *cmpl,
GtkTreeModel *model,
GtkTreeIter *iter);
MooTextPopup *moo_text_completion_get_popup (MooTextCompletion *cmpl); MooTextPopup *moo_text_completion_get_popup (MooTextCompletion *cmpl);
void _moo_text_completion_replace_text (MooTextCompletion *cmpl,
GtkTextIter *start,
GtkTextIter *end,
const char *text);
G_END_DECLS G_END_DECLS