1
0
mirror of https://github.com/gryf/pythonhelper.git synced 2025-12-19 04:20:20 +01:00

Unifying regexp for detecting class/function

Removing unnecessary functions for "detecting" context of the
class/function. Unifying regexp for detecting class/method/function,
get rid of excess iterator as a facade for vim buffer. Simplifying function
for gathering tags.
This commit is contained in:
2016-05-19 21:14:07 +02:00
parent c9e210a331
commit 03f3c9ef1e

View File

@@ -45,17 +45,15 @@ class SimplePythonTagsParser(object):
TABSIZE = 8 TABSIZE = 8
# regexp used to extract indentation and strip comments # regexp used to extract indentation and strip comments
COMMENTS_INDENT_RE = re.compile('([ \t]*)([^\n#]*).*') COMMENTS_INDENT_RE = re.compile('([ \t]*)([^\n#]*).*')
# regexp used to extract a class name # regexp used to extract a class or function name
CLASS_RE = re.compile('class[ \t]+([^(:]+).*') TAG_TYPE_RE = re.compile('(def|class)[ \t]+([^(:]+).*')
# regexp used to extract a method or function name
METHOD_RE = re.compile('def[ \t]+([^(]+).*')
def __init__(self, source): def __init__(self, source):
""" """
Initializes instances of SimplePythonTagsParser(). Initializes instances of SimplePythonTagsParser().
:param source: source for which the tags will be generated. It must :param source: source for which the tags will be generated. It is
be a generator. simply vim buffer.
""" """
self.source = source self.source = source
@@ -63,49 +61,36 @@ class SimplePythonTagsParser(object):
""" """
Determines all the tags for the buffer. Determines all the tags for the buffer.
:returns: tuple in the format (tag_line_numbers, tags,). :returns: tuple of tags line numbers and tags
""" """
tag_line_numbers = [] tag_line_numbers = []
tags = {} tags = {}
tags_stack = [] tags_stack = []
import itertools
# go through all the lines in the source and localize all Python tags # go through all the lines in the source and localize all Python tags
# in it # in it
for (line, line_number) in zip(self.source, itertools.count(1)): # for (line, line_number) in zip(self.source, itertools.count(1)):
for line_number, line in enumerate(self.source, start=1):
line = line + '\n'
# extract the line's indentation characters and its content # extract the line's indentation characters and its content
line_match = self.COMMENTS_INDENT_RE.match(line) line_match = self.COMMENTS_INDENT_RE.match(line)
line_content = line_match.group(2) line_content = line_match.group(2)
# match for the class tag # match for the class tag
tag_match = self.CLASS_RE.match(line_content) tag_match = self.TAG_TYPE_RE.match(line_content)
# if the class tag has been found, store some information on it # if the class tag has been found, store some information on it
if tag_match: if tag_match:
current_tag = self.get_python_tag(tags_stack, line_number, current_tag = self.get_python_tag(tags_stack, line_number,
line_match.group(1), line_match.group(1),
tag_match.group(1), tag_match.group(2),
self.tag_class_type_deciding_method) tag_match.group(1))
tag_line_numbers.append(line_number) tag_line_numbers.append(line_number)
tags[line_number] = current_tag tags[line_number] = current_tag
else: return tag_line_numbers, tags
# match for the method/function tag
tag_match = self.METHOD_RE.match(line_content)
# if the method/function tag has been found, store some
# information on it
if tag_match:
current_tag = self.get_python_tag(tags_stack,
line_number,
line_match.group(1),
tag_match.group(1),
self.tag_function_type_deciding_method)
tag_line_numbers.append(line_number)
tags[line_number] = current_tag
return (tag_line_numbers, tags,)
def get_parent_tag(self, tags_stack): def get_parent_tag(self, tags_stack):
""" """
@@ -140,7 +125,7 @@ class SimplePythonTagsParser(object):
return indent_level return indent_level
def get_python_tag(self, tags_stack, line_number, indent_chars, tag_name, def get_python_tag(self, tags_stack, line_number, indent_chars, tag_name,
tag_type_deciding_method): obj_type):
""" """
Returns instance of PythonTag based on the specified data. Returns instance of PythonTag based on the specified data.
@@ -150,15 +135,21 @@ class SimplePythonTagsParser(object):
:param indent_chars: characters making up the indentation level of the :param indent_chars: characters making up the indentation level of the
current tag current tag
:param tag_name: short name of the current tag :param tag_name: short name of the current tag
:param tag_type_deciding_method: reference to the method that is :param obj_type: one of 'class' or 'def'
called to determine the type of the
current tag
""" """
indent_level = self.compute_indentation_level(indent_chars) indent_level = self.compute_indentation_level(indent_chars)
parent_tag = self.get_parent_tag(tags_stack) parent_tag = self.get_parent_tag(tags_stack)
if obj_type == 'class':
obj_type = PythonTag.CLASS
else:
obj_type = PythonTag.FUNCTION
# handle enclosed tag # handle enclosed tag
while parent_tag: while parent_tag:
if parent_tag.tag_type == PythonTag.CLASS:
obj_type = PythonTag.METHOD
# if the indent level of the parent tag is greater than of the # if the indent level of the parent tag is greater than of the
# current tag, use parent tag of the parent tag # current tag, use parent tag of the parent tag
if parent_tag.indent_level >= indent_level: if parent_tag.indent_level >= indent_level:
@@ -167,10 +158,9 @@ class SimplePythonTagsParser(object):
# otherwise we have all information on the current tag and can # otherwise we have all information on the current tag and can
# return it # return it
else: else:
tag = PythonTag(tag_type_deciding_method(parent_tag.tag_type), tag = PythonTag(obj_type,
"%s.%s" % (parent_tag.full_name, tag_name,), "%s.%s" % (parent_tag.full_name, tag_name,),
line_number, indent_level) line_number, indent_level)
break break
# use the parent tag of the parent tag # use the parent tag of the parent tag
@@ -178,25 +168,13 @@ class SimplePythonTagsParser(object):
# handle a top-indent level tag # handle a top-indent level tag
else: else:
tag = PythonTag(tag_type_deciding_method(None), tag_name, tag = PythonTag(obj_type, tag_name, line_number, indent_level)
line_number, indent_level)
# add the tag to the list of tags # add the tag to the list of tags
tags_stack.append(tag) tags_stack.append(tag)
return tag return tag
def tag_class_type_deciding_method(self, parent_tag_type):
"""
Returns tag type of the current tag based on its previous tag (super
tag) for classes.
Parameters
parent_tag_type -- type of the enclosing/parent tag
"""
return PythonTag.CLASS
def tag_function_type_deciding_method(self, parent_tag_type): def tag_function_type_deciding_method(self, parent_tag_type):
""" """
Returns tag type of the current tag based on its previous tag (super Returns tag type of the current tag based on its previous tag (super
@@ -387,29 +365,20 @@ def get_nearest_line_index(row, tag_line_numbers):
def get_tags(buffer_number, changed_tick): def get_tags(buffer_number, changed_tick):
""" """
Reads the tags for the buffer specified by the number. Returns a tuple Reads the tags for the buffer specified by the number..
of the format (taglinenumber[buffer], tags[buffer],).
Parameters :param buffer_number: Number of the current buffer
:param changed_tick: Always-increasing number used to indicate that the
buffer_number -- number of the current buffer buffer has been modified since the last time
:returns: Tuple of the format (taglinenumber[buffer], tags[buffer])
changed_tick -- always-increasing number used to indicate that the
buffer has been modified since the last time
""" """
def vim_buffer_iterator(vim_buffer):
"""Iterator over vim buffer"""
for line in vim_buffer:
yield line + "\n"
# return immediately if there's no need to update the tags # return immediately if there's no need to update the tags
if PythonHelper.BUFFER_TICKS.get(buffer_number, None) == changed_tick: if PythonHelper.BUFFER_TICKS.get(buffer_number, None) == changed_tick:
return (PythonHelper.TAG_LINE_NUMBERS[buffer_number], return (PythonHelper.TAG_LINE_NUMBERS[buffer_number],
PythonHelper.TAGS[buffer_number]) PythonHelper.TAGS[buffer_number])
# get the tags # get the tags
simple_tags_parser = SimplePythonTagsParser(vim_buffer_iterator(vim.current.buffer)) simple_tags_parser = SimplePythonTagsParser(vim.current.buffer)
tag_line_numbers, tags = simple_tags_parser.get_tags() tag_line_numbers, tags = simple_tags_parser.get_tags()
# update the global variables # update the global variables