[geany/geany] 1c4aaa: Add an API-like ctags layer

Jiří Techet git-noreply at xxxxx
Fri Jun 10 21:57:25 UTC 2016


Branch:      refs/heads/master
Author:      Jiří Techet <techet at gmail.com>
Committer:   Jiří Techet <techet at gmail.com>
Date:        Mon, 21 Mar 2016 17:28:36 UTC
Commit:      1c4aaa0eb72aa9e3c60cd604d4c20ca5f7dc5c8f
             https://github.com/geany/geany/commit/1c4aaa0eb72aa9e3c60cd604d4c20ca5f7dc5c8f

Log Message:
-----------
Add an API-like ctags layer

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.


Modified Paths:
--------------
    tagmanager/src/Makefile.am
    tagmanager/src/tm_ctags_wrappers.c
    tagmanager/src/tm_ctags_wrappers.h
    tagmanager/src/tm_source_file.c

Modified: tagmanager/src/Makefile.am
4 lines changed, 3 insertions(+), 1 deletions(-)
===================================================================
@@ -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


Modified: tagmanager/src/tm_ctags_wrappers.c
119 lines changed, 119 insertions(+), 0 deletions(-)
===================================================================
@@ -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);
+}
+
+


Modified: tagmanager/src/tm_ctags_wrappers.h
49 lines changed, 49 insertions(+), 0 deletions(-)
===================================================================
@@ -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 */


Modified: tagmanager/src/tm_source_file.c
82 lines changed, 22 insertions(+), 60 deletions(-)
===================================================================
@@ -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
@@ -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
 {
@@ -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);
 
@@ -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. */
@@ -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;
 }
@@ -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;
@@ -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.
@@ -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);
 }



--------------
This E-Mail was brought to you by github_commit_mail.py (Source: https://github.com/geany/infrastructure).


More information about the Commits mailing list