Skip to content

Commit f822d65

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 8dd45e3 commit f822d65

File tree

2 files changed

+26
-8
lines changed

2 files changed

+26
-8
lines changed

bears/general/AnnotationBear.py

+16-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,8 @@ 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+
end_end = get_end_position(annotation_end, text, position)
204+
if end_end == -1:
206205
_range = SourceRange.from_absolute_position(
207206
filename,
208207
AbsolutePosition(file, position))
@@ -238,9 +237,8 @@ def get_singleline_strings(file,
238237
A SourceRange object identifying the range of the single-line
239238
string and the end_position of the string as an integer.
240239
"""
241-
end_position = (text.find(string_end, position + 1)
242-
+ len(string_end) - 1)
243-
newline = text.find("\n", position + 1)
240+
end_position = get_end_position(string_end, text, position)
241+
newline = get_end_position("\n", text, position)
244242
if newline == -1:
245243
newline = len(text)
246244
if end_position == -1:
@@ -273,7 +271,7 @@ def get_singleline_comment(file, filename, text, comment, position):
273271
A SourceRange object identifying the range of the single-line
274272
comment and the end_position of the comment as an integer.
275273
"""
276-
end_position = text.find("\n", position + 1)
274+
end_position = get_end_position("\n", text, position)
277275
if end_position == -1:
278276
end_position = len(text) - 1
279277
return (SourceRange.from_absolute_position(
@@ -283,6 +281,16 @@ def get_singleline_comment(file, filename, text, comment, position):
283281
end_position)
284282

285283

284+
def get_end_position(end_marker, text, position):
285+
try:
286+
end_match = next(unescaped_search_for(end_marker, text[position + 1:]))
287+
end_position = position + end_match.span()[1]
288+
except StopIteration:
289+
end_position = -1
290+
291+
return end_position
292+
293+
286294
class NoCloseError(Exception):
287295

288296
def __init__(self, annotation, code):

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)