Skip to content

Commit 7890601

Browse files
committed
AnnotationBear: Use unescaped_search_for
Fixes #993
1 parent 3b73a58 commit 7890601

File tree

2 files changed

+39
-8
lines changed

2 files changed

+39
-8
lines changed

bears/general/AnnotationBear.py

+28-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,14 @@ 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+
print(end_match)
209+
end_end = position + end_match.span()[1] if end_match else -1
210+
if end_end == -1:
206211
_range = SourceRange.from_absolute_position(
207212
filename,
208213
AbsolutePosition(file, position))
@@ -238,9 +243,19 @@ def get_singleline_strings(file,
238243
A SourceRange object identifying the range of the single-line
239244
string and the end_position of the string as an integer.
240245
"""
241-
end_position = (text.find(string_end, position + 1)
242-
+ len(string_end) - 1)
243-
newline = text.find("\n", position + 1)
246+
try:
247+
end_match = next(unescaped_search_for(
248+
string_end, text[position + 1:]))
249+
except StopIteration:
250+
end_match = None
251+
end_position = (position + end_match.span()[1] if end_match
252+
else -1)
253+
try:
254+
newline_match = next(
255+
unescaped_search_for("\n", text[position + 1:]))
256+
except StopIteration:
257+
newline_match = None
258+
newline = position + newline_match.span()[1] if newline_match else -1
244259
if newline == -1:
245260
newline = len(text)
246261
if end_position == -1:
@@ -273,7 +288,12 @@ def get_singleline_comment(file, filename, text, comment, position):
273288
A SourceRange object identifying the range of the single-line
274289
comment and the end_position of the comment as an integer.
275290
"""
276-
end_position = text.find("\n", position + 1)
291+
try:
292+
end_match = next(unescaped_search_for("\n", text[position + 1:]))
293+
except StopIteration:
294+
end_match = None
295+
end_position = (position + end_match.span()[1] if end_match
296+
else -1)
277297
if end_position == -1:
278298
end_position = len(text) - 1
279299
return (SourceRange.from_absolute_position(

tests/general/AnnotationBearTest.py

+11
Original file line numberDiff line numberDiff line change
@@ -140,3 +140,14 @@ 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+
print(result[0].contents['strings'], test_range)
153+
self.assertEqual(result[0].contents["strings"], (test_range,))

0 commit comments

Comments
 (0)