mirror of
https://github.com/gryf/pythonhelper.git
synced 2025-12-18 12:00:27 +01:00
Simplify implementation for tag search
Provide alternative implementation of searching for the right tag. Also, make functionality testable.
This commit is contained in:
@@ -29,9 +29,6 @@ RE_INDENT = re.compile(r'([ \t]*).*')
|
|||||||
|
|
||||||
class PythonTag(object):
|
class PythonTag(object):
|
||||||
"""A simple storage class representing a python tag."""
|
"""A simple storage class representing a python tag."""
|
||||||
CLASS = "class"
|
|
||||||
METHOD = "method"
|
|
||||||
FUNCTION = "function"
|
|
||||||
|
|
||||||
def __init__(self, tag_type='', full_name='', line_number=0,
|
def __init__(self, tag_type='', full_name='', line_number=0,
|
||||||
indent_level=0):
|
indent_level=0):
|
||||||
@@ -70,28 +67,40 @@ class EvenSimplerPythonTagsParser(object):
|
|||||||
|
|
||||||
tag_match = RE_TAG_TYPE.match(line)
|
tag_match = RE_TAG_TYPE.match(line)
|
||||||
|
|
||||||
if tag_match:
|
if not tag_match:
|
||||||
indent_level = self._get_indent_level(line)
|
continue
|
||||||
|
|
||||||
for _ in range(len(tags_stack)):
|
indent_level = self._get_indent_level(line)
|
||||||
if tags_stack and \
|
|
||||||
tags_stack[-1].indent_level >= indent_level:
|
|
||||||
tags_stack.pop()
|
|
||||||
|
|
||||||
if not tags_stack:
|
for _ in range(len(tags_stack)):
|
||||||
break
|
if tags_stack and tags_stack[-1].indent_level >= indent_level:
|
||||||
|
tags_stack.pop()
|
||||||
|
|
||||||
tag = PythonTag(tag_match.group(1),
|
if not tags_stack:
|
||||||
self._get_full_name(tags_stack,
|
break
|
||||||
tag_match.group(2)),
|
|
||||||
line_no,
|
|
||||||
indent_level)
|
|
||||||
|
|
||||||
tags[line_no] = tag
|
tag = PythonTag(tag_match.group(1),
|
||||||
tags_stack.append(tag)
|
self._get_full_name(tags_stack,
|
||||||
|
tag_match.group(2)),
|
||||||
|
line_no,
|
||||||
|
indent_level)
|
||||||
|
tag.tag_type = self._get_tag_type(tag, tags_stack)
|
||||||
|
|
||||||
|
tags[line_no] = tag
|
||||||
|
tags_stack.append(tag)
|
||||||
|
|
||||||
return tags
|
return tags
|
||||||
|
|
||||||
|
def _get_tag_type(self, tag, tags_stack):
|
||||||
|
"""Return proper type of the tag depending on context"""
|
||||||
|
if tag.tag_type == 'class':
|
||||||
|
return 'class'
|
||||||
|
|
||||||
|
if tags_stack and tags_stack[-1].tag_type == 'class':
|
||||||
|
return 'method'
|
||||||
|
|
||||||
|
return 'function'
|
||||||
|
|
||||||
def _get_full_name(self, tags_stack, name):
|
def _get_full_name(self, tags_stack, name):
|
||||||
"""Return full logical name dot separated starting from upper entity"""
|
"""Return full logical name dot separated starting from upper entity"""
|
||||||
if tags_stack:
|
if tags_stack:
|
||||||
@@ -119,6 +128,13 @@ class PythonHelper(object):
|
|||||||
changed_tick -- always-increasing number used to indicate that the
|
changed_tick -- always-increasing number used to indicate that the
|
||||||
buffer has been modified since the last time
|
buffer has been modified since the last time
|
||||||
"""
|
"""
|
||||||
|
tag = PythonHelper._get_tag(buffer_number, changed_tick)
|
||||||
|
update_vim_vars(tag)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def _get_tag(cls, buffer_number, changed_tick):
|
||||||
|
"""Return the nearset tag object or None"""
|
||||||
|
|
||||||
if PythonHelper.TAGS.get(buffer_number) and \
|
if PythonHelper.TAGS.get(buffer_number) and \
|
||||||
PythonHelper.TAGS[buffer_number]['changed_tick'] == changed_tick:
|
PythonHelper.TAGS[buffer_number]['changed_tick'] == changed_tick:
|
||||||
tags = PythonHelper.TAGS[buffer_number]['tags']
|
tags = PythonHelper.TAGS[buffer_number]['tags']
|
||||||
@@ -133,24 +149,46 @@ class PythonHelper(object):
|
|||||||
# one, so that it will not confuse us while operating vim interface by
|
# one, so that it will not confuse us while operating vim interface by
|
||||||
# python, where everything starts from 0.
|
# python, where everything starts from 0.
|
||||||
line_number = vim.current.window.cursor[0] - 1
|
line_number = vim.current.window.cursor[0] - 1
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
line = vim.current.buffer[line_number]
|
line = vim.current.buffer[line_number]
|
||||||
|
line_indent = len(RE_INDENT.match(line).group(1))
|
||||||
if line.strip():
|
if line.strip():
|
||||||
line_indent = len(RE_INDENT.match(line).group(1))
|
|
||||||
break
|
break
|
||||||
# line contains nothing but white characters, looking up to grab
|
# line contains nothing but white characters, looking up to grab
|
||||||
# some more context
|
# some more context
|
||||||
line_number -= 1
|
line_number -= 1
|
||||||
|
|
||||||
tag = tags.get(line_number)
|
tag = tags.get(line_number)
|
||||||
if not tag:
|
|
||||||
key = None
|
|
||||||
for key in reversed(tags.keys()):
|
|
||||||
if line_number >= key and line_indent > tags[key].indent_level:
|
|
||||||
tag = tags.get(key)
|
|
||||||
break
|
|
||||||
|
|
||||||
update_vim_vars(tag)
|
# if we have something at the beginning of the line, just return it;
|
||||||
|
# it doesn't matter if it is the tag found there or not
|
||||||
|
if line_indent == 0 or tag:
|
||||||
|
return tag
|
||||||
|
|
||||||
|
# get nearest tag
|
||||||
|
for line_no in range(line_number - 1, 0, -1):
|
||||||
|
tag = tags.get(line_no)
|
||||||
|
line = vim.current.buffer[line_no]
|
||||||
|
upper_line_indent = len(RE_INDENT.match(line).group(1))
|
||||||
|
|
||||||
|
if tag and upper_line_indent < line_indent:
|
||||||
|
return tag
|
||||||
|
|
||||||
|
if not line.strip():
|
||||||
|
continue
|
||||||
|
|
||||||
|
if upper_line_indent == 0:
|
||||||
|
return None
|
||||||
|
|
||||||
|
if upper_line_indent >= line_indent:
|
||||||
|
continue
|
||||||
|
|
||||||
|
if tag and tag.indent_level >= line_indent:
|
||||||
|
tag = None
|
||||||
|
continue
|
||||||
|
|
||||||
|
return tag
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def delete_tags(cls, buffer_number):
|
def delete_tags(cls, buffer_number):
|
||||||
|
|||||||
Reference in New Issue
Block a user