[anjuta] libanjuta: Rename a few anjuta_token functions, Get token position



commit 13c265e652794969fb52ca77bff50d400d9b206b
Author: Sébastien Granjoux <seb sfo free fr>
Date:   Sun Feb 13 17:06:36 2011 +0100

    libanjuta: Rename a few anjuta_token functions, Get token position

 libanjuta/anjuta-token-file.c   |   58 +++++++++++++++++++++++++++-
 libanjuta/anjuta-token-file.h   |    1 +
 libanjuta/anjuta-token-stream.c |    2 +-
 libanjuta/anjuta-token.c        |   78 +++++++++++++++++++++++----------------
 libanjuta/anjuta-token.h        |   10 +++--
 5 files changed, 109 insertions(+), 40 deletions(-)
---
diff --git a/libanjuta/anjuta-token-file.c b/libanjuta/anjuta-token-file.c
index cbc5053..3b3a1f6 100644
--- a/libanjuta/anjuta-token-file.c
+++ b/libanjuta/anjuta-token-file.c
@@ -117,7 +117,7 @@ anjuta_token_file_load (AnjutaTokenFile *file, GError **error)
 	{
 		AnjutaToken *token;
 			
-		token =	anjuta_token_new_with_string (ANJUTA_TOKEN_FILE, content, length);
+		token =	anjuta_token_new_string_len (ANJUTA_TOKEN_FILE, content, length);
 		anjuta_token_prepend_child (file->save, token);
 		
 		token =	anjuta_token_new_static (ANJUTA_TOKEN_FILE, content);
@@ -332,7 +332,7 @@ anjuta_token_file_update (AnjutaTokenFile *file, AnjutaToken *token)
 		AnjutaToken *start = NULL;
 		
 		value = g_new (gchar, added);
-		add = anjuta_token_prepend_child (file->save, anjuta_token_new_with_string (ANJUTA_TOKEN_NAME, value, added));
+		add = anjuta_token_prepend_child (file->save, anjuta_token_new_string_len (ANJUTA_TOKEN_NAME, value, added));
 		
 		/* Find token position */
 		if (prev != NULL)
@@ -342,7 +342,7 @@ anjuta_token_file_update (AnjutaTokenFile *file, AnjutaToken *token)
 		}
 
 		/* Insert token */
-		add = anjuta_token_new_fragment (ANJUTA_TOKEN_NAME, value, added);
+		add = anjuta_token_new_static_len (ANJUTA_TOKEN_NAME, value, added);
 		if (start == NULL)
 		{
 			anjuta_token_prepend_child (file->content, add);
@@ -376,6 +376,58 @@ anjuta_token_file_update (AnjutaTokenFile *file, AnjutaToken *token)
 	return TRUE;
 }
 
+/**
+ * anjuta_token_file_get_token_position:
+ * @file: #AnjutaTokenFile object
+ * @token: token
+ *
+ * Returns the position of the token in the file. This position is a number
+ * which doesn't correspond to a line number or a character but respect the
+ * order of token in the file.
+ *
+ * Returns: The position of the token or 0 if the token is not in the file.
+ */
+gsize
+anjuta_token_file_get_token_position (AnjutaTokenFile *file, AnjutaToken *token)
+{
+	AnjutaToken *content;
+	const gchar *string;
+	gsize pos = 1;
+	
+
+	do
+	{
+		string = anjuta_token_get_string (token); 
+		if (string != NULL) break;
+
+		/* token is a group or an empty token, looks for group members or
+		 * following token */
+		token = anjuta_token_next_after_children (token);
+	} while (token != NULL);
+
+	for (content = file->content; content != NULL; content = anjuta_token_next (content))
+	{
+		const gchar *start;
+		gsize len;
+
+		start = anjuta_token_get_string (content); 
+		len = anjuta_token_get_length (content);
+
+		if ((string >= start) && ((string - start) < len))
+		{
+			pos += string - start;
+			break;
+		}
+		else
+		{
+			pos += len;
+		}
+	}
+
+	return content == NULL ? 0 : pos;
+}
+
+
 gboolean
 anjuta_token_file_get_token_location (AnjutaTokenFile *file, AnjutaTokenFileLocation *location, AnjutaToken *token)
 {
diff --git a/libanjuta/anjuta-token-file.h b/libanjuta/anjuta-token-file.h
index 63a0cbf..0af87ec 100644
--- a/libanjuta/anjuta-token-file.h
+++ b/libanjuta/anjuta-token-file.h
@@ -57,6 +57,7 @@ void anjuta_token_file_move (AnjutaTokenFile *file, GFile *new_file);
 gboolean anjuta_token_file_update (AnjutaTokenFile *file, AnjutaToken *token);
 
 gboolean anjuta_token_file_get_token_location (AnjutaTokenFile *file, AnjutaTokenFileLocation *location, AnjutaToken *token);
+gsize anjuta_token_file_get_token_position (AnjutaTokenFile *file, AnjutaToken *token);
 GFile *anjuta_token_file_get_file (AnjutaTokenFile *file);
 AnjutaToken *anjuta_token_file_get_content (AnjutaTokenFile *file);
 gboolean anjuta_token_file_is_dirty (AnjutaTokenFile *file);
diff --git a/libanjuta/anjuta-token-stream.c b/libanjuta/anjuta-token-stream.c
index 63b551c..3bef95e 100644
--- a/libanjuta/anjuta-token-stream.c
+++ b/libanjuta/anjuta-token-stream.c
@@ -121,7 +121,7 @@ anjuta_token_stream_tokenize (AnjutaTokenStream *stream, gint type, gsize length
     AnjutaToken *frag;
     AnjutaToken *end;
 
-    frag = anjuta_token_new_fragment (type, NULL, 0);
+    frag = anjuta_token_new_static (type, NULL);
 
     for (end = stream->start; end != NULL;)
     {
diff --git a/libanjuta/anjuta-token.c b/libanjuta/anjuta-token.c
index 86c7cef..3185149 100644
--- a/libanjuta/anjuta-token.c
+++ b/libanjuta/anjuta-token.c
@@ -110,7 +110,6 @@ typedef struct _AnjutaTokenData AnjutaTokenData;
 
 struct _AnjutaTokenData
 {
-	AnjutaTokenData *data;
 	AnjutaTokenType type;	
 	gint flags;
 	gchar *pos;
@@ -317,7 +316,6 @@ anjuta_token_show (AnjutaToken *token, gint indent, gchar parent)
 	static gchar type[] = "\0";
 	const gchar *string;
 	gsize length;
-	const gchar *newline;
 	
 	type[0] = parent;
 	fprintf (stderr, "%*s%s %p", indent, "", type, token);
@@ -325,44 +323,54 @@ anjuta_token_show (AnjutaToken *token, gint indent, gchar parent)
 	    anjuta_token_get_type (token));
 	string = anjuta_token_get_string (token);
 	length = anjuta_token_get_length (token);
-	newline = string == NULL ? NULL : g_strrstr_len (string, length, "\n");
-	if (newline == NULL)
+	if (string == NULL)
 	{
 		/* Value doesn't contain a newline */	
-		fprintf (stderr, "\"%.*s\"",
-	    	length,
-	    	string);
+		fprintf (stderr, "(%d)", length);
 	}
 	else
 	{
-		/* Value contains a newline, take care of indentation */
-		newline++;
-		fprintf (stderr, "\"%.*s",
-	    	newline - string,
-	    	string);
-		for (;;)
+		const gchar *newline;
+		
+		newline = g_strrstr_len (string, length, "\n");
+		if (newline == NULL)
 		{
-			length -= newline - string;
-			string = newline;
+			/* Value doesn't contain a newline */	
+			fprintf (stderr, "\"%.*s\"",
+				length,
+		    	string);
+		}
+		else
+		{
+			/* Value contains a newline, take care of indentation */
+			newline++;
+			fprintf (stderr, "\"%.*s",
+	    		newline - string,
+		    	string);
+			for (;;)
+			{
+				length -= newline - string;
+				string = newline;
 
-			newline = g_strrstr_len (string, length, "\n");
-			if (newline == NULL) break;
+				newline = g_strrstr_len (string, length, "\n");
+				if (newline == NULL) break;
 			
-			newline++;
-			fprintf (stderr, "%*s  %.*s",
-			    indent, "",
-			    newline - string,
-    			string);
+				newline++;
+				fprintf (stderr, "%*s  %.*s",
+				    indent, "",
+					newline - string,
+	    			string);
+			}
+			fprintf (stderr, "%*s  %.*s\"",
+				    indent, "",
+					length,
+	    			string);
 		}
-		fprintf (stderr, "%*s  %.*s\"",
-			    indent, "",
-			    length,
-    			string);
 	}
 	fprintf (stderr, " %p/%p (%p/%p) %s\n",
-	    token->last, token->children,
+		token->last, token->children,
 		token->group, token->parent,
-	    anjuta_token_get_flags (token) & ANJUTA_TOKEN_REMOVED ? " (removed)" : "");
+		anjuta_token_get_flags (token) & ANJUTA_TOKEN_REMOVED ? " (removed)" : "");
 }
 
 static AnjutaToken*
@@ -450,7 +458,7 @@ anjuta_token_get_flags (AnjutaToken *token)
 }
 
 void
-anjuta_token_set_string (AnjutaToken *token, const gchar *data, guint length)
+anjuta_token_set_string (AnjutaToken *token, const gchar *data, gsize length)
 {
 	if (!(token->data.flags & ANJUTA_TOKEN_STATIC))
 	{
@@ -467,6 +475,12 @@ anjuta_token_get_string (AnjutaToken *token)
 	return token->data.pos;
 }
 
+void
+anjuta_token_set_length (AnjutaToken *token, gsize length)
+{
+	token->data.length = length;
+}
+
 guint
 anjuta_token_get_length (AnjutaToken *token)
 {
@@ -1492,7 +1506,7 @@ AnjutaToken *anjuta_token_new_string (AnjutaTokenType type, const gchar *value)
 }
 
 AnjutaToken*
-anjuta_token_new_with_string (AnjutaTokenType type, gchar *value, gsize length)
+anjuta_token_new_string_len (AnjutaTokenType type, gchar *value, gsize length)
 {
 	AnjutaToken *token;
 	
@@ -1513,7 +1527,7 @@ anjuta_token_new_with_string (AnjutaTokenType type, gchar *value, gsize length)
 }
 
 AnjutaToken *
-anjuta_token_new_fragment (gint type, const gchar *pos, gsize length)
+anjuta_token_new_static_len (gint type, const gchar *pos, gsize length)
 {
 	AnjutaToken *token;
 
@@ -1528,7 +1542,7 @@ anjuta_token_new_fragment (gint type, const gchar *pos, gsize length)
 
 AnjutaToken *anjuta_token_new_static (AnjutaTokenType type, const char *value)
 {
-	return anjuta_token_new_fragment (type, value, value == NULL ? 0 : strlen (value));	
+	return anjuta_token_new_static_len (type, value, value == NULL ? 0 : strlen (value));	
 }
 
 AnjutaToken*
diff --git a/libanjuta/anjuta-token.h b/libanjuta/anjuta-token.h
index fa3009c..5c0399d 100644
--- a/libanjuta/anjuta-token.h
+++ b/libanjuta/anjuta-token.h
@@ -88,10 +88,11 @@ typedef struct _AnjutaToken AnjutaToken;
 
 typedef void (*AnjutaTokenForeachFunc) (AnjutaToken *token, gpointer data);
 
+
 AnjutaToken *anjuta_token_new_string (AnjutaTokenType type, const gchar *value);
-AnjutaToken *anjuta_token_new_with_string (AnjutaTokenType type, gchar *value, gsize length);
+AnjutaToken *anjuta_token_new_string_len (AnjutaTokenType type, gchar *value, gsize length);
 AnjutaToken *anjuta_token_new_static (AnjutaTokenType type, const gchar *value);
-AnjutaToken *anjuta_token_new_fragment (gint type, const gchar *pos, gsize length);
+AnjutaToken *anjuta_token_new_static_len (gint type, const gchar *pos, gsize length);
 
 AnjutaToken* anjuta_token_free_children (AnjutaToken *token);
 AnjutaToken* anjuta_token_free (AnjutaToken *token);
@@ -101,9 +102,10 @@ gint anjuta_token_get_type (AnjutaToken *token);
 void anjuta_token_set_flags (AnjutaToken *token, gint flags);
 void anjuta_token_clear_flags (AnjutaToken *token, gint flags);
 gint anjuta_token_get_flags (AnjutaToken *token);
+void anjuta_token_set_string (AnjutaToken *token, const char *value, gsize length);
 const gchar *anjuta_token_get_string (AnjutaToken *token);
-void anjuta_token_set_string (AnjutaToken *token, const char *value, guint length);
-guint anjuta_token_get_length (AnjutaToken *token);
+void anjuta_token_set_length (AnjutaToken *token, gsize length);
+gsize anjuta_token_get_length (AnjutaToken *token);
 
 AnjutaToken *anjuta_token_next (AnjutaToken *token);
 AnjutaToken *anjuta_token_previous (AnjutaToken *token);



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]