diff --git a/gitinspector/blame.py b/gitinspector/blame.py index 2d4118d..655671b 100644 --- a/gitinspector/blame.py +++ b/gitinspector/blame.py @@ -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. diff --git a/gitinspector/comment.py b/gitinspector/comment.py index 00edce1..40c4e54 100644 --- a/gitinspector/comment.py +++ b/gitinspector/comment.py @@ -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) diff --git a/gitinspector/metrics.py b/gitinspector/metrics.py index c80d4a1..0343111 100644 --- a/gitinspector/metrics.py +++ b/gitinspector/metrics.py @@ -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