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
abh3po authored and sils committed Nov 16, 2016
1 parent 8b54ef3 commit e68e893
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
24 changes: 16 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,8 @@ 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:
end_end = get_end_position(annotation_end, text, position)
if end_end == -1:
_range = SourceRange.from_absolute_position(
filename,
AbsolutePosition(file, position))
Expand Down Expand Up @@ -238,9 +237,8 @@ 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)
end_position = get_end_position(string_end, text, position)
newline = get_end_position("\n", text, position)
if newline == -1:
newline = len(text)
if end_position == -1:
Expand Down Expand Up @@ -273,7 +271,7 @@ 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)
end_position = get_end_position("\n", text, position)
if end_position == -1:
end_position = len(text) - 1
return (SourceRange.from_absolute_position(
Expand All @@ -283,6 +281,16 @@ def get_singleline_comment(file, filename, text, comment, position):
end_position)


def get_end_position(end_marker, text, position):
try:
end_match = next(unescaped_search_for(end_marker, text[position + 1:]))
end_position = position + end_match.span()[1]
except StopIteration:
end_position = -1

return end_position


class NoCloseError(Exception):

def __init__(self, annotation, code):
Expand Down
10 changes: 10 additions & 0 deletions tests/general/AnnotationBearTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,13 @@ 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:
self.assertEqual(result[0].contents["strings"], (test_range,))

0 comments on commit e68e893

Please sign in to comment.