Skip to content

Commit d83a9ca

Browse files
committed
AnnotationBear: Use unescaped_search_for
Find function doesn't ignore escape sequences hence we use unescaped_search_for to ignore the sequences which are escaped. Fixes #993
1 parent 3b73a58 commit d83a9ca

File tree

2 files changed

+37
-8
lines changed

2 files changed

+37
-8
lines changed

bears/general/AnnotationBear.py

+27-8
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from coalib.results.Result import Result, RESULT_SEVERITY
55
from coalib.results.SourceRange import SourceRange
66
from coalib.results.AbsolutePosition import AbsolutePosition
7+
from coala_utils.string_processing.Core import unescaped_search_for
78

89

910
class AnnotationBear(LocalBear):
@@ -199,10 +200,13 @@ def get_multiline(file,
199200
A SourceRange object holding the range of the multi-line annotation
200201
and the end_position of the annotation as an integer.
201202
"""
202-
end_start = text.find(annotation_end,
203-
position + 1)
204-
end_end = end_start + len(annotation_end) - 1
205-
if end_start == -1:
203+
try:
204+
end_match = next(unescaped_search_for(annotation_end,
205+
text[position + 1:]))
206+
except StopIteration:
207+
end_match = None
208+
end_end = position + end_match.span()[1] if end_match else -1
209+
if end_end == -1:
206210
_range = SourceRange.from_absolute_position(
207211
filename,
208212
AbsolutePosition(file, position))
@@ -238,9 +242,19 @@ def get_singleline_strings(file,
238242
A SourceRange object identifying the range of the single-line
239243
string and the end_position of the string as an integer.
240244
"""
241-
end_position = (text.find(string_end, position + 1)
242-
+ len(string_end) - 1)
243-
newline = text.find("\n", position + 1)
245+
try:
246+
end_match = next(unescaped_search_for(
247+
string_end, text[position + 1:]))
248+
except StopIteration:
249+
end_match = None
250+
end_position = (position + end_match.span()[1] if end_match
251+
else -1)
252+
try:
253+
newline_match = next(
254+
unescaped_search_for("\n", text[position + 1:]))
255+
except StopIteration:
256+
newline_match = None
257+
newline = position + newline_match.span()[1] if newline_match else -1
244258
if newline == -1:
245259
newline = len(text)
246260
if end_position == -1:
@@ -273,7 +287,12 @@ def get_singleline_comment(file, filename, text, comment, position):
273287
A SourceRange object identifying the range of the single-line
274288
comment and the end_position of the comment as an integer.
275289
"""
276-
end_position = text.find("\n", position + 1)
290+
try:
291+
end_match = next(unescaped_search_for("\n", text[position + 1:]))
292+
except StopIteration:
293+
end_match = None
294+
end_position = (position + end_match.span()[1] if end_match
295+
else -1)
277296
if end_position == -1:
278297
end_position = len(text) - 1
279298
return (SourceRange.from_absolute_position(

tests/general/AnnotationBearTest.py

+10
Original file line numberDiff line numberDiff line change
@@ -140,3 +140,13 @@ def test_no_coalang(self):
140140
with execute_bear(uut, "F", text) as result:
141141
self.assertEqual(result[0].contents,
142142
"coalang specification for Valyrian not found.")
143+
144+
def test_escape_strings(self):
145+
text = [r"'I\'ll be back' -T1000"]
146+
uut = AnnotationBear(self.section1, Queue())
147+
test_range = SourceRange.from_absolute_position(
148+
"F",
149+
AbsolutePosition(text, 0),
150+
AbsolutePosition(text, text[0].find("'", 4)))
151+
with execute_bear(uut, "F", text) as result:
152+
self.assertEqual(result[0].contents["strings"], (test_range,))

0 commit comments

Comments
 (0)