Skip to content

Commit

Permalink
Merge pull request #164 from Christoph-Koschel/patch-2
Browse files Browse the repository at this point in the history
Allows to have no spaces between a comment pattern and a TODO identifier
  • Loading branch information
alstr authored Sep 12, 2023
2 parents b71583d + 9d485f0 commit b6dc42b
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 4 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,5 @@ dmypy.json
# Cython debug symbols
cython_debug/


# JetBrains
.idea
47 changes: 46 additions & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,32 @@ def parse(self, diff_file):
for marker in block['markers']:
# Check if there are line or block comments.
if marker['type'] == 'line':
comment_pattern = r'(^[+\-\s].*' + marker['pattern'] + r'\s.+$)'
# Add a negative lookup include the second character from alternative comment patterns
# This step is essential to handle cases like in Julia, where '#' and '#=' are comment patterns.
# It ensures that when a space after the comment is optional ('\s' => '\s*'),
# the second character would be matched because of the any character expression ('.+').
suff_escape_list = []
pref_escape_list = []
for to_escape in block['markers']:
if to_escape['type'] == 'line':
if to_escape['pattern'] == marker['pattern']:
continue
if marker['pattern'][0] == to_escape['pattern'][0]:
suff_escape_list.append(self._extract_character(to_escape['pattern'], 1))
else:
# Block comments and line comments cannot have the same comment pattern,
# so a check if the string is the same is unnecessary
if to_escape['pattern']['start'][0] == marker['pattern'][0]:
suff_escape_list.append(self._extract_character(to_escape['pattern']['start'], 1))
search = to_escape['pattern']['end'].find(marker['pattern'])
if search != -1:
pref_escape_list.append(self._extract_character(to_escape['pattern']['end'], search - 1))

comment_pattern = (r'(^[+\-\s].*' +
(r'(?<!(' + '|'.join(pref_escape_list) + r'))' if len(pref_escape_list) > 0 else '') +
marker['pattern'] +
(r'(?!(' + '|'.join(suff_escape_list) + r'))' if len(suff_escape_list) > 0 else '') +
r'\s*.+$)')
comments = re.finditer(comment_pattern, block['hunk'], re.MULTILINE)
extracted_comments = []
prev_comment = None
Expand Down Expand Up @@ -564,6 +589,26 @@ def _escape_markdown(comment):
escaped += c
return escaped

@staticmethod
def _extract_character(input_str, pos):
# Extracts a character from the input string at the specified position,
# considering escape sequences when applicable.
# Test cases
# print(_extract_character("/\\*", 1)) # Output: "\*"
# print(_extract_character("\\*", 0)) # Output: "\*"
# print(_extract_character("\\", 0)) # Output: "\\"
# print(_extract_character("w", 0)) # Output: "w"
# print(_extract_character("wa", 1)) # Output: "a"
# print(_extract_character("\\\\w", 1)) # Output: "\\"
if input_str[pos] == '\\':
if pos >= 1 and not input_str[pos - 1] == '\\' and len(input_str) > pos + 1:
return '\\' + input_str[pos + 1]
return '\\\\'
if pos >= 1:
if input_str[pos - 1] == '\\':
return '\\' + input_str[pos]
return input_str[pos]

def _get_line_status(self, comment):
"""Return a Tuple indicating whether this is an addition/deletion/unchanged, plus the cleaned comment."""
addition_search = self.ADDITION_PATTERN.search(comment)
Expand Down
7 changes: 6 additions & 1 deletion tests/test_closed.diff
Original file line number Diff line number Diff line change
Expand Up @@ -153,12 +153,17 @@ index 0000000..7cccc5b
--- /dev/null
+++ b/src/tests/example_file.jl
@@ -0,0 +1,2 @@
- # TODO: Hopefully this comment turns into an issue
- #TODO: Hopefully this comment turns into an issue
- # TODO: Hopefully this comment also turns into an issue
- print("Hello World")
- #= TODO: Multiline comments
- also need to be turned into task, and hopefully
- kept together as one.
- =#
- #=TODO: Another copied multiline comment
- also need to be turned into task, and hopefully
- kept together as one.
- =#
diff --git a/tests/defs.bzl b/tests/defs.bzl
index 525e25d..ba4e68d 100644
--- a/tests/defs.bzl
Expand Down
2 changes: 1 addition & 1 deletion tests/test_todo_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ def test_tex_issues(self):
self.assertEqual(count_issues_for_file_type(self.raw_issues, 'tex'), 2)

def test_julia_issues(self):
self.assertEqual(count_issues_for_file_type(self.raw_issues, 'julia'), 2)
self.assertEqual(count_issues_for_file_type(self.raw_issues, 'julia'), 4)

def test_starlark_issues(self):
self.assertEqual(count_issues_for_file_type(self.raw_issues, 'python'), 5)
Expand Down

0 comments on commit b6dc42b

Please sign in to comment.