[geany/geany] dd7c20: python: Improve access reporting

Colomban Wendling git-noreply at xxxxx
Thu Jan 9 14:50:25 UTC 2014


Branch:      refs/heads/master
Author:      Colomban Wendling <ban at herbesfolles.org>
Committer:   Colomban Wendling <ban at herbesfolles.org>
Date:        Thu, 09 Jan 2014 14:50:25 UTC
Commit:      dd7c20a19470f588eaea6196d864142458d3d5ee
             https://github.com/geany/geany/commit/dd7c20a19470f588eaea6196d864142458d3d5ee

Log Message:
-----------
python: Improve access reporting

See:
* http://www.python.org/dev/peps/pep-0008/#method-names-and-instance-variables
* http://www.python.org/dev/peps/pep-0008/#designing-for-inheritance


Modified Paths:
--------------
    tagmanager/ctags/python.c

Modified: tagmanager/ctags/python.c
50 files changed, 37 insertions(+), 13 deletions(-)
===================================================================
@@ -36,6 +36,14 @@
     {TRUE, 'i', "namespace", "imports"}
 };
 
+typedef enum {
+	A_PUBLIC, A_PRIVATE, A_PROTECTED
+} pythonAccess;
+
+static const char *const PythonAccesses[] = {
+	"public", "private", "protected"
+};
+
 static char const * const singletriple = "'''";
 static char const * const doubletriple = "\"\"\"";
 
@@ -78,6 +86,29 @@ static const char *get_class_name_from_parent (const char *parent)
 	return result;
 }
 
+/* follows PEP-8, and always reports single-underscores as protected
+ * See:
+ * - http://www.python.org/dev/peps/pep-0008/#method-names-and-instance-variables
+ * - http://www.python.org/dev/peps/pep-0008/#designing-for-inheritance
+ */
+static pythonAccess accessFromIdentifier (const vString *const ident)
+{
+	const char *const p = vStringValue (ident);
+	const size_t len = vStringLength (ident);
+
+	/* not starting with "_", public */
+	if (len < 1 || p[0] != '_')
+		return A_PUBLIC;
+	/* "__...__": magic methods */
+	else if (len > 3 && p[1] == '_' && p[len - 2] == '_' && p[len - 1] == '_')
+		return A_PUBLIC;
+	/* "__...": name mangling */
+	else if (len > 1 && p[1] == '_')
+		return A_PRIVATE;
+	/* "_...": suggested as non-public, but easily accessible */
+	else
+		return A_PROTECTED;
+}
 
 /* Given a string with the contents of a line directly after the "def" keyword,
  * extract all relevant information and create a tag.
@@ -85,6 +116,7 @@ static const char *get_class_name_from_parent (const char *parent)
 static void makeFunctionTag (vString *const function,
 	vString *const parent, int is_class_parent, const char *arglist)
 {
+	pythonAccess access;
 	tagEntryInfo tag;
 	initTagEntry (&tag, vStringValue (function));
 
@@ -115,20 +147,12 @@ static void makeFunctionTag (vString *const function,
 		}
 	}
 
-	/* If a function starts with __, we mark it as file scope.
-	 * FIXME: What is the proper way to signal such attributes?
-	 * TODO: What does functions/classes starting with _ and __ mean in python?
-	 */
-	if (strncmp (vStringValue (function), "__", 2) == 0 &&
-		strcmp (vStringValue (function), "__init__") != 0)
-	{
-		tag.extensionFields.access = "private";
+	access = accessFromIdentifier (function);
+	tag.extensionFields.access = PythonAccesses [access];
+	/* FIXME: should we really set isFileScope in addition to access? */
+	if (access == A_PRIVATE)
 		tag.isFileScope = TRUE;
-	}
-	else
-	{
-		tag.extensionFields.access = "public";
-	}
+
 	makeTagEntry (&tag);
 }
 



--------------
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