Simplify updating Scintilla keywords on reload config and tab switch.

Remove type keywords caching function since it doesn't really speed up
anything and we don't care if the keywords change since they can/should
still be reloaded.  This also prevents "leaking" a static GString once
when the application closes and saves a call to g_string_free() when the
type keywords have changed.

Rename document_update_highlighting() to document_update_type_keywords()
since no re-highlighting is needed when updating Scintilla keywords.
Scintilla highlights the new keywords automatically so this saves a call
to queue_colourise().

Remove update_type_keywords() function since this is now all handled
in the document_update_type_keywords() function.  This function had a
comment about updating all documents when sci is NULL but it was never
used in this way since it was only called on document_load_config() which
always operates on a single document and the sci should not be NULL.
This commit is contained in:
Matthew Brush 2011-10-19 21:39:17 -07:00
parent 6c8e80271e
commit b1b88286cf
3 changed files with 30 additions and 113 deletions

View File

@ -632,7 +632,7 @@ void on_notebook1_switch_page_after(GtkNotebook *notebook, GtkNotebookPage *page
ui_document_show_hide(doc); /* update the document menu */
build_menu_update(doc);
sidebar_update_tag_list(doc, FALSE);
document_update_highlighting(doc);
document_update_type_keywords(doc);
/* We delay the check to avoid weird fast, unintended switching of notebook pages when
* the 'file has changed' dialog is shown while the switch event is not yet completely

View File

@ -2348,59 +2348,24 @@ void document_update_tag_list_in_idle(GeanyDocument *doc)
}
/* Caches the list of project typenames, as a space separated GString.
* Returns: TRUE if typenames have changed.
* (*types) is set to the list of typenames, or NULL if there are none. */
static gboolean get_project_typenames(const GString **types, gint lang)
/*
* Updates the type keywords in the document's Scintilla widget.
*
* @param doc The document
*/
void document_update_type_keywords(GeanyDocument *doc)
{
static GString *last_typenames = NULL;
GString *s = NULL;
gint keyword_idx;
gchar *keywords = NULL;
GString *str;
GPtrArray *tags_array;
if (app->tm_workspace)
{
GPtrArray *tags_array = app->tm_workspace->work_object.tags_array;
if (tags_array)
{
s = symbols_find_tags_as_string(tags_array, TM_GLOBAL_TYPE_MASK, lang);
}
}
if (s && last_typenames && g_string_equal(s, last_typenames))
{
g_string_free(s, TRUE);
*types = last_typenames;
return FALSE; /* project typenames haven't changed */
}
/* cache typename list for next time */
if (last_typenames)
g_string_free(last_typenames, TRUE);
last_typenames = s;
*types = s;
if (s == NULL)
return FALSE;
return TRUE;
}
/* If sci is NULL, update project typenames for all documents that support typenames,
* if typenames have changed.
* If sci is not NULL, then if sci supports typenames, project typenames are updated
* if necessary, and typename keywords are set for sci.
* Returns: TRUE if any scintilla type keywords were updated. */
static gboolean update_type_keywords(GeanyDocument *doc, gint lang)
{
gboolean ret = FALSE;
guint n;
const GString *s;
ScintillaObject *sci;
g_return_val_if_fail(doc != NULL, FALSE);
sci = doc->editor->sci;
g_return_if_fail(DOC_VALID(doc));
g_return_if_fail(IS_SCINTILLA(doc->editor->sci));
switch (doc->file_type->id)
{ /* continue working with the following languages, skip on all others */
{
/* continue working with the following languages, skip on all others */
case GEANY_FILETYPES_C:
case GEANY_FILETYPES_CPP:
case GEANY_FILETYPES_CS:
@ -2409,73 +2374,25 @@ static gboolean update_type_keywords(GeanyDocument *doc, gint lang)
case GEANY_FILETYPES_VALA:
break;
default:
return FALSE;
return;
}
sci = doc->editor->sci;
if (sci != NULL && editor_lexer_get_type_keyword_idx(sci_get_lexer(sci)) == -1)
return FALSE;
keyword_idx = editor_lexer_get_type_keyword_idx(sci_get_lexer(doc->editor->sci));
if (G_UNLIKELY(keyword_idx < 1))
return;
if (! get_project_typenames(&s, lang))
{ /* typenames have not changed */
if (s != NULL && sci != NULL)
{
gint keyword_idx = editor_lexer_get_type_keyword_idx(sci_get_lexer(sci));
if (G_UNLIKELY(app->tm_workspace == NULL))
return;
sci_set_keywords(sci, keyword_idx, s->str);
queue_colourise(doc);
}
return FALSE;
}
g_return_val_if_fail(s != NULL, FALSE);
for (n = 0; n < documents_array->len; n++)
tags_array = app->tm_workspace->work_object.tags_array;
if (tags_array)
{
if (documents[n]->is_valid)
str = symbols_find_tags_as_string(tags_array, TM_GLOBAL_TYPE_MASK, doc->file_type->lang);
if (str)
{
ScintillaObject *wid = documents[n]->editor->sci;
gint keyword_idx = editor_lexer_get_type_keyword_idx(sci_get_lexer(wid));
if (keyword_idx > 0)
{
sci_set_keywords(wid, keyword_idx, s->str);
queue_colourise(documents[n]);
ret = TRUE;
}
}
}
return ret;
}
/*
* Updates the keywords in a document and re-colourizes it.
*
* @param doc The document
*/
void document_update_highlighting(GeanyDocument *doc)
{
const GString *s = NULL;
ScintillaObject *sci;
gint keyword_idx;
g_return_if_fail(DOC_VALID(doc));
g_return_if_fail(IS_SCINTILLA(doc->editor->sci));
sci = doc->editor->sci;
/* get possibly cached list of keywords for the current language.
* we don't care whether the cached keywords have changed. */
get_project_typenames(&s, doc->file_type->lang);
if (s)
{
keyword_idx = editor_lexer_get_type_keyword_idx(sci_get_lexer(sci));
if (keyword_idx > 0)
{
sci_set_keywords(sci, keyword_idx, s->str);
queue_colourise(doc);
keywords = g_string_free(str, FALSE);
sci_set_keywords(doc->editor->sci, keyword_idx, keywords);
g_free(keywords);
}
}
}
@ -2512,7 +2429,7 @@ static void document_load_config(GeanyDocument *doc, GeanyFiletype *type,
document_update_tag_list(doc, TRUE);
/* Update session typename keywords. */
update_type_keywords(doc, type->lang);
document_update_type_keywords(doc);
}

View File

@ -235,7 +235,7 @@ void document_update_tag_list(GeanyDocument *doc, gboolean update);
void document_update_tag_list_in_idle(GeanyDocument *doc);
void document_update_highlighting(GeanyDocument *doc);
void document_update_type_keywords(GeanyDocument *doc);
void document_set_encoding(GeanyDocument *doc, const gchar *new_encoding);