Skip to content

Commit

Permalink
AnnotationBear: Use unescaped_search_for
Browse files Browse the repository at this point in the history
find function doesn't ignore escape sequences
hence we use unescaped_search_for to ignore
the sequences which are escaped.
Fixes #993
  • Loading branch information
abhay-raizada committed Nov 14, 2016
1 parent 3b73a58 commit e4fb678
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 8 deletions.
36 changes: 28 additions & 8 deletions bears/general/AnnotationBear.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from coalib.results.Result import Result, RESULT_SEVERITY
from coalib.results.SourceRange import SourceRange
from coalib.results.AbsolutePosition import AbsolutePosition
from coala_utils.string_processing.Core import unescaped_search_for


class AnnotationBear(LocalBear):
Expand Down Expand Up @@ -199,10 +200,14 @@ def get_multiline(file,
A SourceRange object holding the range of the multi-line annotation
and the end_position of the annotation as an integer.
"""
end_start = text.find(annotation_end,
position + 1)
end_end = end_start + len(annotation_end) - 1
if end_start == -1:
try:
end_match = next(unescaped_search_for(annotation_end,
text[position + 1:]))
except StopIteration:
end_match = None
print(end_match)
end_end = position + end_match.span()[1] if end_match else -1
if end_end == -1:
_range = SourceRange.from_absolute_position(
filename,
AbsolutePosition(file, position))
Expand Down Expand Up @@ -238,9 +243,19 @@ def get_singleline_strings(file,
A SourceRange object identifying the range of the single-line
string and the end_position of the string as an integer.
"""
end_position = (text.find(string_end, position + 1)
+ len(string_end) - 1)
newline = text.find("\n", position + 1)
try:
end_match = next(unescaped_search_for(
string_end, text[position + 1:]))
except StopIteration:
end_match = None
end_position = (position + end_match.span()[1] if end_match
else -1)
try:
newline_match = next(
unescaped_search_for("\n", text[position + 1:]))
except StopIteration:
newline_match = None
newline = position + newline_match.span()[1] if newline_match else -1
if newline == -1:
newline = len(text)
if end_position == -1:
Expand Down Expand Up @@ -273,7 +288,12 @@ def get_singleline_comment(file, filename, text, comment, position):
A SourceRange object identifying the range of the single-line
comment and the end_position of the comment as an integer.
"""
end_position = text.find("\n", position + 1)
try:
end_match = next(unescaped_search_for("\n", text[position + 1:]))
except StopIteration:
end_match = None
end_position = (position + end_match.span()[1] if end_match
else -1)
if end_position == -1:
end_position = len(text) - 1
return (SourceRange.from_absolute_position(
Expand Down
11 changes: 11 additions & 0 deletions tests/general/AnnotationBearTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,14 @@ def test_no_coalang(self):
with execute_bear(uut, "F", text) as result:
self.assertEqual(result[0].contents,
"coalang specification for Valyrian not found.")

def test_escape_strings(self):
text = [r"'I\'ll be back' -T1000"]
uut = AnnotationBear(self.section1, Queue())
test_range = SourceRange.from_absolute_position(
"F",
AbsolutePosition(text, 0),
AbsolutePosition(text, text[0].find("'", 4)))
with execute_bear(uut, "F", text) as result:
print(result[0].contents['strings'], test_range)
self.assertEqual(result[0].contents["strings"], (test_range,))

0 comments on commit e4fb678

Please sign in to comment.