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

Get rid of globals

This commit is contained in:
2016-05-19 06:48:31 +02:00
parent 327dce4b7a
commit c9e210a331
2 changed files with 164 additions and 171 deletions

View File

@@ -8,12 +8,6 @@ import time
import vim import vim
# global dictionaries of tags and their line numbers, keys are buffer numbers
TAGS = {}
TAGLINENUMBERS = {}
BUFFERTICKS = {}
class PythonTag(object): class PythonTag(object):
"""A simple storage class representing a python tag.""" """A simple storage class representing a python tag."""
CLASS = "class" CLASS = "class"
@@ -150,20 +144,15 @@ class SimplePythonTagsParser(object):
""" """
Returns instance of PythonTag based on the specified data. Returns instance of PythonTag based on the specified data.
Parameters :param tags_stack: list (stack) of tags currently active.
Note: Modified in this method!
tags_stack -- list (stack) of tags currently active. Note: Modified :param line_number: current line number
in this method! :param indent_chars: characters making up the indentation level of the
current tag
line_number -- current line number :param tag_name: short name of the current tag
:param tag_type_deciding_method: reference to the method that is
indent_chars -- characters making up the indentation level of the called to determine the type of the
current tag current tag
tag_name -- short name of the current tag
tag_type_deciding_method -- reference to the method that is 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)
@@ -223,71 +212,13 @@ class SimplePythonTagsParser(object):
return PythonTag.FUNCTION return PythonTag.FUNCTION
def vim_buffer_iterator(vim_buffer): class PythonHelper(object):
for line in vim_buffer: TAG_LINE_NUMBERS = {}
yield line + "\n" TAGS = {}
BUFFER_TICKS = {}
@classmethod
def get_nearest_line_index(row, tag_line_numbers): def find_tag(cls, buffer_number, changed_tick):
"""
Returns the index of 'tag_line_numbers' that contains the line nearest to
the specified cursor row.
Parameters
row -- current cursor row
tag_line_numbers -- list of tags' line numbers (ie. their position)
"""
nearest_line_number = -1
nearest_line_index = -1
# go through all tag line numbers and find the one nearest to the
# specified row
for line_index, line_number in enumerate(tag_line_numbers):
# if the current line is nearer the current cursor position, take it
if nearest_line_number < line_number <= row:
nearest_line_number = line_number
nearest_line_index = line_index
# if we've come past the current cursor position, end the search
if line_number >= row:
break
return nearest_line_index
def get_tags(buffer_number, changed_tick):
"""
Reads the tags for the buffer specified by the number. Returns a tuple
of the format (taglinenumber[buffer], tags[buffer],).
Parameters
buffer_number -- number of the current buffer
changed_tick -- always-increasing number used to indicate that the
buffer has been modified since the last time
"""
global TAGLINENUMBERS, TAGS, BUFFERTICKS
# return immediately if there's no need to update the tags
if (BUFFERTICKS.get(buffer_number, None) == changed_tick):
return (TAGLINENUMBERS[buffer_number], TAGS[buffer_number])
# get the tags
simple_tags_parser = SimplePythonTagsParser(vim_buffer_iterator(vim.current.buffer))
tag_line_numbers, tags = simple_tags_parser.get_tags()
# update the global variables
TAGS[buffer_number] = tags
TAGLINENUMBERS[buffer_number] = tag_line_numbers
BUFFERTICKS[buffer_number] = changed_tick
return (tag_line_numbers, tags)
def find_tag(buffer_number, changed_tick):
""" """
Tries to find the best tag for the current cursor position. Tries to find the best tag for the current cursor position.
@@ -397,8 +328,8 @@ def find_tag(buffer_number, changed_tick):
# where you have encountered exceptions? # where you have encountered exceptions?
# bury into the traceback # bury into the traceback
ec, ei, tb = sys.exc_info() ec, ei, tb = sys.exc_info()
while tb != None: while tb is not None:
if tb.tb_next == None: if tb.tb_next is None:
break break
tb = tb.tb_next tb = tb.tb_next
@@ -408,8 +339,8 @@ def find_tag(buffer_number, changed_tick):
tb.tb_lineno) tb.tb_lineno)
time.sleep(0.5) time.sleep(0.5)
@classmethod
def delete_tags(buffer_number): def delete_tags(cls, buffer_number):
""" """
Removes tag data for the specified buffer number. Removes tag data for the specified buffer number.
@@ -417,11 +348,73 @@ def delete_tags(buffer_number):
buffer_number -- number of the buffer buffer_number -- number of the buffer
""" """
global TAGS, TAGLINENUMBERS, BUFFERTICKS for item in (PythonHelper.TAGS, PythonHelper.TAG_LINE_NUMBERS,
PythonHelper.BUFFER_TICKS):
# try to delete the tags for the buffer
for o in (TAGS, TAGLINENUMBERS, BUFFERTICKS):
try: try:
del o[buffer_number] del item[buffer_number]
except KeyError: except KeyError:
pass pass
def get_nearest_line_index(row, tag_line_numbers):
"""
Returns the index of 'tag_line_numbers' that contains the line nearest to
the specified cursor row.
Parameters
row -- current cursor row
tag_line_numbers -- list of tags' line numbers (ie. their position)
"""
nearest_line_number = -1
nearest_line_index = -1
# go through all tag line numbers and find the one nearest to the
# specified row
for line_index, line_number in enumerate(tag_line_numbers):
# if the current line is nearer the current cursor position, take it
if nearest_line_number < line_number <= row:
nearest_line_number = line_number
nearest_line_index = line_index
# if we've come past the current cursor position, end the search
if line_number >= row:
break
return nearest_line_index
def get_tags(buffer_number, changed_tick):
"""
Reads the tags for the buffer specified by the number. Returns a tuple
of the format (taglinenumber[buffer], tags[buffer],).
Parameters
buffer_number -- number of the current 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
if PythonHelper.BUFFER_TICKS.get(buffer_number, None) == changed_tick:
return (PythonHelper.TAG_LINE_NUMBERS[buffer_number],
PythonHelper.TAGS[buffer_number])
# get the tags
simple_tags_parser = SimplePythonTagsParser(vim_buffer_iterator(vim.current.buffer))
tag_line_numbers, tags = simple_tags_parser.get_tags()
# update the global variables
PythonHelper.TAGS[buffer_number] = tags
PythonHelper.TAG_LINE_NUMBERS[buffer_number] = tag_line_numbers
PythonHelper.BUFFER_TICKS[buffer_number] = changed_tick
return (tag_line_numbers, tags)

View File

@@ -67,7 +67,7 @@ function! PHCursorHold()
" call Python function findTag() with the current buffer number and change " call Python function findTag() with the current buffer number and change
" status indicator " status indicator
execute g:pythonhelper_python . ' find_tag(' . expand("<abuf>") . execute g:pythonhelper_python . ' PythonHelper.find_tag(' . expand("<abuf>") .
\ ', ' . b:changedtick . ')' \ ', ' . b:changedtick . ')'
endfunction endfunction
@@ -80,7 +80,7 @@ function! PHBufferDelete()
" call Python function deleteTags() with the current buffer number and " call Python function deleteTags() with the current buffer number and
" change status indicator " change status indicator
execute g:pythonhelper_python . ' delete_tags(' . expand("<abuf>") . ')' execute g:pythonhelper_python . ' PythonHelper.delete_tags(' . expand("<abuf>") . ')'
endfunction endfunction