Skip to content

Commit

Permalink
Add an API-like ctags layer
Browse files Browse the repository at this point in the history
At the moment it is a bit hard to distinguish which of the functions
we are using belong to ctags. To make more explicit what we need
from ctags, wrap all ctags-related code and access ctags only using
this layer.

The interface from tm_ctags_wrappers.h can serve as a base for
proper ctags interface if ctags becomes a library.
  • Loading branch information
techee committed Mar 21, 2016
1 parent 103d2c5 commit 1c4aaa0
Show file tree
Hide file tree
Showing 4 changed files with 193 additions and 61 deletions.
4 changes: 3 additions & 1 deletion tagmanager/src/Makefile.am
Expand Up @@ -28,4 +28,6 @@ libtagmanager_la_SOURCES =\
tm_tag.h \
tm_tag.c \
tm_workspace.h \
tm_workspace.c
tm_workspace.c \
tm_ctags_wrappers.h \
tm_ctags_wrappers.c
119 changes: 119 additions & 0 deletions tagmanager/src/tm_ctags_wrappers.c
@@ -0,0 +1,119 @@
/*
* tm_ctags_wrappers.c - this file is part of Geany, a fast and lightweight IDE
*
* Copyright 2016 Jiri Techet <techet(at)gmail(dot)com>
*
* 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 later version.
*
* 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.
*/

#include "tm_ctags_wrappers.h"

#include "general.h"
#include "entry.h"
#include "parse.h"
#include "read.h"


typedef struct {
tm_ctags_callback callback;
gpointer user_data;
} CallbackUserData;


void tm_ctags_init()
{
initializeParsing();
installLanguageMapDefaults();
}


static gboolean parse_callback(const tagEntryInfo *tag, gpointer user_data)
{
CallbackUserData *callback_data = user_data;

return callback_data->callback(tag, FALSE, callback_data->user_data);
}


void tm_ctags_parse(guchar *buffer, gsize buffer_size,
const gchar *file_name, TMParserType lang, tm_ctags_callback callback,
gpointer user_data)
{
CallbackUserData callback_data = {callback, user_data};
gboolean retry = TRUE;
guint passCount = 0;

g_return_if_fail(buffer || file_name);

if (! LanguageTable [lang]->enabled)
{
#ifdef TM_DEBUG
g_warning("ignoring %s (language disabled)\n", file_name);
#endif
return;
}

setTagEntryFunction(parse_callback, &callback_data);
while (retry && passCount < 3)
{
callback(NULL, TRUE, user_data);
if (!buffer && fileOpen (file_name, lang))
{
if (LanguageTable [lang]->parser != NULL)
{
LanguageTable [lang]->parser ();
fileClose ();
retry = FALSE;
break;
}
else if (LanguageTable [lang]->parser2 != NULL)
retry = LanguageTable [lang]->parser2 (passCount);
fileClose ();
}
else if (buffer && bufferOpen (buffer, buffer_size, file_name, lang))
{
if (LanguageTable [lang]->parser != NULL)
{
LanguageTable [lang]->parser ();
bufferClose ();
retry = FALSE;
break;
}
else if (LanguageTable [lang]->parser2 != NULL)
retry = LanguageTable [lang]->parser2 (passCount);
bufferClose ();
}
else
{
g_warning("Unable to open %s", file_name);
return;
}
++ passCount;
}
}


const gchar *tm_ctags_get_lang_name(TMParserType lang)
{
return getLanguageName(lang);
}


TMParserType tm_ctags_get_named_lang(const gchar *name)
{
return getNamedLanguage(name);
}


49 changes: 49 additions & 0 deletions tagmanager/src/tm_ctags_wrappers.h
@@ -0,0 +1,49 @@
/*
* tm_ctags_wrappers.h - this file is part of Geany, a fast and lightweight IDE
*
* Copyright 2016 Jiri Techet <techet(at)gmail(dot)com>
*
* 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 later version.
*
* 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.
*/

#ifndef TM_CTAGS_WRAPPERS
#define TM_CTAGS_WRAPPERS

#include <glib.h>

#include "tm_parser.h"

#include "entry.h" /* for sTagEntryInfo */


G_BEGIN_DECLS

typedef gboolean (*tm_ctags_callback) (const tagEntryInfo *const tag,
gboolean invalidate, void *user_data);


void tm_ctags_init();

void tm_ctags_parse(guchar *buffer, gsize buffer_size,
const gchar *file_name, TMParserType lang, tm_ctags_callback callback,
gpointer user_data);

const gchar *tm_ctags_get_lang_name(TMParserType lang);

TMParserType tm_ctags_get_named_lang(const gchar *name);

G_END_DECLS

#endif /* TM_CTAGS_WRAPPERS */
82 changes: 22 additions & 60 deletions tagmanager/src/tm_source_file.c
Expand Up @@ -18,6 +18,7 @@
#include <limits.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <sys/stat.h>
#include <glib/gstdio.h>
#ifdef G_OS_WIN32
Expand All @@ -26,14 +27,10 @@
# include <windows.h> /* for GetFullPathName */
#endif

#include "general.h"
#include "entry.h"
#include "parse.h"
#include "read.h"

#include "tm_source_file.h"
#include "tm_tag.h"
#include "tm_parser.h"
#include "tm_ctags_wrappers.h"

typedef struct
{
Expand Down Expand Up @@ -725,10 +722,19 @@ static void update_python_arglist(const TMTag *tag, TMSourceFile *current_source
it finds a new tag. You should not have to use this function.
@see tm_source_file_parse()
*/
static int tm_source_file_tags(const tagEntryInfo *tag, void *user_data)
static gboolean tm_source_file_tags(const tagEntryInfo *const tag,
gboolean invalidate, void *user_data)
{
TMSourceFile *current_source_file = user_data;
TMTag *tm_tag = tm_tag_new();
TMTag *tm_tag;

if (invalidate)
{
tm_tags_array_free(current_source_file->tags_array, FALSE);
return TRUE;
}

tm_tag = tm_tag_new();

init_tag(tm_tag, current_source_file, tag);

Expand All @@ -742,8 +748,7 @@ static int tm_source_file_tags(const tagEntryInfo *tag, void *user_data)

void tm_source_file_ctags_init()
{
initializeParsing();
installLanguageMapDefaults();
tm_ctags_init();
}

/* Initializes a TMSourceFile structure from a file name. */
Expand Down Expand Up @@ -783,7 +788,7 @@ static gboolean tm_source_file_init(TMSourceFile *source_file, const char *file_
if (name == NULL)
source_file->lang = TM_PARSER_NONE;
else
source_file->lang = getNamedLanguage(name);
source_file->lang = tm_ctags_get_named_lang(name);

return TRUE;
}
Expand Down Expand Up @@ -920,54 +925,11 @@ gboolean tm_source_file_parse(TMSourceFile *source_file, guchar* text_buf, gsize
return TRUE;
}

if (! LanguageTable [source_file->lang]->enabled)
{
#ifdef TM_DEBUG
g_warning("ignoring %s (language disabled)\n", file_name);
#endif
}
else
{
setTagEntryFunction(tm_source_file_tags, source_file);
guint passCount = 0;
while (retry && passCount < 3)
{
tm_tags_array_free(source_file->tags_array, FALSE);
if (parse_file && fileOpen (file_name, source_file->lang))
{
if (LanguageTable [source_file->lang]->parser != NULL)
{
LanguageTable [source_file->lang]->parser ();
fileClose ();
retry = FALSE;
break;
}
else if (LanguageTable [source_file->lang]->parser2 != NULL)
retry = LanguageTable [source_file->lang]->parser2 (passCount);
fileClose ();
}
else if (!parse_file && bufferOpen (text_buf, buf_size, file_name, source_file->lang))
{
if (LanguageTable [source_file->lang]->parser != NULL)
{
LanguageTable [source_file->lang]->parser ();
bufferClose ();
retry = FALSE;
break;
}
else if (LanguageTable [source_file->lang]->parser2 != NULL)
retry = LanguageTable [source_file->lang]->parser2 (passCount);
bufferClose ();
}
else
{
g_warning("Unable to open %s", file_name);
return FALSE;
}
++ passCount;
}
}

tm_tags_array_free(source_file->tags_array, FALSE);

tm_ctags_parse(parse_file ? NULL : text_buf, buf_size, file_name,
source_file->lang, tm_source_file_tags, source_file);

if (free_buf)
g_free(text_buf);
return !retry;
Expand All @@ -979,7 +941,7 @@ gboolean tm_source_file_parse(TMSourceFile *source_file, guchar* text_buf, gsize
*/
const gchar *tm_source_file_get_lang_name(TMParserType lang)
{
return getLanguageName(lang);
return tm_ctags_get_lang_name(lang);
}

/* Gets the language index for \a name.
Expand All @@ -988,5 +950,5 @@ const gchar *tm_source_file_get_lang_name(TMParserType lang)
*/
TMParserType tm_source_file_get_named_lang(const gchar *name)
{
return getNamedLanguage(name);
return tm_ctags_get_named_lang(name);
}

0 comments on commit 1c4aaa0

Please sign in to comment.