Added a handle_comment_block() function in the comment module.

This function moves most of the logic of handling comments into the
comment module itself, thus avoiding duplicated code and allowing for
a cleaner solution.
This commit is contained in:
Adam Waldenberg 2013-07-02 23:03:09 +02:00
parent f20b826d2d
commit bb72cc8f02
3 changed files with 31 additions and 27 deletions

View File

@ -67,16 +67,8 @@ class BlameThread(threading.Thread):
if self.blames.get((author, self.filename), None) == None:
self.blames[(author, self.filename)] = BlameEntry()
if comment.is_comment(self.extension, content):
self.blames[(author, self.filename)].comments += 1
if is_inside_comment:
if comment.has_comment_end(self.extension, content):
is_inside_comment = False
else:
self.blames[(author, self.filename)].comments += 1
elif comment.has_comment_begining(self.extension, content) and not comment.has_comment_end(self.extension, content):
is_inside_comment = True
(comments, is_inside_comment) = comment.handle_comment_block(is_inside_comment, self.extension, content)
self.blames[(author, self.filename)].comments += comments
self.blames[(author, self.filename)].rows += 1
__blame_lock__.release() # ...to here.

View File

@ -32,6 +32,22 @@ __comment__ = {"java": "//", "c": "//", "cpp": "//", "h": "//", "hpp": "//", "pl
__comment_markers_must_be_at_begining__ = {"tex": True}
def __has_comment_begining__(extension, string):
if __comment_markers_must_be_at_begining__.get(extension, None) == True:
return string.find(__comment_begining__[extension]) == 0
elif __comment_begining__.get(extension, None) != None and string.find(__comment_end__[extension], 2) == -1:
return string.find(__comment_begining__[extension]) != -1
return False
def __has_comment_end__(extension, string):
if __comment_markers_must_be_at_begining__.get(extension, None) == True:
return string.find(__comment_end__[extension]) == 0
elif __comment_end__.get(extension, None) != None:
return string.find(__comment_end__[extension]) != -1
return False
def is_comment(extension, string):
if __comment_begining__.get(extension, None) != None and string.strip().startswith(__comment_begining__[extension]):
return True
@ -42,18 +58,17 @@ def is_comment(extension, string):
return False
def has_comment_begining(extension, string):
if __comment_markers_must_be_at_begining__.get(extension, None) == True:
return string.find(__comment_begining__[extension]) == 0
elif __comment_begining__.get(extension, None) != None and string.find(__comment_end__[extension], 2) == -1:
return string.find(__comment_begining__[extension]) != -1
def handle_comment_block(is_inside_comment, extension, content):
comments = 0
return False
if is_comment(extension, content):
comments += 1
if is_inside_comment:
if __has_comment_end__(extension, content):
is_inside_comment = False
else:
comments += 1
elif __has_comment_begining__(extension, content) and not __has_comment_end__(extension, content):
is_inside_comment = True
def has_comment_end(extension, string):
if __comment_markers_must_be_at_begining__.get(extension, None) == True:
return string.find(__comment_end__[extension]) == 0
elif __comment_end__.get(extension, None) != None:
return string.find(__comment_end__[extension]) != -1
return False
return (comments, is_inside_comment)

View File

@ -57,10 +57,7 @@ class MetricsLogic:
for j in file_r.readlines():
j = j.decode("utf-8", "replace")
if is_inside_comment and comment.has_comment_end(extension, j):
is_inside_comment = False
elif comment.has_comment_begining(extension, j):
is_inside_comment = True
(is_inside_comment, _) = comment.handle_comment_block(is_inside_comment, extension, j)
if not is_inside_comment and not comment.is_comment(extension, j):
eloc_counter += 1