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

Get rid of catch-everything exception mechanism.

It seams, that entire block for looking for tags has been closed between
try:except keywords, catching all the exceptions, which is not necessary just
for iterating all over the file looking for particular pattern. Remove it
until real problem appear on wild - for sure I will extensively test that ;).
This commit is contained in:
2016-05-19 21:24:22 +02:00
parent 03f3c9ef1e
commit d8a44e8439

View File

@@ -111,7 +111,8 @@ class SimplePythonTagsParser(object):
""" """
Computes the indentation level from the specified string. Computes the indentation level from the specified string.
indent_chars -- white space before any other character on line :param indent_chars: White space before any other character on line
:returns: indent level as an int
""" """
indent_level = 0 indent_level = 0
@@ -136,6 +137,7 @@ class SimplePythonTagsParser(object):
current tag current tag
:param tag_name: short name of the current tag :param tag_name: short name of the current tag
:param obj_type: one of 'class' or 'def' :param obj_type: one of 'class' or 'def'
:returns: PythonTag object
""" """
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)
@@ -207,115 +209,93 @@ 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
""" """
try: # get the tag data for the current buffer
# get the tag data for the current buffer tag_line_numbers, tags = get_tags(buffer_number, changed_tick)
tag_line_numbers, tags = get_tags(buffer_number, changed_tick)
# link to Vim's internal data # link to Vim's internal data
current_buffer = vim.current.buffer current_buffer = vim.current.buffer
current_window = vim.current.window current_window = vim.current.window
row, col = current_window.cursor row = current_window.cursor[0]
# get the index of the nearest line # get the index of the nearest line
nearest_line_index = get_nearest_line_index(row, tag_line_numbers) nearest_line_index = get_nearest_line_index(row, tag_line_numbers)
# if a line has been found, find out if the tag is correct {{{ # if a line has been found, find out if the tag is correct {{{
# E.g. the cursor might be below the last tag, but in code that has # E.g. the cursor might be below the last tag, but in code that has
# nothing to do with the tag, which we know because the line is # nothing to do with the tag, which we know because the line is
# indented differently. In such a case no applicable tag has been # indented differently. In such a case no applicable tag has been
# found. # found.
while nearest_line_index > -1: while nearest_line_index > -1:
# get the line number of the nearest tag # get the line number of the nearest tag
nearest_line_number = tag_line_numbers[nearest_line_index] nearest_line_number = tag_line_numbers[nearest_line_index]
# walk through all the lines in the range (nearestTagLine, # walk through all the lines in the range (nearestTagLine,
# cursorRow) # cursorRow)
for line_number in xrange(nearest_line_number + 1, row): for line_number in xrange(nearest_line_number + 1, row):
# get the current line # get the current line
line = current_buffer[line_number] line = current_buffer[line_number]
# count the indentation of the line, if it's lower than the # count the indentation of the line, if it's lower than the
# tag's, the tag is invalid # tag's, the tag is invalid
if len(line): if len(line):
# initialize local auxiliary variables # initialize local auxiliary variables
line_start = 0 line_start = 0
i = 0 i = 0
# compute the indentation of the line # compute the indentation of the line
while (i < len(line)) and (line[i].isspace()): while (i < len(line)) and (line[i].isspace()):
# move the start of the line code # move the start of the line code
if line[i] == '\t': if line[i] == '\t':
line_start += SimplePythonTagsParser.TABSIZE line_start += SimplePythonTagsParser.TABSIZE
else: else:
line_start += 1 line_start += 1
# go to the next character on the line # go to the next character on the line
i += 1 i += 1
# if the line contains only spaces, skip it # if the line contains only spaces, skip it
if i == len(line): if i == len(line):
continue continue
# if the next character is a '#' (python comment), skip # if the next character is a '#' (python comment), skip
# to the next line # to the next line
if line[i] == '#': if line[i] == '#':
continue continue
# if the line's indentation starts before or at the # if the line's indentation starts before or at the
# nearest tag's, the tag is invalid # nearest tag's, the tag is invalid
if line_start <= tags[nearest_line_number].indent_level: if line_start <= tags[nearest_line_number].indent_level:
nearest_line_index -= 1 nearest_line_index -= 1
break break
# the tag is correct, so use it # the tag is correct, so use it
else:
break
# no applicable tag has been found
else: else:
nearest_line_number = -1 break
# describe the cursor position (what tag the cursor is on) # no applicable tag has been found
# reset the description else:
tag_description = "" nearest_line_number = -1
tag_description_tag = ""
tag_description_type = ""
# if an applicable tag has been found, set the description accordingly # describe the cursor position (what tag the cursor is on)
if nearest_line_number > -1: # reset the description
tag_info = tags[nearest_line_number] tag_description = ""
tag_description_tag = tag_info.full_name tag_description_tag = ""
tag_description_type = tag_info.tag_type tag_description_type = ""
tag_description = "%s (%s)" % (tag_description_tag,
tag_description_type)
# update the variable for the status line so it get updated with the # if an applicable tag has been found, set the description
# new description # accordingly
vim.command("let w:PHStatusLine=\"%s\"" % (tag_description,)) if nearest_line_number > -1:
vim.command("let w:PHStatusLineTag=\"%s\"" % (tag_description_tag,)) tag_info = tags[nearest_line_number]
vim.command("let w:PHStatusLineType=\"%s\"" % (tag_description_type,)) tag_description_tag = tag_info.full_name
tag_description_type = tag_info.tag_type
tag_description = "%s (%s)" % (tag_description_tag,
tag_description_type)
# handle possible exceptions # update the variable for the status line so it get updated with
except Exception: # the new description
# FIXME: wrap try/except blocks around single sources of exceptions vim.command("let w:PHStatusLine=\"%s\"" % tag_description)
# ONLY. Break this try/except block into as many small ones as you vim.command("let w:PHStatusLineTag=\"%s\"" % tag_description_tag)
# need, and only catch classes of exceptions that you have encountered. vim.command("let w:PHStatusLineType=\"%s\"" % tag_description_type)
# Catching "Exception" is very, very bad style!
# To the author: why is this clause here? There's no git log for why you
# have added it. Can you please put in a comment of a specific situation
# where you have encountered exceptions?
# bury into the traceback
ec, ei, tb = sys.exc_info()
while tb is not None:
if tb.tb_next is None:
break
tb = tb.tb_next
# spit out the error
print "ERROR: %s %s %s:%u" % (ec.__name__, ei,
tb.tb_frame.f_code.co_filename,
tb.tb_lineno)
time.sleep(0.5)
@classmethod @classmethod
def delete_tags(cls, buffer_number): def delete_tags(cls, buffer_number):