[geany/geany] 0a9743: python: Improve and generalize access reporting

Colomban Wendling git-noreply at xxxxx
Wed Feb 11 17:21:14 UTC 2015


Branch:      refs/heads/master
Author:      Colomban Wendling <ban at herbesfolles.org>
Committer:   Colomban Wendling <ban at herbesfolles.org>
Date:        Wed, 11 Feb 2015 17:21:14 UTC
Commit:      0a97439701466011cf4231b838860c543d442e17
             https://github.com/geany/geany/commit/0a97439701466011cf4231b838860c543d442e17

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

Anything at the module/file level:
 * _... is protected
 * rest is public
Anything at the class level:
 * __...__ is public (magic method)
 * __... is private
 * _... is protected
 * rest is public
Anything at the function/method level:
 * everything is private

Closes https://github.com/fishman/ctags/issues/216.


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

Modified: tagmanager/ctags/python.c
43 lines changed, 31 insertions(+), 12 deletions(-)
===================================================================
@@ -91,32 +91,49 @@ static const char *get_class_name_from_parent (const char *parent)
  * - 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)
+static pythonAccess accessFromIdentifier (const vString *const ident,
+	pythonKind kind, boolean has_parent, boolean parent_is_class)
 {
 	const char *const p = vStringValue (ident);
 	const size_t len = vStringLength (ident);
 
+	/* inside a function/method, private */
+	if (has_parent && !parent_is_class)
+		return A_PRIVATE;
 	/* not starting with "_", public */
-	if (len < 1 || p[0] != '_')
+	else if (len < 1 || p[0] != '_')
 		return A_PUBLIC;
 	/* "__...__": magic methods */
-	else if (len > 3 && p[1] == '_' && p[len - 2] == '_' && p[len - 1] == '_')
+	else if (kind == K_METHOD && parent_is_class &&
+			 len > 3 && p[1] == '_' && p[len - 2] == '_' && p[len - 1] == '_')
 		return A_PUBLIC;
 	/* "__...": name mangling */
-	else if (len > 1 && p[1] == '_')
+	else if (parent_is_class && len > 1 && p[1] == '_')
 		return A_PRIVATE;
 	/* "_...": suggested as non-public, but easily accessible */
 	else
 		return A_PROTECTED;
 }
 
+static void addAccessFields (tagEntryInfo *const entry,
+	const vString *const ident, pythonKind kind,
+	boolean has_parent, boolean parent_is_class)
+{
+	pythonAccess access;
+
+	access = accessFromIdentifier (ident, kind, has_parent, parent_is_class);
+	entry->extensionFields.access = PythonAccesses [access];
+	/* FIXME: should we really set isFileScope in addition to access? */
+	if (access == A_PRIVATE)
+		entry->isFileScope = TRUE;
+}
+
 /* Given a string with the contents of a line directly after the "def" keyword,
  * extract all relevant information and create a tag.
  */
 static void makeFunctionTag (vString *const function,
 	vString *const parent, int is_class_parent, const char *arglist)
 {
-	pythonAccess access;
 	tagEntryInfo tag;
 	initTagEntry (&tag, vStringValue (function));
 
@@ -147,11 +164,8 @@ static void makeFunctionTag (vString *const function,
 		}
 	}
 
-	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;
+	addAccessFields (&tag, function, is_class_parent ? K_METHOD : K_FUNCTION,
+		vStringLength (parent) > 0, is_class_parent);
 
 	makeTagEntry (&tag);
 }
@@ -180,10 +194,13 @@ static void makeClassTag (vString *const class, vString *const inheritance,
 		}
 	}
 	tag.extensionFields.inheritance = vStringValue (inheritance);
+	addAccessFields (&tag, class, K_CLASS, vStringLength (parent) > 0,
+		is_class_parent);
 	makeTagEntry (&tag);
 }
 
-static void makeVariableTag (vString *const var, vString *const parent)
+static void makeVariableTag (vString *const var, vString *const parent,
+	boolean is_class_parent)
 {
 	tagEntryInfo tag;
 	initTagEntry (&tag, vStringValue (var));
@@ -194,6 +211,8 @@ static void makeVariableTag (vString *const var, vString *const parent)
 		tag.extensionFields.scope [0] = PythonKinds[K_CLASS].name;
 		tag.extensionFields.scope [1] = vStringValue (parent);
 	}
+	addAccessFields (&tag, var, K_VARIABLE, vStringLength (parent) > 0,
+		is_class_parent);
 	makeTagEntry (&tag);
 }
 
@@ -755,7 +774,7 @@ static void findPythonTags (void)
 			{
 				/* skip variables in methods */
 				if (parent_is_class || vStringLength(parent) == 0)
-					makeVariableTag (name, parent);
+					makeVariableTag (name, parent, parent_is_class);
 			}
 		}
 



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