Kaydet (Commit) 6010e5bf authored tarafından Nick Treleaven's avatar Nick Treleaven

Cache the current function name for efficiency in utils_get_current_function, other related fixes

git-svn-id: https://geany.svn.sourceforge.net/svnroot/geany/trunk@451 ea778897-0a13-0410-b9d1-a72fbfd435f5
üst 6f864d88
...@@ -11,6 +11,9 @@ ...@@ -11,6 +11,9 @@
* src/notebook.c, src/notebook.h, src/main.c, src/Makefile.am: * src/notebook.c, src/notebook.h, src/main.c, src/Makefile.am:
Added currently disabled drag reordering of notebook tabs. Added currently disabled drag reordering of notebook tabs.
* src/callbacks.c: Execute: only save file if the run command uses it. * src/callbacks.c: Execute: only save file if the run command uses it.
* src/templates.c, src/templates.h, src/utils.c, src/utils.h,
src/callbacks.c: Cache the current function name for efficiency in
utils_get_current_function, other related fixes.
2006-06-15 Enrico Troeger <enrico.troeger@uvena.de> 2006-06-15 Enrico Troeger <enrico.troeger@uvena.de>
......
...@@ -2008,7 +2008,7 @@ on_comments_function_activate (GtkMenuItem *menuitem, ...@@ -2008,7 +2008,7 @@ on_comments_function_activate (GtkMenuItem *menuitem,
{ {
gint idx = document_get_cur_idx(); gint idx = document_get_cur_idx();
gchar *text; gchar *text;
gchar *cur_tag = NULL; const gchar *cur_tag = NULL;
gint line = -1, pos = 0; gint line = -1, pos = 0;
if (doc_list[idx].file_type == NULL) if (doc_list[idx].file_type == NULL)
...@@ -2017,14 +2017,10 @@ on_comments_function_activate (GtkMenuItem *menuitem, ...@@ -2017,14 +2017,10 @@ on_comments_function_activate (GtkMenuItem *menuitem,
return; return;
} }
if (doc_list[idx].file_type->id != GEANY_FILETYPES_JAVA && // utils_get_current_function returns -1 on failure, so sci_get_position_from_line
doc_list[idx].file_type->id != GEANY_FILETYPES_ALL) // returns the current position, so it should be safe
{ line = utils_get_current_function(idx, &cur_tag);
line = utils_get_current_tag(idx, &cur_tag); pos = sci_get_position_from_line(doc_list[idx].sci, line - 1);
// utils_get_current_tag returns -1 on failure, so sci_get_position_from_line
// returns the current position, so it should be safe
pos = sci_get_position_from_line(doc_list[idx].sci, line - 1);
}
switch (doc_list[idx].file_type->id) switch (doc_list[idx].file_type->id)
{ {
...@@ -2049,7 +2045,6 @@ on_comments_function_activate (GtkMenuItem *menuitem, ...@@ -2049,7 +2045,6 @@ on_comments_function_activate (GtkMenuItem *menuitem,
} }
sci_insert_text(doc_list[idx].sci, pos, text); sci_insert_text(doc_list[idx].sci, pos, text);
g_free(cur_tag);
g_free(text); g_free(text);
} }
......
...@@ -229,7 +229,7 @@ gchar *templates_get_template_generic(gint template) ...@@ -229,7 +229,7 @@ gchar *templates_get_template_generic(gint template)
} }
gchar *templates_get_template_function(gint template, gchar *func_name) gchar *templates_get_template_function(gint template, const gchar *func_name)
{ {
gchar *result = g_strdup(templates[template]); gchar *result = g_strdup(templates[template]);
gchar *date = utils_get_date(); gchar *date = utils_get_date();
......
...@@ -32,7 +32,7 @@ gchar *templates_get_template_changelog(void); ...@@ -32,7 +32,7 @@ gchar *templates_get_template_changelog(void);
gchar *templates_get_template_generic(gint template); gchar *templates_get_template_generic(gint template);
gchar *templates_get_template_function(gint template, gchar *func_name); gchar *templates_get_template_function(gint template, const gchar *func_name);
gchar *templates_get_template_gpl(gint template); gchar *templates_get_template_gpl(gint template);
......
...@@ -94,23 +94,14 @@ void utils_update_statusbar(gint idx, gint pos) ...@@ -94,23 +94,14 @@ void utils_update_statusbar(gint idx, gint pos)
{ {
// currently text need in German and C locale about 150 chars // currently text need in German and C locale about 150 chars
gchar *text = (gchar*) g_malloc0(250); gchar *text = (gchar*) g_malloc0(250);
gchar *cur_tag; const gchar *cur_tag;
guint line, col; guint line, col;
if (idx == -1) idx = document_get_cur_idx(); if (idx == -1) idx = document_get_cur_idx();
if (idx >= 0 && doc_list[idx].is_valid) if (idx >= 0 && doc_list[idx].is_valid)
{ {
if (doc_list[idx].file_type == NULL || utils_get_current_function(idx, &cur_tag);
doc_list[idx].file_type->id == GEANY_FILETYPES_JAVA ||
doc_list[idx].file_type->id == GEANY_FILETYPES_ALL)
{
cur_tag = g_strdup(_("unknown"));
}
else
{
utils_get_current_tag(idx, &cur_tag);
}
if (pos == -1) pos = sci_get_current_position(doc_list[idx].sci); if (pos == -1) pos = sci_get_current_position(doc_list[idx].sci);
line = sci_get_line_from_position(doc_list[idx].sci, pos); line = sci_get_line_from_position(doc_list[idx].sci, pos);
...@@ -129,7 +120,6 @@ _("%c line: % 4d column: % 3d selection: % 4d %s mode: %s%s cur. f ...@@ -129,7 +120,6 @@ _("%c line: % 4d column: % 3d selection: % 4d %s mode: %s%s cur. f
(doc_list[idx].file_type) ? doc_list[idx].file_type->title : _("unknown")); (doc_list[idx].file_type) ? doc_list[idx].file_type->title : _("unknown"));
gtk_statusbar_pop(GTK_STATUSBAR(app->statusbar), 1); gtk_statusbar_pop(GTK_STATUSBAR(app->statusbar), 1);
gtk_statusbar_push(GTK_STATUSBAR(app->statusbar), 1, text); gtk_statusbar_push(GTK_STATUSBAR(app->statusbar), 1, text);
g_free(cur_tag);
} }
else else
{ {
...@@ -966,69 +956,92 @@ void utils_check_disk_status(gint idx) ...@@ -966,69 +956,92 @@ void utils_check_disk_status(gint idx)
} }
gint utils_get_current_tag(gint idx, gchar **tagname) gint utils_get_current_function(gint idx, const gchar **tagname)
{ {
gint tag_line; static gint tag_line = -1;
gint pos;
gint line; gint line;
static gint old_line = -1;
static gint old_idx = -1;
static gchar *cur_tag = NULL;
gint fold_level; gint fold_level;
gint start, end, last_pos; gint start, end, last_pos;
gint tmp; gint tmp;
const GList *tags; const GList *tags;
pos = sci_get_current_position(doc_list[idx].sci); line = sci_get_current_line(doc_list[idx].sci, -1);
line = sci_get_line_from_position(doc_list[idx].sci, pos); // check if the cached line and file index have changed since last time:
if (line == old_line && idx == old_idx)
{
// we can assume same current function as before
*tagname = cur_tag;
return tag_line;
}
g_free(cur_tag); // free the old tag, it will be replaced.
//record current line and file index for next time
old_line = line;
old_idx = idx;
fold_level = sci_get_fold_level(doc_list[idx].sci, line); // look first in the tag list
if ((fold_level & 0xFF) != 0) tags = utils_get_tag_list(idx, tm_tag_max_t);
for (; tags; tags = g_list_next(tags))
{ {
while((fold_level & SC_FOLDLEVELNUMBERMASK) != SC_FOLDLEVELBASE && line >= 0) tag_line = ((GeanySymbol*)tags->data)->line;
if (line + 1 == tag_line)
{ {
fold_level = sci_get_fold_level(doc_list[idx].sci, --line); cur_tag = g_strdup(strtok(((GeanySymbol*)tags->data)->str, " "));
*tagname = cur_tag;
return tag_line;
} }
}
// look first in the tag list if (doc_list[idx].file_type != NULL &&
tags = utils_get_tag_list(idx, tm_tag_max_t); doc_list[idx].file_type->id != GEANY_FILETYPES_JAVA &&
for (; tags; tags = g_list_next(tags)) doc_list[idx].file_type->id != GEANY_FILETYPES_ALL)
{
fold_level = sci_get_fold_level(doc_list[idx].sci, line);
if ((fold_level & 0xFF) != 0)
{ {
tag_line = ((GeanySymbol*)tags->data)->line; tag_line = line;
if (line == tag_line) while((fold_level & SC_FOLDLEVELNUMBERMASK) != SC_FOLDLEVELBASE && tag_line >= 0)
{ {
*tagname = g_strdup(strtok(((GeanySymbol*)tags->data)->str, " ")); fold_level = sci_get_fold_level(doc_list[idx].sci, --tag_line);
return tag_line;
} }
start = sci_get_position_from_line(doc_list[idx].sci, tag_line - 2);
last_pos = sci_get_length(doc_list[idx].sci);
tmp = 0;
while (sci_get_style_at(doc_list[idx].sci, start) != SCE_C_IDENTIFIER
&& sci_get_style_at(doc_list[idx].sci, start) != SCE_C_GLOBALCLASS
&& start < last_pos) start++;
end = start;
// Use tmp to find SCE_C_IDENTIFIER or SCE_C_GLOBALCLASS chars
// this fails on C++ code like 'Vek3 Vek3::mul(double s)' this code returns
// "Vek3" because the return type of the prototype is also a GLOBALCLASS,
// no idea how to fix at the moment
// fails also in C with code like
// typedef void viod;
// viod do_nothing() {} -> return viod instead of do_nothing
// perhaps: get the first colon, read forward the second colon and then method
// name, then go from the first colon backwards and read class name until space
while (((tmp = sci_get_style_at(doc_list[idx].sci, end)) == SCE_C_IDENTIFIER
|| tmp == SCE_C_GLOBALCLASS
|| sci_get_char_at(doc_list[idx].sci, end) == '~'
|| sci_get_char_at(doc_list[idx].sci, end) == ':')
&& end < last_pos) end++;
cur_tag = g_malloc(end - start + 1);
sci_get_text_range(doc_list[idx].sci, start, end, cur_tag);
*tagname = cur_tag;
return tag_line;
} }
}
start = sci_get_position_from_line(doc_list[idx].sci, line - 2); cur_tag = g_strdup(_("unknown"));
last_pos = sci_get_length(doc_list[idx].sci); *tagname = cur_tag;
tmp = 0; tag_line = -1;
while (sci_get_style_at(doc_list[idx].sci, start) != SCE_C_IDENTIFIER return tag_line;
&& sci_get_style_at(doc_list[idx].sci, start) != SCE_C_GLOBALCLASS
&& start < last_pos) start++;
end = start;
// Use tmp to find SCE_C_IDENTIFIER or SCE_C_GLOBALCLASS chars
// this fails on C++ code like 'Vek3 Vek3::mul(double s)' this code returns
// "Vek3" because the return type of the prototype is also a GLOBALCLASS,
// no idea how to fix at the moment
// fails also in C with code like
// typedef void viod;
// viod do_nothing() {} -> return viod instead of do_nothing
// perhaps: get the first colon, read forward the second colon and then method
// name, then go from the first colon backwards and read class name until space
while (((tmp = sci_get_style_at(doc_list[idx].sci, end)) == SCE_C_IDENTIFIER
|| tmp == SCE_C_GLOBALCLASS
|| sci_get_char_at(doc_list[idx].sci, end) == '~'
|| sci_get_char_at(doc_list[idx].sci, end) == ':')
&& end < last_pos) end++;
*tagname = g_malloc(end - start + 1);
sci_get_text_range(doc_list[idx].sci, start, end, *tagname);
return line;
}
*tagname = g_strdup(_("unknown"));
return -1;
} }
......
...@@ -99,7 +99,7 @@ gchar *utils_find_open_xml_tag(const gchar sel[], gint size, gboolean check_tag) ...@@ -99,7 +99,7 @@ gchar *utils_find_open_xml_tag(const gchar sel[], gint size, gboolean check_tag)
void utils_check_disk_status(gint idx); void utils_check_disk_status(gint idx);
//gchar *utils_get_current_tag(gint idx, gint direction); //gchar *utils_get_current_tag(gint idx, gint direction);
gint utils_get_current_tag(gint idx, gchar **tagname); gint utils_get_current_function(gint idx, const gchar **tagname);
void utils_find_current_word(ScintillaObject *sci, gint pos, gchar *word, size_t wordlen); void utils_find_current_word(ScintillaObject *sci, gint pos, gchar *word, size_t wordlen);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment